forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTree.java
More file actions
323 lines (275 loc) · 8.72 KB
/
BTree.java
File metadata and controls
323 lines (275 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.thealgorithms.datastructures.trees;
import java.util.ArrayList;
/**
* Implementation of a B-Tree, a self-balancing tree data structure that maintains sorted data
* and allows searches, sequential access, insertions, and deletions in logarithmic time.
*
* B-Trees are generalizations of binary search trees in that a node can have more than two children.
* They're widely used in databases and file systems.
*
* For more information: https://en.wikipedia.org/wiki/B-tree
*/
public class BTree {
static class BTreeNode {
int[] keys;
int t; // Minimum degree (defines range for number of keys)
BTreeNode[] children;
int n; // Current number of keys
boolean leaf;
BTreeNode(int t, boolean leaf) {
this.t = t;
this.leaf = leaf;
this.keys = new int[2 * t - 1];
this.children = new BTreeNode[2 * t];
this.n = 0;
}
void traverse(ArrayList<Integer> result) {
for (int i = 0; i < n; i++) {
if (!leaf) {
children[i].traverse(result);
}
result.add(keys[i]);
}
if (!leaf) {
children[n].traverse(result);
}
}
BTreeNode search(int key) {
int i = 0;
while (i < n && key > keys[i]) {
i++;
}
if (i < n && keys[i] == key) {
return this;
}
if (leaf) {
return null;
}
return children[i].search(key);
}
void insertNonFull(int key) {
int i = n - 1;
if (leaf) {
while (i >= 0 && keys[i] > key) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = key;
n++;
} else {
while (i >= 0 && keys[i] > key) {
i--;
}
if (children[i + 1].n == 2 * t - 1) {
splitChild(i + 1, children[i + 1]);
if (keys[i + 1] < key) {
i++;
}
}
children[i + 1].insertNonFull(key);
}
}
void splitChild(int i, BTreeNode y) {
BTreeNode z = new BTreeNode(y.t, y.leaf);
z.n = t - 1;
System.arraycopy(y.keys, t, z.keys, 0, t - 1);
if (!y.leaf) {
System.arraycopy(y.children, t, z.children, 0, t);
}
y.n = t - 1;
for (int j = n; j >= i + 1; j--) {
children[j + 1] = children[j];
}
children[i + 1] = z;
for (int j = n - 1; j >= i; j--) {
keys[j + 1] = keys[j];
}
keys[i] = y.keys[t - 1];
n++;
}
void remove(int key) {
int idx = findKey(key);
if (idx < n && keys[idx] == key) {
if (leaf) {
removeFromLeaf(idx);
} else {
removeFromNonLeaf(idx);
}
} else {
if (leaf) {
return; // Key not found
}
boolean flag = idx == n;
if (children[idx].n < t) {
fill(idx);
}
if (flag && idx > n) {
children[idx - 1].remove(key);
} else {
children[idx].remove(key);
}
}
}
private int findKey(int key) {
int idx = 0;
while (idx < n && keys[idx] < key) {
++idx;
}
return idx;
}
private void removeFromLeaf(int idx) {
for (int i = idx + 1; i < n; ++i) {
keys[i - 1] = keys[i];
}
n--;
}
private void removeFromNonLeaf(int idx) {
int key = keys[idx];
if (children[idx].n >= t) {
int pred = getPredecessor(idx);
keys[idx] = pred;
children[idx].remove(pred);
} else if (children[idx + 1].n >= t) {
int succ = getSuccessor(idx);
keys[idx] = succ;
children[idx + 1].remove(succ);
} else {
merge(idx);
children[idx].remove(key);
}
}
private int getPredecessor(int idx) {
BTreeNode cur = children[idx];
while (!cur.leaf) {
cur = cur.children[cur.n];
}
return cur.keys[cur.n - 1];
}
private int getSuccessor(int idx) {
BTreeNode cur = children[idx + 1];
while (!cur.leaf) {
cur = cur.children[0];
}
return cur.keys[0];
}
private void fill(int idx) {
if (idx != 0 && children[idx - 1].n >= t) {
borrowFromPrev(idx);
} else if (idx != n && children[idx + 1].n >= t) {
borrowFromNext(idx);
} else {
if (idx != n) {
merge(idx);
} else {
merge(idx - 1);
}
}
}
private void borrowFromPrev(int idx) {
BTreeNode child = children[idx];
BTreeNode sibling = children[idx - 1];
for (int i = child.n - 1; i >= 0; --i) {
child.keys[i + 1] = child.keys[i];
}
if (!child.leaf) {
for (int i = child.n; i >= 0; --i) {
child.children[i + 1] = child.children[i];
}
}
child.keys[0] = keys[idx - 1];
if (!child.leaf) {
child.children[0] = sibling.children[sibling.n];
}
keys[idx - 1] = sibling.keys[sibling.n - 1];
child.n += 1;
sibling.n -= 1;
}
private void borrowFromNext(int idx) {
BTreeNode child = children[idx];
BTreeNode sibling = children[idx + 1];
child.keys[child.n] = keys[idx];
if (!child.leaf) {
child.children[child.n + 1] = sibling.children[0];
}
keys[idx] = sibling.keys[0];
for (int i = 1; i < sibling.n; ++i) {
sibling.keys[i - 1] = sibling.keys[i];
}
if (!sibling.leaf) {
for (int i = 1; i <= sibling.n; ++i) {
sibling.children[i - 1] = sibling.children[i];
}
}
child.n += 1;
sibling.n -= 1;
}
private void merge(int idx) {
BTreeNode child = children[idx];
BTreeNode sibling = children[idx + 1];
child.keys[t - 1] = keys[idx];
for (int i = 0; i < sibling.n; ++i) {
child.keys[i + t] = sibling.keys[i];
}
if (!child.leaf) {
for (int i = 0; i <= sibling.n; ++i) {
child.children[i + t] = sibling.children[i];
}
}
for (int i = idx + 1; i < n; ++i) {
keys[i - 1] = keys[i];
}
for (int i = idx + 2; i <= n; ++i) {
children[i - 1] = children[i];
}
child.n += sibling.n + 1;
n--;
}
}
private BTreeNode root;
private final int t;
public BTree(int t) {
this.root = null;
this.t = t;
}
public void traverse(ArrayList<Integer> result) {
if (root != null) {
root.traverse(result);
}
}
public boolean search(int key) {
return root != null && root.search(key) != null;
}
public void insert(int key) {
if (search(key)) {
return;
}
if (root == null) {
root = new BTreeNode(t, true);
root.keys[0] = key;
root.n = 1;
} else {
if (root.n == 2 * t - 1) {
BTreeNode s = new BTreeNode(t, false);
s.children[0] = root;
s.splitChild(0, root);
int i = 0;
if (s.keys[0] < key) {
i++;
}
s.children[i].insertNonFull(key);
root = s;
} else {
root.insertNonFull(key);
}
}
}
public void delete(int key) {
if (root == null) {
return;
}
root.remove(key);
if (root.n == 0) {
root = root.leaf ? null : root.children[0];
}
}
}