data-structure-typed 1.41.0 → 1.41.2
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/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +29 -10
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +109 -44
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
- package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +9 -6
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +28 -10
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +20 -4
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +110 -45
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +9 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +73 -25
- package/src/data-structures/binary-tree/bst.ts +7 -4
- package/src/data-structures/binary-tree/rb-tree.ts +121 -68
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +2 -2
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +11 -11
- package/test/unit/data-structures/binary-tree/bst.test.ts +22 -20
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +214 -21
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +8 -8
- package/test/utils/big-o.js +10 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { RedBlackTree,
|
|
2
|
-
import {getRandomInt} from
|
|
1
|
+
import {RBTNColor, RBTreeNode, RedBlackTree, NIL} from '../../../../src';
|
|
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;
|
|
@@ -17,7 +20,7 @@ describe('RedBlackTree', () => {
|
|
|
17
20
|
expect(tree.getNode(10)).toBeInstanceOf(RBTreeNode);
|
|
18
21
|
expect(tree.getNode(20)).toBeInstanceOf(RBTreeNode);
|
|
19
22
|
expect(tree.getNode(5)).toBeInstanceOf(RBTreeNode);
|
|
20
|
-
expect(tree.getNode(15)).toBe(
|
|
23
|
+
expect(tree.getNode(15)).toBe(null);
|
|
21
24
|
});
|
|
22
25
|
|
|
23
26
|
test('should insert and find nodes with negative keys', () => {
|
|
@@ -36,7 +39,7 @@ describe('RedBlackTree', () => {
|
|
|
36
39
|
tree.insert(5);
|
|
37
40
|
tree.delete(20);
|
|
38
41
|
|
|
39
|
-
expect(tree.getNode(20)).toBe(
|
|
42
|
+
expect(tree.getNode(20)).toBe(null);
|
|
40
43
|
});
|
|
41
44
|
|
|
42
45
|
test('should handle deleting a non-existent node', () => {
|
|
@@ -45,7 +48,7 @@ describe('RedBlackTree', () => {
|
|
|
45
48
|
tree.insert(5);
|
|
46
49
|
tree.delete(15);
|
|
47
50
|
|
|
48
|
-
expect(tree.getNode(15)).toBe(
|
|
51
|
+
expect(tree.getNode(15)).toBe(null);
|
|
49
52
|
});
|
|
50
53
|
});
|
|
51
54
|
|
|
@@ -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,13 +133,12 @@ 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
|
});
|
|
137
140
|
});
|
|
138
141
|
|
|
139
|
-
|
|
140
142
|
describe('RedBlackTree', () => {
|
|
141
143
|
let tree: RedBlackTree;
|
|
142
144
|
|
|
@@ -158,7 +160,7 @@ describe('RedBlackTree', () => {
|
|
|
158
160
|
tree.insert(20);
|
|
159
161
|
tree.insert(5);
|
|
160
162
|
tree.delete(20);
|
|
161
|
-
expect(tree.getNode(20)).toBe(
|
|
163
|
+
expect(tree.getNode(20)).toBe(null);
|
|
162
164
|
});
|
|
163
165
|
|
|
164
166
|
it('should get the successor of a node', () => {
|
|
@@ -199,25 +201,158 @@ describe('RedBlackTree', () => {
|
|
|
199
201
|
expect(node.right.key).toBe(25);
|
|
200
202
|
});
|
|
201
203
|
|
|
202
|
-
it('should
|
|
204
|
+
it('should all node attributes fully conform to the red-black tree standards.', () => {
|
|
203
205
|
tree.insert(10);
|
|
204
206
|
tree.insert(20);
|
|
205
207
|
tree.insert(5);
|
|
206
208
|
tree.insert(15);
|
|
207
|
-
tree.
|
|
208
|
-
|
|
209
|
-
|
|
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);
|
|
210
344
|
});
|
|
211
345
|
|
|
212
346
|
it('should fix the tree after insertion', () => {
|
|
213
|
-
for (let i = 0; i < 1000; i++) {
|
|
214
|
-
tree.insert(getRandomInt(-100, 1000));
|
|
215
|
-
tree.delete(getRandomInt(-100, 1000));
|
|
216
|
-
}
|
|
217
347
|
tree.insert(1);
|
|
218
348
|
tree.insert(2);
|
|
219
349
|
tree.insert(5);
|
|
220
350
|
tree.insert(15);
|
|
351
|
+
const node15F = tree.getNode(15);
|
|
352
|
+
expect(node15F.left).toBe(NIL);
|
|
353
|
+
expect(node15F.right).toBe(NIL);
|
|
354
|
+
expect(node15F.parent).toBe(tree.getNode(5));
|
|
355
|
+
|
|
221
356
|
tree.insert(25);
|
|
222
357
|
tree.insert(10);
|
|
223
358
|
tree.insert(8);
|
|
@@ -229,14 +364,72 @@ describe('RedBlackTree', () => {
|
|
|
229
364
|
tree.insert(50);
|
|
230
365
|
tree.insert(155);
|
|
231
366
|
tree.insert(225);
|
|
367
|
+
const node225F = tree.getNode(225);
|
|
368
|
+
expect(node225F.left).toBe(NIL);
|
|
369
|
+
expect(node225F.right).toBe(NIL);
|
|
370
|
+
expect(node225F.parent.key).toBe(155);
|
|
232
371
|
tree.insert(7);
|
|
372
|
+
|
|
373
|
+
const node15S = tree.getNode(15);
|
|
374
|
+
expect(node15S.left.key).toBe(8);
|
|
375
|
+
expect(node15S.right.key).toBe(28);
|
|
376
|
+
expect(node15S).toBe(tree.root);
|
|
377
|
+
expect(node15S.parent).toBe(null);
|
|
233
378
|
tree.delete(15);
|
|
379
|
+
expect(tree.root.key).toBe(22);
|
|
380
|
+
expect(tree.root.parent).toBe(null);
|
|
381
|
+
|
|
382
|
+
const node15T = tree.getNode(15);
|
|
383
|
+
expect(node15T).toBe(null);
|
|
384
|
+
|
|
234
385
|
tree.insert(23);
|
|
235
386
|
tree.insert(33);
|
|
236
387
|
tree.insert(15);
|
|
237
388
|
|
|
389
|
+
const nodeLM = tree.getLeftMost();
|
|
390
|
+
expect(nodeLM.key).toBe(1);
|
|
391
|
+
|
|
392
|
+
const node50 = tree.getNode(50);
|
|
393
|
+
expect(node50.key).toBe(50);
|
|
394
|
+
expect(node50.left.key).toBe(33);
|
|
395
|
+
expect(node50.right).toBe(NIL);
|
|
396
|
+
const node15Fo = tree.getNode(15);
|
|
397
|
+
|
|
398
|
+
expect(node15Fo.key).toBe(15);
|
|
399
|
+
expect(node15Fo.left).toBe(NIL);
|
|
400
|
+
const node225S = tree.getNode(225);
|
|
401
|
+
expect(node225S.left).toBe(NIL);
|
|
402
|
+
expect(node225S.right).toBe(NIL);
|
|
403
|
+
expect(node225S.parent.key).toBe(155);
|
|
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();
|
|
427
|
+
});
|
|
238
428
|
|
|
239
|
-
|
|
240
|
-
|
|
429
|
+
it('should fix the tree after insertion and deletion', () => {
|
|
430
|
+
for (let i = 0; i < 1000; i++) {
|
|
431
|
+
tree.insert(getRandomInt(-100, 1000));
|
|
432
|
+
tree.delete(getRandomInt(-100, 1000));
|
|
433
|
+
}
|
|
241
434
|
});
|
|
242
435
|
});
|
|
@@ -23,10 +23,10 @@ describe('TreeMultiset operations test', () => {
|
|
|
23
23
|
|
|
24
24
|
expect(treeMultiset.getHeight(6)).toBe(3);
|
|
25
25
|
expect(treeMultiset.getDepth(6)).toBe(1);
|
|
26
|
-
const nodeId10 = treeMultiset.
|
|
26
|
+
const nodeId10 = treeMultiset.getNode(10);
|
|
27
27
|
expect(nodeId10?.key).toBe(10);
|
|
28
28
|
|
|
29
|
-
const nodeVal9 = treeMultiset.
|
|
29
|
+
const nodeVal9 = treeMultiset.getNode(9, node => node.value);
|
|
30
30
|
expect(nodeVal9?.key).toBe(9);
|
|
31
31
|
|
|
32
32
|
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
|
@@ -37,7 +37,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
37
37
|
const leftMost = treeMultiset.getLeftMost();
|
|
38
38
|
expect(leftMost?.key).toBe(1);
|
|
39
39
|
|
|
40
|
-
const node15 = treeMultiset.
|
|
40
|
+
const node15 = treeMultiset.getNode(15);
|
|
41
41
|
const minNodeBySpecificNode = node15 && treeMultiset.getLeftMost(node15);
|
|
42
42
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
43
43
|
|
|
@@ -53,7 +53,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
53
53
|
const subTreeAdd = treeMultiset.subTreeTraverse((node: TreeMultisetNode<number>) => (node.count += 1), 15);
|
|
54
54
|
expect(subTreeAdd);
|
|
55
55
|
}
|
|
56
|
-
const node11 = treeMultiset.
|
|
56
|
+
const node11 = treeMultiset.getNode(11);
|
|
57
57
|
expect(node11 instanceof TreeMultisetNode);
|
|
58
58
|
if (node11 instanceof TreeMultisetNode) {
|
|
59
59
|
const allGreaterNodesAdded = treeMultiset.lesserOrGreaterTraverse(node => (node.count += 2), CP.gt, 11);
|
|
@@ -263,10 +263,10 @@ describe('TreeMultiset operations test recursively', () => {
|
|
|
263
263
|
|
|
264
264
|
expect(treeMultiset.getHeight(6)).toBe(3);
|
|
265
265
|
expect(treeMultiset.getDepth(6)).toBe(1);
|
|
266
|
-
const nodeId10 = treeMultiset.
|
|
266
|
+
const nodeId10 = treeMultiset.getNode(10);
|
|
267
267
|
expect(nodeId10?.key).toBe(10);
|
|
268
268
|
|
|
269
|
-
const nodeVal9 = treeMultiset.
|
|
269
|
+
const nodeVal9 = treeMultiset.getNode(9, node => node.value);
|
|
270
270
|
expect(nodeVal9?.key).toBe(9);
|
|
271
271
|
|
|
272
272
|
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
|
@@ -277,7 +277,7 @@ describe('TreeMultiset operations test recursively', () => {
|
|
|
277
277
|
const leftMost = treeMultiset.getLeftMost();
|
|
278
278
|
expect(leftMost?.key).toBe(1);
|
|
279
279
|
|
|
280
|
-
const node15 = treeMultiset.
|
|
280
|
+
const node15 = treeMultiset.getNode(15);
|
|
281
281
|
const minNodeBySpecificNode = node15 && treeMultiset.getLeftMost(node15);
|
|
282
282
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
283
283
|
|
|
@@ -293,7 +293,7 @@ describe('TreeMultiset operations test recursively', () => {
|
|
|
293
293
|
const subTreeAdd = treeMultiset.subTreeTraverse((node: TreeMultisetNode<number>) => (node.count += 1), 15);
|
|
294
294
|
expect(subTreeAdd);
|
|
295
295
|
}
|
|
296
|
-
const node11 = treeMultiset.
|
|
296
|
+
const node11 = treeMultiset.getNode(11);
|
|
297
297
|
expect(node11 instanceof TreeMultisetNode);
|
|
298
298
|
if (node11 instanceof TreeMultisetNode) {
|
|
299
299
|
const allGreaterNodesAdded = treeMultiset.lesserOrGreaterTraverse(node => (node.count += 2), CP.gt, 11);
|
package/test/utils/big-o.js
CHANGED
|
@@ -23,9 +23,11 @@ exports.bigO = {
|
|
|
23
23
|
CUBED: Math.pow(exports.magnitude.SQUARED, 3) / 1000,
|
|
24
24
|
FACTORIAL: 10000
|
|
25
25
|
};
|
|
26
|
+
|
|
26
27
|
function findPotentialN(input) {
|
|
27
28
|
var longestArray = [];
|
|
28
29
|
var mostProperties = {};
|
|
30
|
+
|
|
29
31
|
function recurse(obj) {
|
|
30
32
|
if (Array.isArray(obj)) {
|
|
31
33
|
if (obj.length > longestArray.length) {
|
|
@@ -41,6 +43,7 @@ function findPotentialN(input) {
|
|
|
41
43
|
});
|
|
42
44
|
}
|
|
43
45
|
}
|
|
46
|
+
|
|
44
47
|
if (Array.isArray(input)) {
|
|
45
48
|
input.forEach(function (item) {
|
|
46
49
|
recurse(item);
|
|
@@ -51,6 +54,7 @@ function findPotentialN(input) {
|
|
|
51
54
|
// return [longestArray, mostProperties] : [any[], { [key: string]: any }];
|
|
52
55
|
return Math.max(longestArray.length, Object.keys(mostProperties).length);
|
|
53
56
|
}
|
|
57
|
+
|
|
54
58
|
function linearRegression(x, y) {
|
|
55
59
|
var n = x.length;
|
|
56
60
|
var sumX = x.reduce(function (acc, val) {
|
|
@@ -87,6 +91,7 @@ function linearRegression(x, y) {
|
|
|
87
91
|
var rSquared = 1 - totalVariation / explainedVariation;
|
|
88
92
|
return {slope: slope, intercept: intercept, rSquared: rSquared};
|
|
89
93
|
}
|
|
94
|
+
|
|
90
95
|
function estimateBigO(runtimes, dataSizes) {
|
|
91
96
|
// Make sure the input runtimes and data sizes have the same length
|
|
92
97
|
if (runtimes.length !== dataSizes.length) {
|
|
@@ -137,7 +142,9 @@ function estimateBigO(runtimes, dataSizes) {
|
|
|
137
142
|
return complexities.join(' or ');
|
|
138
143
|
}
|
|
139
144
|
}
|
|
145
|
+
|
|
140
146
|
var methodLogs = new Map();
|
|
147
|
+
|
|
141
148
|
function logBigOMetricsWrap(fn, args, fnName) {
|
|
142
149
|
var startTime = performance.now();
|
|
143
150
|
var result = fn(args);
|
|
@@ -169,7 +176,9 @@ function logBigOMetricsWrap(fn, args, fnName) {
|
|
|
169
176
|
}
|
|
170
177
|
return result;
|
|
171
178
|
}
|
|
179
|
+
|
|
172
180
|
exports.logBigOMetricsWrap = logBigOMetricsWrap;
|
|
181
|
+
|
|
173
182
|
function logBigOMetrics(target, propertyKey, descriptor) {
|
|
174
183
|
var originalMethod = descriptor.value;
|
|
175
184
|
descriptor.value = function () {
|
|
@@ -209,4 +218,5 @@ function logBigOMetrics(target, propertyKey, descriptor) {
|
|
|
209
218
|
};
|
|
210
219
|
return descriptor;
|
|
211
220
|
}
|
|
221
|
+
|
|
212
222
|
exports.logBigOMetrics = logBigOMetrics;
|