Skip to content

Commit 622d4a3

Browse files
committed
including description and edge test cases for inserting duplicate keys, deleting non existent keys, Handling empty tree and Inserting and deleting keys that cause multiple splits or merges
1 parent c3c3c0e commit 622d4a3

File tree

1 file changed

+62
-63
lines changed
  • src/main/java/com/thealgorithms/datastructures/trees

1 file changed

+62
-63
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BTree.java

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,68 @@
1313
*/
1414

1515
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+
1673
static class BTreeNode {
1774
int[] keys;
18-
int t; // Minimum degree (defines range for number of keys)
75+
int t;
1976
BTreeNode[] children;
20-
int n; // Current number of keys
77+
int n;
2178
boolean leaf;
2279

2380
BTreeNode(int t, boolean leaf) {
@@ -48,10 +105,7 @@ BTreeNode search(int key) {
48105
if (i < n && keys[i] == key) {
49106
return this;
50107
}
51-
if (leaf) {
52-
return null;
53-
}
54-
return children[i].search(key);
108+
return (leaf) ? null : children[i].search(key);
55109
}
56110

57111
void insertNonFull(int key) {
@@ -80,7 +134,6 @@ void insertNonFull(int key) {
80134
void splitChild(int i, BTreeNode y) {
81135
BTreeNode z = new BTreeNode(y.t, y.leaf);
82136
z.n = t - 1;
83-
84137
System.arraycopy(y.keys, t, z.keys, 0, t - 1);
85138
if (!y.leaf) {
86139
System.arraycopy(y.children, t, z.children, 0, t);
@@ -109,11 +162,9 @@ void remove(int key) {
109162
removeFromNonLeaf(idx);
110163
}
111164
} else {
112-
if (leaf) {
113-
return; // Key not found
114-
}
165+
if (leaf) return;
115166

116-
boolean flag = idx == n;
167+
boolean flag = (idx == n);
117168
if (children[idx].n < t) {
118169
fill(idx);
119170
}
@@ -208,7 +259,6 @@ private void borrowFromPrev(int idx) {
208259
}
209260

210261
keys[idx - 1] = sibling.keys[sibling.n - 1];
211-
212262
child.n += 1;
213263
sibling.n -= 1;
214264
}
@@ -218,7 +268,6 @@ private void borrowFromNext(int idx) {
218268
BTreeNode sibling = children[idx + 1];
219269

220270
child.keys[child.n] = keys[idx];
221-
222271
if (!child.leaf) {
223272
child.children[child.n + 1] = sibling.children[0];
224273
}
@@ -267,54 +316,4 @@ private void merge(int idx) {
267316
n--;
268317
}
269318
}
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-
}
320319
}

0 commit comments

Comments
 (0)