data-structure-typed 1.41.1 → 1.41.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/benchmark/report.json +30 -0
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +9 -9
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +2 -5
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +17 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +87 -20
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +9 -9
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -4
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +17 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +87 -20
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/jest.integration.config.js +5 -0
- package/package.json +10 -6
- package/src/data-structures/binary-tree/binary-tree.ts +13 -13
- package/src/data-structures/binary-tree/rb-tree.ts +94 -20
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +7 -7
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/binary-index-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/bst.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/segment-tree.test.ts +0 -0
- package/test/performance/data-structures/binary-tree/tree-multiset.test.ts +0 -0
- package/test/performance/data-structures/graph/abstract-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/map-graph.test.ts +0 -0
- package/test/performance/data-structures/graph/overall.test.ts +0 -0
- package/test/performance/data-structures/graph/undirected-graph.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-map.test.ts +0 -0
- package/test/performance/data-structures/hash/coordinate-set.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-map.test.ts +0 -0
- package/test/performance/data-structures/hash/hash-table.test.ts +0 -0
- package/test/performance/data-structures/heap/heap.test.ts +0 -0
- package/test/performance/data-structures/heap/max-heap.test.ts +0 -0
- package/test/performance/data-structures/heap/min-heap.test.ts +0 -0
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/skip-linked-list.test.ts +0 -0
- package/test/performance/data-structures/linked-list/skip-list.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/navigator.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/min-priority-queue.test.ts +0 -0
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +0 -0
- package/test/performance/data-structures/queue/deque.test.ts +19 -0
- package/test/performance/data-structures/queue/queue.test.ts +20 -0
- package/test/performance/data-structures/stack/stack.test.ts +0 -0
- package/test/performance/data-structures/tree/tree.test.ts +0 -0
- package/test/performance/data-structures/trie/trie.test.ts +0 -0
- package/test/performance/index.ts +47 -0
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +175 -18
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {RBTreeNode, RedBlackTree,
|
|
1
|
+
import {RBTNColor, RBTreeNode, RedBlackTree, NIL} from '../../../../src';
|
|
2
2
|
import {getRandomInt} from '../../../utils';
|
|
3
|
+
import {isDebugTest} from '../../../config';
|
|
4
|
+
|
|
5
|
+
const isDebug = isDebugTest;
|
|
3
6
|
|
|
4
7
|
describe('RedBlackTree', () => {
|
|
5
8
|
let tree: RedBlackTree;
|
|
@@ -63,7 +66,7 @@ describe('RedBlackTree', () => {
|
|
|
63
66
|
|
|
64
67
|
test('should handle an empty tree', () => {
|
|
65
68
|
const minNode = tree.getLeftMost(tree.root);
|
|
66
|
-
expect(minNode).toBe(
|
|
69
|
+
expect(minNode).toBe(NIL);
|
|
67
70
|
});
|
|
68
71
|
});
|
|
69
72
|
|
|
@@ -81,7 +84,7 @@ describe('RedBlackTree', () => {
|
|
|
81
84
|
|
|
82
85
|
test('should handle an empty tree', () => {
|
|
83
86
|
const maxNode = tree.getRightMost(tree.root);
|
|
84
|
-
expect(maxNode).toBe(
|
|
87
|
+
expect(maxNode).toBe(NIL);
|
|
85
88
|
});
|
|
86
89
|
});
|
|
87
90
|
|
|
@@ -105,7 +108,7 @@ describe('RedBlackTree', () => {
|
|
|
105
108
|
|
|
106
109
|
const node = tree.getNode(10);
|
|
107
110
|
const successorNode = tree.getSuccessor(node);
|
|
108
|
-
// TODO not sure if it should be null or
|
|
111
|
+
// TODO not sure if it should be null or NIL
|
|
109
112
|
expect(successorNode).toBe(null);
|
|
110
113
|
});
|
|
111
114
|
});
|
|
@@ -130,7 +133,7 @@ describe('RedBlackTree', () => {
|
|
|
130
133
|
|
|
131
134
|
const node = tree.getNode(20);
|
|
132
135
|
const predecessorNode = tree.getPredecessor(node);
|
|
133
|
-
// TODO not sure if it should be
|
|
136
|
+
// TODO not sure if it should be NIL or something else.
|
|
134
137
|
expect(predecessorNode).toBe(tree.getNode(10));
|
|
135
138
|
});
|
|
136
139
|
});
|
|
@@ -198,14 +201,146 @@ describe('RedBlackTree', () => {
|
|
|
198
201
|
expect(node.right.key).toBe(25);
|
|
199
202
|
});
|
|
200
203
|
|
|
201
|
-
it('should
|
|
204
|
+
it('should all node attributes fully conform to the red-black tree standards.', () => {
|
|
202
205
|
tree.insert(10);
|
|
203
206
|
tree.insert(20);
|
|
204
207
|
tree.insert(5);
|
|
205
208
|
tree.insert(15);
|
|
206
|
-
tree.
|
|
207
|
-
|
|
208
|
-
|
|
209
|
+
tree.insert(21);
|
|
210
|
+
tree.insert(6);
|
|
211
|
+
tree.insert(2);
|
|
212
|
+
|
|
213
|
+
let node10F = tree.getNode(10);
|
|
214
|
+
let node20F = tree.getNode(20);
|
|
215
|
+
let node5F = tree.getNode(5);
|
|
216
|
+
let node15F = tree.getNode(15);
|
|
217
|
+
let node21F = tree.getNode(21);
|
|
218
|
+
let node6F = tree.getNode(6);
|
|
219
|
+
let node2F = tree.getNode(2);
|
|
220
|
+
expect(node10F.key).toBe(10);
|
|
221
|
+
expect(node10F.color).toBe(RBTNColor.BLACK);
|
|
222
|
+
expect(node10F.left).toBe(node5F);
|
|
223
|
+
expect(node10F.right).toBe(node20F);
|
|
224
|
+
expect(node10F.parent).toBe(null);
|
|
225
|
+
expect(node20F.key).toBe(20);
|
|
226
|
+
expect(node20F.color).toBe(RBTNColor.BLACK);
|
|
227
|
+
expect(node20F.left).toBe(node15F);
|
|
228
|
+
expect(node20F.right).toBe(node21F);
|
|
229
|
+
expect(node20F.parent).toBe(node10F);
|
|
230
|
+
expect(node5F.key).toBe(5);
|
|
231
|
+
expect(node5F.color).toBe(RBTNColor.BLACK);
|
|
232
|
+
expect(node5F.left).toBe(node2F);
|
|
233
|
+
expect(node5F.right).toBe(node6F);
|
|
234
|
+
expect(node5F.parent).toBe(node10F);
|
|
235
|
+
expect(node15F.key).toBe(15);
|
|
236
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
237
|
+
expect(node15F.left).toBe(NIL);
|
|
238
|
+
expect(node15F.right).toBe(NIL);
|
|
239
|
+
expect(node15F.parent).toBe(node20F);
|
|
240
|
+
expect(node21F.key).toBe(21);
|
|
241
|
+
expect(node21F.color).toBe(RBTNColor.RED);
|
|
242
|
+
expect(node21F.left).toBe(NIL);
|
|
243
|
+
expect(node21F.right).toBe(NIL);
|
|
244
|
+
expect(node21F.parent).toBe(node20F);
|
|
245
|
+
expect(node6F.key).toBe(6);
|
|
246
|
+
expect(node6F.color).toBe(RBTNColor.RED);
|
|
247
|
+
expect(node6F.left).toBe(NIL);
|
|
248
|
+
expect(node6F.right).toBe(NIL);
|
|
249
|
+
expect(node6F.parent).toBe(node5F);
|
|
250
|
+
expect(node2F.key).toBe(2);
|
|
251
|
+
expect(node2F.color).toBe(RBTNColor.RED);
|
|
252
|
+
expect(node2F.left).toBe(NIL);
|
|
253
|
+
expect(node2F.right).toBe(NIL);
|
|
254
|
+
expect(node2F.parent).toBe(node5F);
|
|
255
|
+
expect(node15F.key).toBe(15);
|
|
256
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
257
|
+
expect(node15F.left).toBe(NIL);
|
|
258
|
+
expect(node15F.right).toBe(NIL);
|
|
259
|
+
expect(node15F.parent).toBe(node20F);
|
|
260
|
+
tree.delete(5);
|
|
261
|
+
node10F = tree.getNode(10);
|
|
262
|
+
node20F = tree.getNode(20);
|
|
263
|
+
node5F = tree.getNode(5);
|
|
264
|
+
node15F = tree.getNode(15);
|
|
265
|
+
node21F = tree.getNode(21);
|
|
266
|
+
node6F = tree.getNode(6);
|
|
267
|
+
node2F = tree.getNode(2);
|
|
268
|
+
expect(node10F.key).toBe(10);
|
|
269
|
+
expect(node10F.color).toBe(RBTNColor.BLACK);
|
|
270
|
+
expect(node10F.left).toBe(node6F);
|
|
271
|
+
expect(node10F.right).toBe(node20F);
|
|
272
|
+
expect(node10F.parent).toBe(null);
|
|
273
|
+
expect(node20F.key).toBe(20);
|
|
274
|
+
expect(node20F.color).toBe(RBTNColor.BLACK);
|
|
275
|
+
expect(node20F.left).toBe(node15F);
|
|
276
|
+
expect(node20F.right).toBe(node21F);
|
|
277
|
+
expect(node20F.parent).toBe(node10F);
|
|
278
|
+
expect(node5F).toBe(null);
|
|
279
|
+
expect(node15F.key).toBe(15);
|
|
280
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
281
|
+
expect(node15F.left).toBe(NIL);
|
|
282
|
+
expect(node15F.right).toBe(NIL);
|
|
283
|
+
expect(node15F.parent).toBe(node20F);
|
|
284
|
+
expect(node21F.key).toBe(21);
|
|
285
|
+
expect(node21F.color).toBe(RBTNColor.RED);
|
|
286
|
+
expect(node21F.left).toBe(NIL);
|
|
287
|
+
expect(node21F.right).toBe(NIL);
|
|
288
|
+
expect(node21F.parent).toBe(node20F);
|
|
289
|
+
expect(node6F.key).toBe(6);
|
|
290
|
+
expect(node6F.color).toBe(RBTNColor.BLACK);
|
|
291
|
+
expect(node6F.left).toBe(node2F);
|
|
292
|
+
expect(node6F.right).toBe(NIL);
|
|
293
|
+
expect(node6F.parent).toBe(node10F);
|
|
294
|
+
expect(node2F.key).toBe(2);
|
|
295
|
+
expect(node2F.color).toBe(RBTNColor.RED);
|
|
296
|
+
expect(node2F.left).toBe(NIL);
|
|
297
|
+
expect(node2F.right).toBe(NIL);
|
|
298
|
+
expect(node2F.parent).toBe(node6F);
|
|
299
|
+
expect(node15F.key).toBe(15);
|
|
300
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
301
|
+
expect(node15F.left).toBe(NIL);
|
|
302
|
+
expect(node15F.right).toBe(NIL);
|
|
303
|
+
expect(node15F.parent).toBe(node20F);
|
|
304
|
+
tree.delete(20);
|
|
305
|
+
node10F = tree.getNode(10);
|
|
306
|
+
node20F = tree.getNode(20);
|
|
307
|
+
node5F = tree.getNode(5);
|
|
308
|
+
node15F = tree.getNode(15);
|
|
309
|
+
node21F = tree.getNode(21);
|
|
310
|
+
node6F = tree.getNode(6);
|
|
311
|
+
node2F = tree.getNode(2);
|
|
312
|
+
expect(node10F.key).toBe(10);
|
|
313
|
+
expect(node10F.color).toBe(RBTNColor.BLACK);
|
|
314
|
+
expect(node10F.left).toBe(node6F);
|
|
315
|
+
expect(node10F.right).toBe(node21F);
|
|
316
|
+
expect(node10F.parent).toBe(null);
|
|
317
|
+
expect(node20F).toBe(null);
|
|
318
|
+
expect(node5F).toBe(null);
|
|
319
|
+
expect(node15F.key).toBe(15);
|
|
320
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
321
|
+
expect(node15F.left).toBe(NIL);
|
|
322
|
+
expect(node15F.right).toBe(NIL);
|
|
323
|
+
expect(node15F.parent).toBe(node21F);
|
|
324
|
+
expect(node21F.key).toBe(21);
|
|
325
|
+
expect(node21F.color).toBe(RBTNColor.BLACK);
|
|
326
|
+
expect(node21F.left).toBe(node15F);
|
|
327
|
+
expect(node21F.right).toBe(NIL);
|
|
328
|
+
expect(node21F.parent).toBe(node10F);
|
|
329
|
+
expect(node6F.key).toBe(6);
|
|
330
|
+
expect(node6F.color).toBe(RBTNColor.BLACK);
|
|
331
|
+
expect(node6F.left).toBe(node2F);
|
|
332
|
+
expect(node6F.right).toBe(NIL);
|
|
333
|
+
expect(node6F.parent).toBe(node10F);
|
|
334
|
+
expect(node2F.key).toBe(2);
|
|
335
|
+
expect(node2F.color).toBe(RBTNColor.RED);
|
|
336
|
+
expect(node2F.left).toBe(NIL);
|
|
337
|
+
expect(node2F.right).toBe(NIL);
|
|
338
|
+
expect(node2F.parent).toBe(node6F);
|
|
339
|
+
expect(node15F.key).toBe(15);
|
|
340
|
+
expect(node15F.color).toBe(RBTNColor.RED);
|
|
341
|
+
expect(node15F.left).toBe(NIL);
|
|
342
|
+
expect(node15F.right).toBe(NIL);
|
|
343
|
+
expect(node15F.parent).toBe(node21F);
|
|
209
344
|
});
|
|
210
345
|
|
|
211
346
|
it('should fix the tree after insertion', () => {
|
|
@@ -214,8 +349,8 @@ describe('RedBlackTree', () => {
|
|
|
214
349
|
tree.insert(5);
|
|
215
350
|
tree.insert(15);
|
|
216
351
|
const node15F = tree.getNode(15);
|
|
217
|
-
expect(node15F.left).toBe(
|
|
218
|
-
expect(node15F.right).toBe(
|
|
352
|
+
expect(node15F.left).toBe(NIL);
|
|
353
|
+
expect(node15F.right).toBe(NIL);
|
|
219
354
|
expect(node15F.parent).toBe(tree.getNode(5));
|
|
220
355
|
|
|
221
356
|
tree.insert(25);
|
|
@@ -230,8 +365,8 @@ describe('RedBlackTree', () => {
|
|
|
230
365
|
tree.insert(155);
|
|
231
366
|
tree.insert(225);
|
|
232
367
|
const node225F = tree.getNode(225);
|
|
233
|
-
expect(node225F.left).toBe(
|
|
234
|
-
expect(node225F.right).toBe(
|
|
368
|
+
expect(node225F.left).toBe(NIL);
|
|
369
|
+
expect(node225F.right).toBe(NIL);
|
|
235
370
|
expect(node225F.parent.key).toBe(155);
|
|
236
371
|
tree.insert(7);
|
|
237
372
|
|
|
@@ -257,20 +392,42 @@ describe('RedBlackTree', () => {
|
|
|
257
392
|
const node50 = tree.getNode(50);
|
|
258
393
|
expect(node50.key).toBe(50);
|
|
259
394
|
expect(node50.left.key).toBe(33);
|
|
260
|
-
expect(node50.right).toBe(
|
|
395
|
+
expect(node50.right).toBe(NIL);
|
|
261
396
|
const node15Fo = tree.getNode(15);
|
|
262
397
|
|
|
263
398
|
expect(node15Fo.key).toBe(15);
|
|
264
|
-
expect(node15Fo.left).toBe(
|
|
399
|
+
expect(node15Fo.left).toBe(NIL);
|
|
265
400
|
const node225S = tree.getNode(225);
|
|
266
|
-
expect(node225S.left).toBe(
|
|
267
|
-
expect(node225S.right).toBe(
|
|
401
|
+
expect(node225S.left).toBe(NIL);
|
|
402
|
+
expect(node225S.right).toBe(NIL);
|
|
268
403
|
expect(node225S.parent.key).toBe(155);
|
|
269
404
|
expect(tree.getNode(0)).toBe(null);
|
|
405
|
+
tree.insert(1);
|
|
406
|
+
tree.insert(2);
|
|
407
|
+
tree.insert(3);
|
|
408
|
+
tree.insert(4);
|
|
409
|
+
tree.insert(5);
|
|
410
|
+
tree.insert(6);
|
|
411
|
+
tree.insert(7);
|
|
412
|
+
tree.insert(8);
|
|
413
|
+
tree.insert(9);
|
|
414
|
+
tree.insert(10);
|
|
415
|
+
tree.insert(11);
|
|
416
|
+
tree.insert(12);
|
|
417
|
+
tree.insert(13);
|
|
418
|
+
tree.insert(14);
|
|
419
|
+
tree.insert(15);
|
|
420
|
+
tree.insert(16);
|
|
421
|
+
tree.insert(17);
|
|
422
|
+
tree.insert(18);
|
|
423
|
+
tree.insert(19);
|
|
424
|
+
tree.insert(110);
|
|
425
|
+
|
|
426
|
+
isDebug && tree.print();
|
|
270
427
|
});
|
|
271
428
|
|
|
272
429
|
it('should fix the tree after insertion and deletion', () => {
|
|
273
|
-
for (let i = 0; i <
|
|
430
|
+
for (let i = 0; i < 100; i++) {
|
|
274
431
|
tree.insert(getRandomInt(-100, 1000));
|
|
275
432
|
tree.delete(getRandomInt(-100, 1000));
|
|
276
433
|
}
|