data-structure-typed 1.52.5 → 1.52.7
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 +33 -30
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +156 -156
- package/dist/cjs/constants/index.d.ts +4 -0
- package/dist/cjs/constants/index.js +9 -0
- package/dist/cjs/constants/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- 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 +23 -24
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +3 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.js +4 -2
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/cjs/data-structures/queue/deque.js +29 -29
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/cjs/data-structures/tree/tree.js +2 -0
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/constants/index.d.ts +4 -0
- package/dist/mjs/constants/index.js +5 -0
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +668 -592
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.js +1 -1
- package/dist/mjs/data-structures/heap/heap.js +3 -3
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.js +4 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/mjs/data-structures/queue/deque.js +29 -29
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/mjs/data-structures/tree/tree.js +2 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +1344 -1614
- package/dist/umd/data-structure-typed.min.js +10 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/eslint.config.mjs +69 -0
- package/package.json +30 -28
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +1 -0
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
- package/test/config.ts +1 -7
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +19 -18
- package/test/integration/heap.test.js +6 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +140 -112
- package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/test/unit/data-structures/stack/stack.test.ts +6 -0
- package/test/unit/unrestricted-interconversion.test.ts +24 -24
- package/test/utils/big-o.ts +5 -4
- package/.eslintrc.js +0 -64
|
@@ -69,7 +69,7 @@ describe('BST operations test', () => {
|
|
|
69
69
|
const nodeId10 = bst.getNode(10);
|
|
70
70
|
expect(nodeId10?.key).toBe(10);
|
|
71
71
|
|
|
72
|
-
const nodeVal9 = bst.getNode(
|
|
72
|
+
const nodeVal9 = bst.getNode(node => node.value === 9);
|
|
73
73
|
expect(nodeVal9?.key).toBe(9);
|
|
74
74
|
|
|
75
75
|
const leftMost = bst.getLeftMost();
|
|
@@ -81,8 +81,11 @@ describe('BST operations test', () => {
|
|
|
81
81
|
const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15);
|
|
82
82
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
83
83
|
|
|
84
|
+
const nodes = bst.getNodes(node => node.value === 15);
|
|
85
|
+
expect(nodes.map(node => node.key)).toEqual([15]);
|
|
86
|
+
|
|
84
87
|
let subTreeSum = 0;
|
|
85
|
-
node15
|
|
88
|
+
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
86
89
|
expect(subTreeSum).toBe(70);
|
|
87
90
|
|
|
88
91
|
let lesserSum = 0;
|
|
@@ -231,6 +234,9 @@ describe('BST operations test', () => {
|
|
|
231
234
|
expect(bfsNodes[0].key).toBe(2);
|
|
232
235
|
expect(bfsNodes[1].key).toBe(12);
|
|
233
236
|
expect(bfsNodes[2].key).toBe(16);
|
|
237
|
+
bst.clear();
|
|
238
|
+
expect(bst.perfectlyBalance()).toBe(false);
|
|
239
|
+
expect(bst.isAVLBalanced()).toBe(true);
|
|
234
240
|
});
|
|
235
241
|
|
|
236
242
|
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
@@ -288,7 +294,7 @@ describe('BST operations test', () => {
|
|
|
288
294
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
289
295
|
|
|
290
296
|
let subTreeSum = 0;
|
|
291
|
-
node15
|
|
297
|
+
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', node15);
|
|
292
298
|
expect(subTreeSum).toBe(70);
|
|
293
299
|
|
|
294
300
|
let lesserSum = 0;
|
|
@@ -484,7 +490,7 @@ describe('BST operations test recursively', () => {
|
|
|
484
490
|
expect(bst.get(10)).toBe(undefined);
|
|
485
491
|
expect(nodeId10?.key).toBe(10);
|
|
486
492
|
|
|
487
|
-
const nodeVal9 = bst.getNode(
|
|
493
|
+
const nodeVal9 = bst.getNode(node => node.value === 9);
|
|
488
494
|
expect(nodeVal9?.key).toBe(undefined);
|
|
489
495
|
|
|
490
496
|
const leftMost = bst.getLeftMost();
|
|
@@ -495,7 +501,7 @@ describe('BST operations test recursively', () => {
|
|
|
495
501
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
496
502
|
|
|
497
503
|
let subTreeSum = 0;
|
|
498
|
-
node15
|
|
504
|
+
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', 15);
|
|
499
505
|
expect(subTreeSum).toBe(70);
|
|
500
506
|
|
|
501
507
|
let lesserSum = 0;
|
|
@@ -702,7 +708,7 @@ describe('BST operations test recursively', () => {
|
|
|
702
708
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
703
709
|
|
|
704
710
|
let subTreeSum = 0;
|
|
705
|
-
node15
|
|
711
|
+
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', node15);
|
|
706
712
|
expect(subTreeSum).toBe(70);
|
|
707
713
|
|
|
708
714
|
let lesserSum = 0;
|
|
@@ -869,6 +875,28 @@ describe('BST operations test recursively', () => {
|
|
|
869
875
|
expect(numBST.size).toBe(0);
|
|
870
876
|
});
|
|
871
877
|
|
|
878
|
+
it('should listLevels', () => {
|
|
879
|
+
const bst = new BST<number>();
|
|
880
|
+
bst.addMany([2, 4, 5, 3, 1]);
|
|
881
|
+
expect(bst.size).toBe(5);
|
|
882
|
+
bst.delete(1);
|
|
883
|
+
bst.delete(5);
|
|
884
|
+
const levelKeys = bst.listLevels();
|
|
885
|
+
expect(levelKeys).toEqual([[3], [2, 4]]);
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it('should lesserOrGreaterTraverse', () => {
|
|
889
|
+
const bst = new BST<number>();
|
|
890
|
+
const levelKeys = bst.lesserOrGreaterTraverse();
|
|
891
|
+
expect(levelKeys).toEqual([]);
|
|
892
|
+
bst.addMany([2, 4, 5, 3, 1]);
|
|
893
|
+
expect(bst.size).toBe(5);
|
|
894
|
+
bst.delete(1);
|
|
895
|
+
bst.delete(5);
|
|
896
|
+
const levelKeys1 = bst.lesserOrGreaterTraverse();
|
|
897
|
+
expect(levelKeys1).toEqual([2]);
|
|
898
|
+
});
|
|
899
|
+
|
|
872
900
|
it('should the clone method', () => {
|
|
873
901
|
function checkTreeStructure(bst: BST<string, number>) {
|
|
874
902
|
expect(bst.size).toBe(4);
|
|
@@ -972,7 +1000,7 @@ describe('BST Performance test', function () {
|
|
|
972
1000
|
it(`Observe the time consumption of BST.dfs be good`, function () {
|
|
973
1001
|
const startDFS = performance.now();
|
|
974
1002
|
const dfs = bst.dfs(node => node);
|
|
975
|
-
isDebug
|
|
1003
|
+
if (isDebug) console.log('---bfs', performance.now() - startDFS, dfs.length);
|
|
976
1004
|
});
|
|
977
1005
|
|
|
978
1006
|
it('Should the time consumption of lesserOrGreaterTraverse fitting O(n log n)', function () {
|
|
@@ -982,16 +1010,16 @@ describe('BST Performance test', function () {
|
|
|
982
1010
|
}
|
|
983
1011
|
const start = performance.now();
|
|
984
1012
|
bst.addMany(nodes);
|
|
985
|
-
isDebug
|
|
1013
|
+
if (isDebug) console.log('---add', performance.now() - start);
|
|
986
1014
|
const startL = performance.now();
|
|
987
1015
|
bst.lesserOrGreaterTraverse(
|
|
988
1016
|
node => {
|
|
989
|
-
node.key
|
|
1017
|
+
node.key -= 1;
|
|
990
1018
|
},
|
|
991
1019
|
-1,
|
|
992
1020
|
inputSize / 2
|
|
993
1021
|
);
|
|
994
|
-
isDebug
|
|
1022
|
+
if (isDebug) console.log('---lesserOrGreaterTraverse', performance.now() - startL);
|
|
995
1023
|
});
|
|
996
1024
|
|
|
997
1025
|
it('Should the time consumption of listLevels fitting well', function () {
|
|
@@ -1001,11 +1029,11 @@ describe('BST Performance test', function () {
|
|
|
1001
1029
|
}
|
|
1002
1030
|
const start = performance.now();
|
|
1003
1031
|
bst.addMany(nodes);
|
|
1004
|
-
isDebug
|
|
1032
|
+
if (isDebug) console.log('---add', performance.now() - start);
|
|
1005
1033
|
const startL = performance.now();
|
|
1006
1034
|
const arr: number[][] = bst.listLevels(node => node.key);
|
|
1007
|
-
isDebug
|
|
1008
|
-
isDebug
|
|
1035
|
+
if (isDebug) console.log('---listLevels', arr);
|
|
1036
|
+
if (isDebug) console.log('---listLevels', performance.now() - startL);
|
|
1009
1037
|
});
|
|
1010
1038
|
|
|
1011
1039
|
it('should the lastKey of a BST to be the largest key', function () {
|
|
@@ -6,18 +6,13 @@ describe('Overall BinaryTree Test', () => {
|
|
|
6
6
|
bst.add(11);
|
|
7
7
|
bst.add(3);
|
|
8
8
|
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
|
|
9
|
-
bst.size === 16; // true
|
|
10
9
|
expect(bst.size).toBe(16); // true
|
|
11
10
|
bst.has(6); // true
|
|
12
11
|
expect(bst.has(6)).toBe(true); // true
|
|
13
|
-
bst.getHeight(6) === 2; // true
|
|
14
|
-
bst.getHeight() === 5; // true
|
|
15
|
-
bst.getDepth(6) === 3; // true
|
|
16
12
|
expect(bst.getHeight(6)).toBe(2); // true
|
|
17
13
|
expect(bst.getHeight()).toBe(5); // true
|
|
18
14
|
expect(bst.getDepth(6)).toBe(3); // true
|
|
19
15
|
const leftMost = bst.getLeftMost();
|
|
20
|
-
leftMost?.key === 1; // true
|
|
21
16
|
expect(leftMost).toBe(1);
|
|
22
17
|
bst.delete(6);
|
|
23
18
|
bst.getNode(6); // undefined
|
|
@@ -26,7 +21,6 @@ describe('Overall BinaryTree Test', () => {
|
|
|
26
21
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
27
22
|
const bfsIDs: number[] = [];
|
|
28
23
|
bst.bfs(node => bfsIDs.push(node.key));
|
|
29
|
-
bfsIDs[0] === 11; // true
|
|
30
24
|
expect(bfsIDs[0]).toBe(11);
|
|
31
25
|
|
|
32
26
|
const objBST = new BST<number, { key: number; keyA: number }>();
|
|
@@ -414,7 +414,7 @@ describe('RedBlackTree 2', () => {
|
|
|
414
414
|
expect(node225F?.right).toBe(rbTree.NIL);
|
|
415
415
|
expect(node225F?.parent?.key).toBe(155);
|
|
416
416
|
rbTree.add(7);
|
|
417
|
-
isDebug
|
|
417
|
+
if (isDebug) rbTree.print();
|
|
418
418
|
|
|
419
419
|
const node15S = rbTree.getNode(15);
|
|
420
420
|
expect(node15S?.left?.key).toBe(10);
|
|
@@ -463,7 +463,7 @@ describe('RedBlackTree 2', () => {
|
|
|
463
463
|
rbTree.add(19);
|
|
464
464
|
rbTree.add(110);
|
|
465
465
|
|
|
466
|
-
isDebug
|
|
466
|
+
if (isDebug) rbTree.print();
|
|
467
467
|
|
|
468
468
|
expect(rbTree.dfs()).toEqual([
|
|
469
469
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 22, 23, 25, 28, 33, 50, 110, 111, 155, 225
|
|
@@ -525,19 +525,19 @@ describe('RedBlackTree 2', () => {
|
|
|
525
525
|
for (let i = 0; i < arr.length; i++) {
|
|
526
526
|
rbTree.add(arr[i]);
|
|
527
527
|
}
|
|
528
|
-
isDebug
|
|
528
|
+
if (isDebug) console.log(performance.now() - tS);
|
|
529
529
|
|
|
530
530
|
const cS = performance.now();
|
|
531
531
|
|
|
532
532
|
for (let i = 0; i < arr.length; i++) {
|
|
533
533
|
competitor.setElement(arr[i], arr[i]);
|
|
534
534
|
}
|
|
535
|
-
isDebug
|
|
535
|
+
if (isDebug) console.log(performance.now() - cS);
|
|
536
536
|
});
|
|
537
537
|
|
|
538
538
|
it('duplicates', () => {
|
|
539
539
|
rbTree.addMany([9, 8, 7, 8, 8, 8, 2, 3, 6, 5, 5, 4]);
|
|
540
|
-
isDebug
|
|
540
|
+
if (isDebug) rbTree.print();
|
|
541
541
|
|
|
542
542
|
expect(rbTree.size).toBe(8);
|
|
543
543
|
expect(rbTree.isBST()).toBe(true);
|
|
@@ -606,7 +606,7 @@ describe('RedBlackTree 2', () => {
|
|
|
606
606
|
expect(rbTree.getHeight()).toBe(-1);
|
|
607
607
|
expect(nanCount).toBeLessThanOrEqual(inputSize);
|
|
608
608
|
|
|
609
|
-
isDebug
|
|
609
|
+
if (isDebug) rbTree.print();
|
|
610
610
|
});
|
|
611
611
|
});
|
|
612
612
|
|
|
@@ -1,50 +1,104 @@
|
|
|
1
|
-
import { SegmentTree } from '../../../../src';
|
|
1
|
+
import { SegmentTree, SegmentTreeNode } from '../../../../src';
|
|
2
2
|
|
|
3
|
-
describe('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
describe('SegmentTreeNode', () => {
|
|
4
|
+
it('should initialize with correct start, end, sum, and optional value', () => {
|
|
5
|
+
const node = new SegmentTreeNode(0, 1, 10, 2);
|
|
6
|
+
expect(node.start).toBe(0);
|
|
7
|
+
expect(node.end).toBe(1);
|
|
8
|
+
expect(node.sum).toBe(10);
|
|
9
|
+
expect(node.value).toBe(2);
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
-
it('should
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
it('should allow setting start, end, sum, value, left, and right', () => {
|
|
13
|
+
const node = new SegmentTreeNode(0, 1, 10);
|
|
14
|
+
node.start = 2;
|
|
15
|
+
node.end = 3;
|
|
16
|
+
node.sum = 15;
|
|
17
|
+
node.value = 2;
|
|
18
|
+
|
|
19
|
+
const leftNode = new SegmentTreeNode(0, 1, 5);
|
|
20
|
+
const rightNode = new SegmentTreeNode(2, 3, 10);
|
|
21
|
+
|
|
22
|
+
node.left = leftNode;
|
|
23
|
+
node.right = rightNode;
|
|
24
|
+
|
|
25
|
+
expect(node.start).toBe(2);
|
|
26
|
+
expect(node.end).toBe(3);
|
|
27
|
+
expect(node.sum).toBe(15);
|
|
28
|
+
expect(node.value).toBe(2);
|
|
29
|
+
expect(node.left).toBe(leftNode);
|
|
30
|
+
expect(node.right).toBe(rightNode);
|
|
15
31
|
});
|
|
32
|
+
});
|
|
16
33
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
34
|
+
describe('SegmentTree', () => {
|
|
35
|
+
it('should initialize with correct values, start, end, and root', () => {
|
|
36
|
+
const values = [1, 2, 3, 4];
|
|
37
|
+
const tree = new SegmentTree(values);
|
|
20
38
|
|
|
21
|
-
|
|
22
|
-
expect(
|
|
39
|
+
expect(tree.values).toEqual(values);
|
|
40
|
+
expect(tree.start).toBe(0);
|
|
41
|
+
expect(tree.end).toBe(3);
|
|
42
|
+
expect(tree.root).toBeDefined();
|
|
23
43
|
});
|
|
24
44
|
|
|
25
|
-
it('should
|
|
26
|
-
|
|
27
|
-
|
|
45
|
+
it('should correctly build segment tree with sum of values', () => {
|
|
46
|
+
const values = [1, 2, 3, 4];
|
|
47
|
+
const tree = new SegmentTree(values);
|
|
48
|
+
|
|
49
|
+
expect(tree.root?.sum).toBe(10); // 1 + 2 + 3 + 4
|
|
28
50
|
});
|
|
29
51
|
|
|
30
|
-
it('should handle
|
|
31
|
-
|
|
32
|
-
expect(
|
|
33
|
-
expect(
|
|
34
|
-
expect(segmentTree.querySumByRange(0, 10)).toBe(NaN); // Beyond upper bound, should return NaN
|
|
52
|
+
it('should handle empty values array gracefully', () => {
|
|
53
|
+
const tree = new SegmentTree([]);
|
|
54
|
+
expect(tree.values).toEqual([]);
|
|
55
|
+
expect(tree.root).toBeUndefined();
|
|
35
56
|
});
|
|
36
57
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
58
|
+
describe('updateNode', () => {
|
|
59
|
+
it('should update node value and sum correctly', () => {
|
|
60
|
+
const values = [1, 2, 3, 4];
|
|
61
|
+
const tree = new SegmentTree(values);
|
|
62
|
+
tree.updateNode(1, 5, 2);
|
|
63
|
+
|
|
64
|
+
expect(tree.root?.sum).toBe(13); // 1 + 5 + 3 + 4
|
|
65
|
+
expect(tree.root?.left?.right?.sum).toBe(5);
|
|
66
|
+
expect(tree.root?.left?.right?.value).toBe(2);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should do nothing if root is undefined', () => {
|
|
70
|
+
const tree = new SegmentTree([]);
|
|
71
|
+
tree.updateNode(0, 10);
|
|
72
|
+
expect(tree.root).toBeUndefined();
|
|
73
|
+
});
|
|
42
74
|
});
|
|
43
75
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
76
|
+
describe('querySumByRange', () => {
|
|
77
|
+
it('should return sum for a given range', () => {
|
|
78
|
+
const values = [1, 2, 3, 4, 5];
|
|
79
|
+
const tree = new SegmentTree(values);
|
|
80
|
+
const result = tree.querySumByRange(1, 3); // 2 + 3 + 4 = 9
|
|
81
|
+
expect(result).toBe(9);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should return NaN for an invalid range', () => {
|
|
85
|
+
const values = [1, 2, 3, 4, 5];
|
|
86
|
+
const tree = new SegmentTree(values);
|
|
87
|
+
const result = tree.querySumByRange(3, 1); // Invalid range
|
|
88
|
+
expect(result).toBeNaN();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should return 0 if root is undefined', () => {
|
|
92
|
+
const tree = new SegmentTree([]);
|
|
93
|
+
const result = tree.querySumByRange(0, 1);
|
|
94
|
+
expect(result).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should return NaN if range is out of bounds', () => {
|
|
98
|
+
const values = [1, 2, 3, 4];
|
|
99
|
+
const tree = new SegmentTree(values);
|
|
100
|
+
const result = tree.querySumByRange(-1, 10);
|
|
101
|
+
expect(result).toBeNaN();
|
|
102
|
+
});
|
|
49
103
|
});
|
|
50
104
|
});
|