|
| 1 | +/* eslint-disable class-methods-use-this */ |
| 2 | +/* eslint-disable no-param-reassign */ |
| 3 | +const Node = require('./Node'); |
| 4 | + |
| 5 | +class BTree { |
| 6 | + constructor(t) { |
| 7 | + this.t = t; |
| 8 | + this.root = null; |
| 9 | + } |
| 10 | + |
| 11 | + splitChild(index, parent, y) { |
| 12 | + const z = new Node(this.t, y.isLeaf); |
| 13 | + z.totalKey = this.t - 1; |
| 14 | + |
| 15 | + for (let i = 0; i < this.t - 1; i += 1) { |
| 16 | + z.keys.push(y.keys[this.t + i]); |
| 17 | + } |
| 18 | + |
| 19 | + if (y.isLeaf === false) { |
| 20 | + for (let i = 0; i < this.t; i += 1) { |
| 21 | + z.child.push(y.child[this.t + 1]); |
| 22 | + } |
| 23 | + y.child = y.child.slice(0, this.t); |
| 24 | + } |
| 25 | + y.totalKey = this.t - 1; |
| 26 | + |
| 27 | + for (let i = parent.totalKey; i >= index + 1; i -= 1) { |
| 28 | + parent.child[i + 1] = parent.child[i]; |
| 29 | + } |
| 30 | + parent.child[index + 1] = z; |
| 31 | + |
| 32 | + for (let i = parent.totalKey - 1; i >= index; i -= 1) { |
| 33 | + parent.keys[i + 1] = parent.keys[i]; |
| 34 | + } |
| 35 | + |
| 36 | + parent.keys[index] = y.keys[this.t - 1]; |
| 37 | + y.keys = y.keys.slice(0, this.t - 1); |
| 38 | + parent.totalKey += 1; |
| 39 | + } |
| 40 | + |
| 41 | + insertNonFull(node, key) { |
| 42 | + let i = node.totalKey - 1; |
| 43 | + |
| 44 | + if (node.isLeaf === true) { |
| 45 | + while (i >= 0 && node.keys[i] > key) { |
| 46 | + node.keys[i + 1] = node.keys[i]; |
| 47 | + i -= 1; |
| 48 | + } |
| 49 | + node.keys[i + 1] = key; |
| 50 | + node.totalKey += 1; |
| 51 | + } else { |
| 52 | + while (i >= 0 && node.keys[i] > key) i -= 1; |
| 53 | + |
| 54 | + if (node.child[i + 1].totalKey === 2 * this.t - 1) { |
| 55 | + this.splitChild(i + 1, node, node.child[i + 1]); |
| 56 | + |
| 57 | + if (node.keys[i + 1] < key) i += 1; |
| 58 | + } |
| 59 | + this.insertNonFull(node.child[i + 1], key); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + insert(key) { |
| 64 | + if (this.root === null) { |
| 65 | + this.root = new Node(this.t, true); |
| 66 | + this.root.keys.push(key); |
| 67 | + this.root.totalKey = 1; |
| 68 | + } else if (this.root.totalKey === 2 * this.t - 1) { |
| 69 | + const newRoot = new Node(this.t, false); |
| 70 | + newRoot.child[0] = this.root; |
| 71 | + this.splitChild(0, newRoot, this.root); |
| 72 | + let i = 0; |
| 73 | + if (newRoot.keys[i] < key) i += 1; |
| 74 | + this.insertNonFull(newRoot.child[i], key); |
| 75 | + this.root = newRoot; |
| 76 | + } else { |
| 77 | + this.insertNonFull(this.root, key); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// const tree = new BTree(3); |
| 83 | +// tree.insert(10); |
| 84 | +// tree.insert(20); |
| 85 | +// tree.insert(30); |
| 86 | +// tree.insert(40); |
| 87 | +// tree.insert(50); |
| 88 | +// tree.insert(60); |
| 89 | +// tree.insert(70); |
| 90 | +// tree.insert(80); |
| 91 | +// tree.insert(90); |
| 92 | +// tree.insert(100); |
| 93 | +// tree.insert(110); |
| 94 | +// tree.insert(120); |
| 95 | +// tree.insert(130); |
| 96 | +// tree.insert(140); |
| 97 | +// tree.insert(150); |
| 98 | +// tree.insert(160); |
| 99 | + |
| 100 | + |
| 101 | +// console.log('----root---'); |
| 102 | +// console.log(tree.root); |
| 103 | + |
| 104 | +// console.log('----child0---'); |
| 105 | +// console.log(tree.root.child[0]); |
| 106 | + |
| 107 | +// console.log('----child1---'); |
| 108 | +// console.log(tree.root.child[1]); |
| 109 | + |
| 110 | +// console.log('----child2---'); |
| 111 | +// console.log(tree.root.child[2]); |
| 112 | + |
| 113 | + |
| 114 | +module.export = BTree; |
0 commit comments