Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | /* eslint-disable */ // code copied from https://levelup.gitconnected.com/building-a-b-tree-in-javascript-4482dee083cb import { v4 as uuidv4 } from 'uuid'; export class BTreeNode { constructor(isLeaf) { /** * @type {number[]} list of values in the node */ this.values = []; /** * @type {boolean} is a leaf */ this.leaf = isLeaf; /** * @type {BTreeNode[]} */ this.children = []; /** * Reference to the tree its belong. * @type {BTree} */ this.tree = null; /** * @type {BTreeNode} */ this.parent = null; this.id = 'N' + uuidv4(); } getId() { return this.id; } /** * Number of values * @returns {number} */ get n() { return this.values.length; } /** * Add value * @param {number} value * @param {number} pos */ addValue(value) { if (!value) { return; } let pos = 0; while (pos < this.n && this.values[pos] < value) { pos++; } this.values.splice(pos, 0, value); } /** * Delete value and return it * @param {number} pos position * @return {number} */ removeValue(pos) { if (pos >= this.n) { return null; } return this.values.splice(pos, 1)[0]; } /** * Add child node at position pos * @param {BTreeNode} node * @param {number} pos */ addChild(node, pos) { this.children.splice(pos, 0, node); node.parent = this; } /** * Delete node from position and return it * @param {number} pos * @return {BTreeNode} */ deleteChild(pos) { return this.children.splice(pos, 1)[0]; } } /** * btree namespace. * @type {BTree} */ export default class BTree { constructor(order) { /** @type {number} */ this.order = order; /** * Root node of the tree. * @type {BTreeNode} */ this.root = null; } /** * Search a value in the Tree and return the node. O(log N) * @param {number} value * @param {BTreeNode} node * @returns {BTreeNode} */ searchValue(node, value) { if (node.values.includes(value)) { return node; } if (node.leaf) { // Value was not found return null; } let child = 0; while (child <= node.n && node.values[child] < parseInt(value, 10)) { child++; } return this.searchValue(node.children[child]); } /** * Deletes the value from the Tree. O(log N) * @param {number} value */ delete(value) { if (this.root.n === 1 && !this.root.leaf && this.root.children[0].n === this.order-1 && this.root.children[1].n === this.order -1) { // Check if the root can shrink the tree into its childs this.mergeNodes(this.root.children[1], this.root.children[0]); this.root = this.root.children[0]; } // Start looking for the value to delete this.deleteFromNode(this.root, parseInt(value, 10)); } /** * Delete a value from a node. O(log N) * @param {BTreeNode} node * @param {number} value */ deleteFromNode(node, value) { // Check if value is in the actual node const index = node.values.indexOf(value); if (index >= 0) { // Value present in the node if (node.leaf && node.n > this.order - 1) { // If the node is a leaf and has more than order-1 values, just delete it node.removeValue(node.values.indexOf(value)); return true; } // Check if one children has enough values to transfer if (node.children[index].n > this.order - 1 || node.children[index + 1].n > this.order - 1) { // One of the immediate children has enough values to transfer if (node.children[index].n > this.order - 1) { // Replace the target value for the higher of left node. // Then delete that value from the child const predecessor = this.getMinMaxFromSubTree(node.children[index], 1); node.values[index] = predecessor; return this.deleteFromNode(node.children[index], predecessor); } const successor = this.getMinMaxFromSubTree(node.children[index+1], 0); node.values[index] = successor; return this.deleteFromNode(node.children[index+1], successor); } // Children has not enough values to transfer. Do a merge this.mergeNodes(node.children[index + 1], node.children[index]); return this.deleteFromNode(node.children[index], value); } // Value is not present in the node if (node.leaf) { // Value is not in the tree return false; } // Value is not present in the node, search in the children let nextNode = 0; while (nextNode < node.n && node.values[nextNode] < value) { nextNode++; } if (node.children[nextNode].n > this.order - 1) { // Child node has enough values to continue return this.deleteFromNode(node.children[nextNode], value); } // Child node has not enough values to continue // Before visiting next node transfer a value or merge it with a brother if ((nextNode > 0 && node.children[nextNode - 1].n > this.order - 1) || (nextNode < node.n && node.children[nextNode + 1].n > this.order - 1)) { // One of the immediate children has enough values to transfer if (nextNode > 0 && node.children[nextNode - 1].n > this.order - 1) { this.transferValue(node.children[nextNode - 1], node.children[nextNode]); } else { this.transferValue(node.children[nextNode + 1], node.children[nextNode]); } return this.deleteFromNode(node.children[nextNode], value); } // No immediate brother with enough values. // Merge node with immediate one brother this.mergeNodes( nextNode > 0 ? node.children[nextNode - 1] : node.children[nextNode + 1], node.children[nextNode]); return this.deleteFromNode(node.children[nextNode], value); } /** * Transfer one value from the origin to the target. O(1) * @param {BTreeNode} origin * @param {BTreeNode} target */ transferValue(origin, target) { const indexo = origin.parent.children.indexOf(origin); const indext = origin.parent.children.indexOf(target); if (indexo < indext) { target.addValue(target.parent.removeValue(indexo)); origin.parent.addValue(origin.removeValue(origin.n-1)); if (!origin.leaf) { target.addChild(origin.deleteChild(origin.children.length-1), 0); } } else { target.addValue(target.parent.removeValue(indext)); origin.parent.addValue(origin.removeValue(0)); if (!origin.leaf) { target.addChild(origin.deleteChild(0), target.children.length); } } } /** * Merge 2 nodes into one with the parent median value. O(1) * @param {BTreeNode} origin * @param {BTreeNode} target */ mergeNodes(origin, target) { const indexo = origin.parent.children.indexOf(origin); const indext = target.parent.children.indexOf(target); target.addValue(target.parent.removeValue(Math.min(indexo, indext))); for (let i = origin.n - 1; i >= 0; i--) { target.addValue(origin.removeValue(i)); } // Remove reference to origin node target.parent.deleteChild(indexo); // Transfer all the children from origin node to target if (!origin.leaf) { while (origin.children.length) { if (indexo > indext) { target.addChild(origin.deleteChild(0), target.children.length); } else { target.addChild(origin.deleteChild(origin.children.length - 1), 0); } } } } /** * Get the lower or higher value in a sub-tree. O(log N) * @param {BTreeNode} node * @param { 0 | 1 } max 1 for find max, 0 for min * @returns {number} */ getMinMaxFromSubTree(node, max) { while (!node.leaf) { node = node.children[max ? node.n : 0]; } return node.values[max ? node.n - 1 : 0]; } /** * Insert a new value in the tree O(log N) * @param {number} value */ insert(value) { const actual = this.root; if (actual.n === 2 * this.order - 1) { // Create a new node to become the root // Append the old root to the new one const temp = new BTreeNode(false); temp.tree = this; this.root = temp; temp.addChild(actual, 0); this.split(actual, temp, 1); this.insertNonFull(temp, parseInt(value, 10)); } else { this.insertNonFull(actual, parseInt(value, 10)); } }; insert_(value) { const currentNode = this.root; while (currentNode.leaf === false) { } } /** * Divide child node from parent into parent.values[pos-1] and parent.values[pos]. O(1) * @param {BTreeNode} child * @param {BTreeNode} parent * @param {number} pos */ split(child, parent, pos) { const newChild = new BTreeNode(child.leaf); newChild.tree = this.root.tree; // Create a new child // Pass values from the old child to the new for (let k = 1; k < this.order; k++) { newChild.addValue(child.removeValue(this.order)); } // Trasspass child nodes from the old child to the new if (!child.leaf) { for (let k = 1; k <= this.order; k++) { newChild.addChild(child.deleteChild(this.order), k - 1); } } // Add new child to the parent parent.addChild(newChild, pos); // Pass value to parent parent.addValue(child.removeValue(this.order - 1)); parent.leaf = false; } /** * Insert a value in a not-full node. O(1) * @param {BTreeNode} node * @param {number} value */ insertNonFull(node, value) { if (node.leaf) { node.addValue(value); return; } let temp = node.n; while (temp >= 1 && value < node.values[temp - 1]) { temp = temp - 1; } if (node.children[temp].n === 2 * this.order - 1) { this.split(node.children[temp], node, temp + 1); if (value > node.values[temp]) { temp = temp + 1; } } this.insertNonFull(node.children[temp], value); } } |