1
- import { BST } from ' ./binary-search-tree' ;
1
+ import { BST } from " ./binary-search-tree" ;
2
2
3
- describe ( ' Binary Search Tree' , ( ) => {
4
- let bst ;
3
+ describe ( " Binary Search Tree" , ( ) => {
4
+ let bst : BST < number > ;
5
5
6
6
beforeEach ( ( ) => {
7
- bst = new BST ( ) ;
7
+ bst = new BST < number > ( ) ;
8
8
} ) ;
9
9
10
- it ( ' should create bst object' , ( ) => {
10
+ it ( " should create bst object" , ( ) => {
11
11
expect ( bst ) . toBeDefined ( ) ;
12
12
} ) ;
13
13
14
- it ( ' should create BST with null root' , ( ) => {
14
+ it ( " should create BST with null root" , ( ) => {
15
15
expect ( bst . root ) . toBe ( null ) ;
16
16
expect ( bst . len ) . toBe ( 0 ) ;
17
17
} ) ;
18
18
19
- describe ( ' Height of the node' , ( ) => {
19
+ describe ( " Height of the node" , ( ) => {
20
20
beforeEach ( ( ) => {
21
21
bst . insert ( 5 ) ;
22
22
bst . insert ( 6 ) ;
23
23
bst . insert ( 4 ) ;
24
24
bst . insert ( 2 ) ;
25
25
} ) ;
26
26
27
- it ( ' should have height of node 4 as 1' , ( ) => {
27
+ it ( " should have height of node 4 as 1" , ( ) => {
28
28
expect ( bst . height ( bst . lookup ( 4 ) . currentNode ) ) . toBe ( 1 ) ;
29
29
} ) ;
30
30
31
- it ( ' should have height of tree = 2' , ( ) => {
31
+ it ( " should have height of tree = 2" , ( ) => {
32
32
expect ( bst . height ( ) ) . toBe ( 2 ) ;
33
33
} ) ;
34
34
35
- it ( ' should have height 0 of leaf nodes' , ( ) => {
35
+ it ( " should have height 0 of leaf nodes" , ( ) => {
36
36
expect ( bst . height ( bst . lookup ( 6 ) . currentNode ) ) . toBe ( 0 ) ;
37
37
} ) ;
38
38
39
- it ( ' should have height 0 of leaf nodes' , ( ) => {
39
+ it ( " should have height 0 of leaf nodes" , ( ) => {
40
40
expect ( bst . height ( bst . lookup ( 2 ) . currentNode ) ) . toBe ( 0 ) ;
41
41
} ) ;
42
42
} ) ;
43
43
44
- describe ( ' Insertion operation' , ( ) => {
45
- it ( ' should insert values in the BST' , ( ) => {
44
+ describe ( " Insertion operation" , ( ) => {
45
+ it ( " should insert values in the BST" , ( ) => {
46
46
bst . insert ( 5 ) ;
47
47
expect ( bst . root . key ) . toBe ( 5 ) ;
48
48
expect ( bst . len ) . toBe ( 1 ) ;
@@ -57,37 +57,37 @@ describe('Binary Search Tree', () => {
57
57
} ) ;
58
58
} ) ;
59
59
60
- describe ( ' Lookup operation' , ( ) => {
60
+ describe ( " Lookup operation" , ( ) => {
61
61
beforeEach ( ( ) => {
62
62
bst . insert ( 5 ) ;
63
63
bst . insert ( 6 ) ;
64
64
bst . insert ( 4 ) ;
65
65
bst . insert ( 2 ) ;
66
66
} ) ;
67
67
68
- it ( ' should lookup for value 6 in the bst and return the node with key 6 and its parent node' , ( ) => {
68
+ it ( " should lookup for value 6 in the bst and return the node with key 6 and its parent node" , ( ) => {
69
69
const findNode = bst . lookup ( 6 ) ;
70
70
expect ( findNode . hasVal ) . toBeTruthy ( ) ;
71
71
expect ( findNode . currentNode . key ) . toBe ( 6 ) ;
72
72
expect ( findNode . parentNode . key ) . toBe ( 5 ) ;
73
73
} ) ;
74
74
75
- it ( ' should lookup for value 100 in the bst and should return the currentNode and parentNode as null' , ( ) => {
75
+ it ( " should lookup for value 100 in the bst and should return the currentNode and parentNode as null" , ( ) => {
76
76
const findNode = bst . lookup ( 100 ) ;
77
77
expect ( findNode . hasVal ) . toBeFalsy ( ) ;
78
78
expect ( findNode . currentNode ) . toBe ( null ) ;
79
79
expect ( findNode . parentNode ) . toBe ( null ) ;
80
80
} ) ;
81
81
82
- it ( ' should lookup for value 5 and should return currentNode as 5 and parentNode as null' , ( ) => {
82
+ it ( " should lookup for value 5 and should return currentNode as 5 and parentNode as null" , ( ) => {
83
83
const findNode = bst . lookup ( 5 ) ;
84
84
expect ( findNode . hasVal ) . toBeTruthy ( ) ;
85
85
expect ( findNode . currentNode . key ) . toBe ( 5 ) ;
86
86
expect ( findNode . parentNode ) . toBe ( null ) ;
87
87
} ) ;
88
88
} ) ;
89
89
90
- describe ( ' Delete operation' , ( ) => {
90
+ describe ( " Delete operation" , ( ) => {
91
91
beforeEach ( ( ) => {
92
92
bst . insert ( 11 ) ;
93
93
bst . insert ( 6 ) ;
@@ -102,11 +102,11 @@ describe('Binary Search Tree', () => {
102
102
bst . insert ( 49 ) ;
103
103
} ) ;
104
104
105
- it ( ' should have 11 nodes in the bst' , ( ) => {
105
+ it ( " should have 11 nodes in the bst" , ( ) => {
106
106
expect ( bst . len ) . toBe ( 11 ) ;
107
107
} ) ;
108
108
109
- it ( ' should delete leaf with value 5' , ( ) => {
109
+ it ( " should delete leaf with value 5" , ( ) => {
110
110
expect ( bst . len ) . toBe ( 11 ) ;
111
111
112
112
const lookUpFor5 = bst . lookup ( 5 ) ;
@@ -119,7 +119,7 @@ describe('Binary Search Tree', () => {
119
119
expect ( lookUpFor5 . parentNode . right ) . toBe ( null ) ;
120
120
} ) ;
121
121
122
- it ( ' should delete node 8, with one child' , ( ) => {
122
+ it ( " should delete node 8, with one child" , ( ) => {
123
123
expect ( bst . len ) . toBe ( 11 ) ;
124
124
125
125
const lookUpFor8 = bst . lookup ( 8 ) ;
@@ -133,7 +133,7 @@ describe('Binary Search Tree', () => {
133
133
expect ( lookUpFor8 . parentNode . right . key ) . toBe ( 10 ) ;
134
134
} ) ;
135
135
136
- it ( ' should delete node 19, with two children' , ( ) => {
136
+ it ( " should delete node 19, with two children" , ( ) => {
137
137
expect ( bst . len ) . toBe ( 11 ) ;
138
138
139
139
const lookupFor19 = bst . lookup ( 19 ) ;
@@ -146,7 +146,10 @@ describe('Binary Search Tree', () => {
146
146
const successor = bst . findMin ( lookupFor19 . currentNode . right ) ;
147
147
expect ( successor . subtree . key ) . toBe ( 31 ) ;
148
148
149
- const dir = lookupFor19 . currentNode . key > lookupFor19 . parentNode . key ? 'right' : 'left' ;
149
+ const dir =
150
+ lookupFor19 . currentNode . key > lookupFor19 . parentNode . key
151
+ ? "right"
152
+ : "left" ;
150
153
151
154
bst . delete ( 19 ) ;
152
155
expect ( bst . len ) . toBe ( 10 ) ;
@@ -155,7 +158,7 @@ describe('Binary Search Tree', () => {
155
158
} ) ;
156
159
} ) ;
157
160
158
- describe ( ' Traversal Operation' , ( ) => {
161
+ describe ( " Traversal Operation" , ( ) => {
159
162
beforeEach ( ( ) => {
160
163
bst . insert ( 11 ) ;
161
164
bst . insert ( 6 ) ;
@@ -169,20 +172,68 @@ describe('Binary Search Tree', () => {
169
172
bst . insert ( 31 ) ;
170
173
bst . insert ( 49 ) ;
171
174
} ) ;
172
- it ( 'should return inorder list' , ( ) => {
173
- expect ( bst . traverse ( 'inOrder' ) ) . toEqual ( [ 4 , 5 , 6 , 8 , 10 , 11 , 17 , 19 , 31 , 43 , 49 ] ) ;
174
- } ) ;
175
-
176
- it ( 'should return preorder list' , ( ) => {
177
- expect ( bst . traverse ( 'preOrder' ) ) . toEqual ( [ 11 , 6 , 4 , 5 , 8 , 10 , 19 , 17 , 43 , 31 , 49 ] ) ;
178
- } ) ;
179
-
180
- it ( 'should return postorder list' , ( ) => {
181
- expect ( bst . traverse ( 'postOrder' ) ) . toEqual ( [ 5 , 4 , 10 , 8 , 6 , 17 , 31 , 49 , 43 , 19 , 11 ] ) ;
182
- } ) ;
183
-
184
- it ( 'should return bfs list' , ( ) => {
185
- expect ( bst . traverse ( 'levelOrder' ) ) . toEqual ( [ 11 , 6 , 19 , 4 , 8 , 17 , 43 , 5 , 10 , 31 , 49 ] ) ;
175
+ it ( "should return inorder list" , ( ) => {
176
+ expect ( bst . traverse ( "inOrder" ) ) . toEqual ( [
177
+ 4 ,
178
+ 5 ,
179
+ 6 ,
180
+ 8 ,
181
+ 10 ,
182
+ 11 ,
183
+ 17 ,
184
+ 19 ,
185
+ 31 ,
186
+ 43 ,
187
+ 49
188
+ ] ) ;
189
+ } ) ;
190
+
191
+ it ( "should return preorder list" , ( ) => {
192
+ expect ( bst . traverse ( "preOrder" ) ) . toEqual ( [
193
+ 11 ,
194
+ 6 ,
195
+ 4 ,
196
+ 5 ,
197
+ 8 ,
198
+ 10 ,
199
+ 19 ,
200
+ 17 ,
201
+ 43 ,
202
+ 31 ,
203
+ 49
204
+ ] ) ;
205
+ } ) ;
206
+
207
+ it ( "should return postorder list" , ( ) => {
208
+ expect ( bst . traverse ( "postOrder" ) ) . toEqual ( [
209
+ 5 ,
210
+ 4 ,
211
+ 10 ,
212
+ 8 ,
213
+ 6 ,
214
+ 17 ,
215
+ 31 ,
216
+ 49 ,
217
+ 43 ,
218
+ 19 ,
219
+ 11
220
+ ] ) ;
221
+ } ) ;
222
+
223
+ it ( "should return bfs list" , ( ) => {
224
+ expect ( bst . traverse ( "levelOrder" ) ) . toEqual ( [
225
+ 11 ,
226
+ 6 ,
227
+ 19 ,
228
+ 4 ,
229
+ 8 ,
230
+ 17 ,
231
+ 43 ,
232
+ 5 ,
233
+ 10 ,
234
+ 31 ,
235
+ 49
236
+ ] ) ;
186
237
} ) ;
187
238
} ) ;
188
- } ) ;
239
+ } ) ;
0 commit comments