-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAVL Tree.html
325 lines (277 loc) · 9.75 KB
/
AVL Tree.html
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AVL(平衡树)</title>
</head>
<body>
<script>
class Node {
constructor(key, value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
this.height = 1;
}
}
class AVLTree {
constructor() {
this.root = null;
this.size = 0;
}
add(key, value) {
this.root = this._add(this.root, key, value)
}
_add(node, key, value) {
if (node === null) {
this.size++;
return new Node(key, value)
}
if (value < node.value) {
node.left = this._add(node.left, key, value)
} else if (value > node.value) {
node.right = this._add(node.right, key, value)
} else {
node.key = key;
node.value = value
}
node.height = 1 + Math.max(this.getHeight(node.left), this.getHeight(node.right));
//计算平衡因子
let balanceFactor = this.getBalanceFactor(node);
//平衡维护
//LL
if (balanceFactor > 1 && this.getBalanceFactor(node.left) >= 0) {
return this.rightRotate(node)
}
//RR
if (balanceFactor < -1 && this.getBalanceFactor(node.right) <= 0) {
return this.leftRotate(node)
}
//LR
//region 图示
// y y z
// / \ / \ / \
// x T4 向左旋转 (x) z T4 向右旋转(y) x y
// / \ - - - - - - - -> / \ - - - - - - - -> / \ / \
// T1 z x T3 T1 T2 T3 T4
// / \ / \
// T2 T3 T1 T2
//endregion
if (balanceFactor > 1 && this.getBalanceFactor(node.left) < 0) {
node.left = this.leftRotate(node.left);
return this.rightRotate(node)
}
//RL
//region 图示
// y y z
// / \ / \ / \
// T1 x 向右旋转 (x) T1 z 向右旋转(y) y x
// / \ - - - - - - - -> / \ - - - - - - - -> / \ / \
// z T4 T2 x T1 T2 T3 T4
// / \ / \
// T2 T3 T3 T4
//endregion
if (balanceFactor < -1 && this.getBalanceFactor(node.right) > 0) {
node.right = this.rightRotate(node.right);
return this.leftRotate(node)
}
return node
}
// 对节点y进行向右旋转操作,返回旋转后新的根节点x
// y x
// / \ / \
// x T4 向右旋转 (y) z y
// / \ - - - - - - - -> / \ / \
// z T3 T1 T2 T3 T4
// / \
// T1 T2
rightRotate(y) {
let x = y.left,
T3 = x.right;
x.right = y;
y.left = T3;
y.height = 1 + Math.max(this.getHeight(y.left), this.getHeight(y.right));
x.height = 1 + Math.max(this.getHeight(x.left), this.getHeight(x.right));
return x
}
// 对节点y进行向左旋转操作,返回旋转后新的根节点x
// y x
// / \ / \
// T1 x 向左旋转 (y) y z
// / \ - - - - - - - -> / \ / \
// T2 z T1 T2 T3 T4
// / \
// T3 T4
leftRotate(y) {
let x = y.right,
T2 = x.left;
x.left = y;
y.right = T2;
y.height = 1 + Math.max(this.getHeight(y.left), this.getHeight(y.right));
x.height = 1 + Math.max(this.getHeight(x.left), this.getHeight(x.right));
return x
}
remove(key) {
let node = this.getNode(this.root, key);
if (node) {
this.root = this._remove(this.root, key);
return node.value
}
return null
}
_remove(node, key) {
if (!node) return null;
let retNode;
if (key < node.key) {
node.left = this._remove(node.left, key)
retNode = node
} else if (key > node.key) {
node.right = this._remove(node.right, key)
retNode = node
} else {
if (node.left === null) {
let rightNode = node.right;
node.right = null;
this.size--;
retNode = rightNode
} else if (node.right === null) {
let leftNode = node.left;
node.left = null;
this.size--;
retNode = leftNode;
} else {
let successor = this.minNum(node);
successor.right = this._remove(node.right, successor.key);
successor.left = node.left;
retNode = successor
}
}
if (retNode === null) return null;
retNode.height = 1 + Math.max(this.getHeight(retNode.left), this.getHeight(retNode.right));
//计算平衡因子
let balanceFactor = this.getBalanceFactor(retNode);
//平衡维护
//LL
if (balanceFactor > 1 && this.getBalanceFactor(retNode.left) >= 0) {
return this.rightRotate(retNode)
}
//RR
if (balanceFactor < -1 && this.getBalanceFactor(retNode.right) <= 0) {
return this.leftRotate(retNode)
}
//LR
if (balanceFactor > 1 && this.getBalanceFactor(retNode.left) < 0) {
retNode.left = this.leftRotate(retNode.left);
return this.rightRotate(retNode)
}
//RL
if (balanceFactor < -1 && this.getBalanceFactor(retNode.right) > 0) {
retNode.right = this.rightRotate(retNode.right);
return this.leftRotate(retNode)
}
return retNode
}
isBST() {
let keys = [];
this.inOrder(this.root, keys);
for (let i = 1; i < keys.length; i++) {
if (keys[i - 1] > keys[i]) return false
}
return true
}
contains(key) {
return this.getNode(this.root, key) !== null
}
set(key, val) {
let node = this.getNode(this.root, key);
if (!node) {
throw new Error(`${key} doesn't exist!`)
}
node.value = val
}
//获取节点平衡因子
getBalanceFactor(node) {
if (node === null) return 0;
return this.getHeight(node.left) - this.getHeight(node.right)
}
getHeight(node) {
if (node === null) return 0;
return node.height;
}
getNode(node, key) {
if (!node) return null;
if (key === node.key) {
return node
}
if (node.key < key) {
return this.getNode(node.right, key)
} else {
return this.getNode(node.left, key)
}
}
getSize() {
return this.size
}
isBalanced() {
return this._isBalanced(this.root)
}
_isBalanced(node) {
if (!node) return true;
let balanceFactor = this.getBalanceFactor(node);
if (Math.abs(balanceFactor) > 1) {
return false
}
return this._isBalanced(node.left) && this._isBalanced(node.right)
}
minNum(node) {
if (!node) return null;
while (node) {
if (!node.left) {
return node
}
node = node.left
}
}
inOrder(node, keys) {
if (!node) return null;
let pre;
while (node) {
if (!node.left) {
keys.push(node.key);
node = node.right
} else {
pre = node.left;
while (pre.right && pre.right !== node) {
pre = pre.right
}
if (!pre.right) {
pre.right = node;
node = node.left
} else {
keys.push(node)
node = node.right
}
}
}
}
}
let avl = new AVLTree();
avl.add(12, 12);
avl.add(8, 8);
avl.add(18, 18);
avl.add(5, 5);
avl.add(11, 11);
// // avl.add(7,7); //添加7后需进行右旋转
//RL
// avl.add(14,14);
// avl.add(8,8);
// avl.add(18,18);
// avl.add(5,5);
// avl.add(11,11);
// avl.add(17,17);
// avl.add(10,10);
console.log(avl)
</script>
</body>
</html>