-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.js
51 lines (44 loc) · 903 Bytes
/
BinarySearchTree.js
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
43
44
45
46
47
48
49
50
51
class BinarySearchTree {
constructor(value) {
this.value = value;
this.length = 1;
this.left = null;
this.right = null;
}
insert(value) {
if (this.value > value) {
if (!this.left) {
this.left = new BinarySearchTree(value);
} else {
this.left.insert(value);
}
} else {
if (!this.right) {
this.right = new BinarySearchTree(value);
} else {
this.right.insert(value);
}
}
this.length++;
}
contains(value) {
if (this.value === value) {
return true
}
else {
if (this.left && this.value > value) {
return this.left.contains(value)
} else if (this.right && this.value < value) {
return this.right.contains(value)
}
}
return false;
}
depthFirstForEach() {
}
breadthFirstForEach() {
}
size() {
return this.length;
}
}