data-structure-typed 2.2.7 → 2.2.8
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/README.md +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +106 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +106 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +106 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +106 -107
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +102 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -14,9 +14,9 @@ describe('TreeMultiMap 1', () => {
|
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
it('Should add and delete values', () => {
|
|
17
|
-
tmm.
|
|
18
|
-
tmm.
|
|
19
|
-
tmm.
|
|
17
|
+
tmm.set(3, 3);
|
|
18
|
+
tmm.set(3, 33);
|
|
19
|
+
tmm.set(3, 333);
|
|
20
20
|
expect(tmm.get(3)).toEqual([3, 33, 333]);
|
|
21
21
|
tmm.deleteValue(3, 33);
|
|
22
22
|
expect(tmm.get(3)).toEqual([3, 333]);
|
|
@@ -24,16 +24,16 @@ describe('TreeMultiMap 1', () => {
|
|
|
24
24
|
expect(tmm.get(3)).toEqual([333]);
|
|
25
25
|
tmm.deleteValue(3, 333);
|
|
26
26
|
expect(tmm.get(3)).toBe(undefined);
|
|
27
|
-
tmm.
|
|
28
|
-
tmm.
|
|
27
|
+
tmm.set(3, 3);
|
|
28
|
+
tmm.set([3, [3333, 33333]]);
|
|
29
29
|
expect(tmm.get(3)).toEqual([3, 3333, 33333]);
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
describe('add and getNode', () => {
|
|
33
33
|
it('should add and find a node in the tmm', () => {
|
|
34
|
-
tmm.
|
|
35
|
-
tmm.
|
|
36
|
-
tmm.
|
|
34
|
+
tmm.set(10);
|
|
35
|
+
tmm.set(20);
|
|
36
|
+
tmm.set(5);
|
|
37
37
|
|
|
38
38
|
expect(tmm.getNode(10)).toBeInstanceOf(TreeMultiMapNode);
|
|
39
39
|
expect(tmm.getNode(20)).toBeInstanceOf(TreeMultiMapNode);
|
|
@@ -42,8 +42,8 @@ describe('TreeMultiMap 1', () => {
|
|
|
42
42
|
});
|
|
43
43
|
|
|
44
44
|
it('should add and find nodes with negative keys', () => {
|
|
45
|
-
tmm.
|
|
46
|
-
tmm.
|
|
45
|
+
tmm.set(-10);
|
|
46
|
+
tmm.set(-20);
|
|
47
47
|
|
|
48
48
|
expect(tmm.getNode(-10)).toBeInstanceOf(TreeMultiMapNode);
|
|
49
49
|
expect(tmm.getNode(-20)).toBeInstanceOf(TreeMultiMapNode);
|
|
@@ -52,36 +52,36 @@ describe('TreeMultiMap 1', () => {
|
|
|
52
52
|
|
|
53
53
|
describe('deleteNode', () => {
|
|
54
54
|
it('should delete a node from the tmm', () => {
|
|
55
|
-
tmm.
|
|
56
|
-
tmm.
|
|
57
|
-
tmm.
|
|
55
|
+
tmm.set(10);
|
|
56
|
+
tmm.set(20);
|
|
57
|
+
tmm.set(5);
|
|
58
58
|
tmm.delete(20);
|
|
59
59
|
|
|
60
60
|
expect(tmm.getNode(20)).toBe(undefined);
|
|
61
61
|
});
|
|
62
62
|
|
|
63
63
|
it('should handle deleting a non-existent node', () => {
|
|
64
|
-
tmm.
|
|
65
|
-
tmm.
|
|
66
|
-
tmm.
|
|
64
|
+
tmm.set(10);
|
|
65
|
+
tmm.set(20);
|
|
66
|
+
tmm.set(5);
|
|
67
67
|
tmm.delete(15);
|
|
68
68
|
|
|
69
69
|
expect(tmm.getNode(15)).toBe(undefined);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
it('should getNode performance O(log n)', () => {
|
|
73
|
-
for (let i = 0; i < 10; i++) tmm.
|
|
73
|
+
for (let i = 0; i < 10; i++) tmm.set(i);
|
|
74
74
|
tmm.getNode(6);
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
|
|
78
78
|
describe('minimum', () => {
|
|
79
79
|
it('should find the minimum node in the tmm', () => {
|
|
80
|
-
tmm.
|
|
81
|
-
tmm.
|
|
82
|
-
tmm.
|
|
83
|
-
tmm.
|
|
84
|
-
tmm.
|
|
80
|
+
tmm.set(10);
|
|
81
|
+
tmm.set(20);
|
|
82
|
+
tmm.set(5);
|
|
83
|
+
tmm.set(15);
|
|
84
|
+
tmm.set(3);
|
|
85
85
|
|
|
86
86
|
const minNode = tmm.getLeftMost(node => node, tmm.root);
|
|
87
87
|
expect(minNode?.key).toBe(3);
|
|
@@ -95,11 +95,11 @@ describe('TreeMultiMap 1', () => {
|
|
|
95
95
|
|
|
96
96
|
describe('getRightMost', () => {
|
|
97
97
|
it('should find the getRightMost node in the tmm', () => {
|
|
98
|
-
tmm.
|
|
99
|
-
tmm.
|
|
100
|
-
tmm.
|
|
101
|
-
tmm.
|
|
102
|
-
tmm.
|
|
98
|
+
tmm.set(10);
|
|
99
|
+
tmm.set(20);
|
|
100
|
+
tmm.set(5);
|
|
101
|
+
tmm.set(15);
|
|
102
|
+
tmm.set(25);
|
|
103
103
|
|
|
104
104
|
const maxNode = tmm.getRightMost(node => node, tmm.root);
|
|
105
105
|
expect(maxNode?.key).toBe(25);
|
|
@@ -113,11 +113,11 @@ describe('TreeMultiMap 1', () => {
|
|
|
113
113
|
|
|
114
114
|
describe('getSuccessor', () => {
|
|
115
115
|
it('should find the getSuccessor of a node', () => {
|
|
116
|
-
tmm.
|
|
117
|
-
tmm.
|
|
118
|
-
tmm.
|
|
119
|
-
tmm.
|
|
120
|
-
tmm.
|
|
116
|
+
tmm.set(10);
|
|
117
|
+
tmm.set(20);
|
|
118
|
+
tmm.set(5);
|
|
119
|
+
tmm.set(15);
|
|
120
|
+
tmm.set(25);
|
|
121
121
|
|
|
122
122
|
const node = tmm.getNode(15);
|
|
123
123
|
const successorNode = tmm.getSuccessor(node!);
|
|
@@ -126,8 +126,8 @@ describe('TreeMultiMap 1', () => {
|
|
|
126
126
|
});
|
|
127
127
|
|
|
128
128
|
it('should handle a node with no getSuccessor', () => {
|
|
129
|
-
tmm.
|
|
130
|
-
tmm.
|
|
129
|
+
tmm.set(10);
|
|
130
|
+
tmm.set(5);
|
|
131
131
|
|
|
132
132
|
const node = tmm.getNode(10);
|
|
133
133
|
const successorNode = tmm.getSuccessor(node!);
|
|
@@ -138,11 +138,11 @@ describe('TreeMultiMap 1', () => {
|
|
|
138
138
|
|
|
139
139
|
describe('getPredecessor', () => {
|
|
140
140
|
it('should find the getPredecessor of a node', () => {
|
|
141
|
-
tmm.
|
|
142
|
-
tmm.
|
|
143
|
-
tmm.
|
|
144
|
-
tmm.
|
|
145
|
-
tmm.
|
|
141
|
+
tmm.set(10);
|
|
142
|
+
tmm.set(20);
|
|
143
|
+
tmm.set(5);
|
|
144
|
+
tmm.set(15);
|
|
145
|
+
tmm.set(25);
|
|
146
146
|
|
|
147
147
|
const node = tmm.getNode(20);
|
|
148
148
|
const predecessorNode = tmm.getPredecessor(node!);
|
|
@@ -151,8 +151,8 @@ describe('TreeMultiMap 1', () => {
|
|
|
151
151
|
});
|
|
152
152
|
|
|
153
153
|
it('should handle a node with no getPredecessor', () => {
|
|
154
|
-
tmm.
|
|
155
|
-
tmm.
|
|
154
|
+
tmm.set(10);
|
|
155
|
+
tmm.set(20);
|
|
156
156
|
|
|
157
157
|
const node = tmm.getNode(20);
|
|
158
158
|
const predecessorNode = tmm.getPredecessor(node!);
|
|
@@ -174,7 +174,7 @@ describe('TreeMultiMap 1', () => {
|
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
const tmm = new TreeMultiMap<string, number>();
|
|
177
|
-
tmm.
|
|
177
|
+
tmm.setMany([
|
|
178
178
|
['2', 2],
|
|
179
179
|
['4', 4],
|
|
180
180
|
['5', 5],
|
|
@@ -202,17 +202,17 @@ describe('TreeMultiMap 1', () => {
|
|
|
202
202
|
const tmm = new TreeMultiMap<number, string>([4, 5, [1, ['1']], 2, 3]);
|
|
203
203
|
expect(tmm.get(1)).toEqual(['1']);
|
|
204
204
|
expect(tmm.getNode(1)?.value).toEqual([]);
|
|
205
|
-
tmm.
|
|
205
|
+
tmm.set(1, 'a');
|
|
206
206
|
expect(tmm.get(1)).toEqual(['1', 'a']);
|
|
207
|
-
tmm.
|
|
207
|
+
tmm.set([1, ['b']]);
|
|
208
208
|
expect(tmm.getNode(1)?.value).toEqual([]);
|
|
209
209
|
expect(tmm.get(1)).toEqual(['1', 'a', 'b']);
|
|
210
210
|
const tmmMapped = new TreeMultiMap<number>([4, 5, [1, ['1']], 2, 3]);
|
|
211
211
|
expect(tmmMapped.get(1)).toEqual(['1']);
|
|
212
212
|
expect(tmmMapped.getNode(1)?.value).toEqual([]);
|
|
213
|
-
tmmMapped.
|
|
213
|
+
tmmMapped.set(1, 'a');
|
|
214
214
|
expect(tmmMapped.get(1)).toEqual(['1', 'a']);
|
|
215
|
-
tmmMapped.
|
|
215
|
+
tmmMapped.set([1, ['b']]);
|
|
216
216
|
expect(tmmMapped.getNode(1)?.value).toEqual([]);
|
|
217
217
|
expect(tmmMapped.get(1)).toEqual(['1', 'a', 'b']);
|
|
218
218
|
});
|
|
@@ -226,68 +226,68 @@ describe('TreeMultiMap 2', () => {
|
|
|
226
226
|
});
|
|
227
227
|
|
|
228
228
|
it('should add nodes into the tmm', () => {
|
|
229
|
-
tmm.
|
|
229
|
+
tmm.set(10);
|
|
230
230
|
expect(tmm.getNode(10)).toBeDefined();
|
|
231
|
-
tmm.
|
|
231
|
+
tmm.set(20);
|
|
232
232
|
expect(tmm.getNode(20)).toBeDefined();
|
|
233
|
-
tmm.
|
|
233
|
+
tmm.set(5);
|
|
234
234
|
expect(tmm.getNode(5)).toBeDefined();
|
|
235
235
|
});
|
|
236
236
|
|
|
237
237
|
it('should delete nodes from the tmm', () => {
|
|
238
|
-
tmm.
|
|
239
|
-
tmm.
|
|
240
|
-
tmm.
|
|
238
|
+
tmm.set(10);
|
|
239
|
+
tmm.set(20);
|
|
240
|
+
tmm.set(5);
|
|
241
241
|
tmm.delete(20);
|
|
242
242
|
expect(tmm.getNode(20)).toBe(undefined);
|
|
243
243
|
});
|
|
244
244
|
|
|
245
245
|
it('should get the successor of a node', () => {
|
|
246
|
-
tmm.
|
|
247
|
-
tmm.
|
|
246
|
+
tmm.set(10);
|
|
247
|
+
tmm.set(20);
|
|
248
248
|
const node = tmm.getNode(10);
|
|
249
249
|
const successor = tmm.getSuccessor(node!);
|
|
250
250
|
expect(successor?.key).toBe(20);
|
|
251
251
|
});
|
|
252
252
|
|
|
253
253
|
it('should get the predecessor of a node', () => {
|
|
254
|
-
tmm.
|
|
255
|
-
tmm.
|
|
254
|
+
tmm.set(10);
|
|
255
|
+
tmm.set(20);
|
|
256
256
|
const node = tmm.getNode(20);
|
|
257
257
|
const predecessor = tmm.getPredecessor(node!);
|
|
258
258
|
expect(predecessor?.key).toBe(20);
|
|
259
259
|
});
|
|
260
260
|
|
|
261
261
|
it('should rotate nodes to the left', () => {
|
|
262
|
-
tmm.
|
|
263
|
-
tmm.
|
|
264
|
-
tmm.
|
|
262
|
+
tmm.set(10);
|
|
263
|
+
tmm.set(20);
|
|
264
|
+
tmm.set(5);
|
|
265
265
|
const node = tmm.getNode(10);
|
|
266
|
-
tmm.
|
|
266
|
+
tmm.set(15);
|
|
267
267
|
// Verify that rotation has occurred
|
|
268
268
|
expect(node?.left?.key).toBe(5);
|
|
269
269
|
expect(node?.right?.key).toBe(20);
|
|
270
270
|
});
|
|
271
271
|
|
|
272
272
|
it('should rotate nodes to the right', () => {
|
|
273
|
-
tmm.
|
|
274
|
-
tmm.
|
|
275
|
-
tmm.
|
|
273
|
+
tmm.set(10);
|
|
274
|
+
tmm.set(20);
|
|
275
|
+
tmm.set(5);
|
|
276
276
|
const node = tmm.getNode(20);
|
|
277
|
-
tmm.
|
|
277
|
+
tmm.set(25);
|
|
278
278
|
// Verify that rotation has occurred
|
|
279
279
|
expect(node?.left?.key).toBeNaN();
|
|
280
280
|
expect(node?.right?.key).toBe(25);
|
|
281
281
|
});
|
|
282
282
|
|
|
283
283
|
it('should all node attributes fully conform to the red-black tmm standards.', () => {
|
|
284
|
-
tmm.
|
|
285
|
-
tmm.
|
|
286
|
-
tmm.
|
|
287
|
-
tmm.
|
|
288
|
-
tmm.
|
|
289
|
-
tmm.
|
|
290
|
-
tmm.
|
|
284
|
+
tmm.set(10);
|
|
285
|
+
tmm.set(20);
|
|
286
|
+
tmm.set(5);
|
|
287
|
+
tmm.set(15);
|
|
288
|
+
tmm.set(21);
|
|
289
|
+
tmm.set(6);
|
|
290
|
+
tmm.set(2);
|
|
291
291
|
|
|
292
292
|
let node10F = tmm.getNode(10);
|
|
293
293
|
let node20F = tmm.getNode(20);
|
|
@@ -423,31 +423,31 @@ describe('TreeMultiMap 2', () => {
|
|
|
423
423
|
});
|
|
424
424
|
|
|
425
425
|
it('should fix the tmm after insertion', () => {
|
|
426
|
-
tmm.
|
|
427
|
-
tmm.
|
|
428
|
-
tmm.
|
|
429
|
-
tmm.
|
|
426
|
+
tmm.set(1);
|
|
427
|
+
tmm.set(2);
|
|
428
|
+
tmm.set(5);
|
|
429
|
+
tmm.set(15);
|
|
430
430
|
const node15F = tmm.getNode(15);
|
|
431
431
|
expect(node15F?.left).toBe(tmm.NIL);
|
|
432
432
|
expect(node15F?.right).toBe(tmm.NIL);
|
|
433
433
|
expect(node15F?.parent).toBe(tmm.getNode(5));
|
|
434
434
|
|
|
435
|
-
tmm.
|
|
436
|
-
tmm.
|
|
437
|
-
tmm.
|
|
438
|
-
tmm.
|
|
439
|
-
tmm.
|
|
440
|
-
tmm.
|
|
435
|
+
tmm.set(25);
|
|
436
|
+
tmm.set(10);
|
|
437
|
+
tmm.set(8);
|
|
438
|
+
tmm.set(28);
|
|
439
|
+
tmm.set(111);
|
|
440
|
+
tmm.set(12);
|
|
441
441
|
tmm.delete(2);
|
|
442
|
-
tmm.
|
|
443
|
-
tmm.
|
|
444
|
-
tmm.
|
|
445
|
-
tmm.
|
|
442
|
+
tmm.set(22);
|
|
443
|
+
tmm.set(50);
|
|
444
|
+
tmm.set(155);
|
|
445
|
+
tmm.set(225);
|
|
446
446
|
const node225F = tmm.getNode(225);
|
|
447
447
|
expect(node225F?.left).toBe(tmm.NIL);
|
|
448
448
|
expect(node225F?.right).toBe(tmm.NIL);
|
|
449
449
|
expect(node225F?.parent?.key).toBe(155);
|
|
450
|
-
tmm.
|
|
450
|
+
tmm.set(7);
|
|
451
451
|
if (isDebug) tmm.print();
|
|
452
452
|
|
|
453
453
|
const node15S = tmm.getNode(15);
|
|
@@ -462,9 +462,9 @@ describe('TreeMultiMap 2', () => {
|
|
|
462
462
|
const node15T = tmm.getNode(15);
|
|
463
463
|
expect(node15T).toBe(undefined);
|
|
464
464
|
|
|
465
|
-
tmm.
|
|
466
|
-
tmm.
|
|
467
|
-
tmm.
|
|
465
|
+
tmm.set(23);
|
|
466
|
+
tmm.set(33);
|
|
467
|
+
tmm.set(15);
|
|
468
468
|
|
|
469
469
|
const nodeLM = tmm.getLeftMost();
|
|
470
470
|
expect(nodeLM).toBe(1);
|
|
@@ -483,19 +483,19 @@ describe('TreeMultiMap 2', () => {
|
|
|
483
483
|
expect(node225S?.parent?.key).toBe(155);
|
|
484
484
|
// TODO
|
|
485
485
|
// expect(tmm.getNode(0)).toBe(undefined);
|
|
486
|
-
tmm.
|
|
487
|
-
tmm.
|
|
488
|
-
tmm.
|
|
489
|
-
tmm.
|
|
490
|
-
tmm.
|
|
491
|
-
tmm.
|
|
492
|
-
tmm.
|
|
493
|
-
tmm.
|
|
494
|
-
tmm.
|
|
495
|
-
tmm.
|
|
496
|
-
tmm.
|
|
497
|
-
tmm.
|
|
498
|
-
tmm.
|
|
486
|
+
tmm.set(2);
|
|
487
|
+
tmm.set(3);
|
|
488
|
+
tmm.set(4);
|
|
489
|
+
tmm.set(6);
|
|
490
|
+
tmm.set(9);
|
|
491
|
+
tmm.set(11);
|
|
492
|
+
tmm.set(13);
|
|
493
|
+
tmm.set(14);
|
|
494
|
+
tmm.set(16);
|
|
495
|
+
tmm.set(17);
|
|
496
|
+
tmm.set(18);
|
|
497
|
+
tmm.set(19);
|
|
498
|
+
tmm.set(110);
|
|
499
499
|
|
|
500
500
|
if (isDebug) tmm.print();
|
|
501
501
|
|
|
@@ -508,7 +508,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
508
508
|
|
|
509
509
|
it('should fix the tmm after insertion and deletion', () => {
|
|
510
510
|
for (let i = 0; i < 100; i++) {
|
|
511
|
-
tmm.
|
|
511
|
+
tmm.set(i);
|
|
512
512
|
}
|
|
513
513
|
for (let i = 0; i < 49; i++) {
|
|
514
514
|
tmm.delete(i);
|
|
@@ -530,7 +530,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
530
530
|
|
|
531
531
|
it('should fix the tmm after large scale insertion and deletion', () => {
|
|
532
532
|
for (let i = 0; i < 10000; i++) {
|
|
533
|
-
tmm.
|
|
533
|
+
tmm.set(i);
|
|
534
534
|
}
|
|
535
535
|
for (let i = 0; i < 10000; i++) {
|
|
536
536
|
tmm.delete(i);
|
|
@@ -542,7 +542,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
542
542
|
|
|
543
543
|
tmm.clear();
|
|
544
544
|
for (let i = 0; i < 1000; i++) {
|
|
545
|
-
tmm.
|
|
545
|
+
tmm.set(getRandomInt(-100, 1000));
|
|
546
546
|
tmm.delete(getRandomInt(-100, 1000));
|
|
547
547
|
}
|
|
548
548
|
|
|
@@ -551,19 +551,19 @@ describe('TreeMultiMap 2', () => {
|
|
|
551
551
|
});
|
|
552
552
|
|
|
553
553
|
it('duplicates', () => {
|
|
554
|
-
tmm.
|
|
554
|
+
tmm.setMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
|
|
555
555
|
if (isDebug) tmm.print();
|
|
556
556
|
|
|
557
557
|
expect(tmm.size).toBe(8);
|
|
558
558
|
expect(tmm.isBST()).toBe(true);
|
|
559
559
|
expect(tmm.isAVLBalanced()).toBe(true);
|
|
560
|
-
tmm.
|
|
560
|
+
tmm.setMany([10, 5, 2, 11]);
|
|
561
561
|
expect(tmm.size).toBe(10);
|
|
562
562
|
expect(tmm.isBST()).toBe(true);
|
|
563
563
|
expect(tmm.isAVLBalanced()).toBe(true);
|
|
564
564
|
|
|
565
565
|
tmm.clear();
|
|
566
|
-
tmm.
|
|
566
|
+
tmm.setMany([10, 20, 30, 40, 50, 60]);
|
|
567
567
|
expect(tmm.isAVLBalanced()).toBe(false);
|
|
568
568
|
});
|
|
569
569
|
|
|
@@ -576,7 +576,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
576
576
|
});
|
|
577
577
|
it('The structure remains normal after random deletion', function () {
|
|
578
578
|
for (let i = 0; i < inputSize; i++) {
|
|
579
|
-
tmm.
|
|
579
|
+
tmm.set(i);
|
|
580
580
|
}
|
|
581
581
|
|
|
582
582
|
for (let i = 0; i < inputSize; i++) {
|
|
@@ -602,7 +602,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
602
602
|
for (let i = 0; i < inputSize; i++) {
|
|
603
603
|
const num = getRandomInt(0, inputSize - 1);
|
|
604
604
|
if (i === 0 && isDebug) console.log(`first:`, num);
|
|
605
|
-
tmm.
|
|
605
|
+
tmm.set(num);
|
|
606
606
|
}
|
|
607
607
|
|
|
608
608
|
for (let i = 0; i < inputSize; i++) {
|
|
@@ -629,9 +629,9 @@ describe('TreeMultiMap 2', () => {
|
|
|
629
629
|
let tmm: TreeMultiMap<number, string, object>;
|
|
630
630
|
beforeEach(() => {
|
|
631
631
|
tmm = new TreeMultiMap();
|
|
632
|
-
tmm.
|
|
633
|
-
tmm.
|
|
634
|
-
tmm.
|
|
632
|
+
tmm.set([1, ['a']]);
|
|
633
|
+
tmm.set(2, 'b');
|
|
634
|
+
tmm.set([3, ['c']]);
|
|
635
635
|
});
|
|
636
636
|
|
|
637
637
|
it('The node obtained by get Node should match the node type', () => {
|
|
@@ -701,59 +701,59 @@ describe('TreeMultiMap - _deleteFixup', () => {
|
|
|
701
701
|
});
|
|
702
702
|
|
|
703
703
|
it('should handle deleting a red leaf node', () => {
|
|
704
|
-
tmm.
|
|
705
|
-
tmm.
|
|
706
|
-
tmm.
|
|
704
|
+
tmm.set(10, 10);
|
|
705
|
+
tmm.set(5, 5); // Red leaf
|
|
706
|
+
tmm.set(20, 20);
|
|
707
707
|
|
|
708
708
|
expect(tmm.delete(5)).toHaveLength(1); // Delete red leaf
|
|
709
709
|
expect(tmm.root?.left).toBe(tmm.NIL); // Left child should be NIL
|
|
710
710
|
});
|
|
711
711
|
|
|
712
712
|
it('should handle deleting a black leaf node', () => {
|
|
713
|
-
tmm.
|
|
714
|
-
tmm.
|
|
715
|
-
tmm.
|
|
716
|
-
tmm.
|
|
713
|
+
tmm.set(10, 10);
|
|
714
|
+
tmm.set(5, 5); // Black node
|
|
715
|
+
tmm.set(20, 20);
|
|
716
|
+
tmm.set(1, 1); // Black leaf node
|
|
717
717
|
|
|
718
718
|
expect(tmm.delete(1)).toHaveLength(1); // Delete black leaf
|
|
719
719
|
expect(tmm.root?.left?.left).toBe(tmm.NIL);
|
|
720
720
|
});
|
|
721
721
|
|
|
722
722
|
it('should handle deleting black node with red sibling', () => {
|
|
723
|
-
tmm.
|
|
724
|
-
tmm.
|
|
725
|
-
tmm.
|
|
726
|
-
tmm.
|
|
723
|
+
tmm.set(10, 10);
|
|
724
|
+
tmm.set(5, 5); // Black node
|
|
725
|
+
tmm.set(20, 20); // Red sibling
|
|
726
|
+
tmm.set(25, 25); // Force the sibling to be red
|
|
727
727
|
|
|
728
728
|
expect(tmm.delete(5)).toHaveLength(1); // Delete black node
|
|
729
729
|
expect(tmm.root?.right?.color).toBe('BLACK'); // Ensure sibling color is black after fixup
|
|
730
730
|
});
|
|
731
731
|
|
|
732
732
|
it('should handle deleting black node with black sibling', () => {
|
|
733
|
-
tmm.
|
|
734
|
-
tmm.
|
|
735
|
-
tmm.
|
|
733
|
+
tmm.set(10, 10);
|
|
734
|
+
tmm.set(5, 5); // Black node
|
|
735
|
+
tmm.set(20, 20); // Black sibling
|
|
736
736
|
|
|
737
737
|
expect(tmm.delete(5)).toHaveLength(1); // Delete black node
|
|
738
738
|
expect(tmm.root?.left).toBe(tmm.NIL);
|
|
739
739
|
});
|
|
740
740
|
|
|
741
741
|
it('should handle deleting the root node', () => {
|
|
742
|
-
tmm.
|
|
743
|
-
tmm.
|
|
744
|
-
tmm.
|
|
742
|
+
tmm.set(10, 10); // Root node
|
|
743
|
+
tmm.set(5, 5);
|
|
744
|
+
tmm.set(20, 20);
|
|
745
745
|
|
|
746
746
|
expect(tmm.delete(10)).toHaveLength(1); // Delete root node
|
|
747
747
|
expect(tmm.root?.key).toBe(20); // New root should be 20
|
|
748
748
|
});
|
|
749
749
|
|
|
750
750
|
it('should handle complex case with multiple rotations', () => {
|
|
751
|
-
tmm.
|
|
752
|
-
tmm.
|
|
753
|
-
tmm.
|
|
754
|
-
tmm.
|
|
755
|
-
tmm.
|
|
756
|
-
tmm.
|
|
751
|
+
tmm.set(10, 10);
|
|
752
|
+
tmm.set(5, 5);
|
|
753
|
+
tmm.set(15, 15);
|
|
754
|
+
tmm.set(12, 12);
|
|
755
|
+
tmm.set(18, 18);
|
|
756
|
+
tmm.set(16, 16);
|
|
757
757
|
|
|
758
758
|
// Delete a node that will cause rotations and color changes
|
|
759
759
|
expect(tmm.delete(5)).toHaveLength(1);
|
|
@@ -768,15 +768,15 @@ describe('TreeMultiMap - _deleteFixup', () => {
|
|
|
768
768
|
const tmm = new TreeMultiMap<number, number>();
|
|
769
769
|
|
|
770
770
|
// Build a tmm that will require complex fixup
|
|
771
|
-
tmm.
|
|
772
|
-
tmm.
|
|
773
|
-
tmm.
|
|
774
|
-
tmm.
|
|
775
|
-
tmm.
|
|
776
|
-
tmm.
|
|
777
|
-
tmm.
|
|
778
|
-
tmm.
|
|
779
|
-
tmm.
|
|
771
|
+
tmm.set(20, 20);
|
|
772
|
+
tmm.set(10, 10);
|
|
773
|
+
tmm.set(30, 30);
|
|
774
|
+
tmm.set(5, 5);
|
|
775
|
+
tmm.set(15, 15);
|
|
776
|
+
tmm.set(25, 25);
|
|
777
|
+
tmm.set(35, 35);
|
|
778
|
+
tmm.set(2, 2);
|
|
779
|
+
tmm.set(8, 8);
|
|
780
780
|
|
|
781
781
|
// This deletion should trigger a complex fixup
|
|
782
782
|
tmm.delete(2);
|
package/tsup.config.js
CHANGED
|
@@ -1,34 +1,63 @@
|
|
|
1
1
|
import { defineConfig } from 'tsup';
|
|
2
2
|
|
|
3
|
+
const baseConfig = {
|
|
4
|
+
entry: { index: 'src/index.ts' },
|
|
5
|
+
splitting: false,
|
|
6
|
+
sourcemap: true,
|
|
7
|
+
minify: false,
|
|
8
|
+
keepNames: true,
|
|
9
|
+
treeshake: true,
|
|
10
|
+
esbuildOptions(options) {
|
|
11
|
+
options.drop = ['debugger'];
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
3
15
|
export default defineConfig([
|
|
16
|
+
// ESM (modern) - ES2022
|
|
4
17
|
{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
...baseConfig,
|
|
19
|
+
format: ['esm'],
|
|
20
|
+
outDir: 'dist/esm',
|
|
8
21
|
clean: true,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
globalName: "dataStructureTyped",
|
|
13
|
-
platform: "browser",
|
|
14
|
-
outExtension: () => ({ js: '.min.js' }),
|
|
15
|
-
esbuildOptions(options) {
|
|
16
|
-
options.drop = ['debugger']
|
|
22
|
+
target: 'es2022',
|
|
23
|
+
outExtension() {
|
|
24
|
+
return { js: '.mjs' };
|
|
17
25
|
}
|
|
18
26
|
},
|
|
27
|
+
|
|
28
|
+
// ESM (legacy) - ES2018
|
|
19
29
|
{
|
|
20
|
-
|
|
30
|
+
...baseConfig,
|
|
31
|
+
format: ['esm'],
|
|
32
|
+
outDir: 'dist/esm-legacy',
|
|
33
|
+
clean: false,
|
|
21
34
|
target: 'es2018',
|
|
22
|
-
|
|
35
|
+
outExtension() {
|
|
36
|
+
return { js: '.mjs' };
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
// CJS (modern) - ES2022
|
|
41
|
+
{
|
|
42
|
+
...baseConfig,
|
|
43
|
+
format: ['cjs'],
|
|
44
|
+
outDir: 'dist/cjs',
|
|
23
45
|
clean: false,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
46
|
+
target: 'es2022',
|
|
47
|
+
outExtension() {
|
|
48
|
+
return { js: '.cjs' };
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
// CJS (legacy) - ES2018
|
|
53
|
+
{
|
|
54
|
+
...baseConfig,
|
|
55
|
+
format: ['cjs'],
|
|
56
|
+
outDir: 'dist/cjs-legacy',
|
|
57
|
+
clean: false,
|
|
58
|
+
target: 'es2018',
|
|
59
|
+
outExtension() {
|
|
60
|
+
return { js: '.cjs' };
|
|
32
61
|
}
|
|
33
62
|
}
|
|
34
63
|
]);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup';
|
|
2
|
+
|
|
3
|
+
const baseConfig = {
|
|
4
|
+
entry: { 'data-structure-typed': 'src/index.ts' },
|
|
5
|
+
target: 'es2018',
|
|
6
|
+
format: ['iife'],
|
|
7
|
+
sourcemap: true,
|
|
8
|
+
outDir: 'dist/umd',
|
|
9
|
+
globalName: 'dataStructureTyped',
|
|
10
|
+
platform: 'browser',
|
|
11
|
+
esbuildOptions(options) {
|
|
12
|
+
options.drop = ['debugger'];
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default defineConfig([
|
|
17
|
+
{
|
|
18
|
+
...baseConfig,
|
|
19
|
+
clean: true,
|
|
20
|
+
minify: true,
|
|
21
|
+
outExtension: () => ({ js: '.min.js' })
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
...baseConfig,
|
|
25
|
+
clean: false,
|
|
26
|
+
minify: false,
|
|
27
|
+
outExtension: () => ({ js: '.js' })
|
|
28
|
+
}
|
|
29
|
+
]);
|