|
| 1 | +packagecom.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | + |
| 5 | +/** |
| 6 | + * Implementation of a B-Tree, a self-balancing tree data structure that maintains sorted data |
| 7 | + * and allows searches, sequential access, insertions, and deletions in logarithmic time. |
| 8 | + * |
| 9 | + * B-Trees are generalizations of binary search trees in that a node can have more than two children. |
| 10 | + * They're widely used in databases and file systems. |
| 11 | + * |
| 12 | + * For more information: https://en.wikipedia.org/wiki/B-tree |
| 13 | + */ |
| 14 | + |
| 15 | +publicclassBTree { |
| 16 | +staticclassBTreeNode { |
| 17 | +int[]keys; |
| 18 | +intt;// Minimum degree (defines range for number of keys) |
| 19 | +BTreeNode[]children; |
| 20 | +intn;// Current number of keys |
| 21 | +booleanleaf; |
| 22 | + |
| 23 | +BTreeNode(intt,booleanleaf) { |
| 24 | +this.t =t; |
| 25 | +this.leaf =leaf; |
| 26 | +this.keys =newint[2 *t -1]; |
| 27 | +this.children =newBTreeNode[2 *t]; |
| 28 | +this.n =0; |
| 29 | + } |
| 30 | + |
| 31 | +voidtraverse(ArrayList<Integer>result) { |
| 32 | +for (inti =0;i <n;i++) { |
| 33 | +if (!leaf) { |
| 34 | +children[i].traverse(result); |
| 35 | + } |
| 36 | +result.add(keys[i]); |
| 37 | + } |
| 38 | +if (!leaf) { |
| 39 | +children[n].traverse(result); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | +BTreeNodesearch(intkey) { |
| 44 | +inti =0; |
| 45 | +while (i <n &&key >keys[i]) { |
| 46 | +i++; |
| 47 | + } |
| 48 | +if (i <n &&keys[i] ==key) { |
| 49 | +returnthis; |
| 50 | + } |
| 51 | +if (leaf) { |
| 52 | +returnnull; |
| 53 | + } |
| 54 | +returnchildren[i].search(key); |
| 55 | + } |
| 56 | + |
| 57 | +voidinsertNonFull(intkey) { |
| 58 | +inti =n -1; |
| 59 | +if (leaf) { |
| 60 | +while (i >=0 &&keys[i] >key) { |
| 61 | +keys[i +1] =keys[i]; |
| 62 | +i--; |
| 63 | + } |
| 64 | +keys[i +1] =key; |
| 65 | +n++; |
| 66 | + }else { |
| 67 | +while (i >=0 &&keys[i] >key) { |
| 68 | +i--; |
| 69 | + } |
| 70 | +if (children[i +1].n ==2 *t -1) { |
| 71 | +splitChild(i +1,children[i +1]); |
| 72 | +if (keys[i +1] <key) { |
| 73 | +i++; |
| 74 | + } |
| 75 | + } |
| 76 | +children[i +1].insertNonFull(key); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | +voidsplitChild(inti,BTreeNodey) { |
| 81 | +BTreeNodez =newBTreeNode(y.t,y.leaf); |
| 82 | +z.n =t -1; |
| 83 | + |
| 84 | +System.arraycopy(y.keys,t,z.keys,0,t -1); |
| 85 | +if (!y.leaf) { |
| 86 | +System.arraycopy(y.children,t,z.children,0,t); |
| 87 | + } |
| 88 | +y.n =t -1; |
| 89 | + |
| 90 | +for (intj =n;j >=i +1;j--) { |
| 91 | +children[j +1] =children[j]; |
| 92 | + } |
| 93 | +children[i +1] =z; |
| 94 | + |
| 95 | +for (intj =n -1;j >=i;j--) { |
| 96 | +keys[j +1] =keys[j]; |
| 97 | + } |
| 98 | +keys[i] =y.keys[t -1]; |
| 99 | +n++; |
| 100 | + } |
| 101 | + |
| 102 | +voidremove(intkey) { |
| 103 | +intidx =findKey(key); |
| 104 | + |
| 105 | +if (idx <n &&keys[idx] ==key) { |
| 106 | +if (leaf) { |
| 107 | +removeFromLeaf(idx); |
| 108 | + }else { |
| 109 | +removeFromNonLeaf(idx); |
| 110 | + } |
| 111 | + }else { |
| 112 | +if (leaf) { |
| 113 | +return;// Key not found |
| 114 | + } |
| 115 | + |
| 116 | +booleanflag =idx ==n; |
| 117 | +if (children[idx].n <t) { |
| 118 | +fill(idx); |
| 119 | + } |
| 120 | + |
| 121 | +if (flag &&idx >n) { |
| 122 | +children[idx -1].remove(key); |
| 123 | + }else { |
| 124 | +children[idx].remove(key); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +privateintfindKey(intkey) { |
| 130 | +intidx =0; |
| 131 | +while (idx <n &&keys[idx] <key) { |
| 132 | + ++idx; |
| 133 | + } |
| 134 | +returnidx; |
| 135 | + } |
| 136 | + |
| 137 | +privatevoidremoveFromLeaf(intidx) { |
| 138 | +for (inti =idx +1;i <n; ++i) { |
| 139 | +keys[i -1] =keys[i]; |
| 140 | + } |
| 141 | +n--; |
| 142 | + } |
| 143 | + |
| 144 | +privatevoidremoveFromNonLeaf(intidx) { |
| 145 | +intkey =keys[idx]; |
| 146 | +if (children[idx].n >=t) { |
| 147 | +intpred =getPredecessor(idx); |
| 148 | +keys[idx] =pred; |
| 149 | +children[idx].remove(pred); |
| 150 | + }elseif (children[idx +1].n >=t) { |
| 151 | +intsucc =getSuccessor(idx); |
| 152 | +keys[idx] =succ; |
| 153 | +children[idx +1].remove(succ); |
| 154 | + }else { |
| 155 | +merge(idx); |
| 156 | +children[idx].remove(key); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +privateintgetPredecessor(intidx) { |
| 161 | +BTreeNodecur =children[idx]; |
| 162 | +while (!cur.leaf) { |
| 163 | +cur =cur.children[cur.n]; |
| 164 | + } |
| 165 | +returncur.keys[cur.n -1]; |
| 166 | + } |
| 167 | + |
| 168 | +privateintgetSuccessor(intidx) { |
| 169 | +BTreeNodecur =children[idx +1]; |
| 170 | +while (!cur.leaf) { |
| 171 | +cur =cur.children[0]; |
| 172 | + } |
| 173 | +returncur.keys[0]; |
| 174 | + } |
| 175 | + |
| 176 | +privatevoidfill(intidx) { |
| 177 | +if (idx !=0 &&children[idx -1].n >=t) { |
| 178 | +borrowFromPrev(idx); |
| 179 | + }elseif (idx !=n &&children[idx +1].n >=t) { |
| 180 | +borrowFromNext(idx); |
| 181 | + }else { |
| 182 | +if (idx !=n) { |
| 183 | +merge(idx); |
| 184 | + }else { |
| 185 | +merge(idx -1); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | +privatevoidborrowFromPrev(intidx) { |
| 191 | +BTreeNodechild =children[idx]; |
| 192 | +BTreeNodesibling =children[idx -1]; |
| 193 | + |
| 194 | +for (inti =child.n -1;i >=0; --i) { |
| 195 | +child.keys[i +1] =child.keys[i]; |
| 196 | + } |
| 197 | + |
| 198 | +if (!child.leaf) { |
| 199 | +for (inti =child.n;i >=0; --i) { |
| 200 | +child.children[i +1] =child.children[i]; |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | +child.keys[0] =keys[idx -1]; |
| 205 | + |
| 206 | +if (!child.leaf) { |
| 207 | +child.children[0] =sibling.children[sibling.n]; |
| 208 | + } |
| 209 | + |
| 210 | +keys[idx -1] =sibling.keys[sibling.n -1]; |
| 211 | + |
| 212 | +child.n +=1; |
| 213 | +sibling.n -=1; |
| 214 | + } |
| 215 | + |
| 216 | +privatevoidborrowFromNext(intidx) { |
| 217 | +BTreeNodechild =children[idx]; |
| 218 | +BTreeNodesibling =children[idx +1]; |
| 219 | + |
| 220 | +child.keys[child.n] =keys[idx]; |
| 221 | + |
| 222 | +if (!child.leaf) { |
| 223 | +child.children[child.n +1] =sibling.children[0]; |
| 224 | + } |
| 225 | + |
| 226 | +keys[idx] =sibling.keys[0]; |
| 227 | + |
| 228 | +for (inti =1;i <sibling.n; ++i) { |
| 229 | +sibling.keys[i -1] =sibling.keys[i]; |
| 230 | + } |
| 231 | + |
| 232 | +if (!sibling.leaf) { |
| 233 | +for (inti =1;i <=sibling.n; ++i) { |
| 234 | +sibling.children[i -1] =sibling.children[i]; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | +child.n +=1; |
| 239 | +sibling.n -=1; |
| 240 | + } |
| 241 | + |
| 242 | +privatevoidmerge(intidx) { |
| 243 | +BTreeNodechild =children[idx]; |
| 244 | +BTreeNodesibling =children[idx +1]; |
| 245 | + |
| 246 | +child.keys[t -1] =keys[idx]; |
| 247 | + |
| 248 | +for (inti =0;i <sibling.n; ++i) { |
| 249 | +child.keys[i +t] =sibling.keys[i]; |
| 250 | + } |
| 251 | + |
| 252 | +if (!child.leaf) { |
| 253 | +for (inti =0;i <=sibling.n; ++i) { |
| 254 | +child.children[i +t] =sibling.children[i]; |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | +for (inti =idx +1;i <n; ++i) { |
| 259 | +keys[i -1] =keys[i]; |
| 260 | + } |
| 261 | + |
| 262 | +for (inti =idx +2;i <=n; ++i) { |
| 263 | +children[i -1] =children[i]; |
| 264 | + } |
| 265 | + |
| 266 | +child.n +=sibling.n +1; |
| 267 | +n--; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | +privateBTreeNoderoot; |
| 272 | +privatefinalintt; |
| 273 | + |
| 274 | +publicBTree(intt) { |
| 275 | +this.root =null; |
| 276 | +this.t =t; |
| 277 | + } |
| 278 | + |
| 279 | +publicvoidtraverse(ArrayList<Integer>result) { |
| 280 | +if (root !=null) { |
| 281 | +root.traverse(result); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | +publicbooleansearch(intkey) { |
| 286 | +returnroot !=null &&root.search(key) !=null; |
| 287 | + } |
| 288 | + |
| 289 | +publicvoidinsert(intkey) { |
| 290 | +if (search(key)) { |
| 291 | +return; |
| 292 | + } |
| 293 | +if (root ==null) { |
| 294 | +root =newBTreeNode(t,true); |
| 295 | +root.keys[0] =key; |
| 296 | +root.n =1; |
| 297 | + }else { |
| 298 | +if (root.n ==2 *t -1) { |
| 299 | +BTreeNodes =newBTreeNode(t,false); |
| 300 | +s.children[0] =root; |
| 301 | +s.splitChild(0,root); |
| 302 | +inti =0; |
| 303 | +if (s.keys[0] <key) { |
| 304 | +i++; |
| 305 | + } |
| 306 | +s.children[i].insertNonFull(key); |
| 307 | +root =s; |
| 308 | + }else { |
| 309 | +root.insertNonFull(key); |
| 310 | + } |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | +publicvoiddelete(intkey) { |
| 315 | +if (root ==null) { |
| 316 | +return; |
| 317 | + } |
| 318 | +root.remove(key); |
| 319 | +if (root.n ==0) { |
| 320 | +root =root.leaf ?null :root.children[0]; |
| 321 | + } |
| 322 | + } |
| 323 | +} |