Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update B-Tree-2.md #1700

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 154 additions & 1 deletion docs/b-tree/B-Tree-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,159 @@ int main()
return 0;
}
```
## java
```java
import java.util.Arrays;
class BTreeNode {
int[] keys; // An array of keys
int t; // Minimum degree
BTreeNode[] C; // An array of child pointers
int n; // Current number of keys
boolean leaf; // True if leaf node, otherwise false
// Constructor
BTreeNode(int t, boolean leaf) {
this.t = t;
this.leaf = leaf;
this.keys = new int[2 * t - 1];
this.C = new BTreeNode[2 * t];
this.n = 0;
}
// Function to traverse all nodes in a subtree rooted with this node
public void traverse() {
int i;
for (i = 0; i < n; i++) {
if (!leaf) {
C[i].traverse();
}
System.out.print(keys[i] + " ");
}
if (!leaf) {
C[i].traverse();
}
}
// Function to search a key in subtree rooted with this node
public BTreeNode search(int k) {
int i = 0;
while (i < n && k > keys[i]) {
i++;
}
if (i < n && keys[i] == k) {
return this;
}
if (leaf) {
return null;
}
return C[i].search(k);
}
// Insert a new key in this node
public void insertNonFull(int k) {
int i = n - 1;
if (leaf) {
while (i >= 0 && keys[i] > k) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = k;
n++;
} else {
while (i >= 0 && keys[i] > k) {
i--;
}
if (C[i + 1].n == 2 * t - 1) {
splitChild(i + 1, C[i + 1]);
if (keys[i + 1] < k) {
i++;
}
}
C[i + 1].insertNonFull(k);
}
}
// Split the child y of this node
public void splitChild(int i, BTreeNode y) {
BTreeNode z = new BTreeNode(y.t, y.leaf);
z.n = t - 1;
for (int j = 0; j < t - 1; j++) {
z.keys[j] = y.keys[j + t];
}
if (!y.leaf) {
for (int j = 0; j < t; j++) {
z.C[j] = y.C[j + t];
}
}
y.n = t - 1;
for (int j = n; j >= i + 1; j--) {
C[j + 1] = C[j];
}
C[i + 1] = z;
for (int j = n - 1; j >= i; j--) {
keys[j + 1] = keys[j];
}
keys[i] = y.keys[t - 1];
n++;
}
}
class BTree {
BTreeNode root;
int t; // Minimum degree
// Constructor
public BTree(int t) {
this.root = null;
this.t = t;
}
// Function to traverse the tree
public void traverse() {
if (root != null) {
root.traverse();
}
}
// Function to search a key in this tree
public BTreeNode search(int k) {
return (root == null) ? null : root.search(k);
}
// Insert a new key in this B-Tree
public void insert(int k) {
if (root == null) {
root = new BTreeNode(t, true);
root.keys[0] = k;
root.n = 1;
} else {
if (root.n == 2 * t - 1) {
BTreeNode s = new BTreeNode(t, false);
s.C[0] = root;
s.splitChild(0, root);
int i = 0;
if (s.keys[0] < k) {
i++;
}
s.C[i].insertNonFull(k);
root = s;
} else {
root.insertNonFull(k);
}
}
}
}
// Driver program to test above functions
public class Main {
public static void main(String[] args) {
BTree t = new BTree(3); // A B-Tree with minimum degree 3
t.insert(10);
t.insert(20);
t.insert(5);
t.insert(6);
t.insert(12);
t.insert(30);
t.insert(7);
t.insert(17);
System.out.print("Traversal of the constructed tree is: ");
t.traverse();
int k = 6;
System.out.println("\nSearch for key " + k + ": " + (t.search(k) != null ? "Present" : "Not Present"));
k = 15;
System.out.println("Search for key " + k + ": " + (t.search(k) != null ? "Present" : "Not Present"));
}
}
```
**Output:**
Traversal of the constructed tree is: `5 6 7 10 12 17 20 30`
**Present**
Expand Down Expand Up @@ -358,4 +511,4 @@ The delete operation is also complex. Deleting a key can cause a node to have to

- **Higher Fanout:** B-trees have a higher fanout than binary search trees, resulting in fewer disk accesses when searching for a key.
- **Balanced Structure:** All operations have a worst-case time complexity of \(O(\log n)\).
- **Self-Adjusting:** B-trees can adapt to changes in the dataset without requiring expensive rebalancing operations.
- **Self-Adjusting:** B-trees can adapt to changes in the dataset without requiring expensive rebalancing operations.
Loading