All files / trees BPlusTreeNode.js

0% Statements 0/16
0% Branches 0/2
0% Functions 0/2
0% Lines 0/15

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                                                                                                                                                                                         
import { v4 as uuidv4 } from 'uuid';
 
/**
 * A Node within the B+Tree
 */
export default class Node {
  /**
   * Node Type: Internal Node
   * Does not hold any data, just pointers to child nodes.
   * @type {number}
   */
  static INTERNAL_NODE = 1;
 
  /**
   * Node Type: Leaf Node
   * Holds the data, has no child nodes.
   * @type {number}
   */
  static LEAF_NODE = 2;
 
  /**
   * Node-Type (Internal Node, Leaf Node)
   * @type {number}
   */
  type;
 
  /**
   * Number of used Elements in Node
   * @type {number}
   */
  used;
 
  /**
   * Value-Array
   * size is 2*N
   * @type {number[]}
   */
  values = [];
 
  /**
   * ChildNode-Array
   * size is 2*N+1
   * @type {Node[]}
   */
  children = [];
 
  /**
   * Previous Leaf-Pointer
   * @type {Node}
   */
  previous;
 
  /**
   * Next Leaf-Pointer
   * @type {Node}
   */
  next;
 
  /**
   * @type {String}
   */
  id;
 
  /**
   * Constructor
   * @param {number} type Internal or Leaf Node
   * @param {number} N Order of the Tree
   */
  constructor(type, N) {
    this.type = type;
    this.used = 0;
    this.values = new Array(2 * N);
    this.children = new Array(2 * N + 1);
    this.previous = undefined;
    this.next = undefined;
    this.id = `N${uuidv4()}`;
  }
 
  /**
   * Destructor
   * Delete all child nodes.
   * JS does not have destructors, so we have to implement and call them ourselves.
   */
  destructor() {
    if (this.type === Node.INTERNAL_NODE) {
      for (let i = 0; i < this.used + 1; i += 1) {
        this.children[i].destructor();
        delete this.children[i];
      }
    }
  }
}