-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRedBlackTree.js
446 lines (382 loc) · 14.6 KB
/
RedBlackTree.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import {
RBTreeNode,
NODE_COLOR_RED,
NODE_COLOR_BLACK,
} from './TreeNode'
import {
BalencedBinarySearchTree
} from './BalencedBinarySearchTree'
/**
* RedBlackTree 红黑树
*
* @description
* 非完美平衡二叉搜索树
* 完美黑色平衡二叉搜索树
* @export
* @class RedBlackTree
* @extends {BinarySearchTree}
*/
export class RedBlackTree extends BalencedBinarySearchTree {
constructor(customizedComparer) {
super(customizedComparer)
/**
* 插入节点后检查是否符合红黑树规则并适当调整
*
* @param {RBTreeNode} node 红黑树节点
*/
this.__afterInsertNode = function (node) {
// 到达根节点
if (node.parent === null) {
if (node.isRed) {
node.color(NODE_COLOR_BLACK)
}
return
}
// 父节点黑色
if (node.parent.isBlack) {
return
}
// --- now: node.parent.isRed ---
let parent = node.parent
let grand = parent.parent
// 父红叔红祖黑:变色=>递归
if (node.parent.sibling && node.parent.sibling.isRed) {
// -> 变色:父黑叔黑祖红
parent.color(NODE_COLOR_BLACK)
parent.sibling.color(NODE_COLOR_BLACK)
grand.color(NODE_COLOR_RED)
// -> 将 grand 作为新节点递归添加
this.__afterInsertNode(grand)
return
}
// parent is Red and uncle is Black (contains NIL)
// ↓ 父红叔黑(含空)祖黑:变色+旋转
// 判断是否到达根节点,到达时需修改 root 指向
let reachRoot = (grand === this.__root)
// [Case-1 LL] 左子 + 左子 : grand 绕 parent 右旋
if (parent.isLeftChild && node.isLeftChild) {
// -> 变色
parent.color(NODE_COLOR_BLACK)
grand.color(NODE_COLOR_RED)
// -> 旋转
this.rotateRight(grand, parent)
if (reachRoot) {
this.__root = parent
}
}
// [Case-2 RR] 右子 + 右子 : grand 绕 parent 左旋
else if (parent.isRightChild && node.isRightChild) {
// -> 变色
parent.color(NODE_COLOR_BLACK)
grand.color(NODE_COLOR_RED)
// -> 旋转
this.rotateLeft(grand, parent)
if (reachRoot) {
this.__root = parent
}
}
// [Case-3 LR] 左子 + 右子: 先拉直,再按 [Case-1 LL] 变换
else if (parent.isLeftChild && node.isRightChild) {
// -> 拉直:parent 绕当前节点 node 左旋
this.rotateLeft(parent, node)
// node 已与 parent 对调,以下同 Case-1
// -> 变色
node.color(NODE_COLOR_BLACK)
grand.color(NODE_COLOR_RED)
// -> 旋转
this.rotateRight(grand, node)
if (reachRoot) {
this.__root = node
}
}
// [Case-4 RL] 右子 + 左子: 先拉直,再按 [Case-2 RR] 变换
else {
// -> 拉直: parent 绕当前节点右旋
this.rotateRight(parent, node)
// node 已与 parent 对调,以下同 Case-2
// -> 变色
node.color(NODE_COLOR_BLACK)
grand.color(NODE_COLOR_RED)
// -> 旋转
this.rotateLeft(grand, node)
if (reachRoot) {
this.__root = node
}
}
return
}
// TODO
this.__removeNode = function (data, root) {
if (root == null) return null
// 查找左子树
if (this.__comparator(data, root.data) === COMPARE_LESS) {
root.left = this.__removeNode(data, root.left)
return root
}
// 查找右子树
else if (this.__comparator(data, root.data) === COMPARE_GREATER) {
root.right = this.__removeNode(data, root.right)
return root
}
// --找到要删除的节点对象--
// 既有左子树又有右子树
if (root.left != null && root.right != null) {
let minInRight = root.right
while (minInRight.left) {
minInRight = minInRight.left
}
// => 转变为删除后继节点 minInRight
// minInRight 只有以下三种情况:
// Case-1:红色叶子节点
// Case-2:黑色叶子节点
// Case-3:黑色节点 + 红色右子节点
// 这三种情况外层分支已覆盖,都在递归中进入相应分支处理
// 这里只记录后继的数据值并赋给原删除对象 root 节点
let data = minInRight.data
this.__removeNode(data, minInRight)
root.data = data
}
// 只有左子树(只能是black-red)
else if (root.left != null) {
root.data = root.left.data
root.left = null
}
// 只有右子树(只能是black-red)
else if (root.right != null) {
root.data = root.right.data
root.right = null
}
// 没有任何子节点(叶子节点)
// 删除对象为红色叶子节点
else if (root.color === NODE_COLOR_RED) {
// 直接删除
root = null
}
// 删除对象为黑色叶子节点
else {
root = this.__removeBlackLeaf(root)
}
return root
}
this.__removeBlackLeaf = function (node, deleteFlag = false) {
let parent = node.parent
if (parent === null) {
// 删除的是根节点
node = null
return null
}
let grand = parent.parent
let uncle = parent.sibling
const needStraighten = node.isLeftChild === parent.isLeftChild
// // 红色父节点
// if (parent.isRed) {
// // 父红兄黑近侄红:兄侄旋转+黑红黑(父兄侄)
// if (node.nearNephew && !node.farNephew) {
// // 兄侄按node所在方向的反向旋转
// this.rotate(node.sibling, node.nearNephew, node.isLeftChild)
// // 旋转后变形为:父红兄黑远红侄
// }
// // 有侄子节点时
// // 父红兄黑+[双红侄]或[远红侄]
// if (!node.sibling.isLeaf) {
// // 变色保证[黑红黑]
// parent.color(NODE_COLOR_BLACK)
// node.sibling.color(NODE_COLOR_RED)
// node.sibling.color(NODE_COLOR_BLACK)
// }
// // 向删除节点的方向倾斜(下沉以获取更多黑色节点来做补充)
// this.rotate(parent, node.sibling, node.isRightChild)
// node = null
// return null
// }
// // else 黑色父节点:此时必然三黑
// else if (grand === null) {
// // 父黑根节点:兄变红即可
// node.sibling.color(NODE_COLOR_RED)
// node = null
// return null
// }
// // 父黑叔红的情况:此时必然:表兄双黑
// else if (uncle.isRed) {
// // 变色
// uncle.color(NODE_COLOR_BLACK) // 将调整为新grand,需变黑
// parent.nearNephew.color(NODE_COLOR_RED)
// if (needStraighten) {
// // 旋转1:父兄拉直
// this.rotate(parent, node.sibling, parent.isRightChild)
// parent.color(NODE_COLOR_RED)
// }
// else {
// node.sibling.color(NODE_COLOR_RED)
// }
// // 旋转2:祖叔旋转
// this.rotate(grand, uncle, parent.isRightChild)
// node = null
// return null
// }
// // 父黑叔黑的情况:看近表兄节点颜色来进行颜色变化
// // 近表兄红色:黑红黑
// // 近表兄黑色:红黑红
// else {
// if(parent.nearNephew.isRed) {
// parent.farNephew.color(NODE_COLOR_BLACK)
// if (needStraighten) {
// // 旋转1:父兄拉直
// this.rotate(parent, node.sibling, parent.isRightChild)
// parent.color(NODE_COLOR_RED)
// }
// else {
// node.sibling.color(NODE_COLOR_RED)
// }
// }
// // parent.nearNephew.isBlack
// else {
// grand.color(NODE_COLOR_RED)
// if (needStraighten) {
// // 旋转1:父兄拉直
// this.rotate(parent, node.sibling, parent.isRightChild)
// parent.color(NODE_COLOR_RED)
// }
// else {
// node.sibling.color(NODE_COLOR_RED)
// }
// }
// // 旋转2:祖叔旋转
// this.rotate(grand, uncle, parent.isRightChild)
// }
}
/**
* 失黑调整
*
* @description __fixLostBlack参考自av30515237
*/
this.__fixLostBlack = function (node) {
while (node !== this.__root) {
let parent = node.parent
let brother = node.sibling
// LB-1:父黑兄红双黑侄
if (brother.isRed) {
this.rotate(parent, node.isRightChild)
brother.color(NODE_COLOR_BLACK)
parent.color(NODE_COLOR_RED)
// 此时已变为LB-2R:父红兄黑无红侄(0侄子)
if (this.__root === parent) {
this.__root = brother
}
brother = node.sibling
parent = node.parent
}
// LB-3:兄黑红侄
if (brother.left && brother.left.isRed) {
let oldColor = parent.isRed
parent.color(NODE_COLOR_BLACK)
if (parent === this.__root) {
this.__root = brother
}
if (node.isLeftChild) { // 近侄红
this.rotateRight(brother)
this.rotateLeft(parent)
} else {
brother.left.color(NODE_COLOR_BLACK)
this.rotateRight(parent)
}
parent.parent.color(oldColor)
return
}
// 对称的LB-3
else if (brother.right && brother.right.isRed) {
let oldColor = parent.isRed
parent.color(NODE_COLOR_BLACK)
if (parent === this.__root) {
this.__root = brother
}
if (node.isLeftChild) {
brother.right.color(NODE_COLOR_BLACK)
this.rotateLeft(parent)
}
// 近侄红
else {
this.rotateLeft(brother)
this.rotateRight(parent)
}
parent.parent.color(oldColor)
return
}
// LB-2R:父红兄黑(无红侄)
if (node.parent.isRed) {
parent.color(NODE_COLOR_BLACK)
brother.color(NODE_COLOR_RED)
return
}
// LB-2B:父黑兄黑(无红侄)
else {
brother.color(NODE_COLOR_RED)
// 递归
node = parent
}
}
}
}
/**
* 添加节点
*
* @param {*} data
* @memberof RedBlackTree
*/
insert(data) {
// 1.创建新节点
let newNode = new RBTreeNode(data)
// 2.插入新节点
if (this.__root === null) {
this.__root = newNode
} else {
newNode = this.__insertNode(this.__root, newNode)
}
this.__afterInsertNode(newNode)
}
/**
* 删除节点
*
* @param {*} data
* @memberof RedBlackTree
* @description
* 两种实现方式:
* 递归法:__removeNode
* 迭代法:remove/__fixLostBlack
*/
remove(data) {
let nodeToDelete = this.__searchNode(data, this.__root)
if (!nodeToDelete) return false
let successorNode = null
this.__size -= 1
while (nodeToDelete.left || nodeToDelete.right) {
// 只有红右子节点
if (!nodeToDelete.left) {
successorNode = nodeToDelete.right
}
// 只有红左子节点
else if (!nodeToDelete.right) {
successorNode = nodeToDelete.left
}
// 左右都存在子节点时找后继
else {
successorNode = this.__successor(nodeToDelete.right)
}
nodeToDelete.data = successorNode.data
// 迭代
nodeToDelete = successorNode
}
if (nodeToDelete.isBlack) {
// balance
this.__fixLostBlack(nodeToDelete)
}
if (nodeToDelete.parent) {
nodeToDelete.isLeftChild && (nodeToDelete.parent.left = null)
nodeToDelete.isRightChild && (nodeToDelete.parent.right = null)
}
if (nodeToDelete === this.__root) {
this.__root = null
}
return true
}
}