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
+
16
73
static class BTreeNode {
17
74
int [] keys ;
18
- int t ; // Minimum degree (defines range for number of keys)
75
+ int t ;
19
76
BTreeNode [] children ;
20
- int n ; // Current number of keys
77
+ int n ;
21
78
boolean leaf ;
22
79
23
80
BTreeNode (int t , boolean leaf ) {
@@ -48,10 +105,7 @@ BTreeNode search(int key) {
48
105
if (i < n && keys [i ] == key ) {
49
106
return this ;
50
107
}
51
- if (leaf ) {
52
- return null ;
53
- }
54
- return children [i ].search (key );
108
+ return (leaf ) ? null : children [i ].search (key );
55
109
}
56
110
57
111
void insertNonFull (int key ) {
@@ -80,7 +134,6 @@ void insertNonFull(int key) {
80
134
void splitChild (int i , BTreeNode y ) {
81
135
BTreeNode z = new BTreeNode (y .t , y .leaf );
82
136
z .n = t - 1 ;
83
-
84
137
System .arraycopy (y .keys , t , z .keys , 0 , t - 1 );
85
138
if (!y .leaf ) {
86
139
System .arraycopy (y .children , t , z .children , 0 , t );
@@ -109,11 +162,9 @@ void remove(int key) {
109
162
removeFromNonLeaf (idx );
110
163
}
111
164
} else {
112
- if (leaf ) {
113
- return ; // Key not found
114
- }
165
+ if (leaf ) return ;
115
166
116
- boolean flag = idx == n ;
167
+ boolean flag = ( idx == n ) ;
117
168
if (children [idx ].n < t ) {
118
169
fill (idx );
119
170
}
@@ -208,7 +259,6 @@ private void borrowFromPrev(int idx) {
208
259
}
209
260
210
261
keys [idx - 1 ] = sibling .keys [sibling .n - 1 ];
211
-
212
262
child .n += 1 ;
213
263
sibling .n -= 1 ;
214
264
}
@@ -218,7 +268,6 @@ private void borrowFromNext(int idx) {
218
268
BTreeNode sibling = children [idx + 1 ];
219
269
220
270
child .keys [child .n ] = keys [idx ];
221
-
222
271
if (!child .leaf ) {
223
272
child .children [child .n + 1 ] = sibling .children [0 ];
224
273
}
@@ -267,54 +316,4 @@ private void merge(int idx) {
267
316
n --;
268
317
}
269
318
}
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 (root == null ) {
291
- root = new BTreeNode (t , true );
292
- root .keys [0 ] = key ;
293
- root .n = 1 ;
294
- } else {
295
- if (root .n == 2 * t - 1 ) {
296
- BTreeNode s = new BTreeNode (t , false );
297
- s .children [0 ] = root ;
298
- s .splitChild (0 , root );
299
- int i = 0 ;
300
- if (s .keys [0 ] < key ) {
301
- i ++;
302
- }
303
- s .children [i ].insertNonFull (key );
304
- root = s ;
305
- } else {
306
- root .insertNonFull (key );
307
- }
308
- }
309
- }
310
-
311
- public void delete (int key ) {
312
- if (root == null ) {
313
- return ;
314
- }
315
- root .remove (key );
316
- if (root .n == 0 ) {
317
- root = root .leaf ? null : root .children [0 ];
318
- }
319
- }
320
319
}
0 commit comments