-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstHeight.js
55 lines (50 loc) · 916 Bytes
/
bstHeight.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
52
53
54
55
function height(node, count){
var count;
if(!count){
count = -1;
}
if(!node){
return count;
}
var left = height(node.left, count);
var right = height(node.right, count);
if(left > right){
return left + 1;
} else {
return right + 1;
}
}
function bST(){
this.root = null;
this.add = function(value){
var current;
if(this.root == null){
this.root = new btNode(value);
return this;
}
current = this.root;
while(current){
if(value < current.val){
if(!current.left){
current.left = new btNode(value);
return this;
}
current = current.left;
} else {
if(!current.right){
current.right = new btNode(value);
return this;
}
current = current.right;
}
}
}
}
function btNode(value){
this.val = value;
this.left = null;
this.right = null;
}
a = new bST();
a.add(10).add(5).add(20).add(25).add(30);
console.log(height(a.root));