-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearchTree.js
192 lines (173 loc) · 3.67 KB
/
binarySearchTree.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var bst;
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype.push = 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.val) {
if(!currentNode.left) {
currentNode.left = newNode;
break;
}
else
currentNode = currentNode.left;
} else {
if(!currentNode.right) {
currentNode.right = newNode;
break;
}
else
currentNode = currentNode.right;
}
}
};
BinarySearchTree.prototype.print = function(node) {
if(node) {
console.log(node.val);
this.print(node.left);
this.print(node.right);
} else {
if(node === undefined && node !== null)
this.print(this.root);
}
};
BinarySearchTree.prototype.bfs = function() {
var queue = new Queue();
queue.enqueue(this.root);
while(!queue.isEmpty()) {
var currentNode = queue.dequeue();
console.log(currentNode.val);
if(currentNode.left)
queue.enqueue(currentNode.left);
if(currentNode.right)
queue.enqueue(currentNode.right)
}
}
BinarySearchTree.prototype.search = function(val) {
var node = this.root;
while(node) {
if(node.val == val)
return true;
else {
if(val < node.val)
node = node.left;
else
node = node.right
}
}
return false;
}
BinarySearchTree.prototype.isSubtreeGreater = function(node, val) {
if(!node)
return true;
if(node.val > val && this.isSubtreeGreater(node.left, val) && this.isSubtreeGreater(node.right, val))
return true;
else
return false;
}
BinarySearchTree.prototype.isSubtreeLesser = function(node, val) {
if(!node)
return true;
if(node.val < val && this.isSubtreeLesser(node.left, val) && this.isSubtreeLesser(node.right, val))
return true;
else
return false;
}
BinarySearchTree.prototype.isBST = function (node) {
if(!node)
return true;
if(this.isBST(node.left) && this.isBST(node.right) && this.isSubtreeLesser(node.left, node.val) && this.isSubtreeGreater(node.right, node.val))
return true;
else
return false;
}
BinarySearchTree.prototype.isLeafNode = function() {
if(node.val && node.left != null && node.right != null)
return true;
return false;
}
BinarySearchTree.prototype.findMax = function(node) {
if(!node)
node = this.root;
while(node.right != null) {
node = node.right;
}
return node;
}
BinarySearchTree.prototype.findMin = function(node) {
if(!node)
node = this.root;
while(node.left != null) {
node = node.left;
}
return node;
}
/*
There are mainly 3 cases
1. Leaf node, i.e. no children
2. Only one child.
3. 2 children
4. Case is not defined.
*/
BinarySearchTree.prototype.delete = function(node, val) {
if(node == null)
return null;
else if(val < node.val)
node.left = this.delete(node.left, val);
else if(val > node.val)
node.right = this.delete(node.right, val);
else {
/*Case 1*/
if(node.left == null && node.right == null) {
node = null;
return null;
}
else if(node.left != null) {
var temp = node;
node = node.right;
temp = null;
} else if(node.right != null) {
var temp = node;
node = node.left;
temp = null;
} else {
/*Case 3*/
var temp = this.findMin(node.right);
node.val = temp.val;
node.right = this.delete(node.right, temp.val);
}
}
return node;
}
function push() {
var val = document.getElementById('insert').value;
if(!bst)
bst = new BinarySearchTree();
bst.push(43);
bst.push(3);
bst.push(13);
bst.push(23);
bst.push(33);
bst.push(53);
bst.push(63);
bst.push(73);
bst.push(83);
//bst.push(val);
//redrawBST(bst);
return false;
}
function redrawBST(bst) {
var level = 0;
}
function checkEnter(e) {
if(e.keyCode == 13)
push();
}
window.onload = push;