Skip to content

Commit b9566fa

Browse files
committed
update readme
1 parent bc0375f commit b9566fa

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/main/java/dataStructures/bTree/BTree.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public void insert(int key) {
8888
/**
8989
* Splits a child node of the current node during insertion, promoting the middle key to the parent node.
9090
* This method is called when the child node is full and needs to be split to maintain B-tree properties.
91+
* Refer to the Split Child Method image in the README for a visualisation of this method.
9192
* @param x The parent node.
9293
* @param i The index of the child node to split.
9394
*/
@@ -184,12 +185,12 @@ private void deleteRecursive(BTreeNode x, int key) {
184185
}
185186

186187
if (i < x.keyCount && key == x.keys[i]) {
187-
if (x.leaf) {
188+
if (x.leaf) { // Case 1: key in node x and x is a leaf
188189
for (int curr = i; curr < x.keyCount; curr++) {
189190
x.keys[curr] = x.keys[curr + 1];
190191
}
191192
x.keyCount -= 1;
192-
} else {
193+
} else { // Case 2: key in node x and x is an internal node
193194
BTreeNode y = x.children[i];
194195
BTreeNode z = x.children[i + 1];
195196
if (y.keyCount >= this.t) {
@@ -205,7 +206,7 @@ private void deleteRecursive(BTreeNode x, int key) {
205206
deleteRecursive(y, key);
206207
}
207208
}
208-
} else {
209+
} else { // Case 3: key not present in x
209210
if (x.leaf) {
210211
System.out.println("Key " + key + " does not exist in the B-tree.");
211212
} else {

src/main/java/dataStructures/bTree/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ Rule #3: Leaf depth
6262
All leaf nodes must be at the same depth from root.
6363

6464
## Complexity Analysis
65-
Search:
6665

67-
**Time**: O(bloga(n)) = O(logn)
66+
**Search, Insertion, Deletion Time**: O(bloga(n)) = O(logn)
6867

69-
- The max height of an (a,b) tree is O(loga(n)).
70-
- Linear search takes maximally b nodes per level.
68+
- The max height of an (a,b) tree is O(loga(n)).
69+
- Linear search takes maximally b nodes per level.
7170

7271
**Space**: O(n)
7372

@@ -81,9 +80,25 @@ a value t >= 2, known as its minimum degree.
8180
- Every internal node other than the root has at least t children.
8281
- Following this definition, t = a in the naming convention of (a,b) trees.
8382

84-
## Split Child Method
83+
## Search Operation
84+
Here is an outline of the search operation:
85+
1. Begin the search at the root of the B tree.
86+
2. If the key being searched for is in the current node, return true (i.e. found).
87+
3. Else, determine the child node where the key might be located based on comparison with the keys in the current node.
88+
4. Recursively perform the search operation in the determined child node.
89+
5. If the search reaches a leaf node, and the key is not found, return false (i.e. not found).
90+
91+
## Insert Operation
92+
You can read more about how the insert operation works
93+
[here](https://www.geeksforgeeks.org/insert-operation-in-b-tree/).
94+
95+
### Split Child Method
8596
![split child](../../../../../docs/assets/images/btreesplitchild.jpeg)
8697
Image Source: https://www.geeksforgeeks.org/insert-operation-in-b-tree/
8798

99+
## Delete Operation
100+
The delete operation has a similar idea as the insert operation, but involves a lot more edge cases. If you are
101+
interested to learn about it, you can read more [here](https://www.geeksforgeeks.org/delete-operation-in-b-tree/).
102+
88103
## References
89104
This description heavily references CS2040S Recitation Sheet 4.

0 commit comments

Comments
 (0)