-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.test.js
More file actions
159 lines (115 loc) · 4.62 KB
/
Copy pathBinarySearchTree.test.js
File metadata and controls
159 lines (115 loc) · 4.62 KB
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
const BinarySearchTree = require("./BinarySearchTree");
const insertMockValues = require("../../../mocks/treeUnitTestUtils");
describe("BinarySearchTree", () => {
const mockValues = [25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 90];
let tree;
beforeEach(() => {
tree = new BinarySearchTree();
});
describe("Tree traversal", () => {
const generateTraversalTest = (operation, assertionResult) => {
it(`should execute the callback for each value ${operation}`, () => {
insertMockValues(tree, mockValues);
const result = [];
tree[operation](result.push.bind(result));
expect(result).toEqual(assertionResult);
});
}
generateTraversalTest('inorder', [4, 10, 12, 15, 18, 22, 24, 25, 31, 35, 44, 50, 66, 70, 90]);
generateTraversalTest('preorder', [25, 15, 10, 4, 12, 22, 18, 24, 50, 35, 31, 44, 70, 66, 90]);
generateTraversalTest('postorder', [4, 12, 10, 18, 24, 22, 15, 31, 44, 35, 66, 90, 70, 50, 25]);
});
it("should insert and find the min value", () => {
insertMockValues(tree, mockValues);
expect(tree.findMin()).toBe(4);
});
it("should find specific value", () => {
insertMockValues(tree, mockValues);
expect(tree.find(24)).toBe(24);
});
it("should't find specific value", () => {
insertMockValues(tree, mockValues);
expect(tree.find(100)).toBe(null);
});
it("should insert and find the max value", () => {
insertMockValues(tree, mockValues);
expect(tree.findMax()).toBe(90);
});
describe("remove", () => {
it("should remove the correct element", () => {
insertMockValues(tree, [25, 10, 30]);
tree.remove(25);
const result = [];
tree.inorder(result.push.bind(result));
expect(result).toEqual([10, 30]);
});
it("should remove the correct element", () => {
insertMockValues(tree, [25, 10, 5, 15]);
tree.remove(25);
const result = [];
tree.inorder(result.push.bind(result));
expect(result).toEqual([5, 10, 15]);
});
it("should remove the correct element", () => {
insertMockValues(tree, [25, 10, 50, 15, 5, 4, 7]);
tree.remove(25);
const result = [];
tree.inorder(result.push.bind(result));
expect(result).toEqual([4, 5, 7, 10, 15, 50]);
});
it("should remove the correct element", () => {
insertMockValues(tree, [25, 10, 50, 15, 5, 4, 7]);
tree.remove(10);
const result = [];
tree.inorder(result.push.bind(result));
expect(result).toEqual([4, 5, 7, 15, 25, 50]);
});
it("should remove the correct element", () => {
insertMockValues(tree, [5, 10]);
tree.remove(5);
const result = [];
tree.inorder(result.push.bind(result));
expect(result).toEqual([10]);
});
});
it("should be balanced", () => {
insertMockValues(tree, [25, 15, 50, 7, 20, 29, 60]);
expect(tree.isBalanced()).toBe(true);
});
it("shouldn't be balanced", () => {
insertMockValues(tree, [25, 40, 15, 10, 5, 20]);
expect(tree.isBalanced()).toBe(false);
});
it("should have a height of 2", () => {
insertMockValues(tree, [25, 15, 50, 7, 20, 29, 60]);
expect(tree.height).toBe(2);
});
it("should have a height of 3", () => {
insertMockValues(tree, [25, 40, 15, 10, 5, 20]);
expect(tree.height).toBe(3);
});
it("should have a diameter of 5", () => {
insertMockValues(tree, [25, 40, 15, 10, 5, 20]);
expect(tree.diameter).toBe(5);
});
it("should have a diameter of 2", () => {
insertMockValues(tree, [25, 15]);
expect(tree.diameter).toBe(2);
});
it("should have a diameter of 1", () => {
insertMockValues(tree, [25]);
expect(tree.diameter).toBe(1);
});
it("lowest common anscestor should be 12", () => {
insertMockValues(tree, [20, 8, 22, 4, 12, 10, 14]);
expect(tree.lowestCommonaAncestor(10, 14)).toBe(12);
});
it("lowest common anscestor should be 8", () => {
insertMockValues(tree, [20, 8, 22, 4, 12, 10, 14]);
expect(tree.lowestCommonaAncestor(14, 8)).toBe(8);
});
it("lowest common anscestor should be 20", () => {
insertMockValues(tree, [20, 8, 22, 4, 12, 10, 14]);
expect(tree.lowestCommonaAncestor(10, 22)).toBe(20);
});
});