Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Saarim Salim

12132006
B-Tree Insertion and Deletion

import java.util.Scanner;

class BTreeNode {
int[] keys;
int t;
BTreeNode[] children;
int n;
boolean leaf;

BTreeNode(int t, boolean leaf) {


this.t = t;
this.leaf = leaf;
keys = new int[2 * t - 1];
children = new BTreeNode[2 * t];
n = 0;
}

int searchKey(int key) {


int idx = 0;
while (idx < n && key > keys[idx]) {
idx++;
}
return idx;
}

void insertNonFull(int key) {


int i = n - 1;
if (leaf) {
while (i >= 0 && key < keys[i]) {
keys[i + 1] = keys[i];
i--;
}
keys[i + 1] = key;
n++;
} else {
int idx = searchKey(key);
if (children[idx].n == 2 * t - 1) {
splitChild(idx, children[idx]);
if (key > keys[idx]) {
idx++;
}
}
children[idx].insertNonFull(key);
}
}

void splitChild(int idx, BTreeNode y) {


BTreeNode z = new BTreeNode(y.t, y.leaf);
for (int i = 0; i < t - 1; i++) {
z.keys[i] = y.keys[i + t];
}
if (!y.leaf) {
for (int i = 0; i < t; i++) {
z.children[i] = y.children[i + t];
}
}
y.n = t - 1;
for (int i = n; i > idx; i--) {
children[i + 1] = children[i];
}
children[idx + 1] = z;
for (int i = n - 1; i >= idx; i--) {
keys[i + 1] = keys[i];
}
keys[idx] = y.keys[t - 1];
n++;
}

void traverse() {
int i;
for (i = 0; i < n; i++) {
if (!leaf) {
children[i].traverse();
}
System.out.print(keys[i] + " ");
}
if (!leaf) {
children[i].traverse();
}
}

public boolean search(int k) {


int i = 0;
while (i < n && k > keys[i]) {
i++;
}
if (i < n && k == keys[i]) {
return true; // Key found in this node
}
if (leaf) {
return false; // Key is not in the tree
}
return children[i].search(k); // Recursively search in the
appropriate subtree
}
}

class BTree {
private BTreeNode root;
private int t;

BTree(int t) {
this.root = null;
this.t = t;
}

void insert(int key) {


if (root == null) {
root = new BTreeNode(t, true);
root.keys[0] = key;
root.n = 1;
} else {
if (root.n == (2 * t - 1)) {
BTreeNode newRoot = new BTreeNode(t, false);
newRoot.children[0] = root;
newRoot.splitChild(0, root);
int i = 0;
if (newRoot.keys[0] < key) {
i++;
}
newRoot.children[i].insertNonFull(key);
root = newRoot;
} else {
root.insertNonFull(key);
}
}
}

void traverse() {
if (root != null) {
root.traverse();
}
}

boolean search(int k) {
if (root != null) {
return root.search(k);
}
return false;
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the minimum degree (t) of the B-tree: ");


int t = scanner.nextInt();
BTree bTree = new BTree(t);

System.out.print("Enter the number of elements to insert: ");


int n = scanner.nextInt();
System.out.print("Enter " + n + " elements to insert into the B-
tree: ");
for (int i = 0; i < n; i++) {
int element = scanner.nextInt();
bTree.insert(element);
}

System.out.print("B-tree elements (in-order traversal): ");


bTree.traverse();

System.out.print("Enter an integer to search in the B-tree (or -1


to exit): ");
int searchValue = scanner.nextInt();
while (searchValue != -1) {
boolean found = bTree.search(searchValue);
if (found) {
System.out.println("Value " + searchValue + " found in the
B-tree.");
} else {
System.out.println("Value " + searchValue + " not found in
the B-tree.");
}
System.out.print("Enter another integer to search (or -1 to
exit): ");
searchValue = scanner.nextInt();
}

scanner.close();
}
}

OUTPUT
B-TREE VISUALISATION

You might also like