-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL-Tree.js
307 lines (275 loc) · 8.15 KB
/
AVL-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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* ##AVL Tree
*
* This was derived from Recursive add on uni-directional Tree algorithm
*
* *******
* ###USAGE
* ```
* var tree = new AVLTree();
* tree.add(value);
* tree.remove(value);
* tree.min();
* tree.max();
* tree.find(value);
* tree.destroy();
* ```
*/
function AVLTree() {
// points to the top most of tree
this.root = null;
this.clear = this.destroy = function() {
this.root = null;
};
this.insert = this.add = function (val) {
this.root = add(val, this.root);
};
this.delete = this.remove = function(val) {
this.root = remove(val, this.root);
};
this.find = function(val) {
return find(val, this.root);
};
this.min = function() {
return findMin(this.root);
};
this.max = function() {
return findMax(this.root);
};
/**
* Prints the given tree as a text-pyramid
* in console
* @returns {String[][]}
*/
this.print = function() {
if(!this.root) return;
var height, nodeList = [this.root], newList;
var strBuffer, gap, legends = []; // These variables are only need to generate a text-pyramid
for(height = this.root.height; height!=-1; height--) {
newList = [];
strBuffer = fiboncciSpace(height); // the initial gap
gap = fiboncciSpace(height + 1); // gap between nodes
nodeList.forEach(function(node) {
node = node || { value: " "};
strBuffer = strBuffer
.concat(getChar(node))
.concat(gap);
newList.push(node.left); newList.push(node.right);
});
strBuffer = strBuffer.trimRight();
console.log(strBuffer);
nodeList = newList;
}
// print the legends if any
legends.forEach(function(legend) {
console.log(legend.key, " = ", legend.value);
});
// this function was written to replace big numbers by
// alternate legends so as the pyramid is not disturbed
function getChar(node) {
if((node.value + '').length > 1) {
var key = String.fromCharCode(legends.length + 97);
legends.push({
key: key,
value: node.value,
});
return key;
} else {
return node.value;
}
}
};
/**
* Adds a given values appropriatly
* under a given node and returns updated node
* **********
* **Note:** This uses recursive-updated BST insert with balancing
* **********
* @param {any} val
* @param {Node} node
* @returns {Node}
*/
function add(val, node) {
// leaf node condition
if(!node) return new Node(val);
// where to insert - left or right
var position = val < node.value ? 'left' : 'right';
node[position] = add(val, node[position]);
return balance(node);
}
/**
* Searches and removes a given values
* under the given node and returns updated node
* **********
* **Note:** This uses recursive-updated BST remove with balancing
* **********
* @param {any} val
* @param {Node} node
* @returns {Node}
*/
function remove(val, node) {
if(!node) return null;
if(val === node.value) {
// both nodes exist
if(node.left && node.right) {
var minNode = findMin(node.right);
node.value = minNode.value;
node.right = remove(minNode.value, node.right); // Since, we have copied the min node, remove it
return balance(node);
}
// only 1 or no nodes exist
else {
return node.left ? node.left : node.right;
}
}
var position = val < node.value ? 'left' : 'right';
node[position] = remove(val, node[position]);
return balance(node);
}
/**
* Checks if the node is balanced and returns,
* * < 0 if it is leaning towards left
* * \> 0 if it is leaning towards right
* * 0 if node is balanced
* @param {Node} node
* @returns {number}
*/
function getBalance(node) {
if(node.left && node.right) {
return node.right.height - node.left.height;
} else {
return node.height * (!node.right ? -1 : 1);
}
}
/**
* Get height of the given node
* @param {Node} node
* @returns {number}
*/
function getHeight(node) {
if(node.right && node.left) {
return Math.max(node.left.height, node.right.height) + 1;
} else if(node.right) {
return node.right.height + 1;
} else if(node.left) {
return node.left.height + 1;
}
return 0;
}
/**
* An internal function to Left Rotate
* a givel AVL Tree
* @param {Node} node
* @returns {Node}
*/
function leftRotate(node) {
var right = node.right;
node.right = right.left;
right.left = node;
node.height = getHeight(node); // node is shifted, update its height
right.right.height = getHeight(right.right);
right.height = getHeight(right);
return right;
}
/**
* An internal function to Right Rotate
* a givel AVL Tree
* @param {Node} node
* @returns {Node}
*/
function rightRotate(node) {
var left = node.left;
node.left = left.right;
left.right = node;
node.height = getHeight(node); // node is shifted, update its height
left.left.height = getHeight(left.left);
left.height = getHeight(left);
return left;
}
/**
* Find the lowest value node in a given tree
* @param {Node} node
* @returns {Node}
*/
function findMin(node) {
return node.left ? findMin(node.left) : node;
}
/**
* Find the highest value node in a given tree
* @param {Node} node
* @returns {Node}
*/
function findMax(node) {
return node.right ? findMax(node.right) : node;
}
/**
* Finds the node containing given value
* @param {any} val
* @param {Node} node
* @returns {Node}
*/
function find(val, node) {
if(!node) return "Not Found";
if(node.value == val) return node;
return node.value > val ? find(val, node.left) : find(val, node.right);
}
/**
* The main balancer function that balances that
* updates the height and performs rotation if needed
* and returns the balanced node
* @param {Node} node
* @returns {Node}
*/
function balance(node) {
// update height
node.height = getHeight(node);
// get balance
var balance = getBalance(node);
if(balance === -2) {
// Left Left
if(node.left && getBalance(node.left) == -1) {
return rightRotate(node);
}
// Left Right
else {
node.left = leftRotate(node.left);
return rightRotate(node);
}
} else if(balance === 2) {
// Right Right
if(node.right && getBalance(node.right) == 1) {
return leftRotate(node);
}
// Right Left
else {
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
return node;
}
/**
* generates spaces at power of 2.
* Used while printing tree as text-pyramid
* @param {number} level
* @returns {String}
*/
function fiboncciSpace(level) {
String()
return Array(Math.pow(2, level)).join(" ");
}
/**
* The base Node class. Used to construct a uni-directional node
* with the given value and default height of 0.
* @param {any} val
*/
function Node(val) {
this.value = val;
this.right = this.left = undefined;
Object.defineProperty(this, "height", {
value: 0,
enumerable: false,
writable: true,
});
}
}