Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Lines changed: 16 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /