-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search_tree.js
107 lines (97 loc) · 2.22 KB
/
binary_search_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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function Node (value) {
this.value = value;
this.left = null;
this.right = null;
}
function BSTree() {
this.root = null;
}
BSTree.prototype.insert = function(val) {
var root = this.root;
if (!root) {
this.root = new Node(val);
return;
}
var currentNode = root;
var newNode = new Node(val);
while(currentNode) {
if(val < currentNode.value) {
if(!currentNode.left) {
currentNode.left = newNode;
break;
}
else {
currentNode = currentNode.left;
}
}
else {
if(!currentNode.right) {
currentNode.right = newNode;
break;
}
else {
currentNode = currentNode.right;
}
}
}
};
//This function uses the Queue data structure available in the file called 'queue.js' on this repo
BSTree.prototype.BFS = function () {
if (!this.root) {
return;
}
var queue = new Queue(), res = [];
queue.clear();
queue.enqueue(this.root);
while(!queue.isEmpty()) {
var node = queue.dequeue();
res.push(node.value);
if(node.left) {
queue.enqueue(node.left);
}
if(node.right) {
queue.enqueue(node.right);
}
}
console.log(res.join(', '));
};
BSTree.prototype.minValue = function () {
if (!this.root) {
return;
}
var left = this.root.left, v = 0;
while (left) {
v = left.value;
left = left.left;
}
return v;
};
BSTree.prototype.maxValue = function () {
if (!this.root) {
return;
}
var right = this.root.right, v = 0;
while (right) {
v = right.value;
right = right.right;
}
return v;
};
BSTree.prototype.getHeight = function (node) {
if (!node) {
return 0;
}
var leftHeight = this.getHeight(node.left), rightHeight = this.getHeight(node.right);
return Math.max(leftHeight, rightHeight) + 1;
};
function isBST (node, min, max) {
/* an empty tree is BST */
if (!node)
return true;
/* false if this node violates the min/max constraints */
if (node.value < min || node.value > max)
return false;
/* otherwise check the subtrees recursively tightening the min/max constraints */
// Allow only distinct values
return (isBST(node.left, min, node.value - 1) && isBST(node.right, node.value + 1, max));
}