-
Notifications
You must be signed in to change notification settings - Fork 929
/
Copy pathred-black-tree.spec.js
109 lines (90 loc) · 2.53 KB
/
red-black-tree.spec.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
const { RedBlackTree } = require('../../index');
const { RED, BLACK } = RedBlackTree;
describe('RedBlackTree', () => {
let tree;
beforeEach(() => {
tree = new RedBlackTree();
});
describe('#add', () => {
it('should add and self-balance the tree', () => {
expect(tree).not.toBe(null);
});
it('should make root black', () => {
const root = tree.add(1);
expect(root.meta.color).toBe(BLACK);
expect(tree.size).toBe(1);
});
it('should add a new node as red', () => {
tree.add(1);
const n2 = tree.add(2);
expect(n2.meta.color).toBe(RED);
});
it('should balance tree by rotating left', () => {
tree.add(1);
tree.add(2);
tree.add(3);
expect(tree.size).toBe(3);
expect(tree.toArray()).toEqual([
2,
1, 3,
null, null, null, null,
]);
// verify colors
expect(tree.root.color).toBe(BLACK);
expect(tree.root.right.color).toBe(RED);
expect(tree.root.left.color).toBe(RED);
});
it('should balance tree by rotating right', () => {
tree.add(3);
tree.add(2);
tree.add(1);
expect(tree.toArray()).toEqual([
2,
1, 3,
null, null, null, null,
]);
// verify colors
expect(tree.root.color).toBe(BLACK);
expect(tree.root.right.color).toBe(RED);
expect(tree.root.left.color).toBe(RED);
});
it('should change colors', () => {
const n1 = tree.add(1);
const n2 = tree.add(2);
const n3 = tree.add(3);
const n4 = tree.add(4);
expect(tree.toArray()).toEqual([
2,
1, 3,
null, null, null, 4,
null, null,
]);
expect(tree.root.color).toBe(BLACK);
expect(tree.root.right.color).toBe(BLACK);
expect(tree.root.left.color).toBe(BLACK);
expect(tree.root.right.right.color).toBe(RED);
expect(n1.color).toBe(BLACK);
expect(n2.color).toBe(BLACK);
expect(n3.color).toBe(BLACK);
expect(n4.color).toBe(RED);
});
xtest('letf roation with grandparent', () => {
const n1 = tree.add(1);
const n2 = tree.add(2);
const n3 = tree.add(3);
const n4 = tree.add(4);
const n5 = tree.add(5);
expect(tree.toArray()).toEqual([
2,
1, 4,
null, null, 3, 5,
null, null, null, null,
]);
expect(n1.color).toBe(BLACK);
expect(n2.color).toBe(BLACK);
expect(n4.color).toBe(BLACK);
expect(n3.color).toBe(RED);
expect(n5.color).toBe(RED);
});
});
});