首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

B树的C实现

  • 25-03-02 16:41
  • 4147
  • 9155
blog.csdn.net

                                              从B树谈到R树之B树的C实现

作者:weedge,July。编程艺术室出品。

前言

    代码大全的作者Steve McConnell曾称,他所见识的任何一本书都不是某一个人能完全独立即能完成的。吾深以为然。

    本blog内的文章十有八九系我个人参考资料原创所作,与此同时十有二三系本人与吾的朋友共同创作完成。所以,诸君在浏览本博客内任何一篇文章时,务必尊重他人劳动成果。当然,有任何问题,欢迎随时不吝指正。

    ok,在本blog之前的一篇文章中:从B树、B+树、B*树谈到R 树,各位读者反应热烈。这次,咱们来编码实现B树的查找,插入,删除等操作。同时此文也算作是上一篇文章从B树谈到R树的续。望诸君不吝赐教。谢谢。

第一部分、B树的查找,插入,删除等具体操作

    编码实现B树之前,咱们先来回顾一下上文中所给出的B树的查找,插入,删除等具体的操作都是怎么一回事儿。明白了原理之后,再来编程实现,就相对来说有方向感了。ok,请看下文(援引自从B树、B+树、B*树谈到R 树第3小节):

B树的插入、删除操作

    上文第3小节简单介绍了利用B树这种结构如何访问外存磁盘中的数据的情况,下面咱们通过另外一个实例来对这棵B树的插入(insert),删除(delete)基本操作进行详细的介绍。

    但在此之前,咱们还得简单回顾下一棵m阶的B 树 (m叉树)的特性,如下:

  1. 树中每个结点含有最多含有m个孩子,即m满足:ceil(m/2)<= m<=m。
  2. 除根结点和叶子结点外,其它每个结点至少有[ceil(m / 2)]个孩子(其中ceil(x)是一个取上限的函数);
  3. 若根结点不是叶子结点,则至少有2个孩子(特殊情况:没有孩子的根结点,即根结点为叶子结点,整棵树只有一个根节点);
  4. 所有叶子结点都出现在同一层,叶子结点不包含任何关键字信息(可以看做是外部接点或查询失败的接点,实际上这些结点不存在,指向这些结点的指针都为null);
  5. 每个非终端结点中包含有n个关键字信息: (n,P0,K1,P1,K2,P2,......,Kn,Pn)。其中:
           a)   Ki (i=1...n)为关键字,且关键字按顺序升序排序K(i-1)< Ki。
           b)   Pi为指向子树根的接点,且指针P(i-1)指向子树种所有结点的关键字均小于Ki,但都大于K(i-1)。 
           c)   除根结点之外的结点的关键字的个数n必须满足: [ceil(m / 2)-1]<= n <= m-1(叶子结点也必须满足此条关于关键字数的性质,根结点除外)。

    ok,下面咱们以一棵5阶(m=5,即除根结点和叶子结点之外的内结点最多5个孩子,最少3个孩子)B树实例进行讲。

备注:

  • 关键字数(2-4个)针对--非根结点(包括叶子结点在内),孩子数(3-5个)--针对根结点和叶子结点之外的内结点。当然,根结点是必须至少有2个孩子的,不然就成直线型搜索树了。
  • 我说的再明白点就是,一棵5阶的B树中任何一个结点的关键字数是1-4,孩子树是2-5。同时,一棵5阶的B树的最大高度应为log_ceil(m/2)N(下划线表示以ceil(m/2)为底)。

下图中关键字为大写字母,顺序为字母升序。

结点定义如下:

typedef struct{
   int Count;         // 当前节点中关键元素数目
   ItemType Key[4];   // 存储关键字元素的数组
   long Branch[5];    // 伪指针数组,(记录数目)方便判断合并和分裂的情况
} NodeType;

 

1.1、插入(insert)操作

    插入一个元素时,首先在B树中是否存在,如果不存在,即在叶子结点处结束,然后在叶子结点中插入该新的元素,注意:

  • 如果叶子结点空间足够,这里需要向右移动该叶子结点中大于新插入关键字的元素,
  • 如果空间满了以致没有足够的空间去添加新的元素,则将该结点进行“分裂”,将一半数量的关键字元素分裂到新的其相邻右结点中,中间关键字元素上移到父结点中(当然,如果父结点空间满了,也同样需要“分裂”操作),而且当结点中关键元素向右移动了,相关的指针也需要向右移。
  • 如果在根结点插入新元素,空间满了,则进行分裂操作,这样原来的根结点中的中间关键字元素向上移动到新的根结点中,因此导致树的高度增加一层。

1、咱们通过一个实例来逐步讲解下。插入以下字符字母到一棵空的B 树中(非根结点关键字数小了(小于2个)就合并,大了(超过4个)就分裂):C N G A H E K Q M F W L T Z D P R X Y S,首先,结点空间足够,4个字母插入相同的结点中,如下图:

 

2、当咱们试着插入H时,结点发现空间不够,以致将其分裂成2个结点,移动中间元素G上移到新的根结点中,在实现过程中,咱们把A和C留在当前结点中,而H和N放置新的其右邻居结点中。如下图:

 

3、当咱们插入E,K,Q时,不需要任何分裂操作

4、插入M需要一次分裂,注意M恰好是中间关键字元素,以致向上移到父节点中

 

5、插入F,W,L,T不需要任何分裂操作

 

6、插入Z时,最右的叶子结点空间满了,需要进行分裂操作,中间元素T上移到父节点中,注意通过上移中间元素,树最终还是保持平衡,分裂结果的结点存在2个关键字元素。

 

7、插入D时,导致最左边的叶子结点被分裂,D恰好也是中间元素,上移到父节点中,然后字母P,R,X,Y陆续插入不需要任何分裂操作(别忘了,树中至多5个孩子)。

 

8、最后,当插入S时,含有N,P,Q,R的结点需要分裂,把中间元素Q上移到父节点中,但是情况来了,父节点中空间已经满了,所以也要进行分裂,将父节点中的中间元素M上移到新形成的根结点中,注意以前在父节点中的第三个指针在修改后包括D和G节点中。这样具体插入操作的完成,下面介绍删除操作,删除操作相对于插入操作要考虑的情况多点。

 

1.2、删除(delete)操作

    首先查找B树中需删除的元素,如果该元素在B树中存在,则将该元素在其结点中进行删除,如果删除该元素后,首先判断该元素是否有左右孩子结点,如果有,则上移孩子结点中的某相近元素到父节点中,然后是移动之后的情况;如果没有,直接删除后,移动之后的情况。

    删除元素,移动相应元素之后,

  • 如果某结点中元素数目(即关键字数)小于ceil(m/2)-1,则需要看其某相邻兄弟结点是否丰满(结点中元素个数大于ceil(m/2)-1)(还记得第一节中关于B树的第5个特性中的c点么?: c)除根结点之外的结点(包括叶子结点)的关键字的个数n必须满足: (ceil(m / 2)-1)<= n <= m-1。m表示最多含有m个孩子,n表示关键字数。在本小节中举的一颗B树的示例中,关键字数n满足:2<=n<=4),
  • 如果丰满,则向父节点借一个元素来满足条件;
  • 如果其相邻兄弟都刚脱贫,即借了之后其结点数目小于ceil(m/2)-1,则该结点与其相邻的某一兄弟结点进行“合并”成一个结点,以此来满足条件。

    那咱们通过下面实例来详细了解吧。

    以上述插入操作构造的一棵5阶B树(树中最多含有m(m=5)个孩子,因此关键字数最小为ceil(m / 2)-1=2。还是这句话,关键字数小了(小于2个)就合并,大了(超过4个)就分裂)为例,依次删除H,T,R,E。

1、首先删除元素H,当然首先查找H,H在一个叶子结点中,且该叶子结点元素数目3大于最小元素数目ceil(m/2)-1=2,则操作很简单,咱们只需要移动K至原来H的位置,移动L至K的位置(也就是结点中删除元素后面的元素向前移动)

 

2、下一步,删除T,因为T没有在叶子结点中,而是在中间结点中找到,咱们发现他的继承者W(字母升序的下个元素),将W上移到T的位置,然后将原包含W的孩子结点中的W进行删除,这里恰好删除W后,该孩子结点中元素个数大于2,无需进行合并操作。

 

3、下一步删除R,R在叶子结点中,但是该结点中元素数目为2,删除导致只有1个元素,已经小于最小元素数目ceil(5/2)-1=2,而由前面我们已经知道:如果其某个相邻兄弟结点中比较丰满(元素个数大于ceil(5/2)-1=2),则可以向父结点借一个元素,然后将最丰满的相邻兄弟结点中上移最后或最前一个元素到父节点中(有没有看到红黑树中左旋操作的影子?),在这个实例中,右相邻兄弟结点中比较丰满(3个元素大于2),所以先向父节点借一个元素W下移到该叶子结点中,代替原来S的位置,S前移;然后X在相邻右兄弟结点中上移到父结点中,最后在相邻右兄弟结点中删除X,后面元素前移。

 

4、最后一步删除E, 删除后会导致很多问题,因为E所在的结点数目刚好达标,刚好满足最小元素个数(ceil(5/2)-1=2),而相邻的兄弟结点也是同样的情况,删除一个元素都不能满足条件,所以需要该节点与某相邻兄弟结点进行合并操作;首先移动父结点中的元素(该元素在两个需要合并的两个结点元素之间)下移到其子结点中,然后将这两个结点进行合并成一个结点。所以在该实例中,咱们首先将父节点中的元素D下移到已经删除E而只有F的结点中,然后将含有D和F的结点和含有A,C的相邻兄弟结点进行合并成一个结点。

 

5、也许你认为这样删除操作已经结束了,其实不然,在看看上图,对于这种特殊情况,你立即会发现父节点只包含一个元素G,没达标(因为非根节点包括叶子结点的关键字数n必须满足于2=

 

为了进一步详细讨论删除的情况,再举另外一个实例:

这里是一棵不同的5序B树,那咱们试着删除C

 

于是将删除元素C的右子结点中的D元素上移到C的位置,但是出现上移元素后,只有一个元素的结点的情况。

又因为含有E的结点,其相邻兄弟结点才刚脱贫(最少元素个数为2),不可能向父节点借元素,所以只能进行合并操作,于是这里将含有A,B的左兄弟结点和含有E的结点进行合并成一个结点。

 

这样又出现只含有一个元素F结点的情况,这时,其相邻的兄弟结点是丰满的(元素个数为3>最小元素个数2),这样就可以想父结点借元素了,把父结点中的J下移到该结点中,相应的如果结点中J后有元素则前移,然后相邻兄弟结点中的第一个元素(或者最后一个元素)上移到父节点中,后面的元素(或者前面的元素)前移(或者后移);注意含有K,L的结点以前依附在M的左边,现在变为依附在J的右边。这样每个结点都满足B树结构性质。

 

从以上操作可看出:除根结点之外的结点(包括叶子结点)的关键字的个数n满足:(ceil(m / 2)-1)<= n <= m-1,即2<=n<=4。这也佐证了咱们之前的观点。删除操作完。

第二部分、B+ Tree

B+Tree

    B-Tree有许多变种,其中最常见的是B+Tree,例如MySQL就普遍使用B+Tree实现其索引结构。与B-Tree相比,B+Tree有以下不同点:

  1. 每个节点的指针上限为2d而不是2d+1。
  2. 内节点不存储data,只存储key;叶子节点不存储指针。

    图3是一个简单的B+Tree示意。

image

图1

    由于并不是所有节点都具有相同的域,因此B+Tree中叶节点和内节点一般大小不同。这点与B-Tree不同,虽然B-Tree中不同节点存放的key和指针可能数量不一致,但是每个节点的域和上限是一致的,所以在实现中B-Tree往往对每个节点申请同等大小的空间。

     一般来说,B+Tree比B-Tree更适合实现外存储索引结构,具体原因与外存储器原理及计算机存取原理有关,在此不作具体讨论。

带有顺序访问指针的B+Tree

    一般在数据库系统或文件系统中使用的B+Tree结构都在经典B+Tree的基础上进行了优化,增加了顺序访问指针。

image

                                                                                                      图4

    如上图4所示,在B+Tree的每个叶子节点增加一个指向相邻叶子节点的指针,就形成了带有顺序访问指针的B+Tree。做这个优化的目的是为了提高区间访问的性能,例如图4中如果要查询key为从18到49的所有数据记录,当找到18后,只需顺着节点和指针顺序遍历就可以一次性访问到所有数据节点,极大提到了区间查询效率。(此第二部分参考自:http://www.cnblogs.com/leoo2sk/archive/2011/07/10/mysql-index.html。)

第三部分、B树的编码实现

    既然明白了B树的插入,和删除操作的原理,接下来,咱们来一步一步实现它。不过,有一点必须说明的是:这个实现只是实现了偶数序order(阶)的情况;还有奇数序order(阶)的情况没有考虑。待日后改进。

  • ok,以下是头文件:
  1. //实现对order序(阶)的B-TREE结构基本操作的封装。
  2. //查找:search,插入:insert,删除:remove。
  3. //创建:create,销毁:destory,打印:print。
  4. #ifndef BTREE_H
  5. #define BTREE_H
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. * 定义m序(阶)B 树的最小度数BTree_D=ceil(m)*/
  10. /// 在这里定义每个节点中关键字的最大数目为:2 * BTree_D - 1,即序(阶):2 * BTree_D.
  11. #define BTree_D 2
  12. #define ORDER (BTree_D * 2) //定义为4阶B-tree,2-3-4树。最简单为3阶B-tree,2-3树。
  13. //#define ORDER (BTree_T * 2-1) //最简单为3阶B-tree,2-3树。
  14. typedef int KeyType;
  15. typedef struct BTNode{
  16. int keynum; /// 结点中关键字的个数,keynum <= BTree_N
  17. KeyType key[ORDER-1]; /// 关键字向量为key[0..keynum - 1]
  18. struct BTNode* child[ORDER]; /// 孩子指针向量为child[0..keynum]
  19. bool isLeaf; /// 是否是叶子节点的标志
  20. }BTNode;
  21. typedef BTNode* BTree; ///定义BTree
  22. ///给定数据集data,创建BTree。
  23. void BTree_create(BTree* tree, const KeyType* data, int length);
  24. ///销毁BTree,释放内存空间。
  25. void BTree_destroy(BTree* tree);
  26. ///在BTree中插入关键字key。
  27. void BTree_insert(BTree* tree, KeyType key);
  28. ///在BTree中移除关键字key。
  29. void BTree_remove(BTree* tree, KeyType key);
  30. ///深度遍历BTree打印各层结点信息。
  31. void BTree_print(const BTree tree, int layer=1);
  32. /// 在BTree中查找关键字 key,
  33. /// 成功时返回找到的节点的地址及 key 在其中的位置 *pos
  34. /// 失败时返回 NULL 及查找失败时扫描到的节点位置 *pos
  35. BTNode* BTree_search(const BTree tree, int key, int* pos);
  36. #ifdef __cplusplus
  37. }
  38. #endif
  39. #endif
  • 下面是B树的实现文件:
  1. //实现对order序(阶)的B-TREE结构基本操作的封装。
  2. //查找:search,插入:insert,删除:remove。
  3. //创建:create,销毁:destory,打印:print。
  4. #include
  5. #include
  6. #include
  7. #include "btree.h"
  8. //#define max(a, b) (((a) > (b)) ? (a) : (b))
  9. #define cmp(a, b) ( ( ((a)-(b)) >= (0) ) ? (1) : (0) ) //比较a,b大小
  10. #define DEBUG_BTREE
  11. // 模拟向磁盘写入节点
  12. void disk_write(BTNode* node)
  13. {
  14. //打印出结点中的全部元素,方便调试查看keynum之后的元素是否为0(即是否存在垃圾数据);而不是keynum个元素。
  15. printf("向磁盘写入节点");
  16. for(int i=0;i-1;i++){
  17. printf("%c",node->key[i]);
  18. }
  19. printf("\n");
  20. }
  21. // 模拟从磁盘读取节点
  22. void disk_read(BTNode** node)
  23. {
  24. //打印出结点中的全部元素,方便调试查看keynum之后的元素是否为0(即是否存在垃圾数据);而不是keynum个元素。
  25. printf("向磁盘读取节点");
  26. for(int i=0;i-1;i++){
  27. printf("%c",(*node)->key[i]);
  28. }
  29. printf("\n");
  30. }
  31. // 按层次打印 B 树
  32. void BTree_print(const BTree tree, int layer)
  33. {
  34. int i;
  35. BTNode* node = tree;
  36. if (node) {
  37. printf("第 %d 层, %d node : ", layer, node->keynum);
  38. //打印出结点中的全部元素,方便调试查看keynum之后的元素是否为0(即是否存在垃圾数据);而不是keynum个元素。
  39. for (i = 0; i < ORDER-1; ++i) {
  40. //for (i = 0; i < node->keynum; ++i) {
  41. printf("%c ", node->key[i]);
  42. }
  43. printf("\n");
  44. ++layer;
  45. for (i = 0 ; i <= node->keynum; i++) {
  46. if (node->child[i]) {
  47. BTree_print(node->child[i], layer);
  48. }
  49. }
  50. }
  51. else {
  52. printf("树为空。\n");
  53. }
  54. }
  55. // 结点node内对关键字进行二分查找。
  56. int binarySearch(BTNode* node, int low, int high, KeyType Fkey)
  57. {
  58. int mid;
  59. while (low<=high)
  60. {
  61. mid = low + (high-low)/2;
  62. if (Fkeykey[mid])
  63. {
  64. high = mid-1;
  65. }
  66. if (Fkey>node->key[mid])
  67. {
  68. low = mid+1;
  69. }
  70. if (Fkey==node->key[mid])
  71. {
  72. return mid;//返回下标。
  73. }
  74. }
  75. return 0;//未找到返回0.
  76. }
  77. //insert
  78. /***************************************************************************************
  79. 将分裂的结点中的一半元素给新建的结点,并且将分裂结点中的中间关键字元素上移至父节点中。
  80. parent 是一个非满的父节点
  81. node 是 tree 孩子表中下标为 index 的孩子节点,且是满的,需分裂。
  82. *******************************************************************/
  83. void BTree_split_child(BTNode* parent, int index, BTNode* node)
  84. {
  85. #ifdef DEBUG_BTREE
  86. printf("BTree_split_child!\n");
  87. #endif
  88. assert(parent && node);
  89. int i;
  90. // 创建新节点,存储 node 中后半部分的数据
  91. BTNode* newNode = (BTNode*)calloc(sizeof(BTNode), 1);
  92. if (!newNode) {
  93. printf("Error! out of memory!\n");
  94. return;
  95. }
  96. newNode->isLeaf = node->isLeaf;
  97. newNode->keynum = BTree_D - 1;
  98. // 拷贝 node 后半部分关键字,然后将node后半部分置为0。
  99. for (i = 0; i < newNode->keynum; ++i){
  100. newNode->key[i] = node->key[BTree_D + i];
  101. node->key[BTree_D + i] = 0;
  102. }
  103. // 如果 node 不是叶子节点,拷贝 node 后半部分的指向孩子节点的指针,然后将node后半部分指向孩子节点的指针置为NULL。
  104. if (!node->isLeaf) {
  105. for (i = 0; i < BTree_D; i++) {
  106. newNode->child[i] = node->child[BTree_D + i];
  107. node->child[BTree_D + i] = NULL;
  108. }
  109. }
  110. // 将 node 分裂出 newNode 之后,里面的数据减半
  111. node->keynum = BTree_D - 1;
  112. // 调整父节点中的指向孩子的指针和关键字元素。分裂时父节点增加指向孩子的指针和关键元素。
  113. for (i = parent->keynum; i > index; --i) {
  114. parent->child[i + 1] = parent->child[i];
  115. }
  116. parent->child[index + 1] = newNode;
  117. for (i = parent->keynum - 1; i >= index; --i) {
  118. parent->key[i + 1] = parent->key[i];
  119. }
  120. parent->key[index] = node->key[BTree_D - 1];
  121. ++parent->keynum;
  122. node->key[BTree_D - 1] = 0;
  123. // 写入磁盘
  124. disk_write(parent);
  125. disk_write(newNode);
  126. disk_write(node);
  127. }
  128. void BTree_insert_nonfull(BTNode* node, KeyType key)
  129. {
  130. assert(node);
  131. int i;
  132. // 节点是叶子节点,直接插入
  133. if (node->isLeaf) {
  134. i = node->keynum - 1;
  135. while (i >= 0 && key < node->key[i]) {
  136. node->key[i + 1] = node->key[i];
  137. --i;
  138. }
  139. node->key[i + 1] = key;
  140. ++node->keynum;
  141. // 写入磁盘
  142. disk_write(node);
  143. }
  144. // 节点是内部节点
  145. else {
  146. /* 查找插入的位置*/
  147. i = node->keynum - 1;
  148. while (i >= 0 && key < node->key[i]) {
  149. --i;
  150. }
  151. ++i;
  152. // 从磁盘读取孩子节点
  153. disk_read(&node->child[i]);
  154. // 如果该孩子节点已满,分裂调整值
  155. if (node->child[i]->keynum == (ORDER-1)) {
  156. BTree_split_child(node, i, node->child[i]);
  157. // 如果待插入的关键字大于该分裂结点中上移到父节点的关键字,在该关键字的右孩子结点中进行插入操作。
  158. if (key > node->key[i]) {
  159. ++i;
  160. }
  161. }
  162. BTree_insert_nonfull(node->child[i], key);
  163. }
  164. }
  165. void BTree_insert(BTree* tree, KeyType key)
  166. {
  167. #ifdef DEBUG_BTREE
  168. printf("BTree_insert:\n");
  169. #endif
  170. BTNode* node;
  171. BTNode* root = *tree;
  172. // 树为空
  173. if (NULL == root) {
  174. root = (BTNode*)calloc(sizeof(BTNode), 1);
  175. if (!root) {
  176. printf("Error! out of memory!\n");
  177. return;
  178. }
  179. root->isLeaf = true;
  180. root->keynum = 1;
  181. root->key[0] = key;
  182. *tree = root;
  183. // 写入磁盘
  184. disk_write(root);
  185. return;
  186. }
  187. // 根节点已满,插入前需要进行分裂调整
  188. if (root->keynum == (ORDER-1)) {
  189. // 产生新节点当作根
  190. node = (BTNode*)calloc(sizeof(BTNode), 1);
  191. if (!node) {
  192. printf("Error! out of memory!\n");
  193. return;
  194. }
  195. *tree = node;
  196. node->isLeaf = false;
  197. node->keynum = 0;
  198. node->child[0] = root;
  199. BTree_split_child(node, 0, root);
  200. BTree_insert_nonfull(node, key);
  201. }
  202. // 根节点未满,在当前节点中插入 key
  203. else {
  204. BTree_insert_nonfull(root, key);
  205. }
  206. }
  207.  //remove 
  208. // 对 tree 中的节点 node 进行合并孩子节点处理.
  209. // 注意:孩子节点的 keynum 必须均已达到下限,即均等于 BTree_D - 1
  210. // 将 tree 中索引为 index 的 key 下移至左孩子结点中,
  211. // 将 node 中索引为 index + 1 的孩子节点合并到索引为 index 的孩子节点中,右孩子合并到左孩子结点中。
  212. // 并调相关的 key 和指针。void BTree_merge_child(BTree* tree, BTNode* node, int index)
  213. {
  214. #ifdef DEBUG_BTREE
  215. printf("BTree_merge_child!\n");
  216. #endif
  217. assert(tree && node && index >= 0 && index < node->keynum);
  218. int i;
  219. KeyType key = node->key[index];
  220. BTNode* leftChild = node->child[index];
  221. BTNode* rightChild = node->child[index + 1];
  222. assert(leftChild && leftChild->keynum == BTree_D - 1
  223. && rightChild && rightChild->keynum == BTree_D - 1);
  224. // 将 node中关键字下标为index 的 key 下移至左孩子结点中,该key所对应的右孩子结点指向node的右孩子结点中的第一个孩子。
  225. leftChild->key[leftChild->keynum] = key;
  226. leftChild->child[leftChild->keynum + 1] = rightChild->child[0];
  227. ++leftChild->keynum;
  228. // 右孩子的元素合并到左孩子结点中。
  229. for (i = 0; i < rightChild->keynum; ++i) {
  230. leftChild->key[leftChild->keynum] = rightChild->key[i];
  231. leftChild->child[leftChild->keynum + 1] = rightChild->child[i + 1];
  232. ++leftChild->keynum;
  233. }
  234. // 在 node 中下移的 key后面的元素前移
  235. for (i = index; i < node->keynum - 1; ++i) {
  236. node->key[i] = node->key[i + 1];
  237. node->child[i + 1] = node->child[i + 2];
  238. }
  239. node->key[node->keynum - 1] = 0;
  240. node->child[node->keynum] = NULL;
  241. --node->keynum;
  242. // 如果根节点没有 key 了,并将根节点调整为合并后的左孩子节点;然后删除释放空间。
  243. if (node->keynum == 0) {
  244. if (*tree == node) {
  245. *tree = leftChild;
  246. }
  247. free(node);
  248. node = NULL;
  249. }
  250. free(rightChild);
  251. rightChild = NULL;
  252. }
  253. void BTree_recursive_remove(BTree* tree, KeyType key)
  254. {
  255. // B-数的保持条件之一:
  256. // 非根节点的内部节点的关键字数目不能少于 BTree_D - 1
  257. int i, j, index;
  258. BTNode *root = *tree;
  259. BTNode *node = root;
  260. if (!root) {
  261. printf("Failed to remove %c, it is not in the tree!\n", key);
  262. return;
  263. }
  264. // 结点中找key。
  265. index = 0;
  266. while (index < node->keynum && key > node->key[index]) {
  267. ++index;
  268. }
  269. /*======================含有key的当前结点时的情况====================
  270. node:
  271. index of Key: i-1 i i+1
  272. +---+---+---+---+
  273. * key *
  274. +---+---+---+---+---+
  275. / \
  276. index of Child: i i+1
  277. / \
  278. +---+---+ +---+---+
  279. * * * *
  280. +---+---+---+ +---+---+---+
  281. leftChild rightChild
  282. ============================================================*/
  283. /*一、结点中找到了关键字key的情况.*/
  284. BTNode *leftChild, *rightChild;
  285. KeyType leftKey, rightKey;
  286. if (index < node->keynum && node->key[index] == key) {
  287. /* 1,所在节点是叶子节点,直接删除*/
  288. if (node->isLeaf) {
  289. for (i = index; i < node->keynum-1; ++i) {
  290. node->key[i] = node->key[i + 1];
  291. //node->child[i + 1] = node->child[i + 2];叶子节点的孩子结点为空,无需移动处理。
  292. }
  293. node->key[node->keynum-1] = 0;
  294. //node->child[node->keynum] = NULL;
  295. --node->keynum;
  296. if (node->keynum == 0) {
  297. assert(node == *tree);
  298. free(node);
  299. *tree = NULL;
  300. }
  301. return;
  302. }
  303. /*2.选择脱贫致富的孩子结点。*/
  304. // 2a,选择相对富有的左孩子结点。
  305. // 如果位于 key 前的左孩子结点的 key 数目 >= BTree_D,
  306. // 在其中找 key 的左孩子结点的最后一个元素上移至父节点key的位置。
  307. // 然后在左孩子节点中递归删除元素leftKey。
  308. else if (node->child[index]->keynum >= BTree_D) {
  309. leftChild = node->child[index];
  310. leftKey = leftChild->key[leftChild->keynum - 1];
  311. node->key[index] = leftKey;
  312. BTree_recursive_remove(&leftChild, leftKey);
  313. }
  314. // 2b,选择相对富有的右孩子结点。
  315. // 如果位于 key 后的右孩子结点的 key 数目 >= BTree_D,
  316. // 在其中找 key 的右孩子结点的第一个元素上移至父节点key的位置
  317. // 然后在右孩子节点中递归删除元素rightKey。
  318. else if (node->child[index + 1]->keynum >= BTree_D) {
  319. rightChild = node->child[index + 1];
  320. rightKey = rightChild->key[0];
  321. node->key[index] = rightKey;
  322. BTree_recursive_remove(&rightChild, rightKey);
  323. }
  324. /*左右孩子结点都刚脱贫。删除前需要孩子结点的合并操作*/
  325. // 2c,左右孩子结点只包含 BTree_D - 1 个节点,
  326. // 合并是将 key 下移至左孩子节点,并将右孩子节点合并到左孩子节点中,
  327. // 删除右孩子节点,在父节点node中移除 key 和指向右孩子节点的指针,
  328. // 然后在合并了的左孩子节点中递归删除元素key。
  329. else if (node->child[index]->keynum == BTree_D - 1
  330. && node->child[index + 1]->keynum == BTree_D - 1){
  331. leftChild = node->child[index];
  332. BTree_merge_child(tree, node, index);
  333. // 在合并了的左孩子节点中递归删除 key
  334. BTree_recursive_remove(&leftChild, key);
  335. }
  336. }
  337. /*======================未含有key的当前结点时的情况====================
  338. node:
  339. index of Key: i-1 i i+1
  340. +---+---+---+---+
  341. * keyi *
  342. +---+---+---+---+---+
  343. / | \
  344. index of Child: i-1 i i+1
  345. / | \
  346. +---+---+ +---+---+ +---+---+
  347. * * * * * *
  348. +---+---+---+ +---+---+---+ +---+---+---+
  349. leftSibling Child rightSibling
  350. ============================================================*/
  351. /*二、结点中未找到了关键字key的情况.*/
  352. else {
  353. BTNode *leftSibling, *rightSibling, *child;
  354. // 3. key 不在内节点 node 中,则应当在某个包含 key 的子节点中。
  355. // key < node->key[index], 所以 key 应当在孩子节点 node->child[index] 中
  356. child = node->child[index];
  357. if (!child) {
  358. printf("Failed to remove %c, it is not in the tree!\n", key);
  359. return;
  360. }
  361. /*所需查找的该孩子结点刚脱贫的情况*/
  362. if (child->keynum == BTree_D - 1) {
  363. leftSibling = NULL;
  364. rightSibling = NULL;
  365. if (index - 1 >= 0) {
  366. leftSibling = node->child[index - 1];
  367. }
  368. if (index + 1 <= node->keynum) {
  369. rightSibling = node->child[index + 1];
  370. }
  371. /*选择致富的相邻兄弟结点。*/
  372. // 3a,如果所在孩子节点相邻的兄弟节点中有节点至少包含 BTree_D 个关键字
  373. // 将 node 的一个关键字key[index]下移到 child 中,将相对富有的相邻兄弟节点中一个关键字上移到
  374. // node 中,然后在 child 孩子节点中递归删除 key。
  375. if ((leftSibling && leftSibling->keynum >= BTree_D)
  376. || (rightSibling && rightSibling->keynum >= BTree_D)) {
  377. int richR = 0;
  378. if(rightSibling) richR = 1;
  379. if(leftSibling && rightSibling) {
  380. richR = cmp(rightSibling->keynum,leftSibling->keynum);
  381. }
  382. if (rightSibling && rightSibling->keynum >= BTree_D && richR) {
  383. //相邻右兄弟相对富有,则该孩子先向父节点借一个元素,右兄弟中的第一个元素上移至父节点所借位置,并进行相应调整。
  384. child->key[child->keynum] = node->key[index];
  385. child->child[child->keynum + 1] = rightSibling->child[0];
  386. ++child->keynum;
  387. node->key[index] = rightSibling->key[0];
  388. for (j = 0; j < rightSibling->keynum - 1; ++j) {//元素前移
  389. rightSibling->key[j] = rightSibling->key[j + 1];
  390. rightSibling->child[j] = rightSibling->child[j + 1];
  391. }
  392. rightSibling->key[rightSibling->keynum-1] = 0;
  393. rightSibling->child[rightSibling->keynum-1] = rightSibling->child[rightSibling->keynum];
  394. rightSibling->child[rightSibling->keynum] = NULL;
  395. --rightSibling->keynum;
  396. }
  397. else {//相邻左兄弟相对富有,则该孩子向父节点借一个元素,左兄弟中的最后元素上移至父节点所借位置,并进行相应调整。
  398. for (j = child->keynum; j > 0; --j) {//元素后移
  399. child->key[j] = child->key[j - 1];
  400. child->child[j + 1] = child->child[j];
  401. }
  402. child->child[1] = child->child[0];
  403. child->child[0] = leftSibling->child[leftSibling->keynum];
  404. child->key[0] = node->key[index - 1];
  405. ++child->keynum;
  406. node->key[index - 1] = leftSibling->key[leftSibling->keynum - 1];
  407. leftSibling->key[leftSibling->keynum - 1] = 0;
  408. leftSibling->child[leftSibling->keynum] = NULL;
  409. --leftSibling->keynum;
  410. }
  411. }
  412. /*相邻兄弟结点都刚脱贫。删除前需要兄弟结点的合并操作,*/
  413. // 3b, 如果所在孩子节点相邻的兄弟节点都只包含 BTree_D - 1 个关键字,
  414. // 将 child 与其一相邻节点合并,并将 node 中的一个关键字下降到合并节点中,
  415. // 再在 node 中删除那个关键字和相关指针,若 node 的 key 为空,删之,并调整根为合并结点。
  416. // 最后,在相关孩子节点child中递归删除 key。
  417. else if ((!leftSibling || (leftSibling && leftSibling->keynum == BTree_D - 1))
  418. && (!rightSibling || (rightSibling && rightSibling->keynum == BTree_D - 1))) {
  419. if (leftSibling && leftSibling->keynum == BTree_D - 1) {
  420. BTree_merge_child(tree, node, index - 1);//node中的右孩子元素合并到左孩子中。
  421. child = leftSibling;
  422. }
  423. else if (rightSibling && rightSibling->keynum == BTree_D - 1) {
  424. BTree_merge_child(tree, node, index);//node中的右孩子元素合并到左孩子中。
  425. }
  426. }
  427. }
  428. BTree_recursive_remove(&child, key);//调整后,在key所在孩子结点中继续递归删除key。
  429. }
  430. }
  431. void BTree_remove(BTree* tree, KeyType key)
  432. {
  433. #ifdef DEBUG_BTREE
  434. printf("BTree_remove:\n");
  435. #endif
  436. if (*tree==NULL)
  437. {
  438. printf("BTree is NULL!\n");
  439. return;
  440. }
  441. BTree_recursive_remove(tree, key);
  442. }
  443. //=====================================search====================================
  444. BTNode* BTree_recursive_search(const BTree tree, KeyType key, int* pos)
  445. {
  446. int i = 0;
  447. while (i < tree->keynum && key > tree->key[i]) {
  448. ++i;
  449. }
  450. // Find the key.
  451. if (i < tree->keynum && tree->key[i] == key) {
  452. *pos = i;
  453. return tree;
  454. }
  455. // tree 为叶子节点,找不到 key,查找失败返回
  456. if (tree->isLeaf) {
  457. return NULL;
  458. }
  459. // 节点内查找失败,但 tree->key[i - 1]< key < tree->key[i],
  460. // 下一个查找的结点应为 child[i]
  461. // 从磁盘读取第 i 个孩子的数据
  462. disk_read(&tree->child[i]);
  463. // 递归地继续查找于树 tree->child[i]
  464. return BTree_recursive_search(tree->child[i], key, pos);
  465. }
  466. BTNode* BTree_search(const BTree tree, KeyType key, int* pos)
  467. {
  468. #ifdef DEBUG_BTREE
  469. printf("BTree_search:\n");
  470. #endif
  471. if (!tree) {
  472. printf("BTree is NULL!\n");
  473. return NULL;
  474. }
  475. *pos = -1;
  476. return BTree_recursive_search(tree,key,pos);
  477. }
  478. //===============================create===============================
  479. void BTree_create(BTree* tree, const KeyType* data, int length)
  480. {
  481. assert(tree);
  482. int i;
  483. #ifdef DEBUG_BTREE
  484. printf("\n 开始创建 B-树,关键字为:\n");
  485. for (i = 0; i < length; i++) {
  486. printf(" %c ", data[i]);
  487. }
  488. printf("\n");
  489. #endif
  490. for (i = 0; i < length; i++) {
  491. #ifdef DEBUG_BTREE
  492. printf("\n插入关键字 %c:\n", data[i]);
  493. #endif
  494. int pos = -1;
  495. BTree_search(*tree,data[i],&pos);//树的递归搜索。
  496. if (pos!=-1)
  497. {
  498. printf("this key %c is in the B-tree,not to insert.\n",data[i]);
  499. }else{
  500. BTree_insert(tree, data[i]);//插入元素到BTree中。
  501. }
  502. #ifdef DEBUG_BTREE
  503. BTree_print(*tree);//树的深度遍历。
  504. #endif
  505. }
  506. printf("\n");
  507. }
  508. //===============================destroy===============================
  509. void BTree_destroy(BTree* tree)
  510. {
  511. int i;
  512. BTNode* node = *tree;
  513. if (node) {
  514. for (i = 0; i <= node->keynum; i++) {
  515. BTree_destroy(&node->child[i]);
  516. }
  517. free(node);
  518. }
  519. *tree = NULL;
  520. }
  • 最后,给出测试文件:
  1. //测试order序(阶)的B-TREE结构基本操作。
  2. //查找:search,插入:insert,删除:remove。
  3. //创建:create,销毁:destory,打印:print。
  4. #include
  5. #include "btree.h"
  6. void test_BTree_search(BTree tree, KeyType key)
  7. {
  8. int pos = -1;
  9. BTNode* node = BTree_search(tree, key, &pos);
  10. if (node) {
  11. printf("在%s节点(包含 %d 个关键字)中找到关键字 %c,其索引为 %d\n",
  12. node->isLeaf ? "叶子" : "非叶子",
  13. node->keynum, key, pos);
  14. }
  15. else {
  16. printf("在树中找不到关键字 %c\n", key);
  17. }
  18. }
  19. void test_BTree_remove(BTree* tree, KeyType key)
  20. {
  21. printf("\n移除关键字 %c \n", key);
  22. BTree_remove(tree, key);
  23. BTree_print(*tree);
  24. printf("\n");
  25. }
  26. void test_btree()
  27. {
  28. KeyType array[] = {
  29. 'G','G', 'M', 'P', 'X', 'A', 'C', 'D', 'E', 'J', 'K',
  30. 'N', 'O', 'R', 'S', 'T', 'U', 'V', 'Y', 'Z', 'F', 'X'
  31. };
  32. const int length = sizeof(array)/sizeof(KeyType);
  33. BTree tree = NULL;
  34. BTNode* node = NULL;
  35. int pos = -1;
  36. KeyType key1 = 'R'; // in the tree.
  37. KeyType key2 = 'B'; // not in the tree.
  38. // 创建
  39. BTree_create(&tree, array, length);
  40. printf("\n=== 创建 B- 树 ===\n");
  41. BTree_print(tree);
  42. printf("\n");
  43. // 查找
  44. test_BTree_search(tree, key1);
  45. printf("\n");
  46. test_BTree_search(tree, key2);
  47. // 移除不在B树中的元素
  48. test_BTree_remove(&tree, key2);
  49. printf("\n");
  50. // 插入关键字
  51. printf("\n插入关键字 %c \n", key2);
  52. BTree_insert(&tree, key2);
  53. BTree_print(tree);
  54. printf("\n");
  55. test_BTree_search(tree, key2);
  56. // 移除关键字
  57. test_BTree_remove(&tree, key2);
  58. test_BTree_search(tree, key2);
  59. key2 = 'M';
  60. test_BTree_remove(&tree, key2);
  61. test_BTree_search(tree, key2);
  62. key2 = 'E';
  63. test_BTree_remove(&tree, key2);
  64. test_BTree_search(tree, key2);
  65. key2 = 'G';
  66. test_BTree_remove(&tree, key2);
  67. test_BTree_search(tree, key2);
  68. key2 = 'A';
  69. test_BTree_remove(&tree, key2);
  70. test_BTree_search(tree, key2);
  71. key2 = 'D';
  72. test_BTree_remove(&tree, key2);
  73. test_BTree_search(tree, key2);
  74. key2 = 'K';
  75. test_BTree_remove(&tree, key2);
  76. test_BTree_search(tree, key2);
  77. key2 = 'P';
  78. test_BTree_remove(&tree, key2);
  79. test_BTree_search(tree, key2);
  80. key2 = 'J';
  81. test_BTree_remove(&tree, key2);
  82. test_BTree_search(tree, key2);
  83. key2 = 'C';
  84. test_BTree_remove(&tree, key2);
  85. test_BTree_search(tree, key2);
  86. key2 = 'X';
  87. test_BTree_remove(&tree, key2);
  88. test_BTree_search(tree, key2);
  89. key2 = 'O';
  90. test_BTree_remove(&tree, key2);
  91. test_BTree_search(tree, key2);
  92. key2 = 'V';
  93. test_BTree_remove(&tree, key2);
  94. test_BTree_search(tree, key2);
  95. key2 = 'R';
  96. test_BTree_remove(&tree, key2);
  97. test_BTree_search(tree, key2);
  98. key2 = 'U';
  99. test_BTree_remove(&tree, key2);
  100. test_BTree_search(tree, key2);
  101. key2 = 'T';
  102. test_BTree_remove(&tree, key2);
  103. test_BTree_search(tree, key2);
  104. key2 = 'N';
  105. test_BTree_remove(&tree, key2);
  106. test_BTree_search(tree, key2);
  107. key2 = 'S';
  108. test_BTree_remove(&tree, key2);
  109. test_BTree_search(tree, key2);
  110. key2 = 'Y';
  111. test_BTree_remove(&tree, key2);
  112. test_BTree_search(tree, key2);
  113. key2 = 'F';
  114. test_BTree_remove(&tree, key2);
  115. test_BTree_search(tree, key2);
  116. key2 = 'Z';
  117. test_BTree_remove(&tree, key2);
  118. test_BTree_search(tree, key2);
  119. // 销毁
  120. BTree_destroy(&tree);
  121. }
  122. int main()
  123. {
  124. test_btree();
  125. return 0;
  126. }

    运行结果部分截图如下:



参考:

  1. http://www.cppblog.com/converse/archive/2009/10/13/98521.html;
  2. 此外,这里有一份google关于B树的C++实现:https://code.google.com/p/cpp-btree/downloads/detail?name=cpp-btree-1.0.1.tar.gz。

联系作者:

    若有任何问题,欢迎随时不吝指正。或者联系我们:

  • weedge,E-mail:[email protected]
  • July, [email protected]

后记:

    本blog日后会更多的关注数据结构与算法之外的东西,如分布式架构,海量数据处理,搜索引擎相关。毕竟,算法之外的东西,如瀚海般无止境,要学的东西,还有很多。

    若转载,请注明出处,谢谢。完。二零一一年八月三十一日。

文章知识点与官方知识档案匹配,可进一步学习相关知识
算法技能树首页概览56481 人正在系统学习中
注:本文转载自blog.csdn.net的v_JULY_v的文章"http://blog.csdn.net/v_july_v/article/details/6735293"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top