File tree 1 file changed +11
-5
lines changed
src/_DataStructures_/Trees/BST
1 file changed +11
-5
lines changed Original file line number Diff line number Diff line change @@ -29,12 +29,17 @@ class BinarySearchTree {
29
29
}
30
30
31
31
preorder ( root ) {
32
- if ( root === null ) return ;
32
+ /** returning an array so as to make testing easy */
33
+ let arr = [ ] ;
34
+ if ( root === null ) return [ ] ;
33
35
// eslint-disable-next-line no-console
34
- console . log ( ` ${ root . value } ` ) ;
36
+ arr . push ( root . value ) ;
35
37
36
- this . preorder ( root . leftChild ) ;
37
- this . preorder ( root . rightChild ) ;
38
+ const left = this . preorder ( root . leftChild ) ;
39
+ arr = [ ...arr , ...left ] ;
40
+ const right = this . preorder ( root . rightChild ) ;
41
+ arr = [ ...arr , ...right ] ;
42
+ return arr ;
38
43
}
39
44
}
40
45
@@ -48,6 +53,7 @@ class BinarySearchTree {
48
53
49
54
// console.log(bst.root);
50
55
51
- // bst.preorder(bst.root);
56
+ // const a = bst.preorder(bst.root);
57
+ // console.log('arr = ', a);
52
58
53
59
module . exports = BinarySearchTree ;
You can’t perform that action at this time.
0 commit comments