-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.js
More file actions
executable file
·28 lines (26 loc) · 867 Bytes
/
Copy pathbinaryTree.js
File metadata and controls
executable file
·28 lines (26 loc) · 867 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
function BinaryTree(value) {
this.value = value;
this.left = null;
this.right = null;
}
BinaryTree.prototype.find = function(value) {
if(this.value === value) {
return true;
} else if(this.left !== null && this.value < value) {
return this.left.find(value);
} else if(this.right !== null && this.value > value) {
return this.right.find(value)
}
return false;
}
BinaryTree.prototype.insert = function(value) {
if(this.left !== null && this.value < value) {
this.left.insert(value);
} else if(this.left === null && this.value < value) {
this.left = new BinaryTree(value);
} else if(this.right !== null && this.value > value) {
this.right = new BinaryTree(value);
} else if(this.right === null && this.value > value) {
this.right = new BinaryTree(value);
}
}