-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_construction.js
50 lines (46 loc) · 1 KB
/
bst_construction.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
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
insert(value) {
// Write your code here.
if (value < this.value) {
if (this.left === null) {
this.left = new BST(value);
} else {
this.left.insert(value);
}
} else {
if (this.right === null) {
this.right = new BST(value);
} else {
this.right.insert(value);
}
}
// Do not edit the return statement of this method.
return this;
}
contains(value) {
// Write your code here.
if (value < this.value) {
if (this.left === null) {
return false;
}
return this.left.contains(value);
} else if (value === this.value) {
return true;
} else {
if (this.right === null) {
return false;
}
return this.right.contains(value);
}
}
remove(value) {
// Write your code here.
// Do not edit the return statement of this method.
return this;
}
}