File tree Expand file tree Collapse file tree 4 files changed +77
-3
lines changed
src/data-structures/trees Expand file tree Collapse file tree 4 files changed +77
-3
lines changed Original file line number Diff line number Diff line change 11const BinarySearchTree = require ( './binary-search-tree' ) ;
2+ // const TreeNode = require('./tree-node');
3+
24/**
35 * Red-Black Tree
46 *
@@ -23,19 +25,29 @@ class RedBlackBST extends BinarySearchTree {
2325 * @param {any } value new nodes' value
2426 */
2527 add ( value ) {
28+ // add node using the regular BST add
2629 const node = super . add ( value ) ;
2730
2831 if ( node === this . root ) {
2932 node . meta . color = RedBlackBST . BLACK ;
3033 } else {
3134 node . meta . color = RedBlackBST . RED ;
32- // this.balance(node);
35+ this . balance ( node ) ;
3336 }
3437
3538 return node ;
3639 }
40+
41+ /**
42+ * Balance tree by doing rotations
43+ * @param {TreeNode } node
44+ */
45+ balance ( node ) {
46+
47+ }
3748}
3849
50+
3951RedBlackBST . RED = Symbol ( 'red' ) ;
4052RedBlackBST . BLACK = Symbol ( 'black' ) ;
4153
Original file line number Diff line number Diff line change @@ -12,11 +12,18 @@ describe('RedBlackBST', () => {
1212 expect ( redBlackBST ) . not . toBe ( undefined ) ;
1313 } ) ;
1414
15- it ( 'should add a node' , ( ) => {
16- redBlackBST . add ( 1 ) ;
15+ it ( 'should make root black' , ( ) => {
16+ const root = redBlackBST . add ( 1 ) ;
17+ expect ( root . meta . color ) . toBe ( RedBlackBST . BLACK ) ;
1718 expect ( redBlackBST . size ) . toBe ( 1 ) ;
1819 } ) ;
1920
21+ it ( 'should add a new node as red' , ( ) => {
22+ redBlackBST . add ( 1 ) ;
23+ const n2 = redBlackBST . add ( 2 ) ;
24+ expect ( n2 . meta . color ) . toBe ( RedBlackBST . RED ) ;
25+ } ) ;
26+
2027 xit ( 'should balance tree by rotating left' , ( ) => {
2128 const n1 = redBlackBST . add ( 1 ) ;
2229 const n2 = redBlackBST . add ( 2 ) ;
Original file line number Diff line number Diff line change @@ -31,6 +31,24 @@ class TreeNode {
3131 this . descendents [ 1 ] = node ;
3232 if ( node ) { node . parent = this ; } // eslint-disable-line no-param-reassign
3333 }
34+
35+ /**
36+ * Get sibling of current node
37+ */
38+ get sibling ( ) {
39+ const { parent } = this ;
40+ if ( ! parent ) return null ;
41+ return parent . right === this ? parent . left : parent . right ;
42+ }
43+
44+ /**
45+ * Get parent sibling = uncle (duh)
46+ */
47+ get uncle ( ) {
48+ const { parent } = this ;
49+ if ( ! parent ) return null ;
50+ return parent . sibling ;
51+ }
3452}
3553
3654module . exports = TreeNode ;
Original file line number Diff line number Diff line change @@ -34,4 +34,41 @@ describe('Tree Node', () => {
3434 expect ( treeNode . right . value ) . toBe ( 1 ) ;
3535 expect ( newNode . parent ) . toBe ( treeNode ) ;
3636 } ) ;
37+
38+ describe ( 'Family operations' , ( ) => {
39+ let g ;
40+ let p ;
41+ let u ;
42+ let c ;
43+ let s ;
44+
45+ beforeEach ( ( ) => {
46+ g = new TreeNode ( 'grandparent' ) ;
47+ p = new TreeNode ( 'parent' ) ;
48+ u = new TreeNode ( 'uncle' ) ;
49+ c = new TreeNode ( 'child' ) ;
50+ s = new TreeNode ( 'sibling' ) ;
51+
52+ g . right = p ;
53+ g . left = u ;
54+ p . right = c ;
55+ p . left = s ;
56+ } ) ;
57+
58+ it ( 'should get the sibling' , ( ) => {
59+ expect ( c . sibling ) . toBe ( s ) ;
60+ } ) ;
61+
62+ it ( 'should get null if no sibling' , ( ) => {
63+ expect ( g . sibling ) . toBe ( null ) ;
64+ } ) ;
65+
66+ it ( 'should get the uncle' , ( ) => {
67+ expect ( c . uncle ) . toBe ( u ) ;
68+ } ) ;
69+
70+ it ( 'should get null if no uncle' , ( ) => {
71+ expect ( g . uncle ) . toBe ( null ) ;
72+ } ) ;
73+ } ) ;
3774} ) ;
You can’t perform that action at this time.
0 commit comments