-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search-tree.spec.js
379 lines (324 loc) · 11 KB
/
binary-search-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
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
const { BinarySearchTree } = require('../../index');
describe('Binary Search Tree', () => {
let bst;
const getValues = treeGenerator => Array.from(treeGenerator).map(node => node.value);
beforeEach(() => {
bst = new BinarySearchTree();
});
describe('when is empty', () => {
describe('#add', () => {
it('should insert an item', () => {
expect(bst.size).toBe(0);
bst.add(1);
expect(bst.size).toBe(1);
});
it('should insert left and right child', () => {
const root = bst.add(5);
const n = bst.add(1);
expect(n.toValues()).toMatchObject({
value: 1, parent: 5, left: null, right: null,
});
expect(root.toValues()).toMatchObject({
value: 5, parent: null, left: 1, right: null,
});
bst.add(10);
expect(root.toValues()).toMatchObject({
value: 5, parent: null, left: 1, right: 10,
});
});
it('should insert low values to the left and higher to the right', () => {
const node5 = bst.add(5);
bst.add(1);
bst.add(10);
expect(bst.size).toBe(3);
// expect(bst.root).toBe(node5);
expect(node5.left.value).toBe(1);
expect(node5.right.value).toBe(10);
});
it('should insert nested values', () => {
const root = bst.add(10);
const n2 = bst.add(2);
const n30 = bst.add(30);
const n40 = bst.add(40);
expect(root.left).toBe(n2);
expect(root.right).toBe(n30);
expect(n30.left).toBe(null);
expect(n30.right).toBe(n40);
});
it('should keep parent reference', () => {
bst.add(1);
bst.add(2);
const n3 = bst.add(3);
expect(n3.parent.value).toBe(2);
expect(bst.toArray()).toEqual([1, null, 2, null, 3, null, null]);
});
it('should deal with duplicates', () => {
const root = bst.add(1);
expect(root.meta.multiplicity).toBe(undefined);
bst.add(1);
expect(bst.size).toBe(2);
expect(root.toValues()).toMatchObject({
value: 1, parent: null, left: null, right: null,
});
expect(root.meta.multiplicity).toBe(2);
});
});
describe('#findNodeAndParent', () => {
it('should return falsy for empty tree', () => {
const { found, parent } = bst.findNodeAndParent(5);
expect(found).toBe(null);
expect(parent).toBe(null);
});
it('should return with a single element', () => {
bst.add(5);
const { found, parent } = bst.findNodeAndParent(5);
expect(found).toMatchObject({ value: 5 });
expect(parent).toBe(null);
});
it('should return with an element and its parent', () => {
bst.add(5);
bst.add(1);
const { found, parent } = bst.findNodeAndParent(1);
expect(found).toMatchObject({ value: 1 });
expect(parent).toMatchObject({ value: 5 });
});
it('should find future parent of a node that doesnt exist yet', () => {
bst.add(5);
bst.add(1);
const { found, parent } = bst.findNodeAndParent(10);
expect(found).toBe(null);
expect(parent).toMatchObject({ value: 5 });
});
it('should find future parent of a node that doesnt exist yet', () => {
bst.add(5);
bst.add(1);
const { found, parent } = bst.findNodeAndParent(-1);
expect(found).toBe(null);
expect(parent).toMatchObject({ value: 1 });
});
});
describe('#remove', () => {
it('should remove root', () => {
bst.add(1);
expect(bst.remove(1)).toBe(true);
expect(bst.has(1)).toBe(false);
bst.add(1);
expect(bst.has(1)).toBe(true);
});
});
describe('#inOrderTraversal', () => {
it('should get all keys', () => {
const fn = () => {};
bst.set(1).data(1);
bst.set('dos').data(2);
bst.set({}).data(fn);
// get keys
expect(getValues(bst.inOrderTraversal())).toEqual([1, {}, 'dos']);
// get data
expect(Array.from(bst.inOrderTraversal()).map(n => n.data())).toEqual([
1,
fn,
2,
]);
});
});
});
describe('when has items', () => {
let root;
let n3;
let n4;
let n5;
let n30;
let n40;
let n15;
beforeEach(() => {
// 10
// / \
// 5 30
// / / \
// 4 15 40
// / \
// 3 (4.5)
root = bst.add(10);
n5 = bst.add(5);
n30 = bst.add(30);
n40 = bst.add(40);
n15 = bst.add(15);
n4 = bst.add(4);
n3 = bst.add(3);
});
describe('#find', () => {
it('should find the value 2', () => {
expect(bst.find(5)).toBe(n5);
expect(bst.size).toBe(7);
});
it('should NOT find the value 20', () => {
expect(bst.find(20)).toBe(null);
});
});
describe('#remove', () => {
it('should remove a left leaf node', () => {
expect(n4.left).toBe(n3);
bst.remove(3);
expect(n4.left).toBe(null);
expect(bst.size).toBe(6);
});
it('should remove a right leaf node', () => {
expect(n30.right).toBe(n40);
bst.remove(40);
expect(n30.right).toBe(null);
expect(bst.size).toBe(6);
});
it('should remove a child with one descent on the left', () => {
expect(n3.toValues()).toMatchObject({
value: 3, left: null, right: null, parent: 4,
});
bst.remove(4);
expect(n3.toValues()).toMatchObject({
value: 3, left: null, right: null, parent: 5,
});
});
it('should remove a child with one descent on the right', () => {
bst.remove(40);
expect(n15.toValues()).toMatchObject({
value: 15, left: null, right: null, parent: 30,
});
bst.remove(30);
expect(n15.toValues()).toMatchObject({
value: 15, left: null, right: null, parent: 10,
});
});
it('should remove a parent with two descents on the right', () => {
expect(root.left.value).toBe(5);
expect(root.right.value).toBe(30);
bst.remove(30);
expect(root.left.value).toBe(5);
expect(root.right.value).toBe(40);
expect(bst.size).toBe(6);
});
it('should remove a parent with two descents on the left', () => {
bst.add(4.5);
expect(n5.left.value).toBe(4);
expect(n5.left.right.value).toBe(4.5);
bst.remove(4);
expect(n5.left.value).toBe(4.5);
expect(bst.size).toBe(7);
});
it('should return false when it does not exist', () => {
expect(bst.remove(4.5)).toBe(false);
expect(bst.size).toBe(7);
});
it('should remove the root', () => {
expect(n30.parent).toBe(root);
expect(n5.parent).toBe(root);
bst.remove(10);
expect(n30.parent).toBe(null);
expect(n5.parent.value).toBe(15);
expect(bst.toArray()).toEqual([
30,
15, 40,
5, null, null, null,
4, null,
3, null,
null, null]);
expect(bst.size).toBe(6);
});
it('should remove duplicates', () => {
bst.add(40); // add duplicate
expect(n40.meta.multiplicity).toBe(2);
expect(bst.remove(40)).toBe(true);
expect(bst.size).toBe(7);
expect(n40.meta.multiplicity).toBe(1);
expect(bst.find(40)).toBe(n40);
expect(bst.remove(40)).toBe(true);
expect(bst.size).toBe(6);
expect(bst.find(40)).toBeFalsy();
expect(bst.remove(40)).toBe(false);
expect(bst.size).toBe(6);
});
});
describe('#bfs', () => {
it('should visit nodes on BFS order using iterator', () => {
const bfs = bst.bfs();
expect(bfs.next().value).toBe(root); // 10
expect(bfs.next().value).toBe(n5);
expect(bfs.next().value.value).toBe(30);
expect(bfs.next().value.value).toBe(4);
expect(bfs.next().value.value).toBe(15);
expect(bfs.next()).toMatchObject({ value: { value: 40 }, done: false });
expect(bfs.next()).toMatchObject({ value: { value: 3 }, done: false });
expect(bfs.next().done).toBe(true);
});
it('should generate an array from bfs', () => {
expect(getValues(bst.bfs())).toEqual([10, 5, 30, 4, 15, 40, 3]);
});
});
describe('#dfs', () => {
it('should visit nodes on dfs order using iterator', () => {
const dfs = bst.dfs();
expect(dfs.next().value).toBe(root); // 10
expect(dfs.next().value).toBe(n5);
expect(dfs.next().value.value).toBe(4);
expect(dfs.next().value.value).toBe(3);
expect(dfs.next().value.value).toBe(30);
expect(dfs.next().value.value).toBe(15);
expect(dfs.next()).toMatchObject({ value: { value: 40 }, done: false });
expect(dfs.next().done).toBe(true);
});
it('should generate an array from dfs', () => {
const nodes = Array.from(bst.dfs());
const values = nodes.map(node => node.value);
expect(values).toEqual([10, 5, 4, 3, 30, 15, 40]);
});
});
describe('#inOrderTraversal', () => {
it('should generate an array', () => {
expect(getValues(bst.inOrderTraversal())).toEqual([3, 4, 5, 10, 15, 30, 40]);
});
});
describe('#preOrderTraversal', () => {
it('should generate an array from preOrderTraversal', () => {
const nodes = Array.from(bst.preOrderTraversal());
const values = nodes.map(node => node.value);
expect(values).toEqual([10, 5, 4, 3, 30, 15, 40]);
});
});
describe('#postOrderTraversal', () => {
it('should generate an array from postOrderTraversal', () => {
const nodes = Array.from(bst.postOrderTraversal());
const values = nodes.map(node => node.value);
expect(values).toEqual([3, 4, 5, 15, 40, 30, 10]);
});
});
describe('#toArray', () => {
it('should serialize the tree as an array', () => {
expect(bst.toArray()).toEqual([10, 5, 30, 4, null, 15, 40, 3,
null, null, null, null, null, null, null]);
});
});
describe('#getMax', () => {
it('should get the maximun value', () => {
expect(bst.getMax().value).toBe(40);
});
it('should get the maximun value of a subtree', () => {
expect(bst.getMax(n4).value).toBe(4);
});
it('should work with empty BST', () => {
bst = new BinarySearchTree();
expect(bst.getMax()).toBe(null);
});
});
describe('#getMin', () => {
it('should get the maximun value', () => {
expect(bst.getMin().value).toBe(3);
});
it('should get the maximun value of a subtree', () => {
expect(bst.getMin(n30).value).toBe(15);
});
it('should work with empty BST', () => {
bst = new BinarySearchTree();
expect(bst.getMin()).toBe(null);
});
});
});
});