-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathavl-tree.ts
265 lines (238 loc) · 7.86 KB
/
avl-tree.ts
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
class AvlTreeNode<T> {
public left: AvlTreeNode<T>;
public right: AvlTreeNode<T>;
public parent: AvlTreeNode<T>;
public value: T;
public height: number;
constructor(nill: AvlTreeNode<null>) {
this.left = nill;
this.right = nill;
this.parent = null;
this.height = 0;
}
}
const nill: AvlTreeNode<null> = new AvlTreeNode(null)
/**
*An AVL tree is a binary search tree that is height balanced: for each node xx,
*the heights of the left and right subtrees of xx differ by at most 1.
*
* @class AVLTree
* @template T
*/
class AVLTree<T> {
private nill: AvlTreeNode<null> = nill;
private root: AvlTreeNode<T>;
constructor() {
this.root = this.nill;
}
private rotateTree(direction: 'left' | 'right', node: AvlTreeNode<T>) {
if (direction === 'left') {
return this.leftRotate(node);
} else if (direction === 'right') {
return this.rightRotate(node);
}
}
private leftRotate(x: AvlTreeNode<T>) {
//Set y
const y = x.right;
//Turn y’s left subtree into x’s right subtree.
x.right = y.left;
if (y.left !== this.nill) {
y.left.parent = x;
}
// Link x’s parent to y.
y.parent = x.parent;
if (x.parent === this.nill) {
this.root = y;
} else if (x.parent.left === x) {
x.parent.left = y
} else {
x.parent.right = y
}
//Put x on y’s left
y.left = x;
x.parent = y;
return y;
}
private rightRotate(y: AvlTreeNode<T>) {
//Set x
const x = y.left;
//Turn x’s right subtree into y’s left subtree.
y.left = x.right;
if (x.right !== this.nill) {
x.right.parent = y;
}
// Link y’s parent to x.
x.parent = y.parent;
if (y.parent === this.nill) {
this.root = x;
} else if (y.parent.left === y) {
y.parent.left = x;
} else {
y.parent.right = x
}
//Put y on x’s right
x.right = y;
y.parent = x;
return x;
}
/**
*@description:Balance function takes a subtree rooted at node-xx whose left and right children are height balanced and have heights
*that differ by at most 22, i.e., x.right.h−x.left.h∣≤2, and alters the subtree rooted at xx to be height balanced
*
* @param {AvlTreeNode<T>} node
* @returns
* @memberof AVLTree
*/
balance(xx: AvlTreeNode<T>) {
const balancingFactor = xx.left.height - xx.right.height;
if (balancingFactor > 1 || balancingFactor < -1) {
if (xx.left.left.height - xx.left.right.height >= 0)
xx = this.rotateTree('right', xx);
else {
xx.left = this.rotateTree('left', xx.left);
xx = this.rotateTree('right', xx);
}
} else if (balancingFactor < -1) {
if (xx.right.left.height - xx.right.right.height <= 0)
xx = this.rotateTree('left', xx);
else {
xx.right = this.rotateTree('right', xx.right);
xx = this.rotateTree('left', xx);
}
}
return xx;
}
/**
*@description:Insert a new value in to tree
*
* @param {T} value
* @memberof AVLTree
*/
insert(value: T) {
const newNode = new AvlTreeNode<T>(null);
newNode.value = value;
this.insertNode(newNode);
}
private insertNode(newNode: AvlTreeNode<T>) {
let insertionPointer: AvlTreeNode<T> = this.nill;
let traversalPointer: AvlTreeNode<T> = this.root;
this.insertNodeRecursively(traversalPointer, insertionPointer, newNode);
let temp = newNode.parent;
while (temp != nill) {
temp = this.balance(temp);
temp = temp.parent;
}
}
private maximumOfTwoNumbers(x1, x2) {
return x1 > x2 ? x1 : x2;
}
private insertNodeRecursively(traversalPointer, insertionPointer, newNode) {
if (traversalPointer === nill) {
if (insertionPointer === nill) {
this.root = newNode;
} else if (newNode.value < insertionPointer.value) {
insertionPointer.left = newNode;
} else {
insertionPointer.right = newNode;
}
newNode.parent = insertionPointer;
newNode.height = 0;
} else {
insertionPointer = traversalPointer;
if (newNode.value < traversalPointer.value) {
this.insertNodeRecursively(traversalPointer.left, insertionPointer, newNode);
} else {
this.insertNodeRecursively(traversalPointer.right, insertionPointer, newNode);
}
traversalPointer.height = this.maximumOfTwoNumbers(traversalPointer.left, traversalPointer.right) + 1;
}
}
private minimum(root: AvlTreeNode<T>): AvlTreeNode<T> {
while (root !== this.nill && root.left !== this.nill) {
root = root.left;
}
return root;
}
private findNode(value: T): AvlTreeNode<T> {
let traverser = this.root;
while (traverser !== this.nill && traverser.value !== value) {
if (value < traverser.value) {
traverser = traverser.left
} else {
traverser = traverser.right;
}
}
return traverser;
}
/**
*@description returns next element value of the given element
*
* @param {RedBlackTreeNode<T>} z
* @returns {RedBlackTreeNode<T>}
* @memberof RedBlackTree
*/
private successor(z: AvlTreeNode<T>): AvlTreeNode<T> {
if (z === this.nill) {
return z
}
return this.minimum(z.right)
}
/**
*@description:Delete a node with value(v) in the tree
*
* @param {T} value
* @memberof AVLTree
*/
delete(value: T) {
const node = this.findNode(value)
if (node === this.nill) {
throw `no value with -(${value}) present in tree to delete`;
} else {
let x: AvlTreeNode<T>;
if (node.left === nill || node.right === nill) {
if (node.parent.left === node) {
node.parent.left = nill;
} else {
node.parent.right = nill;
}
x = node.parent;
} else if (node.left === nill) {
if (node.parent.left === node) {
node.parent.left = node.right;
} else {
node.parent.right = node.right;
}
x = node.parent;
} else if (node.right === nill) {
if (node.parent.left === node) {
node.parent.left = node.left;
} else {
node.parent.right = node.left;
}
x = node.parent;
} else {
const successorNode = this.successor(node.right);
node.value = successorNode.value;
if (successorNode.right != nill) {
if (successorNode.parent.left === successorNode) {
successorNode.parent.left = successorNode.right;
} else {
successorNode.parent.right = successorNode.right;
}
}
x = successorNode.parent;
}
let temp = x;
while (temp != nill) {
temp.height = this.maximumOfTwoNumbers(temp.left.height, temp.right.height) + 1;
temp = temp.parent;
}
temp = x;
while (temp != nill) {
temp = this.balance(temp);
temp = temp.parent;
}
}
}
}