data-structure-typed 2.2.6 → 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/CONTRIBUTING.md +47 -1
- package/README.md +19 -8
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +109 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +109 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +109 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +109 -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 +105 -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.leetcode.config.js +1 -1
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
|
@@ -9,7 +9,7 @@ describe('AVLTreeCounter count', () => {
|
|
|
9
9
|
avlCounter = new AVLTreeCounter<number>();
|
|
10
10
|
});
|
|
11
11
|
it('Should added isolated node count ', () => {
|
|
12
|
-
avlCounter.
|
|
12
|
+
avlCounter.setMany([
|
|
13
13
|
[1, 1],
|
|
14
14
|
[2, 2],
|
|
15
15
|
[3, 3],
|
|
@@ -17,17 +17,17 @@ describe('AVLTreeCounter count', () => {
|
|
|
17
17
|
[5, 5]
|
|
18
18
|
]);
|
|
19
19
|
const newNode = new AVLTreeCounterNode(3, 33, 10);
|
|
20
|
-
avlCounter.
|
|
20
|
+
avlCounter.set(newNode);
|
|
21
21
|
expect(avlCounter.count).toBe(15);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
it('Should count', () => {
|
|
25
|
-
avlCounter.
|
|
25
|
+
avlCounter.setMany([
|
|
26
26
|
[1, 1],
|
|
27
27
|
[2, 2],
|
|
28
28
|
[3, 3]
|
|
29
29
|
]);
|
|
30
|
-
avlCounter.
|
|
30
|
+
avlCounter.set([2, 2], undefined, 10);
|
|
31
31
|
avlCounter.lesserOrGreaterTraverse(node => (node.count += 2), 1, 1);
|
|
32
32
|
avlCounter.delete(2);
|
|
33
33
|
expect(avlCounter.count).toBe(12);
|
|
@@ -40,8 +40,8 @@ describe('AVLTreeCounter operations test1', () => {
|
|
|
40
40
|
const avlCounter = new AVLTreeCounter<number>();
|
|
41
41
|
|
|
42
42
|
expect(avlCounter instanceof AVLTreeCounter);
|
|
43
|
-
avlCounter.
|
|
44
|
-
avlCounter.
|
|
43
|
+
avlCounter.set([11, 11]);
|
|
44
|
+
avlCounter.set([3, 3]);
|
|
45
45
|
const idAndValues: [number, number][] = [
|
|
46
46
|
[11, 11],
|
|
47
47
|
[3, 3],
|
|
@@ -60,7 +60,7 @@ describe('AVLTreeCounter operations test1', () => {
|
|
|
60
60
|
[10, 10],
|
|
61
61
|
[5, 5]
|
|
62
62
|
];
|
|
63
|
-
avlCounter.
|
|
63
|
+
avlCounter.setMany(idAndValues);
|
|
64
64
|
expect(avlCounter.root instanceof AVLTreeCounterNode);
|
|
65
65
|
|
|
66
66
|
if (avlCounter.root) expect(avlCounter.root.key == 11);
|
|
@@ -258,8 +258,8 @@ describe('AVLTreeCounter operations test1', () => {
|
|
|
258
258
|
it('should perform various operations on a AVLTreeCounter with object values', () => {
|
|
259
259
|
const objAvlCounter = new AVLTreeCounter<number, { key: number; keyA: number }>();
|
|
260
260
|
expect(objAvlCounter).toBeInstanceOf(AVLTreeCounter);
|
|
261
|
-
objAvlCounter.
|
|
262
|
-
objAvlCounter.
|
|
261
|
+
objAvlCounter.set([11, { key: 11, keyA: 11 }]);
|
|
262
|
+
objAvlCounter.set([3, { key: 3, keyA: 3 }]);
|
|
263
263
|
const values: [number, { key: number; keyA: number }][] = [
|
|
264
264
|
[15, { key: 15, keyA: 15 }],
|
|
265
265
|
[1, { key: 1, keyA: 1 }],
|
|
@@ -277,7 +277,7 @@ describe('AVLTreeCounter operations test1', () => {
|
|
|
277
277
|
[5, { key: 5, keyA: 5 }]
|
|
278
278
|
];
|
|
279
279
|
|
|
280
|
-
objAvlCounter.
|
|
280
|
+
objAvlCounter.setMany(values);
|
|
281
281
|
|
|
282
282
|
expect(objAvlCounter.root).toBeInstanceOf(AVLTreeCounterNode);
|
|
283
283
|
|
|
@@ -296,8 +296,8 @@ describe('AVLTreeCounter operations test recursively1', () => {
|
|
|
296
296
|
});
|
|
297
297
|
|
|
298
298
|
expect(avlCounter instanceof AVLTreeCounter);
|
|
299
|
-
avlCounter.
|
|
300
|
-
avlCounter.
|
|
299
|
+
avlCounter.set([11, 11]);
|
|
300
|
+
avlCounter.set([3, 3]);
|
|
301
301
|
const idAndValues: [number, number][] = [
|
|
302
302
|
[11, 11],
|
|
303
303
|
[3, 3],
|
|
@@ -316,7 +316,7 @@ describe('AVLTreeCounter operations test recursively1', () => {
|
|
|
316
316
|
[10, 10],
|
|
317
317
|
[5, 5]
|
|
318
318
|
];
|
|
319
|
-
avlCounter.
|
|
319
|
+
avlCounter.setMany(idAndValues);
|
|
320
320
|
expect(avlCounter.root).toBeInstanceOf(AVLTreeCounterNode);
|
|
321
321
|
|
|
322
322
|
if (avlCounter.root) expect(avlCounter.root.key).toBe(6);
|
|
@@ -514,8 +514,8 @@ describe('AVLTreeCounter operations test recursively1', () => {
|
|
|
514
514
|
it('should perform various operations on a AVLTreeCounter with object values', () => {
|
|
515
515
|
const objAvlCounter = new AVLTreeCounter<number, { key: number; keyA: number }>();
|
|
516
516
|
expect(objAvlCounter).toBeInstanceOf(AVLTreeCounter);
|
|
517
|
-
objAvlCounter.
|
|
518
|
-
objAvlCounter.
|
|
517
|
+
objAvlCounter.set([11, { key: 11, keyA: 11 }]);
|
|
518
|
+
objAvlCounter.set([3, { key: 3, keyA: 3 }]);
|
|
519
519
|
const values: [number, { key: number; keyA: number }][] = [
|
|
520
520
|
[15, { key: 15, keyA: 15 }],
|
|
521
521
|
[1, { key: 1, keyA: 1 }],
|
|
@@ -533,7 +533,7 @@ describe('AVLTreeCounter operations test recursively1', () => {
|
|
|
533
533
|
[5, { key: 5, keyA: 5 }]
|
|
534
534
|
];
|
|
535
535
|
|
|
536
|
-
objAvlCounter.
|
|
536
|
+
objAvlCounter.setMany(values);
|
|
537
537
|
|
|
538
538
|
expect(objAvlCounter.root).toBeInstanceOf(AVLTreeCounterNode);
|
|
539
539
|
|
|
@@ -562,7 +562,7 @@ describe('AVLTreeCounter Performance test', function () {
|
|
|
562
562
|
it('Should the time consumption of lesserOrGreaterTraverse fitting O(n log n)', function () {
|
|
563
563
|
const start = performance.now();
|
|
564
564
|
for (let i = 0; i < inputSize; i++) {
|
|
565
|
-
avlCounter.
|
|
565
|
+
avlCounter.set(i);
|
|
566
566
|
}
|
|
567
567
|
if (isDebug) console.log('---add', performance.now() - start);
|
|
568
568
|
const startL = performance.now();
|
|
@@ -583,7 +583,7 @@ describe('AVLTreeCounter Performance test', function () {
|
|
|
583
583
|
}
|
|
584
584
|
|
|
585
585
|
const avlCounter = new AVLTreeCounter<string, number>();
|
|
586
|
-
avlCounter.
|
|
586
|
+
avlCounter.setMany([
|
|
587
587
|
['2', 2],
|
|
588
588
|
['4', 4],
|
|
589
589
|
['5', 5],
|
|
@@ -612,9 +612,9 @@ describe('AVLTreeCounter iterative methods test', () => {
|
|
|
612
612
|
let avlCounter: AVLTreeCounter<number, string>;
|
|
613
613
|
beforeEach(() => {
|
|
614
614
|
avlCounter = new AVLTreeCounter<number, string>();
|
|
615
|
-
avlCounter.
|
|
616
|
-
avlCounter.
|
|
617
|
-
avlCounter.
|
|
615
|
+
avlCounter.set(1, 'a', 10);
|
|
616
|
+
avlCounter.set([2, 'b'], undefined, 10);
|
|
617
|
+
avlCounter.set([3, 'c'], undefined, 1);
|
|
618
618
|
});
|
|
619
619
|
|
|
620
620
|
it('The node obtained by get Node should match the node type', () => {
|
|
@@ -698,7 +698,7 @@ describe('AVLTreeCounter toEntryFn', () => {
|
|
|
698
698
|
const avlCounter = new AVLTreeCounter<number, number, { obj: { id: number } }>([], {
|
|
699
699
|
toEntryFn: ele => [ele.obj.id, ele.obj.id]
|
|
700
700
|
});
|
|
701
|
-
avlCounter.
|
|
701
|
+
avlCounter.setMany([
|
|
702
702
|
{ obj: { id: 1 } },
|
|
703
703
|
{ obj: { id: 2 } },
|
|
704
704
|
{ obj: { id: 3 } },
|
|
@@ -771,7 +771,7 @@ describe('AVLTreeCounter not map mode count', () => {
|
|
|
771
771
|
avlCounter = new AVLTreeCounter<number>([], { isMapMode: false });
|
|
772
772
|
});
|
|
773
773
|
it('Should added isolated node count ', () => {
|
|
774
|
-
avlCounter.
|
|
774
|
+
avlCounter.setMany([
|
|
775
775
|
[1, 1],
|
|
776
776
|
[2, 2],
|
|
777
777
|
[3, 3],
|
|
@@ -779,7 +779,7 @@ describe('AVLTreeCounter not map mode count', () => {
|
|
|
779
779
|
[5, 5]
|
|
780
780
|
]);
|
|
781
781
|
const newNode = new AVLTreeCounterNode(3, undefined, 10);
|
|
782
|
-
avlCounter.
|
|
782
|
+
avlCounter.set(newNode, 33);
|
|
783
783
|
expect(avlCounter.count).toBe(15);
|
|
784
784
|
});
|
|
785
785
|
});
|
|
@@ -789,8 +789,8 @@ describe('AVLTreeCounter not map mode operations test1', () => {
|
|
|
789
789
|
const avlCounter = new AVLTreeCounter<number>([], { isMapMode: false });
|
|
790
790
|
|
|
791
791
|
expect(avlCounter instanceof AVLTreeCounter);
|
|
792
|
-
avlCounter.
|
|
793
|
-
avlCounter.
|
|
792
|
+
avlCounter.set([11, 11]);
|
|
793
|
+
avlCounter.set([3, 3]);
|
|
794
794
|
const idAndValues: [number, number][] = [
|
|
795
795
|
[11, 11],
|
|
796
796
|
[3, 3],
|
|
@@ -809,7 +809,7 @@ describe('AVLTreeCounter not map mode operations test1', () => {
|
|
|
809
809
|
[10, 10],
|
|
810
810
|
[5, 5]
|
|
811
811
|
];
|
|
812
|
-
avlCounter.
|
|
812
|
+
avlCounter.setMany(idAndValues);
|
|
813
813
|
expect(avlCounter.root instanceof AVLTreeCounterNode);
|
|
814
814
|
|
|
815
815
|
if (avlCounter.root) expect(avlCounter.root.key == 11);
|
|
@@ -837,8 +837,8 @@ describe('AVLTreeCounter not map mode operations test recursively1', () => {
|
|
|
837
837
|
});
|
|
838
838
|
|
|
839
839
|
expect(avlCounter instanceof AVLTreeCounter);
|
|
840
|
-
avlCounter.
|
|
841
|
-
avlCounter.
|
|
840
|
+
avlCounter.set([11, 11]);
|
|
841
|
+
avlCounter.set([3, 3]);
|
|
842
842
|
const idAndValues: [number, number][] = [
|
|
843
843
|
[11, 11],
|
|
844
844
|
[3, 3],
|
|
@@ -857,7 +857,7 @@ describe('AVLTreeCounter not map mode operations test recursively1', () => {
|
|
|
857
857
|
[10, 10],
|
|
858
858
|
[5, 5]
|
|
859
859
|
];
|
|
860
|
-
avlCounter.
|
|
860
|
+
avlCounter.setMany(idAndValues);
|
|
861
861
|
expect(avlCounter.root).toBeInstanceOf(AVLTreeCounterNode);
|
|
862
862
|
|
|
863
863
|
if (avlCounter.root) expect(avlCounter.root.key).toBe(6);
|
|
@@ -10,9 +10,9 @@ describe('AVLTreeMultiMap', () => {
|
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
it('Should add and delete values', () => {
|
|
13
|
-
avlTmm.
|
|
14
|
-
avlTmm.
|
|
15
|
-
avlTmm.
|
|
13
|
+
avlTmm.set(3, 3);
|
|
14
|
+
avlTmm.set(3, 33);
|
|
15
|
+
avlTmm.set(3, 333);
|
|
16
16
|
expect(avlTmm.get(3)).toEqual([3, 33, 333]);
|
|
17
17
|
avlTmm.deleteValue(3, 33);
|
|
18
18
|
expect(avlTmm.get(3)).toEqual([3, 333]);
|
|
@@ -20,8 +20,8 @@ describe('AVLTreeMultiMap', () => {
|
|
|
20
20
|
expect(avlTmm.get(3)).toEqual([333]);
|
|
21
21
|
avlTmm.deleteValue(3, 333);
|
|
22
22
|
expect(avlTmm.get(3)).toBe(undefined);
|
|
23
|
-
avlTmm.
|
|
24
|
-
avlTmm.
|
|
23
|
+
avlTmm.set(3, 3);
|
|
24
|
+
avlTmm.set([3, [3333, 33333]]);
|
|
25
25
|
expect(avlTmm.get(3)).toEqual([3, 3333, 33333]);
|
|
26
26
|
});
|
|
27
27
|
});
|
|
@@ -31,9 +31,9 @@ describe('AVLTreeMultiMap Test', () => {
|
|
|
31
31
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
32
32
|
const avlTmm = new AVLTreeMultiMap<number>();
|
|
33
33
|
|
|
34
|
-
for (const i of arr) avlTmm.
|
|
34
|
+
for (const i of arr) avlTmm.set([i, [i]]);
|
|
35
35
|
|
|
36
|
-
avlTmm.
|
|
36
|
+
avlTmm.set(null);
|
|
37
37
|
const node6 = avlTmm.getNode(6);
|
|
38
38
|
|
|
39
39
|
expect(node6 && avlTmm.getHeight(node6)).toBe(3);
|
|
@@ -137,17 +137,17 @@ describe('AVLTreeMultiMap Test', () => {
|
|
|
137
137
|
const avlTmm = new AVLTreeMultiMap<number, string>([4, 5, [1, ['1']], 2, 3]);
|
|
138
138
|
expect(avlTmm.get(1)).toEqual(['1']);
|
|
139
139
|
expect(avlTmm.getNode(1)?.value).toEqual([]);
|
|
140
|
-
avlTmm.
|
|
140
|
+
avlTmm.set(1, 'a');
|
|
141
141
|
expect(avlTmm.get(1)).toEqual(['1', 'a']);
|
|
142
|
-
avlTmm.
|
|
142
|
+
avlTmm.set([1, ['b']]);
|
|
143
143
|
expect(avlTmm.getNode(1)?.value).toEqual([]);
|
|
144
144
|
expect(avlTmm.get(1)).toEqual(['1', 'a', 'b']);
|
|
145
145
|
const treeMap = new AVLTreeMultiMap<number>([4, 5, [1, ['1']], 2, 3]);
|
|
146
146
|
expect(treeMap.get(1)).toEqual(['1']);
|
|
147
147
|
expect(treeMap.getNode(1)?.value).toEqual([]);
|
|
148
|
-
treeMap.
|
|
148
|
+
treeMap.set(1, 'a');
|
|
149
149
|
expect(treeMap.get(1)).toEqual(['1', 'a']);
|
|
150
|
-
treeMap.
|
|
150
|
+
treeMap.set([1, ['b']]);
|
|
151
151
|
expect(treeMap.getNode(1)?.value).toEqual([]);
|
|
152
152
|
expect(treeMap.get(1)).toEqual(['1', 'a', 'b']);
|
|
153
153
|
});
|
|
@@ -158,7 +158,7 @@ describe('AVLTreeMultiMap Test recursively', () => {
|
|
|
158
158
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
159
159
|
const avlTmm = new AVLTreeMultiMap<number>([], { iterationType: 'RECURSIVE' });
|
|
160
160
|
|
|
161
|
-
for (const i of arr) avlTmm.
|
|
161
|
+
for (const i of arr) avlTmm.set([i, [i]]);
|
|
162
162
|
|
|
163
163
|
const node6 = avlTmm.getNode(6);
|
|
164
164
|
|
|
@@ -269,17 +269,17 @@ describe('AVLTreeMultiMap APIs test', () => {
|
|
|
269
269
|
});
|
|
270
270
|
|
|
271
271
|
it('add', () => {
|
|
272
|
-
avlTmm.
|
|
272
|
+
avlTmm.set(1);
|
|
273
273
|
const node2 = new AVLTreeMultiMapNode(2, []);
|
|
274
|
-
avlTmm.
|
|
274
|
+
avlTmm.set(node2);
|
|
275
275
|
const node3 = new AVLTreeMultiMapNode(3, [
|
|
276
276
|
{
|
|
277
277
|
id: 3,
|
|
278
278
|
text: 'text3'
|
|
279
279
|
}
|
|
280
280
|
]);
|
|
281
|
-
avlTmm.
|
|
282
|
-
avlTmm.
|
|
281
|
+
avlTmm.set(node3);
|
|
282
|
+
avlTmm.set([3, [{ id: 3, text: 'text33' }]]);
|
|
283
283
|
|
|
284
284
|
const bfsRes = avlTmm.bfs(node => node.key);
|
|
285
285
|
expect(bfsRes[0]).toBe(2);
|
|
@@ -298,7 +298,7 @@ describe('AVLTreeMultiMap APIs test', () => {
|
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
const avlTmm = new AVLTreeMultiMap<string, number>();
|
|
301
|
-
avlTmm.
|
|
301
|
+
avlTmm.setMany([
|
|
302
302
|
['2', 2],
|
|
303
303
|
['4', 4],
|
|
304
304
|
['5', 5],
|
|
@@ -326,26 +326,26 @@ describe('AVLTreeMultiMap APIs test', () => {
|
|
|
326
326
|
describe('AVLTreeMultiMap', () => {
|
|
327
327
|
it('should balance the avlTmm using _balanceLR when nodes are added', () => {
|
|
328
328
|
const avlTmm = new AVLTreeMultiMap();
|
|
329
|
-
avlTmm.
|
|
330
|
-
avlTmm.
|
|
331
|
-
avlTmm.
|
|
332
|
-
avlTmm.
|
|
333
|
-
avlTmm.
|
|
329
|
+
avlTmm.set([10, 'A']);
|
|
330
|
+
avlTmm.set([5, 'B']);
|
|
331
|
+
avlTmm.set([15, 'C']);
|
|
332
|
+
avlTmm.set([3, 'D']);
|
|
333
|
+
avlTmm.set([7, 'E']);
|
|
334
334
|
|
|
335
335
|
// Adding nodes to trigger _balanceLR
|
|
336
|
-
avlTmm.
|
|
336
|
+
avlTmm.set([12, 'F']);
|
|
337
337
|
|
|
338
338
|
// You can add more specific assertions to check the avlTmm's balance and structure.
|
|
339
339
|
});
|
|
340
340
|
|
|
341
341
|
it('should addMany undefined and null', () => {
|
|
342
342
|
const avlTmm = new AVLTreeMultiMap<number, string>();
|
|
343
|
-
const addManyWithUndefined = avlTmm.
|
|
343
|
+
const addManyWithUndefined = avlTmm.setMany([1, undefined, 3]);
|
|
344
344
|
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
345
345
|
expect(avlTmm.get(undefined)).toBe(undefined);
|
|
346
|
-
const addManyWithNull = avlTmm.
|
|
346
|
+
const addManyWithNull = avlTmm.setMany([1, null, 3, 4]);
|
|
347
347
|
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
348
|
-
const addManyEntriesWithNull = avlTmm.
|
|
348
|
+
const addManyEntriesWithNull = avlTmm.setMany([
|
|
349
349
|
[1, '1'],
|
|
350
350
|
[null, 'null'],
|
|
351
351
|
[3, '3'],
|
|
@@ -353,19 +353,19 @@ describe('AVLTreeMultiMap', () => {
|
|
|
353
353
|
]);
|
|
354
354
|
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
355
355
|
expect(avlTmm.get(null)).toBe(undefined);
|
|
356
|
-
const node0 = avlTmm.
|
|
356
|
+
const node0 = avlTmm.set(0, '0');
|
|
357
357
|
expect(node0).toBe(true);
|
|
358
358
|
expect(avlTmm.get(0)).toEqual(['0']);
|
|
359
359
|
});
|
|
360
360
|
|
|
361
361
|
it('should balance the avlTmm using _balanceLR when nodes are deleted', () => {
|
|
362
362
|
const avlTmm = new AVLTreeMultiMap();
|
|
363
|
-
avlTmm.
|
|
364
|
-
avlTmm.
|
|
365
|
-
avlTmm.
|
|
366
|
-
avlTmm.
|
|
367
|
-
avlTmm.
|
|
368
|
-
avlTmm.
|
|
363
|
+
avlTmm.set([10, 'A']);
|
|
364
|
+
avlTmm.set([5, 'B']);
|
|
365
|
+
avlTmm.set([15, 'C']);
|
|
366
|
+
avlTmm.set([3, 'D']);
|
|
367
|
+
avlTmm.set([7, 'E']);
|
|
368
|
+
avlTmm.set([12, 'F']);
|
|
369
369
|
|
|
370
370
|
// Deleting nodes to trigger _balanceLR
|
|
371
371
|
avlTmm.delete(3);
|
|
@@ -380,17 +380,17 @@ describe('AVLTreeMultiMap', () => {
|
|
|
380
380
|
});
|
|
381
381
|
|
|
382
382
|
it('add', () => {
|
|
383
|
-
avlTmm.
|
|
383
|
+
avlTmm.set(1);
|
|
384
384
|
const node2 = new AVLTreeMultiMapNode(2, []);
|
|
385
|
-
avlTmm.
|
|
385
|
+
avlTmm.set(node2);
|
|
386
386
|
const node3 = new AVLTreeMultiMapNode(3, [
|
|
387
387
|
{
|
|
388
388
|
id: 3,
|
|
389
389
|
text: 'text3'
|
|
390
390
|
}
|
|
391
391
|
]);
|
|
392
|
-
avlTmm.
|
|
393
|
-
avlTmm.
|
|
392
|
+
avlTmm.set(node3);
|
|
393
|
+
avlTmm.set([3, [{ id: 3, text: 'text33' }]]);
|
|
394
394
|
|
|
395
395
|
const bfsRes = avlTmm.bfs(node => node);
|
|
396
396
|
expect(bfsRes[0]?.key).toBe(2);
|
|
@@ -402,9 +402,9 @@ describe('AVLTreeMultiMap iterative methods test', () => {
|
|
|
402
402
|
let avlTmm: AVLTreeMultiMap<number, string, object>;
|
|
403
403
|
beforeEach(() => {
|
|
404
404
|
avlTmm = new AVLTreeMultiMap();
|
|
405
|
-
avlTmm.
|
|
406
|
-
avlTmm.
|
|
407
|
-
avlTmm.
|
|
405
|
+
avlTmm.set([1, ['a']]);
|
|
406
|
+
avlTmm.set([2, ['b']]);
|
|
407
|
+
avlTmm.set([3, ['c']]);
|
|
408
408
|
});
|
|
409
409
|
|
|
410
410
|
it('The node obtained by get Node should match the node type', () => {
|
|
@@ -490,9 +490,9 @@ describe('AVLTreeMultiMap not map mode', () => {
|
|
|
490
490
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
491
491
|
const avlTmm = new AVLTreeMultiMap<number>([]);
|
|
492
492
|
|
|
493
|
-
for (const i of arr) avlTmm.
|
|
493
|
+
for (const i of arr) avlTmm.set([i, [i]]);
|
|
494
494
|
|
|
495
|
-
avlTmm.
|
|
495
|
+
avlTmm.set(null);
|
|
496
496
|
const node6 = avlTmm.getNode(6);
|
|
497
497
|
|
|
498
498
|
expect(node6 && avlTmm.getHeight(node6)).toBe(3);
|
|
@@ -526,7 +526,7 @@ describe('AVLTreeMultiMap not map mode test recursively', () => {
|
|
|
526
526
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
527
527
|
const avlTmm = new AVLTreeMultiMap<number>([], { iterationType: 'RECURSIVE' });
|
|
528
528
|
|
|
529
|
-
for (const i of arr) avlTmm.
|
|
529
|
+
for (const i of arr) avlTmm.set([i, [i]]);
|
|
530
530
|
const node6 = avlTmm.getNode(6);
|
|
531
531
|
|
|
532
532
|
expect(node6 && avlTmm.getHeight(node6)).toBe(3);
|
|
@@ -559,9 +559,9 @@ describe('AVLTreeMultiMap iterative methods not map mode', () => {
|
|
|
559
559
|
let avlTmm: AVLTreeMultiMap<number, string>;
|
|
560
560
|
beforeEach(() => {
|
|
561
561
|
avlTmm = new AVLTreeMultiMap<number, string>([]);
|
|
562
|
-
avlTmm.
|
|
563
|
-
avlTmm.
|
|
564
|
-
avlTmm.
|
|
562
|
+
avlTmm.set([1, ['a']]);
|
|
563
|
+
avlTmm.set([2, ['b']]);
|
|
564
|
+
avlTmm.set([3, ['c']]);
|
|
565
565
|
});
|
|
566
566
|
|
|
567
567
|
it('should clone work well', () => {
|
|
@@ -5,9 +5,9 @@ describe('AVL Tree Test', () => {
|
|
|
5
5
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
6
6
|
const avlTree = new AVLTree<number>();
|
|
7
7
|
|
|
8
|
-
for (const i of arr) avlTree.
|
|
8
|
+
for (const i of arr) avlTree.set([i, i]);
|
|
9
9
|
|
|
10
|
-
avlTree.
|
|
10
|
+
avlTree.set(null);
|
|
11
11
|
const node6 = avlTree.getNode(6);
|
|
12
12
|
|
|
13
13
|
expect(node6 && avlTree.getHeight(node6)).toBe(3);
|
|
@@ -111,17 +111,17 @@ describe('AVL Tree Test', () => {
|
|
|
111
111
|
const avlTree = new AVLTree<number, string>([4, 5, [1, '1'], 2, 3], { isMapMode: false });
|
|
112
112
|
expect(avlTree.get(1)).toBe('1');
|
|
113
113
|
expect(avlTree.getNode(1)?.value).toBe('1');
|
|
114
|
-
avlTree.
|
|
114
|
+
avlTree.set(1, 'a');
|
|
115
115
|
expect(avlTree.get(1)).toBe('a');
|
|
116
|
-
avlTree.
|
|
116
|
+
avlTree.set([1, 'b']);
|
|
117
117
|
expect(avlTree.getNode(1)?.value).toBe('b');
|
|
118
118
|
expect(avlTree.get(1)).toBe('b');
|
|
119
119
|
const treeMap = new AVLTree<number>([4, 5, [1, '1'], 2, 3]);
|
|
120
120
|
expect(treeMap.get(1)).toBe('1');
|
|
121
121
|
expect(treeMap.getNode(1)?.value).toBe(undefined);
|
|
122
|
-
treeMap.
|
|
122
|
+
treeMap.set(1, 'a');
|
|
123
123
|
expect(treeMap.get(1)).toBe('a');
|
|
124
|
-
treeMap.
|
|
124
|
+
treeMap.set([1, 'b']);
|
|
125
125
|
expect(treeMap.getNode(1)?.value).toBe(undefined);
|
|
126
126
|
expect(treeMap.get(1)).toBe('b');
|
|
127
127
|
});
|
|
@@ -132,7 +132,7 @@ describe('AVL Tree Test recursively', () => {
|
|
|
132
132
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
133
133
|
const avlTree = new AVLTree<number>([], { iterationType: 'RECURSIVE' });
|
|
134
134
|
|
|
135
|
-
for (const i of arr) avlTree.
|
|
135
|
+
for (const i of arr) avlTree.set([i, i]);
|
|
136
136
|
|
|
137
137
|
const node6 = avlTree.getNode(6);
|
|
138
138
|
|
|
@@ -243,15 +243,15 @@ describe('AVLTree APIs test', () => {
|
|
|
243
243
|
});
|
|
244
244
|
|
|
245
245
|
it('add', () => {
|
|
246
|
-
avlTree.
|
|
246
|
+
avlTree.set(1);
|
|
247
247
|
const node2 = new AVLTreeNode(2);
|
|
248
|
-
avlTree.
|
|
248
|
+
avlTree.set(node2);
|
|
249
249
|
const node3 = new AVLTreeNode(3, {
|
|
250
250
|
id: 3,
|
|
251
251
|
text: 'text3'
|
|
252
252
|
});
|
|
253
|
-
avlTree.
|
|
254
|
-
avlTree.
|
|
253
|
+
avlTree.set(node3);
|
|
254
|
+
avlTree.set([3, { id: 3, text: 'text33' }]);
|
|
255
255
|
|
|
256
256
|
const bfsRes = avlTree.bfs(node => node.key);
|
|
257
257
|
expect(bfsRes[0]).toBe(2);
|
|
@@ -270,7 +270,7 @@ describe('AVLTree APIs test', () => {
|
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
const avlTree = new AVLTree<string, number>();
|
|
273
|
-
avlTree.
|
|
273
|
+
avlTree.setMany([
|
|
274
274
|
['2', 2],
|
|
275
275
|
['4', 4],
|
|
276
276
|
['5', 5],
|
|
@@ -298,26 +298,26 @@ describe('AVLTree APIs test', () => {
|
|
|
298
298
|
describe('AVLTree', () => {
|
|
299
299
|
it('should balance the avlTree using _balanceLR when nodes are added', () => {
|
|
300
300
|
const avlTree = new AVLTree();
|
|
301
|
-
avlTree.
|
|
302
|
-
avlTree.
|
|
303
|
-
avlTree.
|
|
304
|
-
avlTree.
|
|
305
|
-
avlTree.
|
|
301
|
+
avlTree.set([10, 'A']);
|
|
302
|
+
avlTree.set([5, 'B']);
|
|
303
|
+
avlTree.set([15, 'C']);
|
|
304
|
+
avlTree.set([3, 'D']);
|
|
305
|
+
avlTree.set([7, 'E']);
|
|
306
306
|
|
|
307
307
|
// Adding nodes to trigger _balanceLR
|
|
308
|
-
avlTree.
|
|
308
|
+
avlTree.set([12, 'F']);
|
|
309
309
|
|
|
310
310
|
// You can add more specific assertions to check the avlTree's balance and structure.
|
|
311
311
|
});
|
|
312
312
|
|
|
313
313
|
it('should addMany undefined and null', () => {
|
|
314
314
|
const avlTree = new AVLTree<number, string>();
|
|
315
|
-
const addManyWithUndefined = avlTree.
|
|
315
|
+
const addManyWithUndefined = avlTree.setMany([1, undefined, 3]);
|
|
316
316
|
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
317
317
|
expect(avlTree.get(undefined)).toBe(undefined);
|
|
318
|
-
const addManyWithNull = avlTree.
|
|
318
|
+
const addManyWithNull = avlTree.setMany([1, null, 3, 4]);
|
|
319
319
|
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
320
|
-
const addManyEntriesWithNull = avlTree.
|
|
320
|
+
const addManyEntriesWithNull = avlTree.setMany([
|
|
321
321
|
[1, '1'],
|
|
322
322
|
[null, 'null'],
|
|
323
323
|
[3, '3'],
|
|
@@ -325,19 +325,19 @@ describe('AVLTree', () => {
|
|
|
325
325
|
]);
|
|
326
326
|
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
327
327
|
expect(avlTree.get(null)).toBe(undefined);
|
|
328
|
-
const node0 = avlTree.
|
|
328
|
+
const node0 = avlTree.set(0, '0');
|
|
329
329
|
expect(node0).toBe(true);
|
|
330
330
|
expect(avlTree.get(0)).toBe('0');
|
|
331
331
|
});
|
|
332
332
|
|
|
333
333
|
it('should balance the avlTree using _balanceLR when nodes are deleted', () => {
|
|
334
334
|
const avlTree = new AVLTree();
|
|
335
|
-
avlTree.
|
|
336
|
-
avlTree.
|
|
337
|
-
avlTree.
|
|
338
|
-
avlTree.
|
|
339
|
-
avlTree.
|
|
340
|
-
avlTree.
|
|
335
|
+
avlTree.set([10, 'A']);
|
|
336
|
+
avlTree.set([5, 'B']);
|
|
337
|
+
avlTree.set([15, 'C']);
|
|
338
|
+
avlTree.set([3, 'D']);
|
|
339
|
+
avlTree.set([7, 'E']);
|
|
340
|
+
avlTree.set([12, 'F']);
|
|
341
341
|
|
|
342
342
|
// Deleting nodes to trigger _balanceLR
|
|
343
343
|
avlTree.delete(3);
|
|
@@ -352,15 +352,15 @@ describe('AVLTree', () => {
|
|
|
352
352
|
});
|
|
353
353
|
|
|
354
354
|
it('add', () => {
|
|
355
|
-
avlTree.
|
|
355
|
+
avlTree.set(1);
|
|
356
356
|
const node2 = new AVLTreeNode(2);
|
|
357
|
-
avlTree.
|
|
357
|
+
avlTree.set(node2);
|
|
358
358
|
const node3 = new AVLTreeNode(3, {
|
|
359
359
|
id: 3,
|
|
360
360
|
text: 'text3'
|
|
361
361
|
});
|
|
362
|
-
avlTree.
|
|
363
|
-
avlTree.
|
|
362
|
+
avlTree.set(node3);
|
|
363
|
+
avlTree.set([3, { id: 3, text: 'text33' }]);
|
|
364
364
|
|
|
365
365
|
const bfsRes = avlTree.bfs(node => node);
|
|
366
366
|
expect(bfsRes[0]?.key).toBe(2);
|
|
@@ -372,9 +372,9 @@ describe('AVLTree iterative methods test', () => {
|
|
|
372
372
|
let avlTree: AVLTree<number, string>;
|
|
373
373
|
beforeEach(() => {
|
|
374
374
|
avlTree = new AVLTree();
|
|
375
|
-
avlTree.
|
|
376
|
-
avlTree.
|
|
377
|
-
avlTree.
|
|
375
|
+
avlTree.set([1, 'a']);
|
|
376
|
+
avlTree.set([2, 'b']);
|
|
377
|
+
avlTree.set([3, 'c']);
|
|
378
378
|
});
|
|
379
379
|
|
|
380
380
|
it('The node obtained by get Node should match the node type', () => {
|
|
@@ -461,9 +461,9 @@ describe('AVL Tree not map mode', () => {
|
|
|
461
461
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
462
462
|
const avlTree = new AVLTree<number>([], { isMapMode: false });
|
|
463
463
|
|
|
464
|
-
for (const i of arr) avlTree.
|
|
464
|
+
for (const i of arr) avlTree.set([i, i]);
|
|
465
465
|
|
|
466
|
-
avlTree.
|
|
466
|
+
avlTree.set(null);
|
|
467
467
|
const node6 = avlTree.getNode(6);
|
|
468
468
|
|
|
469
469
|
expect(node6 && avlTree.getHeight(node6)).toBe(3);
|
|
@@ -497,7 +497,7 @@ describe('AVL Tree not map mode test recursively', () => {
|
|
|
497
497
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
498
498
|
const avlTree = new AVLTree<number>([], { iterationType: 'RECURSIVE', isMapMode: false });
|
|
499
499
|
|
|
500
|
-
for (const i of arr) avlTree.
|
|
500
|
+
for (const i of arr) avlTree.set([i, i]);
|
|
501
501
|
|
|
502
502
|
const node6 = avlTree.getNode(6);
|
|
503
503
|
|
|
@@ -531,9 +531,9 @@ describe('AVLTree iterative methods not map mode', () => {
|
|
|
531
531
|
let avlTree: AVLTree<number, string>;
|
|
532
532
|
beforeEach(() => {
|
|
533
533
|
avlTree = new AVLTree<number, string>([], { isMapMode: false });
|
|
534
|
-
avlTree.
|
|
535
|
-
avlTree.
|
|
536
|
-
avlTree.
|
|
534
|
+
avlTree.set([1, 'a']);
|
|
535
|
+
avlTree.set([2, 'b']);
|
|
536
|
+
avlTree.set([3, 'c']);
|
|
537
537
|
});
|
|
538
538
|
|
|
539
539
|
it('should clone work well', () => {
|
|
@@ -562,7 +562,7 @@ describe('classic use', () => {
|
|
|
562
562
|
expect(tree.size).toBe(5);
|
|
563
563
|
|
|
564
564
|
// Add a new element
|
|
565
|
-
tree.
|
|
565
|
+
tree.set(3);
|
|
566
566
|
expect(tree.size).toBe(6);
|
|
567
567
|
expect([...tree.keys()]).toEqual([1, 2, 3, 5, 8, 9]);
|
|
568
568
|
});
|
|
@@ -628,7 +628,7 @@ describe('classic use', () => {
|
|
|
628
628
|
expect(universityTree.isAVLBalanced()).toBe(true);
|
|
629
629
|
|
|
630
630
|
// Add more universities
|
|
631
|
-
universityTree.
|
|
631
|
+
universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
632
632
|
expect(universityTree.isAVLBalanced()).toBe(true);
|
|
633
633
|
|
|
634
634
|
// Delete and verify balance is maintained
|