-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
388 lines (334 loc) · 9.16 KB
/
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
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
/**
* 树节点结构
*/
class Node {
constructor(key) {
this.key = key
this.left = null
this.right = null
}
}
/**
* 二叉搜索树
*/
class BinarySearchTree {
constructor() {
this.root = null
}
/**
* 插入操作,可以存储各种值
* @param {any} key
*/
insert(key) {
const newNode = new Node(key)
const insertNode = (node, newNode) => {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode
} else {
insertNode(node.left, newNode)
}
} else {
if (node.right === null) {
node.right = newNode
} else {
insertNode(node.right, newNode)
}
}
}
if (!this.root) {
this.root = newNode
} else {
insertNode(this.root, newNode)
}
}
/**
* 先序遍历
* 根=>左子树=>右子树
* @param {function} cb
*/
preOrderTraverse(cb) {
const preOrderTraverseNode = (node, cb) => {
if (node !== null) {
cb(node.key)
preOrderTraverseNode(node.left, cb)
preOrderTraverseNode(node.right, cb)
}
}
preOrderTraverseNode(this.root, cb)
}
/**
* 中序遍历
* 左子树=>根=>右子树
* @param {function} cb
*/
inOrderTraverse(cb) {
const traverse = (node, cb) => {
if (node !== null) {
traverse(node.left, cb)
cb(node.key)
traverse(node.right, cb)
}
}
traverse(this.root, cb)
}
/**
* 后序遍历
* 左子树=>右子树=>根
* @param {function} cb
*/
postOrderTraverse(cb) {
const traverse = (node, cb) => {
if (node !== null) {
traverse(node.left, cb)
traverse(node.right, cb)
cb(node.key)
}
}
traverse(this.root, cb)
}
/**
* 最小值
* 默认为根节点的,指定节点后,只搜索节点的最小值
*/
min(node) {
const minNode = (node) => {
return node ? (node.left ? minNode(node.left) : node) : null
}
return minNode(node || this.root)
}
/**
* 最大值
* 默认为根节点的,指定节点后,只搜索节点的最大值
*/
max(node) {
const maxNode = (node) => {
return node ? (node.right ? maxNode(node.right) : node) : null
}
return maxNode(node || this.root)
}
/**
* 查找
* @param {string} key
*/
search(key) {
const searchNode = (node, key) => {
if (node === null) return false
if (node.key === key) return node
return searchNode((key < node.key ? node.left : node.right), key)
}
searchNode(this.root, key)
}
/**
* 删除
* @param {string} key
*/
remove(key) {
const removeNode = (node, key) => {
if (node === null) return false
if (node.key === key) {
/** 无子节点 */
if (node.left === null && node.right === null) {
node = null
}
/** 有双子节点,找右子树的最小值来替换 */
if (node.left && node.right) {
node.key = this.min(node.right).key
this.min(node.right) = null
}
/** 有左子节点 */
if (node.left) {
node.key = node.left.key
node.left = null
}
/** 有右子节点 */
if (node.right) {
node.key = node.right.key
node.right = null
}
}
return removeNode((key < node.key) ? node.left : node.right, key)
}
removeNode(this.root, key)
}
}
class AVL {
constructor(key) {
this.root = null
}
/**
* 获取节点的高度
* @param {node} node
*/
height(node) {
const getHeight = (node) => {
if (node === null) return 0
let leftH = getHeight(node.left)
let rightH = getHeight(node.right)
return (leftH > rightH ? leftH : rightH) + 1
}
return getHeight(node || this.root)
}
_factor(node) {
return this.height(node.left) - this.height(node.right)
}
_rotateType(node) {
if (this._factor(node) > 1 && this._factor(node.left) >= 0) {
return 'LL'
} else if (this._factor(node) > 1 && this._factor(node.left) < 0) {
return 'LR'
} else if (this._factor(node) < -1 && this._factor(node.right) <= 0) {
return 'RR'
} else if (this._factor(node) < -1 && this._factor(node.right) > 0) {
return 'RL'
}
}
balance(node) {
if (node === null) return false
const rotateLeft = (node) => {
let tmp = node.right
node.right = tmp.left
tmp.left = node
return tmp
}
const rotateRight = (node) => {
let tmp = node.left
node.left = tmp.right
tmp.right = node
return tmp
}
switch (this._rotateType(node)) {
case 'LL':
node = rotateRight(node)
break;
case 'LR':
node.left = rotateLeft(node.left)
node = rotateRight(node)
break;
case 'RR':
node = rotateLeft(node)
break;
case 'RL':
node.right = rotateRight(node.right)
node = rotateLeft(node)
break;
default:
return node
break;
}
return node
}
/**
* 插入时,保持平衡
* @param {string} key
*/
insert(key) {
const newNode = new Node(key)
const insertNode = (node, newNode) => {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode
} else {
insertNode(node.left, newNode)
node.left = this.balance(node.left)
}
} else {
if (node.right === null) {
node.right = newNode
} else {
insertNode(node.right, newNode)
node.right = this.balance(node.right)
}
}
}
if (!this.root) {
this.root = newNode
} else {
insertNode(this.root, newNode)
this.root = this.balance(this.root)
}
}
}
const RED = 0
const BLACK = 1
class RBNode {
constructor(key, color) {
this.key = key
this.left = null
this.right = null
this.color = color
}
filpColor() {
this.color = this.color === RED ? BLACK : RED
}
}
class RBTree {
constructor() {
this.root = null
}
isRed(node) {
return node.color === RED
}
filpColors(node) {
node.left.filpColor()
node.right.filpColor()
}
rotateLeft(node) {
let tmp = node.right
node.right = tmp.left
tmp.left = node
tmp.color = node.color
node.color = RED
return tmp
}
rotateRight(node) {
let tmp = node.left
node.left = tmp.right
tmp.left = node
tmp.color = node.color
node.color = RED
return tmp
}
insert(key) {
const newNode = new RBNode(key, RED)
const insertNode = (node, newNode) => {
if (newNode.key < node.key) {
if (node.left === null) {
node.left = newNode
} else {
insertNode(node.left, newNode)
node.left = this.REDBLACK(node.left)
}
} else {
if (node.right === null) {
node.right = newNode
} else {
insertNode(node.right, newNode)
node.right = this.REDBLACK(node.right)
}
}
}
if (!this.root) {
this.root = new RBNode(key, BLACK)
} else {
insertNode(this.root, newNode)
this.root = this.REDBLACK(this.root)
}
}
/** 插入红黑平衡 */
REDBLACK(node) {
/** 左子黑,右子红 ,左转*/
if (!this.isRed(node.left) && this.isRed(node.right)) {
node = this.rotateLeft(node)
}
/** 左子红,左左子红,右转 */
if (this.isRed(node.left) && this.isRed(node.left.left)) {
node = this.rotateRight(node)
}
/** 双红,反转 */
if (this.isRed(node.left) && this.isRed(node.right)) {
node = this.filpColors(node)
}
return node
}
}