@@ -70,7 +70,7 @@ describe('Data Structure : Binary Search Tree', () => {
70
70
} ) ;
71
71
} ) ;
72
72
73
- describe ( 'Check if BST `Is Empty`' , ( ) => {
73
+ describe ( 'Check if BST `Is Empty`, find Min & Max in BST ' , ( ) => {
74
74
const keys = [ 4 , 9 , 2 , 5 , 8 , 12 ] ;
75
75
76
76
beforeEach ( ( ) => {
@@ -87,16 +87,11 @@ describe('Data Structure : Binary Search Tree', () => {
87
87
} ) ;
88
88
89
89
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 ) ) ;
91
93
expect ( bst . isEmpty ( ) ) . toEqual ( true ) ;
92
94
} ) ;
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));
100
95
101
96
it ( 'Should expect maximum key' , ( ) => {
102
97
expect ( bst . getMaximum ( ) ) . toEqual ( 12 ) ;
@@ -106,11 +101,6 @@ describe('Data Structure : Binary Search Tree', () => {
106
101
bst . add ( 20 ) ;
107
102
expect ( bst . getMaximum ( ) ) . toEqual ( 20 ) ;
108
103
} ) ;
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));
114
104
115
105
it ( 'Should expect minimum key' , ( ) => {
116
106
expect ( bst . getMinimum ( ) ) . toEqual ( 2 ) ;
@@ -123,8 +113,6 @@ describe('Data Structure : Binary Search Tree', () => {
123
113
} ) ;
124
114
125
115
describe ( 'Remove Node in BST' , ( ) => {
126
- bst = null;
127
-
128
116
beforeEach ( ( ) => {
129
117
bst = new BinarySearchTree ( 5 ) ;
130
118
} ) ;
@@ -133,9 +121,9 @@ describe('Data Structure : Binary Search Tree', () => {
133
121
bst . add ( 4 ) ;
134
122
bst . add ( 9 ) ;
135
123
bst . add ( 2 ) ;
136
- bst.delete(bst.root, 4);
124
+ bst . remove ( 4 ) ;
137
125
expect ( bst . inorder ( ) ) . toEqual ( [ 2 , 5 , 9 ] ) ;
138
- bst.delete(bst.root, 2);
126
+ bst . remove ( 2 ) ;
139
127
expect ( bst . inorder ( ) ) . toEqual ( [ 5 , 9 ] ) ;
140
128
} ) ;
141
129
@@ -149,18 +137,26 @@ describe('Data Structure : Binary Search Tree', () => {
149
137
150
138
describe ( 'Search value in BST' , ( ) => {
151
139
bst = new BinarySearchTree ( 6 ) ;
152
- [4, 9, 2, 5, 8, 12].forEach(el => bst.add(el));
153
140
154
141
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 ) ;
156
144
} ) ;
157
145
158
146
it ( 'Should return `false` for 100' , ( ) => {
159
- expect(bst.searchFor (100)).toEqual(false);
147
+ expect ( bst . search ( 100 ) ) . toEqual ( false ) ;
160
148
} ) ;
161
149
} ) ;
162
150
163
151
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
+
164
160
it ( 'Should return the `Preorder Traversal` for given BST' , ( ) => {
165
161
const preOrderTraversal = bst . preorder ( ) ;
166
162
expect ( preOrderTraversal ) . toEqual ( [ 6 , 4 , 2 , 5 , 9 , 8 , 12 ] ) ;
@@ -176,5 +172,4 @@ describe('Data Structure : Binary Search Tree', () => {
176
172
expect ( postOrderTraversal ) . toEqual ( [ 2 , 5 , 4 , 8 , 12 , 9 , 6 ] ) ;
177
173
} ) ;
178
174
} ) ;
179
- */
180
175
} ) ;
0 commit comments