File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
src/data-structures/trees Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ const BinarySearchTree = require ( './binary-search-tree' ) ;
2+
3+ class RedBlackBST extends BinarySearchTree {
4+ add ( node ) {
5+ return super . add ( node ) ;
6+ }
7+ }
8+
9+ module . exports = RedBlackBST ;
Original file line number Diff line number Diff line change 1+ const RedBlackBST = require ( './red-black-bst.js' ) ;
2+
3+ describe ( 'RedBlackBST' , ( ) => {
4+ let redBlackBST ;
5+
6+ beforeEach ( ( ) => {
7+ redBlackBST = new RedBlackBST ( ) ;
8+ } ) ;
9+
10+ describe ( '#add' , ( ) => {
11+ it ( 'should add and self-balance the tree' , ( ) => {
12+ expect ( redBlackBST ) . not . toBe ( undefined ) ;
13+ } ) ;
14+
15+ it ( 'should add a node' , ( ) => {
16+ redBlackBST . add ( 1 ) ;
17+ expect ( redBlackBST . size ) . toBe ( 1 ) ;
18+ } ) ;
19+
20+ it ( 'should balance tree' , ( ) => {
21+ const n1 = redBlackBST . add ( 1 ) ;
22+ const n2 = redBlackBST . add ( 2 ) ;
23+ const n3 = redBlackBST . add ( 3 ) ;
24+
25+ expect ( n1 . right . value ) . toBe ( 2 ) ;
26+ expect ( n2 . right . value ) . toBe ( 3 ) ;
27+ expect ( n3 . right ) . toBe ( undefined ) ;
28+
29+ expect ( redBlackBST . size ) . toBe ( 3 ) ;
30+ } ) ;
31+ } ) ;
32+ } ) ;
You can’t perform that action at this time.
0 commit comments