当前位置:   article > 正文

算法导论 第十八章;B 树_算法导论 第18章答案

算法导论 第18章答案

       B树是为磁盘或其他直接存取辅助存储设备而设计的一种平衡查找树。B树的”分支因子“可能很大,即每个节点可以有很多子女。这一因子由所用磁盘特性所决定,并且可以降低磁盘I/O操作次数。许多数据库系统都使用B树或B树的变形来存储信息。

B树结构形式如下:

其特点:

1)每个节点x有以下域:

a)  x.n:当前存储在节点x中的关键字

b) x.n 个key值,以非降序顺序存放,即 x.key(1) ≤ x.key(2) ≤ ... ≤ x.key(x.n)

c) x.leaf:bool型,若为叶子节点 x.leaf=TRUE,反之为FALUSE

2) 每个节点x包含x.n+1个指向其子女的指针x.c(1),x.c(2),...x.c(x.n+1)。(叶子节点无此域)

3)各关键字x.key(i)对存储在各子树中的关键字范围加以分隔:如果k(i)为存储在x.c(i)为根的子树中的关键字,则:

        

4) 每个叶子节点具有相同的深度,即树的高度 h

5) 每个节点包含的key数有一个上界和下界,这些界可以用一个称为B树的最小度数的固定整数 t ≥ 2 来表示:

a) 每个非根的节点必须至少有t-1个 key. 每个非根内节点至少有t个子女。如果树非空,则根节点至少包含一个key.

b) 每个节点可包含至多2t-1个key。所以一个内节点至多可能有2t个子女。如果一个节点恰好有2t-1个key,则称该点是满的。

6)如果n ≥ 1,则对任意的一棵包含n个关键字,高度为h,最小度t ≥ 2的B树T,有:

             


B树插入和搜索操作的完整代码如下:

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #define Disk_Write(x)
  5. #define Disk_Read(x)
  6. #define t 2
  7. #define N 2*t
  8. using namespace std;
  9. typedef struct BTNode{
  10. int n; //the number of keys storing in the current node
  11. char key[N]; //N keys storing in nondecreasing order
  12. bool leaf; //TRUE:left;FALSE:internal node
  13. BTNode *c[N+1]; //point n+1 children
  14. }BTNode;
  15. typedef struct BTree{
  16. BTNode *root;
  17. }BTree;
  18. void BTree_Print(BTNode *x)
  19. {
  20. for(int i=1;i<=x->n;i++)
  21. {
  22. if(x->leaf == false)
  23. BTree_Print(x->c[i]);
  24. cout<< x->key[i]<<" ";
  25. }
  26. if(x->leaf == false)
  27. BTree_Print(x->c[x->n+1]);
  28. }
  29. void BTree_SplitChild(BTNode *x,int i)
  30. {
  31. BTNode *z=new BTNode();
  32. BTNode *y=x->c[i]; //split y (2t-1 keys) into y (t-1 keys) and z(t-1 keys)
  33. z->leaf=y->leaf;
  34. z->n=t-1;
  35. for(int j=1 ; j<=t-1 ; j++)
  36. z->key[j]=y->key[t+j];
  37. if(y->leaf==false)//if y has children ,copy its children to z
  38. for(int j=1; j<=t; j++)
  39. z->c[j]=y->c[j+t];
  40. y->n=t-1;
  41. //let z become the (i+1)th child of x
  42. for(int j=x->n+1; j>=i+1; j--)
  43. x->c[j+1]=x->c[j];
  44. x->c[i+1]=z;
  45. //insert the (t)th key of y into (i)th index of x
  46. for(int j=x->n; j>=i; j--)
  47. x->key[j+1]=x->key[j];
  48. x->key[i]=y->key[t];
  49. x->n++;
  50. Disk_Write(y);
  51. Disk_Write(z);
  52. Disk_Write(x);
  53. }
  54. void BTree_Insert_Nonfull(BTNode *x,char k)
  55. {
  56. int i=x->n;
  57. if(x->leaf)
  58. {//x node is leaf
  59. while(i>=1 && k < x->key[i]) //search for the insert index
  60. {
  61. x->key[i+1]=x->key[i];
  62. i--;
  63. }
  64. //insert key
  65. x->key[i+1]=k;
  66. x->n++;
  67. Disk_Write(x);
  68. }
  69. else // x node is not leaf
  70. {
  71. while(i>=1 && k < x->key[i])
  72. i--;
  73. i++;
  74. //Read its child,and insert the key into its child node
  75. Disk_Read(x->c[i]);
  76. //case 1: the child is full
  77. if(x->c[i]->n == 2*t-1)
  78. {
  79. BTree_SplitChild(x,i);
  80. if(k > x->key[i])
  81. i++;
  82. }
  83. //case 2:the child is not full
  84. BTree_Insert_Nonfull(x->c[i],k);
  85. }
  86. }
  87. void BTree_Insert(BTree *T,char k)
  88. {
  89. BTNode *r=T->root;
  90. if(r->n == 2*t-1)
  91. {//root node is full
  92. //a new node s becomes the root
  93. BTNode *s=new BTNode();
  94. T->root=s;
  95. s->leaf=false;
  96. s->n=0;
  97. s->c[1]=r;
  98. //split the original root into two chilren of s
  99. BTree_SplitChild(s,1);
  100. //insert the key into the nonfull node
  101. BTree_Insert_Nonfull(s,k);
  102. }
  103. else//root node is not full
  104. BTree_Insert_Nonfull(r,k); //insert key into root node directly
  105. }
  106. BTNode *BTree_Search(BTNode *x,char k,int &i)
  107. {//return pair(y,i)consisting of a node y and an index i such that y.keyi=k
  108. i=1;
  109. while(i <= x->n && k > x->key[i])
  110. i++;
  111. if(i <= x->n && k == x->key[i])
  112. return x;
  113. else if(x->leaf)
  114. {
  115. i=0;
  116. return NULL;
  117. }
  118. else
  119. {
  120. Disk_Read(x->c[i]);
  121. return BTree_Search(x->c[i],k,i);
  122. }
  123. }
  124. void BTree_Create(BTree *T,string ch)
  125. {
  126. //first,create an empty root node
  127. BTNode *x=new BTNode();
  128. x->leaf=true;
  129. x->n=0;
  130. Disk_Write(x);
  131. T->root=x;
  132. //second,add new keys into T by calling Insert method
  133. for(int i=0;i<ch.length();i++)
  134. BTree_Insert(T,ch[i]);
  135. }
  136. void BTree_PrintDetail(BTNode *x)
  137. {
  138. cout<<"The root is:";
  139. for(int i=1;i<=x->n;i++)
  140. cout<<x->key[i]<<" ";
  141. cout<<endl;
  142. cout<<"The root's child is:"<<endl;
  143. for(int j=1;j<=x->n+1;j++)
  144. {
  145. BTNode *child=x->c[j];
  146. for(int i=1;i<=child->n;i++)
  147. cout<<child->key[i]<<" ";
  148. cout<<endl;
  149. }
  150. for(int i=1;i<=x->n+1;i++)
  151. {
  152. cout<<"The "<<i<<" child"<<endl;
  153. BTNode *child0=x->c[i];
  154. int m=child0->n+1;
  155. for(int j=1;j<=m;j++)
  156. {
  157. BTNode *c1=child0->c[j];
  158. for(int jj=1;jj<=c1->n;jj++)
  159. cout<<c1->key[jj]<<" ";
  160. cout<<endl;
  161. }
  162. }
  163. }
  164. int main()
  165. {
  166. //string test_ch={'F','S','Q','K','C','L','H','T','V','W','M','R','N','P','A','B','X','Y','D','Z','E'};
  167. string test_ch="FSQKCLHTVWMRNPABXYDZE";
  168. cout<<"/*----------------------------Create B-tree---------------------------*/"<<endl;
  169. BTree *T=new BTree();
  170. BTree_Create(T,test_ch);
  171. cout<<"After creating ,the B-tree(its degree is "<<t<<"):"<<endl;
  172. BTree_Print(T->root);
  173. cout<<endl;
  174. cout<<"The detail B-tree is:"<<endl;
  175. BTree_PrintDetail(T->root);
  176. cout<<"/*--------------------------------------------------------------------*/"<<endl;
  177. cout<<"/*---------------------------Insert B-tree----------------------------*/"<<endl;
  178. char ich;
  179. cout<<"Please input the inserting char:";
  180. cin>>ich;
  181. BTree_Insert(T,ich);
  182. cout<<"After inserting ,the B-tree:"<<endl;
  183. BTree_Print(T->root);
  184. cout<<endl;
  185. cout<<"The detail of B-tree is:"<<endl;
  186. BTree_PrintDetail(T->root);
  187. cout<<"/*---------------------------------------------------------------------*/"<<endl;
  188. cout<<"/*--------------------------Search B-tree------------------------------*/"<<endl;
  189. char sch;
  190. BTNode *sNode=NULL;
  191. int index;
  192. cout<<"Please input the searching char:";
  193. cin>>sch;
  194. sNode=BTree_Search(T->root,sch,index);
  195. if(sNode==NULL)
  196. cout<<"The key doesn't exist in the B-tree."<<endl;
  197. else
  198. {
  199. cout<<"The key in the Node:";
  200. for(int i=1;i<=sNode->n;i++)
  201. cout<<sNode->key[i]<<" ";
  202. cout<<endl;
  203. cout<<"The index of the key in the node is:"<<index<<endl;
  204. }
  205. cout<<"/*---------------------------------------------------------------------*/"<<endl;
  206. return 0;
  207. }
运行结果:


【注:若有错误,请指正~~~】


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/375473
推荐阅读
相关标签
  

闽ICP备14008679号