1
- import BSTNode from "./bSTNode" ;
2
- import Queue from "../basics/queue" ;
1
+ import BSTNode from "./bst_node" ;
3
2
import Stack from "../basics/stack" ;
4
3
4
+ // TODO: test
5
+
5
6
class BST {
6
7
root : null | BSTNode ;
7
8
size : number ;
@@ -45,26 +46,26 @@ class BST {
45
46
// **********************************************************
46
47
47
48
// Inserts a node in the right position and rearrange
48
- // Returns this BST
49
+ // Returns the inserted node
49
50
// Returns false whether this val is already in this BST
50
51
insert = ( val : number ) => {
51
52
let node = new BSTNode ( val ) ;
52
53
if ( this . isEmpty ( ) ) {
53
54
this . size ++ ;
54
55
this . root = node ;
55
- return this ;
56
+ return node ;
56
57
}
57
58
let current = this . root ;
58
59
while ( current !== null ) {
59
60
// duplicate val
60
- if ( current . val === node . val ) return false ;
61
+ // if (current.val === node.val) return false;
61
62
// check left
62
63
if ( current . val > node . val ) {
63
64
if ( ! current . left ) {
64
65
node . parent = current ;
65
66
current . left = node ;
66
67
this . size ++ ;
67
- return this ;
68
+ return node ;
68
69
}
69
70
// update current to left
70
71
current = current . left ;
@@ -74,7 +75,7 @@ class BST {
74
75
node . parent = current ;
75
76
current . right = node ;
76
77
this . size ++ ;
77
- return this ;
78
+ return node ;
78
79
}
79
80
// update current
80
81
current = current . right ;
@@ -86,6 +87,19 @@ class BST {
86
87
// SEARCH
87
88
// **********************************************************
88
89
90
+ search = ( val : number ) => {
91
+ if ( this . isEmpty ( ) ) return false ;
92
+ let current = this . root ;
93
+ while ( current !== null ) {
94
+ if ( current . val === val ) return current ;
95
+ if ( current . val > val ) {
96
+ current = current . left ;
97
+ } else {
98
+ current = current . right ;
99
+ }
100
+ }
101
+ } ;
102
+
89
103
// Returns true if this BST contains this val,
90
104
// Otherwise returns false
91
105
contains = ( val : number ) => {
@@ -211,7 +225,7 @@ class BST {
211
225
// OR the sucessor was prepared in the last if
212
226
// Anyway we just transplant and update pointers to left subtree
213
227
this . transplant ( z , sucessor ) ;
214
- // uodate sucessor left (from null) to left subtree
228
+ // update sucessor left (from null) to left subtree
215
229
sucessor . left = z . left ;
216
230
// update left subtree parent (from z) to sucessor
217
231
sucessor . left . parent = sucessor ;
@@ -223,35 +237,78 @@ class BST {
223
237
// TRANSVERSING
224
238
// **********************************************************
225
239
226
- bfs = ( ) => {
227
- if ( ! this . root ) return undefined ;
228
- let q = new Queue ( ) ;
229
- let visited : Array < number > = [ ] ;
230
- let current : BSTNode ;
231
- q . enQueue ( this . root ) ;
232
- while ( q . size !== 0 ) {
233
- current = q . deQueue ( ) ! . key ;
234
- visited . push ( current . val ) ;
235
- if ( current . left ) q . enQueue ( current . left ) ;
236
- if ( current . right ) q . enQueue ( current . right ) ;
240
+ preOrderTreeWalk = ( x : BSTNode | null , arr : number [ ] = [ ] ) => {
241
+ if ( x ) {
242
+ arr . push ( x . val ) ;
243
+ this . preOrderTreeWalk ( x . left , arr ) ;
244
+ this . preOrderTreeWalk ( x . right , arr ) ;
245
+ }
246
+ // check if all nodes already visited
247
+ if ( this . size === arr . length ) return arr ;
248
+ } ;
249
+
250
+ // to tranverse all BST, in order
251
+ // expect x = root in 1st call
252
+ inOrderTreeWalk = (
253
+ x : BSTNode | null ,
254
+ arr : number [ ] = [ ]
255
+ ) : number [ ] | undefined => {
256
+ if ( x ) {
257
+ this . inOrderTreeWalk ( x . left , arr ) ;
258
+ arr . push ( x . val ) ;
259
+ this . inOrderTreeWalk ( x . right , arr ) ;
260
+ }
261
+ if ( this . size === arr . length ) return arr ;
262
+ } ;
263
+
264
+ // to tranverse the BST, only add to arr nodes inside [s,t]
265
+ inOrderTreeWalkInterval = (
266
+ x : BSTNode | null ,
267
+ s : number ,
268
+ t : number ,
269
+ arr : number [ ] = [ ]
270
+ ) => {
271
+ if ( x ) {
272
+ this . inOrderTreeWalkInterval ( x . left , s , t , arr ) ;
273
+ if ( x . val >= s && x . val <= t ) arr . push ( x . val ) ;
274
+ this . inOrderTreeWalkInterval ( x . right , s , t , arr ) ;
275
+ if ( x . val === t ) return arr ;
276
+ }
277
+ } ;
278
+
279
+ inOrderTreeWalkStack = ( ) => {
280
+ const stack = new Stack ( ) ;
281
+ const arr : number [ ] = [ ] ;
282
+ let current = this . root ;
283
+ while ( stack . size !== 0 || current !== null ) {
284
+ if ( current ) {
285
+ // stack to remember previous node
286
+ stack . push ( current ) ;
287
+ current = current . left ;
288
+ } else {
289
+ // no left child to add (min available node found)
290
+ // come back to previous node
291
+ current = stack . pop ( ) ! . key ;
292
+ // To add only insede an interval [s,t]:
293
+ // check if s <= current.val <= t
294
+ // if we found t we can finish
295
+ arr . push ( current ! . val ) ;
296
+ // lets try right child
297
+ current = current ! . right ;
298
+ }
237
299
}
238
- return visited ;
300
+ return arr ;
239
301
} ;
240
302
241
- dfsPreOrder = ( ) => {
242
- if ( ! this . root ) return undefined ;
243
- let stack = new Stack ( ) ;
244
- let visited : Array < number > = [ ] ;
245
- let current : BSTNode ;
246
- stack . push ( this . root ) ;
247
- while ( stack . size !== 0 ) {
248
- current = stack . pop ( ) ! . key ;
249
- visited . push ( current . val ) ;
250
- if ( current . right ) stack . push ( current . right ) ;
251
- if ( current . left ) stack . push ( current . left ) ;
303
+ posOrderTreeWalk = ( x : BSTNode | null , arr : number [ ] = [ ] ) => {
304
+ if ( x ) {
305
+ this . preOrderTreeWalk ( x . left , arr ) ;
306
+ this . preOrderTreeWalk ( x . right , arr ) ;
307
+ arr . push ( x . val ) ;
252
308
}
253
- return visited ;
309
+ // check if all nodes already visited
310
+ if ( this . size === arr . length ) return arr ;
254
311
} ;
255
312
}
256
313
257
- export = BST ;
314
+ export default BST ;
0 commit comments