All files / src/data-structures/binary-tree rb-tree.ts

40% Statements 4/10
0% Branches 0/1
0% Functions 0/5
40% Lines 4/10

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 10219x   19x   19x                                                                                                         19x                                                                                        
import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
import { IRBTree, IRBTreeNode } from '../../interfaces/rb-tree';
import { BST, BSTNode } from './bst';
 
export class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>>
  extends BSTNode<T, NEIGHBOR>
  implements IRBTreeNode<T, NEIGHBOR>
{
  constructor(id: BinaryTreeNodeId, val?: T, color: RBColor = RBColor.RED) {
    super(id, val);
    this._color = color;
  }
 
  private _color: RBColor;
 
  get color(): RBColor {
    return this._color;
  }
 
  set color(value: RBColor) {
    this._color = value;
  }
 
  // private override _parent: RBNode<T> | null;
  // override set parent(v: RBNode<T> | null) {
  //     this._parent = v;
  // }
  // override get parent(): RBNode<T> | null {
  //     return this._parent;
  // }
  // private override _left?: RBNode<T> | null;
  //
  // override get left(): RBNode<T> | null | undefined {
  //     return this._left;
  // }
  //
  // override set left(v: RBNode<T> | null | undefined) {
  //     if (v) {
  //         v.parent = this;
  //     }
  //     this._left = v;
  // }
  //
  // private override _right?: RBNode<T> | null;
  //
  // override get right(): RBNode<T> | null | undefined {
  //     return this._right;
  // }
  //
  // override set right(v: RBNode<T> | null | undefined) {
  //     if (v) {
  //         v.parent = this;
  //     }
  //     this._right = v;
  // }
}
 
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
  constructor(options?: RBTreeOptions) {
    super(options);
  }
 
  override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
    return new RBTreeNode(id, val, RBColor.RED) as N;
  }
 
  // private override _root: BinaryTreeNode<N> | null = null;
  //
  // override get root(): BinaryTreeNode<N> | null {
  //     return this._root;
  // }
 
  // insert(id: number, val?: N | null) {
  //
  // }
  //
  // private leftRotate(node: N) {
  //
  // }
  //
  // private rightRotate(node: N) {
  //
  // }
  //
  // private insertFixup(node: N) {
  //
  // }
  //
  // private deleteFixup(node: N) {
  //
  // }
  //
  // private transplant(u: N, v: N) {
  //
  // }
 
  // override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<N>[] {
  //
  //     return [{deleted: new N(0, 0), needBalanced: null}];
  // }
}