File tree 1 file changed +16
-0
lines changed
src/_DataStructures_/Trees/BST
1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,19 @@ class BinarySearchTree {
69
69
70
70
return [ ...arr , root . value ] ;
71
71
}
72
+
73
+ search ( root , value ) {
74
+ if ( root === null ) return false ;
75
+ if ( value === root . value ) return true ;
76
+
77
+ if ( value < root . value ) {
78
+ return this . search ( root . leftChild , value ) ;
79
+ }
80
+ if ( value > root . value ) {
81
+ return this . search ( root . rightChild , value ) ;
82
+ }
83
+ return false ;
84
+ }
72
85
}
73
86
74
87
// const bst = new BinarySearchTree(6);
@@ -91,4 +104,7 @@ class BinarySearchTree {
91
104
// const postorder = bst.postorder(bst.root);
92
105
// console.log('Postorder Traversal - ', postorder);
93
106
107
+ // const search = 18;
108
+ // console.log(`Search for ${search}`, bst.search(bst.root, search));
109
+
94
110
module . exports = BinarySearchTree ;
You can’t perform that action at this time.
0 commit comments