-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
31 lines (26 loc) · 591 Bytes
/
tree.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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Binary Tree
// Part 1: https://youtu.be/ZNH0MuQ51m4
// Part 2: https://youtu.be/KFEvF_ymuzY
function Tree() {
this.root = null;
}
Tree.prototype.traverse = function() {
this.root.visit(this.root);
};
Tree.prototype.search = function(val) {
var found = this.root.search(val);
return found;
};
Tree.prototype.addValue = function(val) {
var n = new Node(val);
if (this.root == null) {
this.root = n;
this.root.x = width / 2;
this.root.y = 16;
} else {
this.root.addNode(n);
}
};