data-structure-typed 1.54.2 → 2.0.0
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/LICENSE +2 -2
- package/README.md +14 -1
- package/README_zh-CN.md +1 -1
- package/benchmark/report.html +28 -1
- package/benchmark/report.json +327 -18
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/cjs/data-structures/base/iterable-element-base.js +14 -11
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +277 -0
- package/dist/cjs/data-structures/base/linear-base.js +553 -0
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +89 -21
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +76 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +244 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/cjs/data-structures/binary-tree/bst.js +89 -133
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +7 -13
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +211 -41
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +3 -11
- package/dist/cjs/data-structures/heap/heap.js +0 -10
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +217 -169
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +130 -91
- package/dist/cjs/data-structures/queue/deque.js +269 -169
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +84 -40
- package/dist/cjs/data-structures/queue/queue.js +134 -50
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +3 -11
- package/dist/cjs/data-structures/stack/stack.js +0 -10
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +4 -3
- package/dist/cjs/data-structures/trie/trie.js +3 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +9 -4
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/cjs/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/cjs/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/cjs/utils/utils.d.ts +2 -2
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/esm/data-structures/base/iterable-element-base.js +14 -11
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +277 -0
- package/dist/esm/data-structures/base/linear-base.js +549 -0
- package/dist/esm/data-structures/base/linear-base.js.map +1 -0
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +9 -8
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +52 -38
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +89 -21
- package/dist/esm/data-structures/binary-tree/avl-tree.js +77 -9
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/esm/data-structures/binary-tree/binary-tree.js +248 -153
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/esm/data-structures/binary-tree/bst.js +90 -132
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +8 -14
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/esm/data-structures/binary-tree/tree-counter.js +13 -13
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +212 -41
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.js +2 -2
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +3 -11
- package/dist/esm/data-structures/heap/heap.js +0 -10
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +132 -148
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +79 -75
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +217 -170
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +130 -91
- package/dist/esm/data-structures/queue/deque.js +269 -169
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +84 -40
- package/dist/esm/data-structures/queue/queue.js +135 -51
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +3 -11
- package/dist/esm/data-structures/stack/stack.js +0 -10
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +4 -3
- package/dist/esm/data-structures/trie/trie.js +3 -0
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/types/data-structures/base/base.d.ts +9 -4
- package/dist/esm/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/esm/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/esm/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/esm/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/esm/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/esm/types/data-structures/queue/queue.d.ts +2 -2
- package/dist/esm/utils/utils.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +1548 -920
- package/dist/umd/data-structure-typed.min.js +5 -12
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/data-structures/base/iterable-element-base.ts +29 -20
- package/src/data-structures/base/linear-base.ts +649 -0
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
- package/src/data-structures/binary-tree/avl-tree.ts +99 -29
- package/src/data-structures/binary-tree/binary-tree.ts +474 -257
- package/src/data-structures/binary-tree/bst.ts +150 -152
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/heap/heap.ts +3 -14
- package/src/data-structures/heap/max-heap.ts +2 -2
- package/src/data-structures/heap/min-heap.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
- package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
- package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
- package/src/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/data-structures/queue/deque.ts +286 -183
- package/src/data-structures/queue/queue.ts +149 -63
- package/src/data-structures/stack/stack.ts +3 -18
- package/src/data-structures/trie/trie.ts +7 -3
- package/src/types/data-structures/base/base.ts +17 -8
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
- package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/types/data-structures/queue/deque.ts +2 -3
- package/src/types/data-structures/queue/queue.ts +2 -2
- package/src/utils/utils.ts +2 -2
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/avl-tree.test.ts +1 -1
- package/test/integration/bst.test.ts +2 -2
- package/test/integration/compile.mjs +21 -21
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +71 -0
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +81 -0
- package/test/performance/{reportor.js → reportor.mjs} +264 -8
- package/test/performance/reportor.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +172 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +72 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +132 -82
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +5 -13
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +168 -10
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +135 -27
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +111 -11
- package/test/unit/data-structures/queue/deque.test.ts +241 -60
- package/test/unit/data-structures/queue/queue.test.ts +118 -19
- package/test/unit/unrestricted-interconversion.test.ts +1 -1
- package/test/utils/json2html.ts +0 -154
- package/test/performance/data-structures/binary-tree/avl-tree.test.js +0 -45
- /package/test/performance/data-structures/binary-tree/{rb-tree.test.ts → red-black-tree.test.ts} +0 -0
|
@@ -90,7 +90,7 @@ describe('BST operations test', () => {
|
|
|
90
90
|
expect(nodes.map(node => node.key)).toEqual([15]);
|
|
91
91
|
|
|
92
92
|
let subTreeSum = 0;
|
|
93
|
-
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
93
|
+
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
94
94
|
expect(subTreeSum).toBe(70);
|
|
95
95
|
|
|
96
96
|
let lesserSum = 0;
|
|
@@ -299,7 +299,7 @@ describe('BST operations test', () => {
|
|
|
299
299
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
300
300
|
|
|
301
301
|
let subTreeSum = 0;
|
|
302
|
-
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', node15);
|
|
302
|
+
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', false, node15);
|
|
303
303
|
expect(subTreeSum).toBe(70);
|
|
304
304
|
|
|
305
305
|
let lesserSum = 0;
|
|
@@ -492,7 +492,7 @@ describe('BST operations test', () => {
|
|
|
492
492
|
|
|
493
493
|
it('should search in range', () => {
|
|
494
494
|
const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
495
|
-
expect(bst.rangeSearch([4, 12])).toEqual([
|
|
495
|
+
expect(bst.rangeSearch([4, 12])).toEqual([5, 7, 10, 12]);
|
|
496
496
|
expect(() => bst.rangeSearch([12, 4])).toThrow('low must be less than or equal to high');
|
|
497
497
|
expect(bst.rangeSearch([12, 12])).toEqual([12]);
|
|
498
498
|
});
|
|
@@ -535,7 +535,7 @@ describe('BST operations test recursively', () => {
|
|
|
535
535
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
536
536
|
|
|
537
537
|
let subTreeSum = 0;
|
|
538
|
-
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
538
|
+
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
539
539
|
expect(subTreeSum).toBe(70);
|
|
540
540
|
|
|
541
541
|
let lesserSum = 0;
|
|
@@ -742,7 +742,7 @@ describe('BST operations test recursively', () => {
|
|
|
742
742
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
743
743
|
|
|
744
744
|
let subTreeSum = 0;
|
|
745
|
-
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', node15);
|
|
745
|
+
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', false, node15);
|
|
746
746
|
expect(subTreeSum).toBe(70);
|
|
747
747
|
|
|
748
748
|
let lesserSum = 0;
|
|
@@ -1080,10 +1080,10 @@ describe('BST Performance test', function () {
|
|
|
1080
1080
|
it('should dfs as sub tree traversal, null should be ignored', () => {
|
|
1081
1081
|
const bst = new BST();
|
|
1082
1082
|
bst.addMany([4, 2, 6, 1, 3, 5, 7]);
|
|
1083
|
-
expect(bst.dfs(node => node.key, 'PRE', bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1084
|
-
expect(bst.dfs(node => node.key, 'PRE', bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1085
|
-
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1086
|
-
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1083
|
+
expect(bst.dfs(node => node.key, 'PRE', false, bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1084
|
+
expect(bst.dfs(node => node.key, 'PRE', false, bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1085
|
+
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', false, bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1086
|
+
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', false, bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1087
1087
|
});
|
|
1088
1088
|
});
|
|
1089
1089
|
|
|
@@ -1565,9 +1565,9 @@ describe('classic use', () => {
|
|
|
1565
1565
|
// Test case for finding elements in a given range
|
|
1566
1566
|
it('@example Find elements in a range', () => {
|
|
1567
1567
|
const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
1568
|
-
expect(bst.search(new Range(5, 10))).toEqual([
|
|
1569
|
-
expect(bst.rangeSearch([4, 12], node => node.key.toString())).toEqual(['
|
|
1570
|
-
expect(bst.search(new Range(4, 12, true, false))).toEqual([
|
|
1568
|
+
expect(bst.search(new Range(5, 10))).toEqual([5, 7, 10]);
|
|
1569
|
+
expect(bst.rangeSearch([4, 12], node => node.key.toString())).toEqual(['5', '7', '10', '12']);
|
|
1570
|
+
expect(bst.search(new Range(4, 12, true, false))).toEqual([5, 7, 10]);
|
|
1571
1571
|
expect(bst.rangeSearch([15, 20])).toEqual([15, 18]);
|
|
1572
1572
|
expect(bst.search(new Range(15, 20, false))).toEqual([18]);
|
|
1573
1573
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryTreeNode, BSTNode,
|
|
1
|
+
import { BinaryTreeNode, BSTNode, RedBlackTree, RedBlackTreeNode } from '../../../../src';
|
|
2
2
|
import { getRandomInt, getRandomIntArray, magnitude } from '../../../utils';
|
|
3
3
|
import { OrderedMap } from 'js-sdsl';
|
|
4
4
|
|
|
@@ -504,11 +504,11 @@ describe('RedBlackTree 2', () => {
|
|
|
504
504
|
expect(rbTree.isBST()).toBe(true);
|
|
505
505
|
expect(rbTree.isBST(rbTree.root, 'RECURSIVE')).toBe(true);
|
|
506
506
|
|
|
507
|
-
expect(rbTree.dfs(n => n.key, 'IN', rbTree.root, 'ITERATIVE')).toEqual([
|
|
507
|
+
expect(rbTree.dfs(n => n.key, 'IN', false, rbTree.root, 'ITERATIVE')).toEqual([
|
|
508
508
|
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
509
509
|
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
510
510
|
]);
|
|
511
|
-
expect(rbTree.dfs(n => n.key, 'IN', rbTree.root, 'RECURSIVE')).toEqual([
|
|
511
|
+
expect(rbTree.dfs(n => n.key, 'IN', false, rbTree.root, 'RECURSIVE')).toEqual([
|
|
512
512
|
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
513
513
|
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
514
514
|
]);
|
|
@@ -524,7 +524,7 @@ describe('RedBlackTree 2', () => {
|
|
|
524
524
|
|
|
525
525
|
expect(rbTree.size).toBe(0);
|
|
526
526
|
expect(rbTree.isBST()).toBe(true);
|
|
527
|
-
expect(rbTree.dfs(n => n.key, 'IN', rbTree.root, 'ITERATIVE')).toEqual([]);
|
|
527
|
+
expect(rbTree.dfs(n => n.key, 'IN', false, rbTree.root, 'ITERATIVE')).toEqual([]);
|
|
528
528
|
|
|
529
529
|
rbTree.clear();
|
|
530
530
|
for (let i = 0; i < 1000; i++) {
|
|
@@ -838,14 +838,6 @@ describe('real world data', () => {
|
|
|
838
838
|
});
|
|
839
839
|
|
|
840
840
|
describe('classic use', () => {
|
|
841
|
-
// Test case for finding elements in a given range
|
|
842
|
-
it('@example Find elements in a range', () => {
|
|
843
|
-
const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
844
|
-
expect(bst.search(new Range(5, 10))).toEqual([5, 10, 7]);
|
|
845
|
-
expect(bst.search(new Range(4, 12))).toEqual([5, 10, 12, 7]);
|
|
846
|
-
expect(bst.search(new Range(15, 20))).toEqual([15, 18]);
|
|
847
|
-
});
|
|
848
|
-
|
|
849
841
|
it('@example using Red-Black Tree as a price-based index for stock data', () => {
|
|
850
842
|
// Define the structure of individual stock records
|
|
851
843
|
interface StockRecord {
|
|
@@ -887,6 +879,6 @@ describe('classic use', () => {
|
|
|
887
879
|
[200, 400], // Price range
|
|
888
880
|
node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
|
|
889
881
|
);
|
|
890
|
-
expect(stocksInRange).toEqual(['GOOGL', '
|
|
882
|
+
expect(stocksInRange).toEqual(['GOOGL', 'META', 'MSFT']); // Verify stocks in the range
|
|
891
883
|
});
|
|
892
884
|
});
|
|
@@ -149,7 +149,7 @@ describe('TreeCounter operations test1', () => {
|
|
|
149
149
|
expect(minNodeBySpecificNode?.key).toBe(14);
|
|
150
150
|
|
|
151
151
|
let subTreeSum = 0;
|
|
152
|
-
if (node15) treeCounter.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
152
|
+
if (node15) treeCounter.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
153
153
|
expect(subTreeSum).toBe(45);
|
|
154
154
|
let lesserSum = 0;
|
|
155
155
|
treeCounter.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
@@ -157,7 +157,7 @@ describe('TreeCounter operations test1', () => {
|
|
|
157
157
|
|
|
158
158
|
expect(node15 instanceof TreeCounterNode);
|
|
159
159
|
if (node15 instanceof TreeCounterNode) {
|
|
160
|
-
const subTreeAdd = treeCounter.dfs(node => (node.count += 1), 'PRE', 15);
|
|
160
|
+
const subTreeAdd = treeCounter.dfs(node => (node.count += 1), 'PRE', false, 15);
|
|
161
161
|
expect(subTreeAdd);
|
|
162
162
|
}
|
|
163
163
|
const node11 = treeCounter.getNode(11);
|
|
@@ -407,7 +407,7 @@ describe('TreeCounter operations test recursively1', () => {
|
|
|
407
407
|
expect(minNodeBySpecificNode?.key).toBe(14);
|
|
408
408
|
|
|
409
409
|
let subTreeSum = 0;
|
|
410
|
-
if (node15) treeCounter.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
410
|
+
if (node15) treeCounter.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
411
411
|
expect(subTreeSum).toBe(45);
|
|
412
412
|
let lesserSum = 0;
|
|
413
413
|
expect(treeCounter.has(9)).toBe(true);
|
|
@@ -423,7 +423,7 @@ describe('TreeCounter operations test recursively1', () => {
|
|
|
423
423
|
|
|
424
424
|
expect(node15 instanceof TreeCounterNode);
|
|
425
425
|
if (node15 instanceof TreeCounterNode) {
|
|
426
|
-
const subTreeAdd = treeCounter.dfs(node => (node.count += 1), 'PRE', 15);
|
|
426
|
+
const subTreeAdd = treeCounter.dfs(node => (node.count += 1), 'PRE', false, 15);
|
|
427
427
|
expect(subTreeAdd);
|
|
428
428
|
}
|
|
429
429
|
const node11 = treeCounter.getNode(11);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryTreeNode, BSTNode,
|
|
1
|
+
import { BinaryTreeNode, BSTNode, TreeMultiMap, TreeMultiMapNode } from '../../../../src';
|
|
2
2
|
import { getRandomInt } from '../../../utils';
|
|
3
3
|
|
|
4
4
|
import { isDebugTest } from '../../../config';
|
|
@@ -519,11 +519,11 @@ describe('TreeMultiMap 2', () => {
|
|
|
519
519
|
expect(tmm.isBST()).toBe(true);
|
|
520
520
|
expect(tmm.isBST(tmm.root, 'RECURSIVE')).toBe(true);
|
|
521
521
|
|
|
522
|
-
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([
|
|
522
|
+
expect(tmm.dfs(n => n.key, 'IN', false, tmm.root, 'ITERATIVE')).toEqual([
|
|
523
523
|
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
524
524
|
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
525
525
|
]);
|
|
526
|
-
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'RECURSIVE')).toEqual([
|
|
526
|
+
expect(tmm.dfs(n => n.key, 'IN', false, tmm.root, 'RECURSIVE')).toEqual([
|
|
527
527
|
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
|
|
528
528
|
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
|
|
529
529
|
]);
|
|
@@ -539,7 +539,7 @@ describe('TreeMultiMap 2', () => {
|
|
|
539
539
|
|
|
540
540
|
expect(tmm.size).toBe(0);
|
|
541
541
|
expect(tmm.isBST()).toBe(true);
|
|
542
|
-
expect(tmm.dfs(n => n.key, 'IN', tmm.root, 'ITERATIVE')).toEqual([]);
|
|
542
|
+
expect(tmm.dfs(n => n.key, 'IN', false, tmm.root, 'ITERATIVE')).toEqual([]);
|
|
543
543
|
|
|
544
544
|
tmm.clear();
|
|
545
545
|
for (let i = 0; i < 1000; i++) {
|
|
@@ -837,11 +837,169 @@ describe('real world data', () => {
|
|
|
837
837
|
});
|
|
838
838
|
|
|
839
839
|
describe('classic use', () => {
|
|
840
|
-
// Test
|
|
841
|
-
it('@example
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
840
|
+
// Test suite for TreeMultiMap with player ranking and equipment
|
|
841
|
+
it('@example players ranked by score with their equipment', () => {
|
|
842
|
+
type Equipment = {
|
|
843
|
+
name: string; // Equipment name
|
|
844
|
+
quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
845
|
+
level: number;
|
|
846
|
+
};
|
|
847
|
+
|
|
848
|
+
type Player = {
|
|
849
|
+
name: string;
|
|
850
|
+
score: number;
|
|
851
|
+
equipments: Equipment[];
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
// Mock player data with their scores and equipment
|
|
855
|
+
const players: Player[] = [
|
|
856
|
+
{
|
|
857
|
+
name: 'DragonSlayer',
|
|
858
|
+
score: 8750,
|
|
859
|
+
equipments: [
|
|
860
|
+
{ name: 'AWM', quality: 'legendary', level: 85 },
|
|
861
|
+
{ name: 'Level 3 Helmet', quality: 'epic', level: 80 },
|
|
862
|
+
{ name: 'Extended Quickdraw Mag', quality: 'rare', level: 75 },
|
|
863
|
+
{ name: 'Compensator', quality: 'epic', level: 78 },
|
|
864
|
+
{ name: 'Vertical Grip', quality: 'rare', level: 72 }
|
|
865
|
+
]
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
name: 'ShadowNinja',
|
|
869
|
+
score: 7200,
|
|
870
|
+
equipments: [
|
|
871
|
+
{ name: 'M416', quality: 'epic', level: 75 },
|
|
872
|
+
{ name: 'Ghillie Suit', quality: 'rare', level: 70 },
|
|
873
|
+
{ name: 'Red Dot Sight', quality: 'common', level: 65 },
|
|
874
|
+
{ name: 'Extended QuickDraw Mag', quality: 'rare', level: 68 }
|
|
875
|
+
]
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
name: 'RuneMaster',
|
|
879
|
+
score: 9100,
|
|
880
|
+
equipments: [
|
|
881
|
+
{ name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
882
|
+
{ name: 'Level 3 Vest', quality: 'legendary', level: 85 },
|
|
883
|
+
{ name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
884
|
+
{ name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
885
|
+
{ name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
886
|
+
]
|
|
887
|
+
},
|
|
888
|
+
{
|
|
889
|
+
name: 'BattleKing',
|
|
890
|
+
score: 8500,
|
|
891
|
+
equipments: [
|
|
892
|
+
{ name: 'AUG', quality: 'epic', level: 82 },
|
|
893
|
+
{ name: 'Red Dot Sight', quality: 'rare', level: 75 },
|
|
894
|
+
{ name: 'Extended Mag', quality: 'common', level: 70 },
|
|
895
|
+
{ name: 'Tactical Stock', quality: 'rare', level: 76 }
|
|
896
|
+
]
|
|
897
|
+
},
|
|
898
|
+
{
|
|
899
|
+
name: 'SniperElite',
|
|
900
|
+
score: 7800,
|
|
901
|
+
equipments: [
|
|
902
|
+
{ name: 'M24', quality: 'legendary', level: 88 },
|
|
903
|
+
{ name: 'Compensator', quality: 'epic', level: 80 },
|
|
904
|
+
{ name: 'Scope 8x', quality: 'legendary', level: 85 },
|
|
905
|
+
{ name: 'Level 2 Helmet', quality: 'rare', level: 75 }
|
|
906
|
+
]
|
|
907
|
+
},
|
|
908
|
+
{
|
|
909
|
+
name: 'RushMaster',
|
|
910
|
+
score: 7500,
|
|
911
|
+
equipments: [
|
|
912
|
+
{ name: 'Vector', quality: 'rare', level: 72 },
|
|
913
|
+
{ name: 'Level 2 Helmet', quality: 'common', level: 65 },
|
|
914
|
+
{ name: 'Quickdraw Mag', quality: 'common', level: 60 },
|
|
915
|
+
{ name: 'Laser Sight', quality: 'rare', level: 68 }
|
|
916
|
+
]
|
|
917
|
+
},
|
|
918
|
+
{
|
|
919
|
+
name: 'GhostWarrior',
|
|
920
|
+
score: 8200,
|
|
921
|
+
equipments: [
|
|
922
|
+
{ name: 'SCAR-L', quality: 'epic', level: 78 },
|
|
923
|
+
{ name: 'Extended Quickdraw Mag', quality: 'rare', level: 70 },
|
|
924
|
+
{ name: 'Holographic Sight', quality: 'epic', level: 75 },
|
|
925
|
+
{ name: 'Suppressor', quality: 'rare', level: 72 },
|
|
926
|
+
{ name: 'Vertical Grip', quality: 'common', level: 65 }
|
|
927
|
+
]
|
|
928
|
+
},
|
|
929
|
+
{
|
|
930
|
+
name: 'DeathDealer',
|
|
931
|
+
score: 7300,
|
|
932
|
+
equipments: [
|
|
933
|
+
{ name: 'SKS', quality: 'epic', level: 76 },
|
|
934
|
+
{ name: 'Holographic Sight', quality: 'rare', level: 68 },
|
|
935
|
+
{ name: 'Extended Mag', quality: 'common', level: 65 }
|
|
936
|
+
]
|
|
937
|
+
},
|
|
938
|
+
{
|
|
939
|
+
name: 'StormRider',
|
|
940
|
+
score: 8900,
|
|
941
|
+
equipments: [
|
|
942
|
+
{ name: 'MK14', quality: 'legendary', level: 92 },
|
|
943
|
+
{ name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
944
|
+
{ name: 'Scope 8x', quality: 'epic', level: 80 },
|
|
945
|
+
{ name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
946
|
+
{ name: 'Tactical Stock', quality: 'rare', level: 75 }
|
|
947
|
+
]
|
|
948
|
+
},
|
|
949
|
+
{
|
|
950
|
+
name: 'CombatLegend',
|
|
951
|
+
score: 7600,
|
|
952
|
+
equipments: [
|
|
953
|
+
{ name: 'UMP45', quality: 'rare', level: 74 },
|
|
954
|
+
{ name: 'Level 2 Vest', quality: 'common', level: 67 },
|
|
955
|
+
{ name: 'Red Dot Sight', quality: 'common', level: 62 },
|
|
956
|
+
{ name: 'Extended Mag', quality: 'rare', level: 70 }
|
|
957
|
+
]
|
|
958
|
+
}
|
|
959
|
+
];
|
|
960
|
+
|
|
961
|
+
// Create a TreeMultiMap for player rankings
|
|
962
|
+
const playerRankings = new TreeMultiMap<number, Equipment, Player>(players, {
|
|
963
|
+
toEntryFn: ({ score, equipments }) => [score, equipments],
|
|
964
|
+
isMapMode: false
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
const topPlayersEquipments = playerRankings.rangeSearch([8900, 10000], node => playerRankings.get(node));
|
|
968
|
+
expect(topPlayersEquipments).toEqual([
|
|
969
|
+
[
|
|
970
|
+
{
|
|
971
|
+
name: 'MK14',
|
|
972
|
+
quality: 'legendary',
|
|
973
|
+
level: 92
|
|
974
|
+
},
|
|
975
|
+
{ name: 'Level 3 Backpack', quality: 'legendary', level: 85 },
|
|
976
|
+
{
|
|
977
|
+
name: 'Scope 8x',
|
|
978
|
+
quality: 'epic',
|
|
979
|
+
level: 80
|
|
980
|
+
},
|
|
981
|
+
{ name: 'Suppressor', quality: 'legendary', level: 88 },
|
|
982
|
+
{
|
|
983
|
+
name: 'Tactical Stock',
|
|
984
|
+
quality: 'rare',
|
|
985
|
+
level: 75
|
|
986
|
+
}
|
|
987
|
+
],
|
|
988
|
+
[
|
|
989
|
+
{ name: 'KAR98K', quality: 'legendary', level: 90 },
|
|
990
|
+
{
|
|
991
|
+
name: 'Level 3 Vest',
|
|
992
|
+
quality: 'legendary',
|
|
993
|
+
level: 85
|
|
994
|
+
},
|
|
995
|
+
{ name: 'Holographic Sight', quality: 'epic', level: 82 },
|
|
996
|
+
{
|
|
997
|
+
name: 'Suppressor',
|
|
998
|
+
quality: 'legendary',
|
|
999
|
+
level: 88
|
|
1000
|
+
},
|
|
1001
|
+
{ name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
1002
|
+
]
|
|
1003
|
+
]);
|
|
846
1004
|
});
|
|
847
1005
|
});
|
|
@@ -32,14 +32,14 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
it('should deleteAt', () => {
|
|
35
|
-
expect(list.deleteAt(1)).toBe(
|
|
36
|
-
expect(list.deleteAt(-1)).toBe(
|
|
37
|
-
expect(list.deleteAt(list.
|
|
38
|
-
expect(list.
|
|
39
|
-
expect(list.deleteAt(4)).toBe(
|
|
35
|
+
expect(list.deleteAt(1)).toBe(2);
|
|
36
|
+
expect(list.deleteAt(-1)).toBe(undefined);
|
|
37
|
+
expect(list.deleteAt(list.length)).toBe(undefined);
|
|
38
|
+
expect(list.length).toBe(4);
|
|
39
|
+
expect(list.deleteAt(4)).toBe(undefined);
|
|
40
40
|
expect([...list]).toEqual([1, 3, 4, 5]);
|
|
41
41
|
expect(list.isEmpty()).toBe(false);
|
|
42
|
-
expect(list.deleteAt(3)).toBe(
|
|
42
|
+
expect(list.deleteAt(3)).toBe(5);
|
|
43
43
|
expect([...list]).toEqual([1, 3, 4]);
|
|
44
44
|
});
|
|
45
45
|
|
|
@@ -103,7 +103,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
103
103
|
});
|
|
104
104
|
|
|
105
105
|
it('should initialize an empty list', () => {
|
|
106
|
-
expect(list.
|
|
106
|
+
expect(list.length).toBe(0);
|
|
107
107
|
expect(list.head).toBe(undefined);
|
|
108
108
|
expect(list.tail).toBe(undefined);
|
|
109
109
|
});
|
|
@@ -119,17 +119,36 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
119
119
|
list.push(1);
|
|
120
120
|
list.push(2);
|
|
121
121
|
list.push(3);
|
|
122
|
-
expect(list.
|
|
122
|
+
expect(list.length).toBe(3);
|
|
123
123
|
expect(list.head!.value).toBe(1);
|
|
124
124
|
expect(list.tail!.value).toBe(3);
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
+
it('push with maxLen', () => {
|
|
128
|
+
const list = new DoublyLinkedList<number>([], { maxLen: 10 });
|
|
129
|
+
for (let i = 0; i < 1000; i++) {
|
|
130
|
+
list.push(i);
|
|
131
|
+
}
|
|
132
|
+
expect(list.maxLen).toBe(10);
|
|
133
|
+
expect(list.length).toBe(10);
|
|
134
|
+
expect(list.first).toBe(990);
|
|
135
|
+
|
|
136
|
+
list.clear();
|
|
137
|
+
for (let i = 0; i < 1000; i++) {
|
|
138
|
+
list.unshift(i);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
expect(list.maxLen).toBe(10);
|
|
142
|
+
expect(list.length).toBe(10);
|
|
143
|
+
expect(list.last).toBe(990);
|
|
144
|
+
});
|
|
145
|
+
|
|
127
146
|
it('should pop elements from the end of the list', () => {
|
|
128
147
|
list.push(1);
|
|
129
148
|
list.push(2);
|
|
130
149
|
const poppedValue = list.pop();
|
|
131
150
|
expect(poppedValue).toBe(2);
|
|
132
|
-
expect(list.
|
|
151
|
+
expect(list.length).toBe(1);
|
|
133
152
|
expect(list.head!.value).toBe(1);
|
|
134
153
|
expect(list.tail!.value).toBe(1);
|
|
135
154
|
list.pop();
|
|
@@ -145,19 +164,19 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
145
164
|
|
|
146
165
|
// Inserting at the beginning
|
|
147
166
|
list.addAt(0, 0);
|
|
148
|
-
expect(list.
|
|
167
|
+
expect(list.length).toBe(4);
|
|
149
168
|
expect(list.at(0)).toBe(0);
|
|
150
169
|
expect(list.at(1)).toBe(1);
|
|
151
170
|
|
|
152
171
|
// Inserting in the middle
|
|
153
172
|
list.addAt(2, 1.5);
|
|
154
|
-
expect(list.
|
|
173
|
+
expect(list.length).toBe(5);
|
|
155
174
|
expect(list.at(2)).toBe(1.5);
|
|
156
175
|
expect(list.at(3)).toBe(2);
|
|
157
176
|
|
|
158
177
|
// Inserting at the end
|
|
159
178
|
list.addAt(5, 4);
|
|
160
|
-
expect(list.
|
|
179
|
+
expect(list.length).toBe(6);
|
|
161
180
|
expect(list.at(5)).toBe(4);
|
|
162
181
|
expect(list.tail!.value).toBe(4);
|
|
163
182
|
expect(list.at(-1)).toBe(undefined);
|
|
@@ -171,18 +190,18 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
171
190
|
|
|
172
191
|
// Deleting from the beginning
|
|
173
192
|
const deletedValue = list.deleteAt(0);
|
|
174
|
-
expect(deletedValue).toBe(
|
|
175
|
-
expect(list.
|
|
193
|
+
expect(deletedValue).toBe(1);
|
|
194
|
+
expect(list.length).toBe(2);
|
|
176
195
|
expect(list.head!.value).toBe(2);
|
|
177
196
|
|
|
178
197
|
// Deleting from the middle
|
|
179
198
|
list.deleteAt(0); // Deleting the second element
|
|
180
|
-
expect(list.
|
|
199
|
+
expect(list.length).toBe(1);
|
|
181
200
|
expect(list.head!.value).toBe(3);
|
|
182
201
|
|
|
183
202
|
// Deleting from the end
|
|
184
203
|
list.deleteAt(0);
|
|
185
|
-
expect(list.
|
|
204
|
+
expect(list.length).toBe(0);
|
|
186
205
|
expect(list.head).toBe(undefined);
|
|
187
206
|
expect(list.tail).toBe(undefined);
|
|
188
207
|
});
|
|
@@ -193,16 +212,16 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
193
212
|
list.push(3);
|
|
194
213
|
|
|
195
214
|
list.delete(2);
|
|
196
|
-
expect(list.
|
|
215
|
+
expect(list.length).toBe(2);
|
|
197
216
|
expect(list.head!.value).toBe(1);
|
|
198
217
|
expect(list.tail!.value).toBe(3);
|
|
199
218
|
|
|
200
219
|
list.delete(1);
|
|
201
|
-
expect(list.
|
|
220
|
+
expect(list.length).toBe(1);
|
|
202
221
|
expect(list.head!.value).toBe(3);
|
|
203
222
|
|
|
204
223
|
list.delete(3);
|
|
205
|
-
expect(list.
|
|
224
|
+
expect(list.length).toBe(0);
|
|
206
225
|
expect(list.head).toBe(undefined);
|
|
207
226
|
expect(list.tail).toBe(undefined);
|
|
208
227
|
});
|
|
@@ -307,7 +326,7 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
307
326
|
|
|
308
327
|
list.clear();
|
|
309
328
|
|
|
310
|
-
expect(list.
|
|
329
|
+
expect(list.length).toBe(0);
|
|
311
330
|
expect(list.head).toBe(undefined);
|
|
312
331
|
expect(list.tail).toBe(undefined);
|
|
313
332
|
});
|
|
@@ -447,6 +466,95 @@ describe('DoublyLinkedList Operation Test', () => {
|
|
|
447
466
|
});
|
|
448
467
|
});
|
|
449
468
|
|
|
469
|
+
describe('DoublyLinkedList Additional Methods', () => {
|
|
470
|
+
// Slice method implementation and test
|
|
471
|
+
test('slice should return a new list with specified range', () => {
|
|
472
|
+
const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
473
|
+
const slicedList = list.slice(1, 4);
|
|
474
|
+
|
|
475
|
+
expect(slicedList.toArray()).toEqual([2, 3, 4]);
|
|
476
|
+
expect(list.length).toBe(5); // Original list unchanged
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
// Splice method implementation
|
|
480
|
+
test('splice should modify list and return removed elements', () => {
|
|
481
|
+
const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
482
|
+
const removedList = list.splice(2, 2, 6, 7);
|
|
483
|
+
|
|
484
|
+
expect(list.toArray()).toEqual([1, 2, 6, 7, 5]);
|
|
485
|
+
expect(removedList.toArray()).toEqual([3, 4]);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
// Concat method test
|
|
489
|
+
test('concat should combine multiple lists', () => {
|
|
490
|
+
const list1 = new DoublyLinkedList([1, 2]);
|
|
491
|
+
const list2 = new DoublyLinkedList([3, 4]);
|
|
492
|
+
const list3 = new DoublyLinkedList([5, 6]);
|
|
493
|
+
|
|
494
|
+
const concatenatedList = list1.concat(list2, list3);
|
|
495
|
+
expect(concatenatedList.toArray()).toEqual([1, 2, 3, 4, 5, 6]);
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
// Sort method test
|
|
499
|
+
test('sort should order elements in ascending order', () => {
|
|
500
|
+
const list = new DoublyLinkedList([5, 2, 8, 1, 9]);
|
|
501
|
+
list.sort((a, b) => a - b);
|
|
502
|
+
|
|
503
|
+
expect(list.toArray()).toEqual([1, 2, 5, 8, 9]);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
// Reverse method test
|
|
507
|
+
test('reverse should invert the list order', () => {
|
|
508
|
+
const list = new DoublyLinkedList([1, 2, 3, 4, 5]);
|
|
509
|
+
list.reverse();
|
|
510
|
+
|
|
511
|
+
expect(list.toArray()).toEqual([5, 4, 3, 2, 1]);
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
// Join method test
|
|
515
|
+
test('join should convert list to string with separator', () => {
|
|
516
|
+
const list = new DoublyLinkedList(['a', 'b', 'c']);
|
|
517
|
+
|
|
518
|
+
expect(list.join('-')).toBe('a-b-c');
|
|
519
|
+
expect(list.join()).toBe('a,b,c');
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
// IndexOf method test
|
|
523
|
+
test('indexOf should return first occurrence index', () => {
|
|
524
|
+
const list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
525
|
+
|
|
526
|
+
expect(list.indexOf(2)).toBe(1);
|
|
527
|
+
expect(list.indexOf(4)).toBe(-1);
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
// LastIndexOf method test
|
|
531
|
+
test('lastIndexOf should return last occurrence index', () => {
|
|
532
|
+
const list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
533
|
+
|
|
534
|
+
expect(list.lastIndexOf(2)).toBe(3);
|
|
535
|
+
expect(list.lastIndexOf(4)).toBe(-1);
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
// findIndex method test
|
|
539
|
+
test('findIndex should return first occurrence index', () => {
|
|
540
|
+
const list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
541
|
+
expect(list.findIndex(item => item === 2)).toBe(1);
|
|
542
|
+
expect(list.findIndex(item => item === 4)).toBe(-1);
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// fill method test
|
|
546
|
+
test('fill should return fill all the list', () => {
|
|
547
|
+
let list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
548
|
+
expect([...list.fill(9)]).toEqual([9, 9, 9, 9, 9]);
|
|
549
|
+
list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
550
|
+
expect([...list.fill(9, 2, 3)]).toEqual([1, 2, 9, 2, 1]);
|
|
551
|
+
list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
552
|
+
expect([...list.fill(9, -3, -2)]).toEqual([1, 2, 9, 2, 1]);
|
|
553
|
+
list = new DoublyLinkedList([1, 2, 3, 2, 1]);
|
|
554
|
+
expect([...list.fill(9, -2, -3)]).toEqual([1, 2, 3, 2, 1]);
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
|
|
450
558
|
describe('iterable methods', () => {
|
|
451
559
|
it('should forEach, some, every, filter, map, reduce of the deque', () => {
|
|
452
560
|
const dl = new DoublyLinkedList<number>();
|
|
@@ -578,7 +686,7 @@ describe('classic use', () => {
|
|
|
578
686
|
const initialNode = this.currentSong;
|
|
579
687
|
|
|
580
688
|
// Loop through the playlist twice
|
|
581
|
-
for (let i = 0; i < this.playlist.
|
|
689
|
+
for (let i = 0; i < this.playlist.length * 2; i++) {
|
|
582
690
|
playedSongs.push(this.currentSong!.value);
|
|
583
691
|
this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed
|
|
584
692
|
}
|
|
@@ -700,7 +808,7 @@ describe('classic use', () => {
|
|
|
700
808
|
}
|
|
701
809
|
|
|
702
810
|
// Check capacity
|
|
703
|
-
if (this.list.
|
|
811
|
+
if (this.list.length >= this.capacity) {
|
|
704
812
|
// Delete the least recently used element (the tail of the linked list)
|
|
705
813
|
const removedNode = this.list.tail;
|
|
706
814
|
if (removedNode) {
|
|
@@ -745,9 +853,9 @@ describe('classic use', () => {
|
|
|
745
853
|
this.map.clear();
|
|
746
854
|
}
|
|
747
855
|
|
|
748
|
-
// Get the current cache
|
|
749
|
-
get
|
|
750
|
-
return this.list.
|
|
856
|
+
// Get the current cache length
|
|
857
|
+
get length(): number {
|
|
858
|
+
return this.list.length;
|
|
751
859
|
}
|
|
752
860
|
|
|
753
861
|
// Check if it is empty
|
|
@@ -806,7 +914,7 @@ describe('classic use', () => {
|
|
|
806
914
|
|
|
807
915
|
expect(cache.delete('a')).toBe(true);
|
|
808
916
|
expect(cache.get('a')).toBeUndefined();
|
|
809
|
-
expect(cache.
|
|
917
|
+
expect(cache.length).toBe(1);
|
|
810
918
|
|
|
811
919
|
// Should support clearing cache
|
|
812
920
|
cache.clear();
|
|
@@ -814,7 +922,7 @@ describe('classic use', () => {
|
|
|
814
922
|
cache.set('b', 2);
|
|
815
923
|
cache.clear();
|
|
816
924
|
|
|
817
|
-
expect(cache.
|
|
925
|
+
expect(cache.length).toBe(0);
|
|
818
926
|
expect(cache.isEmpty).toBe(true);
|
|
819
927
|
});
|
|
820
928
|
|