BTREE
BTREE
1. contains all the keys and the tree's topology is represented by the
organization of data in this file.
2. a file that contains all the objects and information stored by the “tree”.
Objects contained here are referenced by block pointer references
stored in the index file.
3. keeps keys in sorted order for sequential traversing
4. uses a hierarchical index to minimize the number of disk reads
5. uses partially full blocks to speed insertions and deletions
6. keeps the index balanced with a recursive algorithm
Disadvantages:
1. B-tree has lower branching factor when compared to b+tree, which
increases tree height and average number of disk seeks.
2. A B-tree that doesn't fit RAM requires more disk seeks on-accurate
average than a hash table, when performing a random lookup. B-tree
is much more complicated than a hash table to implement.
3. If the keys are dens then the random access of an array is faster, and
wastes less memory than b-tree. B-tree is much more complicated to
implement than an array.
4. B-trees are not very efficient, when compared to other balanced
trees, when they reside in RAM. Inserting or deleting elements
involve moving many keys/values around. A b-tree has a chance to
be efficient in RAM, if it has a very low branching factor, so that a
node fits a cache line. This may lead to cache miss minimization. (I
have got mixed results for a b+tree in RAM).
Code in C++:
1. #include<stdio.h>
2. #include<conio.h>
3. #include<iostream>
4. using namespace std;
5. struct BTreeNode
6. {
7. int *data;
8. BTreeNode **child_ptr;
9. bool leaf;
10. int n;
11. }*root = NULL, *np = NULL, *x = NULL;
12. BTreeNode * init()
13. {
14. int i;
15. np = new BTreeNode;
16. np->data = new int[5];
17. np->child_ptr = new BTreeNode *[6];
18. np->leaf = true;
19. np->n = 0;
20. for (i = 0; i < 6; i++)
21. {
22. np->child_ptr[i] = NULL;
23. }
24. return np;
25. }
26. void traverse(BTreeNode *p)
27. {
28. cout<<endl;
29. int i;
30. for (i = 0; i < p->n; i++)
31. {
32. if (p->leaf == false)
33. {
34. traverse(p->child_ptr[i]);
35. }
36. cout << " " << p->data[i];
37. }
38. if (p->leaf == false)
39. {
40. traverse(p->child_ptr[i]);
41. }
42. cout<<endl;
43. }
44. void sort(int *p, int n)
45. {
46. int i, j, temp;
47. for (i = 0; i < n; i++)
48. {
49. for (j = i; j <= n; j++)
50. {
51. if (p[i] > p[j])
52. {
53. temp = p[i];
54. p[i] = p[j];
55. p[j] = temp;
56. }
57. }
58. }
59. }
60. int split_child(BTreeNode *x, int i)
61. {
62. int j, mid;
63. BTreeNode *np1, *np3, *y;
64. np3 = init();
65. np3->leaf = true;
66. if (i == -1)
67. {
68. mid = x->data[2];
69. x->data[2] = 0;
70. x->n--;
71. np1 = init();
72. np1->leaf = false;
73. x->leaf = true;
74. for (j = 3; j < 5; j++)
75. {
76. np3->data[j - 3] = x->data[j];
77. np3->child_ptr[j - 3] = x->child_ptr[j];
78. np3->n++;
79. x->data[j] = 0;
80. x->n--;
81. }
82. for (j = 0; j < 6; j++)
83. {
84. x->child_ptr[j] = NULL;
85. }
86. np1->data[0] = mid;
87. np1->child_ptr[np1->n] = x;
88. np1->child_ptr[np1->n + 1] = np3;
89. np1->n++;
90. root = np1;
91. }
92. else
93. {
94. y = x->child_ptr[i];
95. mid = y->data[2];
96. y->data[2] = 0;
97. y->n--;
98. for (j = 3; j < 5; j++)
99. {
100. np3->data[j - 3] = y->data[j];
101. np3->n++;
102. y->data[j] = 0;
103. y->n--;
104. }
105. x->child_ptr[i + 1] = y;
106. x->child_ptr[i + 1] = np3;
107. }
108. return mid;
109. }
110. void insert(int a)
111. {
112. int i, temp;
113. x = root;
114. if (x == NULL)
115. {
116. root = init();
117. x = root;
118. }
119. else
120. {
121. if (x->leaf == true && x->n == 5)
122. {
123. temp = split_child(x, -1);
124. x = root;
125. for (i = 0; i < (x->n); i++)
126. {
127. if ((a > x->data[i]) && (a < x->data[i + 1]))
128. {
129. i++;
130. break;
131. }
132. else if (a < x->data[0])
133. {
134. break;
135. }
136. else
137. {
138. continue;
139. }
140. }
141. x = x->child_ptr[i];
142. }
143. else
144. {
145. while (x->leaf == false)
146. {
147. for (i = 0; i < (x->n); i++)
148. {
149. if ((a > x->data[i]) && (a < x->data[i + 1]))
150. {
151. i++;
152. break;
153. }
154. else if (a < x->data[0])
155. {
156. break;
157. }
158. else
159. {
160. continue;
161. }
162. }
163. if ((x->child_ptr[i])->n == 5)
164. {
165. temp = split_child(x, i);
166. x->data[x->n] = temp;
167. x->n++;
168. continue;
169. }
170. else
171. {
172. x = x->child_ptr[i];
173. }
174. }
175. }
176. }
177. x->data[x->n] = a;
178. sort(x->data, x->n);
179. x->n++;
180. }
181. int main()
182. {
183. int i, n, t;
184. cout<<"enter the no of elements to be inserted\n";
185. cin>>n;
186. for(i = 0; i < n; i++)
187. {
188. cout<<"enter the element\n";
189. cin>>t;
190. insert(t);
191. }
192. cout<<"traversal of constructed tree\n";
193. traverse(root);
194. getch();
195. }
Output
References:
https://www.sciencedirect.com/science/article/pii/S2352340918304530
https://www.cpp.edu/~ftang/courses/CS241/notes/b-tree.htm
https://www.sanfoundry.com/cpp-program-implement-b-tree/
https://cis.stvincent.edu/html/tutorials/swd/btree/btree.html