| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // Java Program for Implementaion B-Tree
- class BTreeNode {
- // Variables Declared
- int[] keys; // Array to store keys
- int t; // Minimum degree (defines the range for number of keys)
- BTreeNode[] children; // Array to store child pointers
- int n; // Current number of keys
- boolean leaf; // True when node is leaf, else False
- public BTreeNode(int t, boolean leaf) {
- this.t = t;
- this.leaf = leaf;
- keys = new int[2 * t - 1];
- children = new BTreeNode[2 * t];
- n = 0;
- }
- // Function to search given key in subtree
- // rooted with this node
- public BTreeNode search(int key) {
- int i = 0;
- while (i < n && key > keys[i])
- i++;
- if (keys[i] == key)
- return this;
- if (leaf)
- return null;
- return children[i].search(key);
- }
- // Function to insert a new key
- // in subtree rooted with this node
- public 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 {
- while (i >= 0 && key < keys[i])
- i--;
- i++;
- if (children[i].n == 2 * t - 1) {
- splitChild(i, children[i]);
- if (key > keys[i])
- i++;
- }
- children[i].insertNonFull(key);
- }
- }
- // Function to split the child 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.children[j] = y.children[j + t];
- }
- y.n = t - 1;
- for (int j = n; j >= i + 1; j--)
- children[j + 1] = children[j];
- children[i + 1] = z;
- for (int j = n - 1; j >= i; j--)
- keys[j + 1] = keys[j];
- keys[i] = y.keys[t - 1];
- n++;
- }
- // Function to print all keys in the
- // subtree rooted with this node
- public void printInOrder() {
- int i;
- for (i = 0; i < n; i++) {
- if (!leaf)
- children[i].printInOrder();
- System.out.print(keys[i] + " ");
- }
- if (!leaf)
- children[i].printInOrder();
- }
- }
- public class BTree {
- // Pointer to root node
- private BTreeNode root;
- // Minimum degree
- private int t;
- public BTree(int t) {
- this.t = t;
- root = null;
- }
- // Function to search a key in this tree
- public BTreeNode search(int key) {
- return (root == null) ? null : root.search(key);
- }
- // Function to insert a key into the B-tree
- public 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);
- }
- }
- }
- // Function to print the entire B-tree
- public void printBTree() {
- if (root != null)
- root.printInOrder();
- System.out.println();
- }
- public static void main(String[] args) {
- // Create a B-tree with minimum degree 3
- BTree bTree = new BTree(3);
- bTree.insert(10);
- bTree.insert(20);
- bTree.insert(5);
- bTree.insert(6);
- bTree.insert(12);
- bTree.insert(30);
- System.out.print("B-tree : ");
- bTree.printBTree();
- int searchKey = 6;
- BTreeNode foundNode = bTree.search(searchKey);
- if (foundNode != null)
- System.out.println("Key " + searchKey + " found in the B-tree.");
- else
- System.out.println("Key " + searchKey + " not found in the B-tree.");
- }
- }
|