Skip to content

Commit 3475013

Browse files
committed
update: search in BST
1 parent 8fae46e commit 3475013

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/_DataStructures_/Trees/BST/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ class BinarySearchTree {
6969

7070
return [...arr, root.value];
7171
}
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+
}
7285
}
7386

7487
// const bst = new BinarySearchTree(6);
@@ -91,4 +104,7 @@ class BinarySearchTree {
91104
// const postorder = bst.postorder(bst.root);
92105
// console.log('Postorder Traversal - ', postorder);
93106

107+
// const search = 18;
108+
// console.log(`Search for ${search}`, bst.search(bst.root, search));
109+
94110
module.exports = BinarySearchTree;

0 commit comments

Comments
 (0)