Skip to content

Commit 0988c61

Browse files
committed
updated code to handle duplicate keys
1 parent 622d4a3 commit 0988c61

File tree

1 file changed

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

1 file changed

+66
-62
lines changed

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

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,11 @@
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-
7316
static class BTreeNode {
7417
int[] keys;
75-
int t;
18+
int t; // Minimum degree (defines range for number of keys)
7619
BTreeNode[] children;
77-
int n;
20+
int n; // Current number of keys
7821
boolean leaf;
7922

8023
BTreeNode(int t, boolean leaf) {
@@ -105,7 +48,10 @@ BTreeNode search(int key) {
10548
if (i < n && keys[i] == key) {
10649
return this;
10750
}
108-
return (leaf) ? null : children[i].search(key);
51+
if (leaf) {
52+
return null;
53+
}
54+
return children[i].search(key);
10955
}
11056

11157
void insertNonFull(int key) {
@@ -134,6 +80,7 @@ void insertNonFull(int key) {
13480
void splitChild(int i, BTreeNode y) {
13581
BTreeNode z = new BTreeNode(y.t, y.leaf);
13682
z.n = t - 1;
83+
13784
System.arraycopy(y.keys, t, z.keys, 0, t - 1);
13885
if (!y.leaf) {
13986
System.arraycopy(y.children, t, z.children, 0, t);
@@ -162,9 +109,11 @@ void remove(int key) {
162109
removeFromNonLeaf(idx);
163110
}
164111
} else {
165-
if (leaf) return;
112+
if (leaf) {
113+
return; // Key not found
114+
}
166115

167-
boolean flag = (idx == n);
116+
boolean flag = idx == n;
168117
if (children[idx].n < t) {
169118
fill(idx);
170119
}
@@ -259,6 +208,7 @@ private void borrowFromPrev(int idx) {
259208
}
260209

261210
keys[idx - 1] = sibling.keys[sibling.n - 1];
211+
262212
child.n += 1;
263213
sibling.n -= 1;
264214
}
@@ -268,6 +218,7 @@ private void borrowFromNext(int idx) {
268218
BTreeNode sibling = children[idx + 1];
269219

270220
child.keys[child.n] = keys[idx];
221+
271222
if (!child.leaf) {
272223
child.children[child.n + 1] = sibling.children[0];
273224
}
@@ -316,4 +267,57 @@ private void merge(int idx) {
316267
n--;
317268
}
318269
}
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+
}
319323
}

0 commit comments

Comments
 (0)