13
13
*/
14
14
15
15
public class BTree {
16
- private BTreeNode root ;
17
- private int t ;
18
-
19
- public BTree (int t ) {
20
- this .root = null ;
21
- this .t = t ;
22
- }
23
-
24
- public void traverse (ArrayList <Integer > result ) {
25
- if (root != null ) {
26
- root .traverse (result );
27
- }
28
- }
29
-
30
- public BTreeNode search (int key ) {
31
- return (root == null ) ? null : root .search (key );
32
- }
33
-
34
- public void insert (int key ) {
35
- // Prevent duplicate insertions
36
- if (search (key ) != null ) {
37
- return ;
38
- }
39
-
40
- if (root == null ) {
41
- root = new BTreeNode (t , true );
42
- root .keys [0 ] = key ;
43
- root .n = 1 ;
44
- } else {
45
- if (root .n == 2 * t - 1 ) {
46
- BTreeNode s = new BTreeNode (t , false );
47
- s .children [0 ] = root ;
48
- s .splitChild (0 , root );
49
-
50
- int i = 0 ;
51
- if (s .keys [0 ] < key ) {
52
- i ++;
53
- }
54
- s .children [i ].insertNonFull (key );
55
- root = s ;
56
- } else {
57
- root .insertNonFull (key );
58
- }
59
- }
60
- }
61
-
62
- public void remove (int key ) {
63
- if (root == null ) {
64
- return ;
65
- }
66
-
67
- root .remove (key );
68
- if (root .n == 0 ) {
69
- root = (root .leaf ) ? null : root .children [0 ];
70
- }
71
- }
72
-
73
16
static class BTreeNode {
74
17
int [] keys ;
75
- int t ;
18
+ int t ; // Minimum degree (defines range for number of keys)
76
19
BTreeNode [] children ;
77
- int n ;
20
+ int n ; // Current number of keys
78
21
boolean leaf ;
79
22
80
23
BTreeNode (int t , boolean leaf ) {
@@ -105,7 +48,10 @@ BTreeNode search(int key) {
105
48
if (i < n && keys [i ] == key ) {
106
49
return this ;
107
50
}
108
- return (leaf ) ? null : children [i ].search (key );
51
+ if (leaf ) {
52
+ return null ;
53
+ }
54
+ return children [i ].search (key );
109
55
}
110
56
111
57
void insertNonFull (int key ) {
@@ -134,6 +80,7 @@ void insertNonFull(int key) {
134
80
void splitChild (int i , BTreeNode y ) {
135
81
BTreeNode z = new BTreeNode (y .t , y .leaf );
136
82
z .n = t - 1 ;
83
+
137
84
System .arraycopy (y .keys , t , z .keys , 0 , t - 1 );
138
85
if (!y .leaf ) {
139
86
System .arraycopy (y .children , t , z .children , 0 , t );
@@ -162,9 +109,11 @@ void remove(int key) {
162
109
removeFromNonLeaf (idx );
163
110
}
164
111
} else {
165
- if (leaf ) return ;
112
+ if (leaf ) {
113
+ return ; // Key not found
114
+ }
166
115
167
- boolean flag = ( idx == n ) ;
116
+ boolean flag = idx == n ;
168
117
if (children [idx ].n < t ) {
169
118
fill (idx );
170
119
}
@@ -259,6 +208,7 @@ private void borrowFromPrev(int idx) {
259
208
}
260
209
261
210
keys [idx - 1 ] = sibling .keys [sibling .n - 1 ];
211
+
262
212
child .n += 1 ;
263
213
sibling .n -= 1 ;
264
214
}
@@ -268,6 +218,7 @@ private void borrowFromNext(int idx) {
268
218
BTreeNode sibling = children [idx + 1 ];
269
219
270
220
child .keys [child .n ] = keys [idx ];
221
+
271
222
if (!child .leaf ) {
272
223
child .children [child .n + 1 ] = sibling .children [0 ];
273
224
}
@@ -316,4 +267,57 @@ private void merge(int idx) {
316
267
n --;
317
268
}
318
269
}
270
+
271
+ private BTreeNode root ;
272
+ private final int t ;
273
+
274
+ public BTree (int t ) {
275
+ this .root = null ;
276
+ this .t = t ;
277
+ }
278
+
279
+ public void traverse (ArrayList <Integer > result ) {
280
+ if (root != null ) {
281
+ root .traverse (result );
282
+ }
283
+ }
284
+
285
+ public boolean search (int key ) {
286
+ return root != null && root .search (key ) != null ;
287
+ }
288
+
289
+ public void insert (int key ) {
290
+ if (search (key )) {
291
+ return ;
292
+ }
293
+ if (root == null ) {
294
+ root = new BTreeNode (t , true );
295
+ root .keys [0 ] = key ;
296
+ root .n = 1 ;
297
+ } else {
298
+ if (root .n == 2 * t - 1 ) {
299
+ BTreeNode s = new BTreeNode (t , false );
300
+ s .children [0 ] = root ;
301
+ s .splitChild (0 , root );
302
+ int i = 0 ;
303
+ if (s .keys [0 ] < key ) {
304
+ i ++;
305
+ }
306
+ s .children [i ].insertNonFull (key );
307
+ root = s ;
308
+ } else {
309
+ root .insertNonFull (key );
310
+ }
311
+ }
312
+ }
313
+
314
+ public void delete (int key ) {
315
+ if (root == null ) {
316
+ return ;
317
+ }
318
+ root .remove (key );
319
+ if (root .n == 0 ) {
320
+ root = root .leaf ? null : root .children [0 ];
321
+ }
322
+ }
319
323
}
0 commit comments