本文内所有代码均由 @JieJiSS 编写,转载请注明出处。

0x00 文件目录树

1
2
3
4
5
6
7
8
./node_modules   // libraries
./package.json // package info
./Node.js // 树节点Class
./BST.js // 二叉树Class
./AVL.js // 完全平衡树Class
./RBNode.js // 红黑节点Class
./RBT.js // 红黑树Class
./test.js // 测试例程

0x01 实现普通树节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ./Node.js
"use strict";

class Node {
/**
* @param {number} val value
* @param {Node | null} parent parentNode
*/
constructor(val, parent) {
this.val = val;
this.parent = parent || null;
this.left = null;
this.right = null;
}
}

export default Node;

0x02 实现普通二叉树

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
// ./BST.js
"use strict";

import Node from "./Node";

class BST {
/**
* @description construct a BST instance.
* @param {number[]} vals values to construct the BST tree
*/
constructor(vals) {
/**
* @type {Node}
*/
this.root = new Node(vals[0], null);
for(let i = 1; i < vals.length; i++) {
this.insert(vals[i]);
}
}
/**
* @param {number} val value to insert
* @returns {Node} the inserted Node
*/
insert(val) {
if(this.root.val === null) {
this.root.val = val;
return this.root;
}
let node = this.root;
const insertedNode = new Node(val, node);
while(true) {
if(val > node.val) {
if(node.right) {
node = node.right;
} else {
node.right = insertedNode;
insertedNode.parent = node;
break;
}
} else if(val < node.val) {
if(node.left) {
node = node.left;
} else {
node.left = insertedNode;
insertedNode.parent = node;
break;
}
} else {
return node;
}
}
return insertedNode;
}
/**
* @param {number} val the value of the node to get
*/
getNode(val) {
let node = this.root;
while(node !== null && node.val !== val) {
if(val > node.val) {
node = node.right;
} else {
node = node.left;
}
}
return node;
}
/**
* @param {Node} node The specific node to get its `left-right` balance
*/
getBalance(node) {
if(node.val === null) {
return 0;
}
return this.getNodeDepth(node.left) - this.getNodeDepth(node.right);
}
/**
* @returns {number} depth
*/
depth() {
return this.getNodeDepth(this.root);
}
/**
* @param {Node} node The specific node to get its max depth
* @returns {number} depth
*/
getNodeDepth(node) {
if(node === null) return 0;
return 1 + Math.max(this.getNodeDepth(node.left), this.getNodeDepth(node.right));
}
/**
* @param {Node} node The node to rotate around
* @description Usage: `bstInstance.rotateLeft(node);`
* @throws `TypeError` when node.right === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
*/
rotateLeft(node) {
const rightNode = node.right;
if(rightNode === null) {
throw new TypeError("The right node of given node is null");
}
node.right = null;

const rightNodeLeft = rightNode.left;
rightNode.left = node;
node.right = rightNodeLeft;

const parentNode = node.parent;
node.parent = rightNode;
if(node.right)
node.right.parent = node;

if(parentNode === null) {
if(this.root === node) {
this.root = rightNode;
} else {
throw new ReferenceError("node.parent is null and node is not root node");
}
} else {
if(parentNode.left === node) {
parentNode.left = rightNode;
} else if(parentNode.right === node) {
parentNode.right = rightNode;
} else {
throw new ReferenceError("node.parent is not the node's parent");
}
}

rightNode.parent = parentNode;
return this;
}
/**
* @param {Node} node The node to rotate around
* @description Usage: `bstInstance.rotateRight(node);`
* @throws `TypeError` when node.left === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
*/
rotateRight(node) {
const leftNode = node.left;
if(leftNode === null) {
throw new TypeError("The right node of given node is null");
}
node.left = null;

const leftNodeRight = leftNode.right;
leftNode.right = node;
node.left = leftNodeRight;

const parentNode = node.parent;
node.parent = leftNode;
if(node.left)
node.left.parent = node;

if(parentNode === null) {
if(this.root === node) {
this.root = leftNode;
} else {
throw new ReferenceError("node.parent is null and node is not root node");
}
} else {
if(parentNode.left === node) {
parentNode.left = leftNode;
} else if(parentNode.right === node) {
parentNode.right = leftNode;
} else {
throw new ReferenceError("node.parent is not the node's parent");
}
}

leftNode.parent = parentNode;
return this;
}
/**
* @param {number} val the value of the Node to remove
*/
remove(val) {
const node = this.getNode(val);
if(node === null) {
throw new ReferenceError("val " + val + " is not in the tree");
}
return this.removeNode(node);
}
/**
* @param {Node} node The specific node to be removed from the tree
* @throws `TypeError` when node === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
* @returns the Node that replaced the removed node's position, or null
*/
removeNode(node) {
if(node === null) {
throw new TypeError("node is null");
}
const parentNode = node.parent;
if(node.parent === null) {
if(node === this.root) {
this.root = node.left || new Node(null, null);
node.left.parent = null;
const leftNodeRight = node.left.right;
node.left.right = node.right;
node.left.right.parent = node.left;
node.left = null;
const rightNode = node.right;
node.right = null;
let rightNodeLeftLeaf = rightNode;
while(rightNodeLeftLeaf && rightNodeLeftLeaf.left) {
rightNodeLeftLeaf = rightNodeLeftLeaf.left;
}
rightNodeLeftLeaf.left = leftNodeRight;
if(leftNodeRight)
leftNodeRight.parent = rightNodeLeftLeaf;
return this.root;
} else {
throw new TypeError("node has no parent and is not root");
}
}
if(node.parent.left !== node && node.parent.right !== node) {
throw new ReferenceError("node.parent is not the node's parent");
}
if(node.parent.left === node) {
const rightNode = node.right;
let rightNodeLeftLeaf = rightNode;
while(rightNodeLeftLeaf && rightNodeLeftLeaf.left) {
rightNodeLeftLeaf = rightNodeLeftLeaf.left;
}
node.parent.left = rightNode;
if(rightNode)
rightNode.parent = node.parent;
node.parent = null;

if(rightNodeLeftLeaf)
rightNodeLeftLeaf.left = node.left;
if(node.left)
node.left.parent = rightNodeLeftLeaf;
node.left = null;
return parentNode.left;
} else {
const leftNode = node.left;
let leftNodeRightLeaf = leftNode;
while(leftNodeRightLeaf && leftNodeRightLeaf.right) {
leftNodeRightLeaf = leftNodeRightLeaf.right;
}
node.parent.right = leftNode;
if(leftNode)
leftNode.parent = node.parent;
node.parent = null;

if(leftNodeRightLeaf)
leftNodeRightLeaf.right = node.right;
if(node.right)
node.right.parent = leftNodeRightLeaf;
node.right = null;
return parentNode.right;
}
}
inOrder() {
return this.getValues(this.root, "inOrder");
}
preOrder() {
return this.getValues(this.root, "preOrder");
}
postOrder() {
return this.getValues(this.root, "postOrder");
}
/**
* @param {Node} node
* @param {string} type inOrder | preOrder | postOrder
* @returns {number[]} values
*/
getValues(node, type) {
if(node === null) return [];
switch(type) {
case "inOrder":
return [...this.getValues(node.left, type), node.val, ...this.getValues(node.right, type)];
case "preOrder":
return [node.val, ...this.getValues(node.left, type), ...this.getValues(node.right, type)];
case "postOrder":
return [...this.getValues(node.left, type), ...this.getValues(node.right, type), node.val];
default:
throw new RangeError("Invalid recurssion order: should be inOrder | preOrder | postOrder");
}
}
}

export default BST;

0x03 实现 AVL

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
// ./AVL.js
"use strict";

import Node from "./Node";
import BST from "./BST";

class AVL extends BST {
/**
* @description construct an AVL Tree instance.
* @param {number[]} vals values to construct the AVL tree
*/
constructor(vals) {
super([vals[0]]);
for(let i = 1; i < vals.length; i++) {
this.insert(vals[i]);
}
}
/**
* @param {number} val value to insert
*/
insert(val) {
const insertedNode = super.insert(val);
return this.updateBalance(insertedNode);
}
/**
* @param {number} val the value of the Node to remove
*/
remove(val) {
const node = super.getNode(val);
if(node === null) {
throw new ReferenceError("val " + val + " is not found in the tree");
}
return this.removeNode(node);
}
/**
* @param {Node} node The specific node to be removed from the tree
* @throws `TypeError` when node === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
* @returns the Node that replaced the removed node's position, or null
*/
removeNode(node) {
const parentNode = node.parent;
const newNode = super.removeNode(node);
if(newNode === null) {
if(parentNode) {
this.updateBalance(parentNode);
}
return newNode;
}
if(newNode.left)
this.updateBalance(newNode.left);
if(newNode.right)
this.updateBalance(newNode.right);
this.updateBalance(newNode);
return newNode;
}
/**
* @param {Node} modifiedNode The specific node to be removed from the tree
* @returns the Node that has been balanced
*/
updateBalance(modifiedNode) {
let parentNode = modifiedNode.parent;
if(parentNode === null) { // node is root
parentNode = modifiedNode;
}

while(parentNode) {
if(Math.abs(super.getBalance(parentNode)) >= 2) {
const minNotBalancedNode = parentNode;
if(super.getBalance(minNotBalancedNode) >= 2) {
if(super.getBalance(minNotBalancedNode.left) >= 0) {
super.rotateRight(minNotBalancedNode);
} else {
super.rotateLeft(minNotBalancedNode.left);
super.rotateRight(minNotBalancedNode);
}
} else { // <= -2
if(super.getBalance(minNotBalancedNode.right) <= 0) {
super.rotateLeft(minNotBalancedNode);
} else {
super.rotateRight(minNotBalancedNode.right);
super.rotateLeft(minNotBalancedNode);
}
}
return modifiedNode;
} else {
parentNode = parentNode.parent;
}
}
return modifiedNode;
}
}

export default AVL;

0x04 实现红黑树节点

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
// ./RBNode.js
//@ts-check

"use strict";

class RBNode {
/**
* @param {number} val value
* @param {RBNode | null} parent parentNode
* @param {string} color the color of the Node, red | black
*/
constructor(val, parent, color) {
if(color !== "red" && color !== "black") {
throw new RangeError("color should be either black or red");
}
this.val = val;
this.parent = parent || null;
/**
* @type {RBNode}
*/
this.left = null;
/**
* @type {RBNode}
*/
this.right = null;
this.color = color;
}
isValidRBNode() {
if(this.color === "red") {
if(this.parent.color === "red") {
return false;
}
}
return true;
}
}

export default RBNode;

0x05 实现红黑树

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
// ./RBT.js
//@ts-check

"use strict";

import RBNode from "./RBNode";

class RBT {
/**
* @description construct a Red Black Tree instance.
* @param {number[]} vals values to construct the RBT tree
*/
constructor(vals) {
/**
* @type {RBNode}
*/
this.root = new RBNode(vals[0] || null, null, "black");
for(let i = 1; i < vals.length; i++) {
this.insert(vals[i]);
}
}
/**
* @param {number} val value to insert
* @returns {RBNode} the inserted Node
*/
insert(val) {
if(this.root.val === null) {
this.root.val = val;
this.root.color = "black";
return this.root;
}
let node = this.root;
const insertedNode = new RBNode(val, node, "red");
while(true) {
if(val > node.val) {
if(node.right) {
node = node.right;
} else {
node.right = insertedNode;
insertedNode.parent = node;
break;
}
} else if(val < node.val) {
if(node.left) {
node = node.left;
} else {
node.left = insertedNode;
insertedNode.parent = node;
break;
}
} else {
return node;
}
}
if(!insertedNode.isValidRBNode()) {
// in this case, insertedNode.parent can't be root
this.updateColor(insertedNode);
}
return insertedNode;
}
/**
* @param {number} val the value of the node to get
*/
getNode(val) {
let node = this.root;
while(node !== null && node.val !== val) {
if(val > node.val) {
node = node.right;
} else {
node = node.left;
}
}
return node;
}
/**
* @returns {number} depth
*/
depth() {
return this.getNodeDepth(this.root);
}
/**
* @param {RBNode} node The specific node to get its max depth
* @returns {number} depth
*/
getNodeDepth(node) {
if(node === null) return 0;
return 1 + Math.max(this.getNodeDepth(node.left), this.getNodeDepth(node.right));
}
/**
* @param {RBNode} node The node to rotate around
* @description Usage: `bstInstance.rotateLeft(node);`
* @throws `TypeError` when node.right === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
*/
rotateLeft(node) {
const rightNode = node.right;
if(rightNode === null) {
throw new TypeError("The right node of given node is null");
}
node.right = null;

const rightNodeLeft = rightNode.left;
rightNode.left = node;
node.right = rightNodeLeft;

const parentNode = node.parent;
node.parent = rightNode;
if(node.right)
node.right.parent = node;

if(parentNode === null) {
if(this.root === node) {
this.root = rightNode;
} else {
throw new ReferenceError("node.parent is null and node is not root node");
}
} else {
if(parentNode.left === node) {
parentNode.left = rightNode;
} else if(parentNode.right === node) {
parentNode.right = rightNode;
} else {
throw new ReferenceError("node.parent is not the node's parent");
}
}

rightNode.parent = parentNode;
return this;
}
/**
* @param {RBNode} node The node to rotate around
* @description Usage: `bstInstance.rotateRight(node);`
* @throws `TypeError` when node.left === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
*/
rotateRight(node) {
const leftNode = node.left;
if(leftNode === null) {
throw new TypeError("The right node of given node is null");
}
node.left = null;

const leftNodeRight = leftNode.right;
leftNode.right = node;
node.left = leftNodeRight;

const parentNode = node.parent;
node.parent = leftNode;
if(node.left)
node.left.parent = node;

if(parentNode === null) {
if(this.root === node) {
this.root = leftNode;
} else {
throw new ReferenceError("node.parent is null and node is not root node");
}
} else {
if(parentNode.left === node) {
parentNode.left = leftNode;
} else if(parentNode.right === node) {
parentNode.right = leftNode;
} else {
throw new ReferenceError("node.parent is not the node's parent");
}
}

leftNode.parent = parentNode;
return this;
}
/**
* @param {number} val the value of the Node to remove
*/
remove(val) {
const node = this.getNode(val);
if(node === null) {
throw new ReferenceError("val " + val + " is not found in the tree");
}
return this.removeNode(node);
}
/**
* @param {RBNode} node The specific node to be removed from the tree
* @throws `TypeError` when node === null;
* @throws `ReferenceError` when node.parent is not the node's parent node
* @returns the Node that replaced the removed node's position, or null
*/
removeNode(node) {
if(node === null) {
throw new TypeError("node is null");
}
const parentNode = node.parent;
if(node.parent === null) {
if(node === this.root) {
this.root = node.left;
node.left.parent = null;
const leftNodeRight = node.left.right;
node.left.right = node.right;
node.left.right.parent = node.left;
node.left = null;
const rightNode = node.right;
node.right = null;
let rightNodeLeftLeaf = rightNode;
while(rightNodeLeftLeaf && rightNodeLeftLeaf.left) {
rightNodeLeftLeaf = rightNodeLeftLeaf.left;
}
rightNodeLeftLeaf.left = leftNodeRight;
if(leftNodeRight)
leftNodeRight.parent = rightNodeLeftLeaf;
return this.root;
} else {
throw new TypeError("node has no parent and is not root");
}
}
if(node.parent.left !== node && node.parent.right !== node) {
throw new ReferenceError("node.parent is not the node's parent");
}
if(node.parent.left === node) {
const rightNode = node.right;
let rightNodeLeftLeaf = rightNode;
while(rightNodeLeftLeaf && rightNodeLeftLeaf.left) {
rightNodeLeftLeaf = rightNodeLeftLeaf.left;
}
node.parent.left = rightNode;
if(rightNode)
rightNode.parent = node.parent;
node.parent = null;

if(rightNodeLeftLeaf)
rightNodeLeftLeaf.left = node.left;
if(node.left)
node.left.parent = rightNodeLeftLeaf;
node.left = null;
return parentNode.left;
} else {
const leftNode = node.left;
let leftNodeRightLeaf = leftNode;
while(leftNodeRightLeaf && leftNodeRightLeaf.right) {
leftNodeRightLeaf = leftNodeRightLeaf.right;
}
node.parent.right = leftNode;
if(leftNode)
leftNode.parent = node.parent;
node.parent = null;

if(leftNodeRightLeaf)
leftNodeRightLeaf.right = node.right;
if(node.right)
node.right.parent = leftNodeRightLeaf;
node.right = null;
return parentNode.right;
}
}
inOrder() {
return this.getValues(this.root, "inOrder");
}
preOrder() {
return this.getValues(this.root, "preOrder");
}
postOrder() {
return this.getValues(this.root, "postOrder");
}
/**
* @param {RBNode} node
* @param {string} type inOrder | preOrder | postOrder
* @returns {number[]} values
*/
getValues(node, type) {
if(node === null) return [];
switch(type) {
case "inOrder":
return [...this.getValues(node.left, type), node.val, ...this.getValues(node.right, type)];
case "preOrder":
return [node.val, ...this.getValues(node.left, type), ...this.getValues(node.right, type)];
case "postOrder":
return [...this.getValues(node.left, type), ...this.getValues(node.right, type), node.val];
default:
throw new RangeError("Invalid recurssion order: should be inOrder | preOrder | postOrder");
}
}
/**
* @param {RBNode} modifiedNode
*/
updateColor(modifiedNode) {
if(modifiedNode === null || modifiedNode.color === "black") {
throw new RangeError("Wrong color for modifiedNode: expecting red, got black");
}
// modified node's color is red, and is not a valid RBNode
const parentNode = modifiedNode.parent; // red
const grandparentNode = parentNode.parent; // must be black
/**
* @type {RBNode}
*/
let uncleNode;
if(grandparentNode.left && grandparentNode.left !== parentNode) {
uncleNode = grandparentNode.left;
} else if(grandparentNode.right && grandparentNode.right !== parentNode) {
uncleNode = grandparentNode.right;
}
/**
* @type {string}
*/
let uncleNodeColor;
if(uncleNode) {
uncleNodeColor = uncleNode.color;
} else {
uncleNodeColor = "black"; // the color of null is black
}
if(uncleNodeColor === "red") {
parentNode.color = "black";
uncleNode.color = "black";
grandparentNode.color = "red";
if(this.root === grandparentNode) {
grandparentNode.color = "black"; // root should be black, and still a valid RBT
}
if(!grandparentNode.isValidRBNode()) {
return this.updateColor(grandparentNode);
}
return;
}
// so uncle node's color is black
if(modifiedNode === parentNode.right && parentNode === grandparentNode.left) {
this.rotateLeft(parentNode); // so that modifiedNode becomes parentNode's parent
if(!parentNode.isValidRBNode()) {
return this.updateColor(parentNode);
}
return;
} else if(modifiedNode === parentNode.left && parentNode === grandparentNode.right) {
this.rotateRight(parentNode);
if(!parentNode.isValidRBNode()) {
return this.updateColor(parentNode);
}
return;
}

if(modifiedNode === parentNode.left && parentNode === grandparentNode.left) {
this.rotateRight(grandparentNode); // so that parentNode becomes grandparentNode's parent
// swap the color
parentNode.color = "black";
grandparentNode.color = "red";
return;
} else if(modifiedNode === parentNode.right && parentNode === grandparentNode.right) {
this.rotateLeft(grandparentNode);
parentNode.color = "black";
grandparentNode.color = "red";
return;
}
}
}

export default RBT;

0x06 可视化调试代码

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
// ./test.js
//@ts-check

"use strict";

const usedNumbers = [];
function getRandomUniqueNumber() {
let num;
do {
num = ~~(Math.random() * 90 + 1);
} while(usedNumbers.indexOf(num) !== -1);
usedNumbers.push(num);
return num;
}

const assert = require("assert");
const cp = require("child_process");
import Node from "./Node";
import RBNode from "./RBNode";
import BST from "./BST";
import AVL from "./AVL";
import RBT from "./RBT";
const fs = require("fs");
const path = require("path");

const startTime = Date.now();

const arr = Array.from(new Array(20), () => getRandomUniqueNumber());
const rbt = new RBT(arr);
console.log(arr);

beautifyRBTree(rbt);

avl.insert(8);
avl.remove(usedNumbers[0]);
avl.remove(usedNumbers[8]);
avl.remove(usedNumbers[3]);

console.log("Test finished in", Date.now() - startTime, "ms");

beautifyTree(avl);

/**
* @param {BST | AVL} bst the Binary Search Tree
*/
function beautifyTree(bst) {
let str = "graph {\n";
str += mapBSTNode(bst.root);
str += "}";
fs.writeFileSync("result.txt", str);
fs.writeFileSync("tree.png", cp.execSync("dot -Tpng result.txt"));
return;
const values = bst.preOrder();
fs.writeFileSync(path.join(__dirname, "output-tree-data.js"), `var values = [${values.join(",")}]`);
}

/**
* @param {RBT} rbt the Red-Black Binary Search Tree
*/
function beautifyRBTree(rbt) {
let str = "graph {\nnode[fontname=Courier];\n";
str += mapRBTNode(rbt.root);
str += "}";
fs.writeFileSync("result.txt", str);
fs.writeFileSync("tree.png", cp.execSync("dot -Tpng result.txt"));
return;
}

/**
* @param {Node} node
*/
function mapBSTNode(node) {
let str = "";
if(node.left) {
str += ` ${node.val} -- ${node.left.val}\n`;
str += mapBSTNode(node.left);
}
if(node.right) {
str += ` ${node.val} -- ${node.right.val}\n`;
str += mapBSTNode(node.right);
}
return str;
}

/**
* @param {RBNode} node
*/
function mapRBTNode(node) {
let str = "";
const DEF = "fontcolor=gray, style=filled, height=.1, shape=point, color=gray";
if(node.left) {
str += ` ${node.val}[fontcolor=white, color=${node.color}, style=filled];\n` +
` ${node.left.val}[fontcolor=white, color=${node.left.color}, style=filled];\n` +
` ${node.val} -- ${node.left.val};`
str += mapRBTNode(node.left);
} else {
str += ` ${node.val}[fontcolor=white, color=${node.color}, style=filled];\n` +
` ${node.val-0.5}[${DEF}];\n` +
` ${node.val} -- ${node.val-0.5};`
}
if(node.right) {
str += ` ${node.val}[fontcolor=white, color=${node.color}, style=filled];\n` +
` ${node.right.val}[fontcolor=white, color=${node.right.color}, style=filled];\n` +
` ${node.val} -- ${node.right.val};`;
str += mapRBTNode(node.right);
} else {
str += ` ${node.val}[fontcolor=white, color=${node.color}, style=filled];\n` +
` ${node.val+0.5}[${DEF}];\n` +
` ${node.val} -- ${node.val+0.5};`
}
return str;
}

来源:https://blog.jiejiss.com/