-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search-tree.js
277 lines (231 loc) · 6.05 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
export const comparator = (a, b) => {
if (a === b) {
return 0;
}
return a < b ? -1 : 1;
};
export class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
export default class BinarySearchTree {
constructor(compareFunc = comparator) {
this.root = null;
this.comparator = compareFunc;
}
/**
* Add a node
* @param {*} value
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(1)
*/
insert(value) {
// CREATE AN NEW NODE
const node = new Node(value);
// IF THERE IS NO ROOT, SET THE NEW NODE AS THE ROOT
if (this.root === null) {
this.root = node;
return;
}
// INSERT THE NEW NODE IN SUBTREE
this._insertNode(this.root, node);
}
/**
* Insert a node in a given parent
* @param {Node} parent
* @param {Node} node
* @time complexity: O(LOG N) where N is the size of the parent node
* @space complexity: O(1)
*/
_insertNode(parent, node) {
// IF THE NODE IS LESS THAN THE PARENT
if (this.comparator(node.value, parent.value) == -1) {
// IF REACH THE LEAF, ADD TO IT
if (parent.left === null) {
parent.left = node;
return;
}
// CONTINUE INSERT INTO LEFT SUBTREE
return this._insertNode(parent.left, node);
}
// IF REACH THE LEFT, ADD TO IT
if (parent.right === null) {
parent.right = node;
return;
}
// CONTINUE INSERT INTO RIGHT SUBTREE
return this._insertNode(parent.right, node);
}
/**
* Traversing the tree from left to right, meaning from the smallest to the largest
* @param {function} callback
* @time complexity: O(N) where N is the size of the tree
* @space complexity: O(N) where N is the size of the tree
*/
inOrderTraverse(callback) {
this._inOrderTraverseNode(this.root, callback);
}
_inOrderTraverseNode(node, callback) {
if (node === null) {
return;
}
this._inOrderTraverseNode(node.left, callback);
callback(node.value);
this._inOrderTraverseNode(node.right, callback);
}
/**
* Traversing the tree from left to right
* Difference between pre-order and in-order traverse,
* is that pre-order traverse visit the root first,
* and then the left node, finally the right node
* @param {function} callback
* @time complexity: O(N) where N is the size of the tree
* @space complexity: O(N) where N is the size of the tree
*/
preOrderTraverse(callback) {
this._preOrderTraverseNode(this.root, callback);
}
_preOrderTraverseNode(node, callback) {
if (node === null) {
return;
}
callback(node.value);
this._preOrderTraverseNode(node.left, callback);
this._preOrderTraverseNode(node.right, callback);
}
/**
* Traversing the tree from bottom up
* Visit the left node and then the right node, finally the root node
* @param {function} callback
* @time complexity: O(N) where N is the size of the tree
* @space complexity: O(N) where N is the size of the tree
*/
postOrderTraverse(callback) {
this._postOrderTraverseNode(this.root, callback);
}
_postOrderTraverseNode(node, callback) {
if (node === null) {
return;
}
this._postOrderTraverseNode(node.left, callback);
this._postOrderTraverseNode(node.right, callback);
callback(node.value);
}
/**
* Find the minimum value
* @return {*}
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(1)
*/
min() {
return this._minNode(this.root);
}
_minNode(node) {
if (node === null) {
return null;
}
while (node && node.left !== null) {
node = node.left;
}
return node.value;
}
/**
* Find the maximum value
* @return {*}
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(1)
*/
max() {
return this.maxNode(this.root);
}
maxNode(node) {
if (node === null) {
return null;
}
while (node && node.right !== null) {
node = node.right;
}
return node.value;
}
/**
* Check whether or not the tree contains the value
* @param {*} value
* @return {boolean}
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(LOG N) where N is the size of the tree
*/
search(value) {
return this._searchNode(this.root, value);
}
_searchNode(node, value) {
if (node === null) {
return false;
}
const compare = this.comparator(value, node.value);
if (compare === -1) {
return this._searchNode(node.left, value);
}
if (compare === 1) {
return this._searchNode(node.right, value);
}
return true;
}
/**
* Remove the value from the tree
* @param {*} value
* @return {Node}
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(LOG N) where N is the size of the tree
*/
remove(value) {
this.root = this._removeNode(this.root, value);
}
_removeNode(node, value) {
if (node === null) {
return null;
}
const compare = this.comparator(value, node.value);
if (compare === -1) {
node.left = this._removeNode(node.left, value);
return node;
}
if (compare === 1) {
node.right = this._removeNode(node.right, value);
return node;
}
if (node.left === null && node.right === null) {
node = null;
return node;
}
if (node.left === null) {
node = node.right;
return node;
}
if (node.right === null) {
node = node.left;
return node;
}
const aux = this._findMinNode(node.right);
node.value = aux.value;
node.right = this._removeNode(node.right, aux.value);
return node;
}
/**
* Find the minimum value node
* @param {Node} node
* @time complexity: O(LOG N) where N is the size of the tree
* @space complexity: O(1)
*/
_findMinNode(node) {
if (node === null) {
return null;
}
while (node && node.left !== null) {
node = node.left;
}
return node;
}
}