@@ -70,7 +70,7 @@ describe('Data Structure : Binary Search Tree', () => {
7070 } ) ;
7171 } ) ;
7272
73- describe ( 'Check if BST `Is Empty`' , ( ) => {
73+ describe ( 'Check if BST `Is Empty`, find Min & Max in BST ' , ( ) => {
7474 const keys = [ 4 , 9 , 2 , 5 , 8 , 12 ] ;
7575
7676 beforeEach ( ( ) => {
@@ -87,16 +87,11 @@ describe('Data Structure : Binary Search Tree', () => {
8787 } ) ;
8888
8989 it ( 'Should return `true` when BST is empty' , ( ) => {
90- bst . remove ( 6 ) ;
90+ // remove all the nodes
91+ keys . push ( 6 ) ; // head node
92+ keys . forEach ( e => bst . remove ( e ) ) ;
9193 expect ( bst . isEmpty ( ) ) . toEqual ( true ) ;
9294 } ) ;
93- } ) ;
94- 95- /*
96-
97- describe('Find maximum value in BST', () => {
98- bst = new BinarySearchTree(6);
99- [4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
10095
10196 it ( 'Should expect maximum key' , ( ) => {
10297 expect ( bst . getMaximum ( ) ) . toEqual ( 12 ) ;
@@ -106,11 +101,6 @@ describe('Data Structure : Binary Search Tree', () => {
106101 bst . add ( 20 ) ;
107102 expect ( bst . getMaximum ( ) ) . toEqual ( 20 ) ;
108103 } ) ;
109- });
110-
111- describe('Find the minimum value in BST', () => {
112- bst = new BinarySearchTree(6);
113- [4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
114104
115105 it ( 'Should expect minimum key' , ( ) => {
116106 expect ( bst . getMinimum ( ) ) . toEqual ( 2 ) ;
@@ -123,8 +113,6 @@ describe('Data Structure : Binary Search Tree', () => {
123113 } ) ;
124114
125115 describe ( 'Remove Node in BST' , ( ) => {
126- bst = null;
127-
128116 beforeEach ( ( ) => {
129117 bst = new BinarySearchTree ( 5 ) ;
130118 } ) ;
@@ -133,9 +121,9 @@ describe('Data Structure : Binary Search Tree', () => {
133121 bst . add ( 4 ) ;
134122 bst . add ( 9 ) ;
135123 bst . add ( 2 ) ;
136- bst.delete(bst.root, 4);
124+ bst . remove ( 4 ) ;
137125 expect ( bst . inorder ( ) ) . toEqual ( [ 2 , 5 , 9 ] ) ;
138- bst.delete(bst.root, 2);
126+ bst . remove ( 2 ) ;
139127 expect ( bst . inorder ( ) ) . toEqual ( [ 5 , 9 ] ) ;
140128 } ) ;
141129
@@ -149,18 +137,26 @@ describe('Data Structure : Binary Search Tree', () => {
149137
150138 describe ( 'Search value in BST' , ( ) => {
151139 bst = new BinarySearchTree ( 6 ) ;
152- [4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
153140
154141 it ( 'Should return `true` for 8' , ( ) => {
155- expect(bst.searchFor(8)).toEqual(true);
142+ [ 4 , 9 , 2 , 5 , 8 , 12 ] . forEach ( el => bst . add ( el ) ) ;
143+ expect ( bst . search ( 8 ) ) . toEqual ( true ) ;
156144 } ) ;
157145
158146 it ( 'Should return `false` for 100' , ( ) => {
159- expect(bst.searchFor (100)).toEqual(false);
147+ expect ( bst . search ( 100 ) ) . toEqual ( false ) ;
160148 } ) ;
161149 } ) ;
162150
163151 describe ( 'Traversals in BST' , ( ) => {
152+ beforeEach ( ( ) => {
153+ bst = new BinarySearchTree ( 6 ) ;
154+ [ 4 , 9 , 2 , 5 , 8 , 12 ] . forEach ( el => bst . add ( el ) ) ;
155+ } ) ;
156+ afterEach ( ( ) => {
157+ if ( bst . root ) bst . root = null ;
158+ } ) ;
159+ 164160 it ( 'Should return the `Preorder Traversal` for given BST' , ( ) => {
165161 const preOrderTraversal = bst . preorder ( ) ;
166162 expect ( preOrderTraversal ) . toEqual ( [ 6 , 4 , 2 , 5 , 9 , 8 , 12 ] ) ;
@@ -176,5 +172,4 @@ describe('Data Structure : Binary Search Tree', () => {
176172 expect ( postOrderTraversal ) . toEqual ( [ 2 , 5 , 4 , 8 , 12 , 9 , 6 ] ) ;
177173 } ) ;
178174 } ) ;
179- */
180175} ) ;
0 commit comments