@@ -39,19 +39,37 @@ class BinarySearchTree {
39
39
arr = [ ...arr , ...right ] ;
40
40
return arr ;
41
41
}
42
+
43
+ inorder ( root ) {
44
+ if ( root === null ) return [ ] ;
45
+ let arr = [ ] ;
46
+ const left = this . inorder ( root . leftChild ) ;
47
+ arr = [ ...left , ...arr ] ;
48
+
49
+ // print root
50
+ arr = [ ...arr , root . value ] ;
51
+
52
+ const right = this . inorder ( root . rightChild ) ;
53
+ arr = [ ...arr , ...right ] ;
54
+ return arr ;
55
+ }
42
56
}
43
57
44
- // const bst = new BinarySearchTree(10 );
58
+ // const bst = new BinarySearchTree(6 );
45
59
// console.log(bst.root);
46
- // bst.insert(bst.root, 12 );
60
+ // bst.insert(bst.root, 4 );
47
61
// bst.insert(bst.root, 9);
48
- // bst.insert(bst.root, 19);
49
- // bst.insert(bst.root, 11);
50
- // bst.insert(bst.root, 6);
62
+ // bst.insert(bst.root, 2);
63
+ // bst.insert(bst.root, 5);
64
+ // bst.insert(bst.root, 8);
65
+ // bst.insert(bst.root, 12);
51
66
52
67
// console.log(bst.root);
53
68
54
- // const a = bst.preorder(bst.root);
55
- // console.log('arr = ', a);
69
+ // const preorder = bst.preorder(bst.root);
70
+ // console.log('Preorder Traversal - ', preorder);
71
+
72
+ // const inorder = bst.inorder(bst.root);
73
+ // console.log('Inorder Traversal - ', inorder);
56
74
57
75
module . exports = BinarySearchTree ;
0 commit comments