-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbst.js
More file actions
42 lines (34 loc) · 760 Bytes
/
bst.js
File metadata and controls
42 lines (34 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearch {
constructor() {
this.root = null;
}
search(value) {
return this._searchHelper(this.root, value);
}
_searchHelper(node, value) {
if (node === null) {
return false;
}
if (node.value === value) {
return true;
}
if (value < node.value) {
return this._searchHelper(node.left, value);
} else {
return this._searchHelper(node.right, value);
}
}
}
const tree = new BinarySearch();
tree.root = new Node(20);
tree.root.left = new Node(4);
tree.root.right = new Node(15);
tree.root.right.right = new Node(150);
console.log(tree.search(150)); // output : true