-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-tree-node.spec.js
114 lines (91 loc) · 2.8 KB
/
binary-tree-node.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
110
111
112
113
114
const { BinaryTreeNode } = require('../../index');
describe('Binary Tree Node', () => {
let treeNode;
beforeEach(() => {
treeNode = new BinaryTreeNode('hola');
});
it('should start with null parent', () => {
expect(treeNode.parent).toBe(null);
});
it('should start with empty metadata', () => {
expect(treeNode.meta).toEqual({});
});
it('should hold a value', () => {
expect(treeNode.value).toBe('hola');
});
it('should have a height 0', () => {
expect(treeNode.height).toBe(0);
});
it('should set/get left node', () => {
expect(treeNode.left).toBe(null);
const newNode = new BinaryTreeNode(1);
treeNode.setLeftAndUpdateParent(newNode);
expect(treeNode.left.value).toBe(1);
expect(newNode.parent).toBe(treeNode);
expect(treeNode.height).toBe(1);
expect(treeNode.balanceFactor).toBe(1);
});
it('should set/get right node', () => {
expect(treeNode.right).toBe(null);
const newNode = new BinaryTreeNode(1);
treeNode.setRightAndUpdateParent(newNode);
expect(treeNode.right.value).toBe(1);
expect(newNode.parent).toBe(treeNode);
expect(treeNode.height).toBe(1);
expect(treeNode.balanceFactor).toBe(-1);
});
describe('Family operations', () => {
let g;
let p;
let u;
let c;
let s;
beforeEach(() => {
g = new BinaryTreeNode('grandparent');
p = new BinaryTreeNode('parent');
u = new BinaryTreeNode('uncle');
c = new BinaryTreeNode('child');
s = new BinaryTreeNode('sibling');
g.setRightAndUpdateParent(p);
g.setLeftAndUpdateParent(u);
p.setRightAndUpdateParent(c);
p.setLeftAndUpdateParent(s);
});
it('should set heights', () => {
expect(g.height).toBe(2);
expect(g.balanceFactor).toBe(-1);
expect(p.height).toBe(1);
expect(p.balanceFactor).toBe(0);
expect(u.height).toBe(0);
expect(u.balanceFactor).toBe(0);
});
it('should get the sibling', () => {
expect(c.sibling).toBe(s);
expect(p.sibling).toBe(u);
});
it('should set leaf correctly', () => {
expect(c.isLeaf).toBe(true);
expect(u.isLeaf).toBe(true);
expect(p.isLeaf).toBe(false);
expect(g.isLeaf).toBe(false);
});
it('should get null if no sibling', () => {
expect(g.sibling).toBe(null);
});
it('should get the uncle', () => {
expect(c.uncle).toBe(u);
});
it('should get null if no uncle', () => {
expect(g.uncle).toBe(null);
expect(p.uncle).toBe(null);
});
it('true if is parent left child', () => {
expect(s.isParentLeftChild).toBe(true);
expect(s.isParentRightChild).toBe(false);
});
it('true if is parent left child', () => {
expect(c.isParentLeftChild).toBe(false);
expect(c.isParentRightChild).toBe(true);
});
});
});