BTree.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Java Program for Implementaion B-Tree
  2. class BTreeNode {
  3. // Variables Declared
  4. int[] keys; // Array to store keys
  5. int t; // Minimum degree (defines the range for number of keys)
  6. BTreeNode[] children; // Array to store child pointers
  7. int n; // Current number of keys
  8. boolean leaf; // True when node is leaf, else False
  9. public BTreeNode(int t, boolean leaf) {
  10. this.t = t;
  11. this.leaf = leaf;
  12. keys = new int[2 * t - 1];
  13. children = new BTreeNode[2 * t];
  14. n = 0;
  15. }
  16. // Function to search given key in subtree
  17. // rooted with this node
  18. public BTreeNode search(int key) {
  19. int i = 0;
  20. while (i < n && key > keys[i])
  21. i++;
  22. if (keys[i] == key)
  23. return this;
  24. if (leaf)
  25. return null;
  26. return children[i].search(key);
  27. }
  28. // Function to insert a new key
  29. // in subtree rooted with this node
  30. public void insertNonFull(int key) {
  31. int i = n - 1;
  32. if (leaf) {
  33. while (i >= 0 && key < keys[i]) {
  34. keys[i + 1] = keys[i];
  35. i--;
  36. }
  37. keys[i + 1] = key;
  38. n++;
  39. } else {
  40. while (i >= 0 && key < keys[i])
  41. i--;
  42. i++;
  43. if (children[i].n == 2 * t - 1) {
  44. splitChild(i, children[i]);
  45. if (key > keys[i])
  46. i++;
  47. }
  48. children[i].insertNonFull(key);
  49. }
  50. }
  51. // Function to split the child node
  52. public void splitChild(int i, BTreeNode y) {
  53. BTreeNode z = new BTreeNode(y.t, y.leaf);
  54. z.n = t - 1;
  55. for (int j = 0; j < t - 1; j++)
  56. z.keys[j] = y.keys[j + t];
  57. if (!y.leaf) {
  58. for (int j = 0; j < t; j++)
  59. z.children[j] = y.children[j + t];
  60. }
  61. y.n = t - 1;
  62. for (int j = n; j >= i + 1; j--)
  63. children[j + 1] = children[j];
  64. children[i + 1] = z;
  65. for (int j = n - 1; j >= i; j--)
  66. keys[j + 1] = keys[j];
  67. keys[i] = y.keys[t - 1];
  68. n++;
  69. }
  70. // Function to print all keys in the
  71. // subtree rooted with this node
  72. public void printInOrder() {
  73. int i;
  74. for (i = 0; i < n; i++) {
  75. if (!leaf)
  76. children[i].printInOrder();
  77. System.out.print(keys[i] + " ");
  78. }
  79. if (!leaf)
  80. children[i].printInOrder();
  81. }
  82. }
  83. public class BTree {
  84. // Pointer to root node
  85. private BTreeNode root;
  86. // Minimum degree
  87. private int t;
  88. public BTree(int t) {
  89. this.t = t;
  90. root = null;
  91. }
  92. // Function to search a key in this tree
  93. public BTreeNode search(int key) {
  94. return (root == null) ? null : root.search(key);
  95. }
  96. // Function to insert a key into the B-tree
  97. public void insert(int key) {
  98. if (root == null) {
  99. root = new BTreeNode(t, true);
  100. root.keys[0] = key;
  101. root.n = 1;
  102. } else {
  103. if (root.n == 2 * t - 1) {
  104. BTreeNode newRoot = new BTreeNode(t, false);
  105. newRoot.children[0] = root;
  106. newRoot.splitChild(0, root);
  107. int i = 0;
  108. if (newRoot.keys[0] < key)
  109. i++;
  110. newRoot.children[i].insertNonFull(key);
  111. root = newRoot;
  112. } else {
  113. root.insertNonFull(key);
  114. }
  115. }
  116. }
  117. // Function to print the entire B-tree
  118. public void printBTree() {
  119. if (root != null)
  120. root.printInOrder();
  121. System.out.println();
  122. }
  123. public static void main(String[] args) {
  124. // Create a B-tree with minimum degree 3
  125. BTree bTree = new BTree(3);
  126. bTree.insert(10);
  127. bTree.insert(20);
  128. bTree.insert(5);
  129. bTree.insert(6);
  130. bTree.insert(12);
  131. bTree.insert(30);
  132. System.out.print("B-tree : ");
  133. bTree.printBTree();
  134. int searchKey = 6;
  135. BTreeNode foundNode = bTree.search(searchKey);
  136. if (foundNode != null)
  137. System.out.println("Key " + searchKey + " found in the B-tree.");
  138. else
  139. System.out.println("Key " + searchKey + " not found in the B-tree.");
  140. }
  141. }