Skip to content

Commit c3c3c0e

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 9ad31af commit c3c3c0e

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

  • src
    • main/java/com/thealgorithms/datastructures/trees
    • test/java/com/thealgorithms/datastructures/trees

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
import java.util.ArrayList;
44

5+
/**
6+
* Implementation of a B-Tree, a self-balancing tree data structure that maintains sorted data
7+
* and allows searches, sequential access, insertions, and deletions in logarithmic time.
8+
*
9+
* B-Trees are generalizations of binary search trees in that a node can have more than two children.
10+
* They're widely used in databases and file systems.
11+
*
12+
* For more information: https://en.wikipedia.org/wiki/B-tree
13+
*/
14+
515
public class BTree {
616
static class BTreeNode {
717
int[] keys;

src/test/java/com/thealgorithms/datastructures/trees/BTreeTest.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.thealgorithms.datastructures.trees;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertFalse;
5-
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
import static org.junit.jupiter.api.Assertions.*;
64

75
import java.util.ArrayList;
86
import java.util.Arrays;
@@ -33,4 +31,62 @@ public void testInsertSearchDelete() {
3331
bTree.traverse(traversal);
3432
assertEquals(Arrays.asList(5, 7, 10, 12, 17, 20, 30), traversal);
3533
}
34+
35+
@Test
36+
public void testEmptyTreeSearch() {
37+
BTree bTree = new BTree(3);
38+
assertFalse(bTree.search(42), "Search in empty tree should return false.");
39+
}
40+
41+
@Test
42+
public void testDuplicateInsertions() {
43+
BTree bTree = new BTree(3);
44+
bTree.insert(15);
45+
bTree.insert(15); // Attempt duplicate
46+
bTree.insert(15); // Another duplicate
47+
48+
ArrayList<Integer> traversal = new ArrayList<>();
49+
bTree.traverse(traversal);
50+
51+
// Should contain only one 15
52+
long count = traversal.stream().filter(x -> x == 15).count();
53+
assertEquals(1, count, "Duplicate keys should not be inserted.");
54+
}
55+
56+
@Test
57+
public void testDeleteNonExistentKey() {
58+
BTree bTree = new BTree(3);
59+
bTree.insert(10);
60+
bTree.insert(20);
61+
bTree.delete(99); // Doesn't exist
62+
assertTrue(bTree.search(10));
63+
assertTrue(bTree.search(20));
64+
}
65+
66+
@Test
67+
public void testComplexInsertDelete() {
68+
BTree bTree = new BTree(2); // Smaller degree to trigger splits more easily
69+
int[] values = {1, 3, 7, 10, 11, 13, 14, 15, 18, 16, 19, 24, 25, 26, 21, 4, 5, 20, 22, 2, 17, 12, 6};
70+
71+
for (int val : values) {
72+
bTree.insert(val);
73+
}
74+
75+
for (int val : values) {
76+
assertTrue(bTree.search(val));
77+
}
78+
79+
int[] toDelete = {6, 13, 7, 4, 2, 16};
80+
for (int val : toDelete) {
81+
bTree.delete(val);
82+
assertFalse(bTree.search(val));
83+
}
84+
85+
ArrayList<Integer> remaining = new ArrayList<>();
86+
bTree.traverse(remaining);
87+
88+
for (int val : toDelete) {
89+
assertFalse(remaining.contains(val));
90+
}
91+
}
3692
}

0 commit comments

Comments
 (0)