data-structure-typed 1.53.6 → 1.53.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 +16 -25
- package/benchmark/report.html +32 -5
- package/benchmark/report.json +326 -23
- package/dist/cjs/common/index.d.ts +12 -0
- package/dist/cjs/common/index.js +24 -0
- package/dist/cjs/common/index.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/cjs/data-structures/binary-tree/bst.js +185 -66
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/cjs/data-structures/heap/heap.js +6 -6
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- 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 +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/cjs/data-structures/trie/trie.js +122 -12
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/utils/utils.d.ts +10 -6
- package/dist/cjs/utils/utils.js +4 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/common/index.d.ts +12 -0
- package/dist/mjs/common/index.js +24 -0
- package/dist/mjs/common/index.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +8 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +95 -61
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/mjs/data-structures/binary-tree/bst.js +187 -66
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/mjs/data-structures/heap/heap.js +6 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/mjs/data-structures/trie/trie.js +122 -12
- package/dist/mjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/index.d.ts +1 -1
- package/dist/mjs/index.js +1 -1
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/utils/utils.d.ts +10 -6
- package/dist/mjs/utils/utils.js +4 -2
- package/dist/mjs/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +299 -156
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +1 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -1
- package/test/performance/reportor.ts +38 -33
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +14 -22
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +100 -1
- package/test/unit/data-structures/trie/trie.test.ts +151 -0
- package/test/unit/utils/utils.test.ts +6 -6
- package/dist/cjs/constants/index.d.ts +0 -4
- package/dist/cjs/constants/index.js +0 -9
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/mjs/constants/index.d.ts +0 -4
- package/dist/mjs/constants/index.js +0 -6
- package/dist/mjs/constants/index.js.map +0 -1
- package/src/constants/index.ts +0 -4
|
@@ -58,11 +58,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
58
58
|
it('Should clone a BST works fine', () => {
|
|
59
59
|
const bst = new BST<number>([3, 6, 7, 1, 9], {
|
|
60
60
|
iterationType: 'RECURSIVE',
|
|
61
|
-
|
|
62
|
-
if (a > b) return -1;
|
|
63
|
-
if (a < b) return 1;
|
|
64
|
-
return 0;
|
|
65
|
-
}
|
|
61
|
+
isReverse: true
|
|
66
62
|
});
|
|
67
63
|
expect(bst.size).toBe(5);
|
|
68
64
|
expect(bst.root?.key).toBe(6);
|
|
@@ -70,7 +66,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
70
66
|
expect(bst.root?.left?.right?.key).toBe(7);
|
|
71
67
|
expect(bst.root?.right?.key).toBe(3);
|
|
72
68
|
expect(bst.root?.right?.right?.key).toBe(1);
|
|
73
|
-
expect(bst.
|
|
69
|
+
expect(bst.getNode(9)?.right?.key).toBe(7);
|
|
74
70
|
expect(bst.getHeight()).toBe(2);
|
|
75
71
|
expect(bst.has(9)).toBe(true);
|
|
76
72
|
expect(bst.has(7)).toBe(true);
|
|
@@ -81,7 +77,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
81
77
|
expect(bst.root?.left?.key).toBe(9);
|
|
82
78
|
expect(bst.root?.right?.key).toBe(3);
|
|
83
79
|
expect(bst.root?.right?.right?.key).toBe(1);
|
|
84
|
-
expect(bst.
|
|
80
|
+
expect(bst.getNode(6)?.left?.key).toBe(9);
|
|
85
81
|
expect(bst.getHeight()).toBe(2);
|
|
86
82
|
expect(bst.has(9)).toBe(true);
|
|
87
83
|
expect(bst.has(7)).toBe(false);
|
|
@@ -92,7 +88,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
92
88
|
expect(clonedBST.root?.left?.key).toBe(9);
|
|
93
89
|
expect(clonedBST.root?.right?.key).toBe(3);
|
|
94
90
|
expect(clonedBST.root?.right?.right?.key).toBe(1);
|
|
95
|
-
expect(clonedBST.
|
|
91
|
+
expect(clonedBST.getNode(6)?.left?.key).toBe(9);
|
|
96
92
|
expect(clonedBST.getHeight()).toBe(2);
|
|
97
93
|
expect(clonedBST.has(9)).toBe(true);
|
|
98
94
|
expect(clonedBST.has(7)).toBe(false);
|
|
@@ -102,11 +98,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
102
98
|
it('Should clone a AVLTree works fine', () => {
|
|
103
99
|
const avl = new AVLTree<number>([3, 6, 7, 1, 9], {
|
|
104
100
|
iterationType: 'RECURSIVE',
|
|
105
|
-
|
|
106
|
-
if (a > b) return -1;
|
|
107
|
-
if (a < b) return 1;
|
|
108
|
-
return 0;
|
|
109
|
-
}
|
|
101
|
+
isReverse: true
|
|
110
102
|
});
|
|
111
103
|
expect(avl.size).toBe(5);
|
|
112
104
|
avl.add(2);
|
|
@@ -117,7 +109,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
117
109
|
expect(avl.root?.left?.left?.key).toBe(9);
|
|
118
110
|
expect(avl.root?.right?.key).toBe(1);
|
|
119
111
|
expect(avl.root?.right?.left?.key).toBe(2);
|
|
120
|
-
expect(avl.
|
|
112
|
+
expect(avl.getNode(7)?.left?.key).toBe(9);
|
|
121
113
|
expect(avl.getHeight()).toBe(3);
|
|
122
114
|
expect(avl.has(9)).toBe(true);
|
|
123
115
|
expect(avl.has(7)).toBe(true);
|
|
@@ -128,7 +120,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
128
120
|
expect(avl.root?.left?.key).toBe(5);
|
|
129
121
|
expect(avl.root?.right?.key).toBe(1);
|
|
130
122
|
expect(avl.root?.right?.left?.key).toBe(2);
|
|
131
|
-
expect(avl.
|
|
123
|
+
expect(avl.getNode(6)?.left?.key).toBe(undefined);
|
|
132
124
|
expect(avl.getHeight()).toBe(3);
|
|
133
125
|
expect(avl.has(9)).toBe(true);
|
|
134
126
|
expect(avl.has(7)).toBe(false);
|
|
@@ -139,7 +131,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
139
131
|
expect(clonedAVL.root?.left?.key).toBe(5);
|
|
140
132
|
expect(clonedAVL.root?.right?.key).toBe(1);
|
|
141
133
|
expect(clonedAVL.root?.right?.left?.key).toBe(2);
|
|
142
|
-
expect(clonedAVL.
|
|
134
|
+
expect(clonedAVL.getNode(6)?.left?.key).toBe(undefined);
|
|
143
135
|
expect(clonedAVL.getHeight()).toBe(3);
|
|
144
136
|
expect(clonedAVL.has(9)).toBe(true);
|
|
145
137
|
expect(clonedAVL.has(7)).toBe(false);
|
|
@@ -162,7 +154,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
162
154
|
expect(tmm.root?.left?.left?.key).toBe(NaN);
|
|
163
155
|
expect(tmm.root?.right?.key).toBe(7);
|
|
164
156
|
expect(tmm.root?.right?.left?.key).toBe(5);
|
|
165
|
-
expect(tmm.
|
|
157
|
+
expect(tmm.getNode(7)?.left?.key).toBe(5);
|
|
166
158
|
expect(tmm.getHeight()).toBe(3);
|
|
167
159
|
expect(tmm.has(9)).toBe(true);
|
|
168
160
|
expect(tmm.has(7)).toBe(true);
|
|
@@ -174,7 +166,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
174
166
|
expect(tmm.root?.left?.key).toBe(1);
|
|
175
167
|
expect(tmm.root?.right?.key).toBe(9);
|
|
176
168
|
expect(tmm.root?.right?.left?.key).toBe(5);
|
|
177
|
-
expect(tmm.
|
|
169
|
+
expect(tmm.getNode(6)?.left?.key).toBe(NaN);
|
|
178
170
|
expect(tmm.getHeight()).toBe(3);
|
|
179
171
|
expect(tmm.has(9)).toBe(true);
|
|
180
172
|
expect(tmm.has(7)).toBe(false);
|
|
@@ -187,7 +179,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
187
179
|
expect(clonedTMM.root?.left?.key).toBe(1);
|
|
188
180
|
expect(clonedTMM.root?.right?.key).toBe(5);
|
|
189
181
|
expect(clonedTMM.root?.right?.left?.key).toBe(4);
|
|
190
|
-
expect(clonedTMM.
|
|
182
|
+
expect(clonedTMM.getNode(6)?.left?.key).toBe(NaN);
|
|
191
183
|
expect(clonedTMM.getHeight()).toBe(3);
|
|
192
184
|
expect(clonedTMM.has(9)).toBe(true);
|
|
193
185
|
expect(clonedTMM.has(7)).toBe(false);
|
|
@@ -209,7 +201,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
209
201
|
expect(rbTree.root?.left?.left?.key).toBe(NaN);
|
|
210
202
|
expect(rbTree.root?.right?.key).toBe(7);
|
|
211
203
|
expect(rbTree.root?.right?.left?.key).toBe(5);
|
|
212
|
-
expect(rbTree.
|
|
204
|
+
expect(rbTree.getNode(7)?.left?.key).toBe(5);
|
|
213
205
|
expect(rbTree.getHeight()).toBe(3);
|
|
214
206
|
expect(rbTree.has(9)).toBe(true);
|
|
215
207
|
expect(rbTree.has(7)).toBe(true);
|
|
@@ -220,7 +212,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
220
212
|
expect(rbTree.root?.left?.key).toBe(1);
|
|
221
213
|
expect(rbTree.root?.right?.key).toBe(9);
|
|
222
214
|
expect(rbTree.root?.right?.left?.key).toBe(5);
|
|
223
|
-
expect(rbTree.
|
|
215
|
+
expect(rbTree.getNode(6)?.left?.key).toBe(NaN);
|
|
224
216
|
expect(rbTree.getHeight()).toBe(3);
|
|
225
217
|
expect(rbTree.has(9)).toBe(true);
|
|
226
218
|
expect(rbTree.has(7)).toBe(false);
|
|
@@ -232,7 +224,7 @@ describe('Overall BinaryTree Test', () => {
|
|
|
232
224
|
expect(clonedRbTree.root?.left?.key).toBe(1);
|
|
233
225
|
expect(clonedRbTree.root?.right?.key).toBe(5);
|
|
234
226
|
expect(clonedRbTree.root?.right?.left?.key).toBe(4);
|
|
235
|
-
expect(clonedRbTree.
|
|
227
|
+
expect(clonedRbTree.getNode(6)?.left?.key).toBe(NaN);
|
|
236
228
|
expect(clonedRbTree.getHeight()).toBe(3);
|
|
237
229
|
expect(clonedRbTree.has(9)).toBe(true);
|
|
238
230
|
expect(clonedRbTree.has(7)).toBe(false);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryTreeNode, BSTNode, RedBlackTree, RedBlackTreeNode } from '../../../../src';
|
|
1
|
+
import { BinaryTreeNode, BSTNode, Range, RedBlackTree, RedBlackTreeNode } from '../../../../src';
|
|
2
2
|
import { getRandomInt, getRandomIntArray, magnitude } from '../../../utils';
|
|
3
3
|
import { OrderedMap } from 'js-sdsl';
|
|
4
4
|
|
|
@@ -819,3 +819,102 @@ describe('RedBlackTree - _deleteFixup', () => {
|
|
|
819
819
|
]);
|
|
820
820
|
});
|
|
821
821
|
});
|
|
822
|
+
|
|
823
|
+
describe('classic use', () => {
|
|
824
|
+
it('Database Index: Add, Search, and Delete Records', () => {
|
|
825
|
+
const dbIndex = new RedBlackTree<number, string>();
|
|
826
|
+
|
|
827
|
+
// Insert records
|
|
828
|
+
dbIndex.add(1, 'Alice');
|
|
829
|
+
dbIndex.add(2, 'Bob');
|
|
830
|
+
dbIndex.add(3, 'Charlie');
|
|
831
|
+
|
|
832
|
+
// Search for records
|
|
833
|
+
expect(dbIndex.get(1)).toBe('Alice');
|
|
834
|
+
expect(dbIndex.get(2)).toBe('Bob');
|
|
835
|
+
expect(dbIndex.get(3)).toBe('Charlie');
|
|
836
|
+
|
|
837
|
+
// Delete a record
|
|
838
|
+
dbIndex.delete(2);
|
|
839
|
+
expect(dbIndex.get(2)).toBeUndefined();
|
|
840
|
+
});
|
|
841
|
+
|
|
842
|
+
it('@example Merge 3 sorted datasets', () => {
|
|
843
|
+
const dataset1 = new RedBlackTree<number, string>([
|
|
844
|
+
[1, 'A'],
|
|
845
|
+
[7, 'G']
|
|
846
|
+
]);
|
|
847
|
+
const dataset2 = [
|
|
848
|
+
[2, 'B'],
|
|
849
|
+
[6, 'F']
|
|
850
|
+
];
|
|
851
|
+
const dataset3 = new RedBlackTree<number, string>([
|
|
852
|
+
[3, 'C'],
|
|
853
|
+
[5, 'E'],
|
|
854
|
+
[4, 'D']
|
|
855
|
+
]);
|
|
856
|
+
|
|
857
|
+
// Merge datasets into a single Red-Black Tree
|
|
858
|
+
const merged = new RedBlackTree<number, string>(dataset1);
|
|
859
|
+
merged.addMany(dataset2);
|
|
860
|
+
merged.merge(dataset3);
|
|
861
|
+
|
|
862
|
+
// Verify merged dataset is in sorted order
|
|
863
|
+
expect([...merged.values()]).toEqual(['A', 'B', 'C', 'D', 'E', 'F', 'G']);
|
|
864
|
+
});
|
|
865
|
+
|
|
866
|
+
// Test case for finding elements in a given range
|
|
867
|
+
it('Find elements in a range', () => {
|
|
868
|
+
const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
869
|
+
expect(bst.search(new Range(5, 10))).toEqual([5, 10, 7]);
|
|
870
|
+
expect(bst.search(new Range(4, 12))).toEqual([5, 10, 12, 7]);
|
|
871
|
+
expect(bst.search(new Range(15, 20))).toEqual([15, 18]);
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it('Timer List: Manage Timed Tasks', () => {
|
|
875
|
+
const timerList = new RedBlackTree<number, string>(); // Key: Time in ms, Value: Task Name
|
|
876
|
+
|
|
877
|
+
// Schedule tasks
|
|
878
|
+
timerList.add(100, 'Task A');
|
|
879
|
+
timerList.add(200, 'Task B');
|
|
880
|
+
timerList.add(50, 'Task C');
|
|
881
|
+
|
|
882
|
+
// Verify the order of tasks by retrieval
|
|
883
|
+
expect([...timerList.values()]).toEqual(['Task C', 'Task A', 'Task B']); // Sorted by key (time)
|
|
884
|
+
|
|
885
|
+
// Remove the earliest task
|
|
886
|
+
timerList.delete(50);
|
|
887
|
+
expect([...timerList.values()]).toEqual(['Task A', 'Task B']);
|
|
888
|
+
});
|
|
889
|
+
|
|
890
|
+
it('Scheduler: Manage Tasks by Priority', () => {
|
|
891
|
+
const scheduler = new RedBlackTree<number, string>(); // Key: Priority, Value: Task Name
|
|
892
|
+
|
|
893
|
+
// Add tasks with different priorities
|
|
894
|
+
scheduler.add(3, 'Low Priority Task');
|
|
895
|
+
scheduler.add(1, 'High Priority Task');
|
|
896
|
+
scheduler.add(2, 'Medium Priority Task');
|
|
897
|
+
|
|
898
|
+
// Verify the order of tasks by retrieval
|
|
899
|
+
expect([...scheduler.values()]).toEqual(['High Priority Task', 'Medium Priority Task', 'Low Priority Task']);
|
|
900
|
+
|
|
901
|
+
// Remove the highest priority task
|
|
902
|
+
scheduler.delete(1);
|
|
903
|
+
expect([...scheduler.values()]).toEqual(['Medium Priority Task', 'Low Priority Task']);
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
it('Routing Table: Manage IP Routes', () => {
|
|
907
|
+
const routingTable = new RedBlackTree<number, string>(); // Key: IP Address, Value: Route
|
|
908
|
+
|
|
909
|
+
// Add routes
|
|
910
|
+
routingTable.add(1921680101, 'Route A');
|
|
911
|
+
routingTable.add(1921680102, 'Route B');
|
|
912
|
+
routingTable.add(1921680100, 'Route C');
|
|
913
|
+
|
|
914
|
+
// Search for a specific route
|
|
915
|
+
expect(routingTable.get(1921680101)).toBe('Route A');
|
|
916
|
+
|
|
917
|
+
// Verify all routes in sorted order
|
|
918
|
+
expect([...routingTable.values()]).toEqual(['Route C', 'Route A', 'Route B']);
|
|
919
|
+
});
|
|
920
|
+
});
|
|
@@ -932,3 +932,154 @@ describe('Trie class', () => {
|
|
|
932
932
|
expect(trieB.hasPrefix('ap')).toBe(false);
|
|
933
933
|
});
|
|
934
934
|
});
|
|
935
|
+
|
|
936
|
+
describe('Trie basic', () => {
|
|
937
|
+
test('Dictionary: Basic word lookup functionality', () => {
|
|
938
|
+
// Initialize a new Trie and add dictionary words
|
|
939
|
+
const dictionary = new Trie<string>();
|
|
940
|
+
const words = ['apple', 'app', 'application', 'approve', 'bread', 'break'];
|
|
941
|
+
words.forEach(word => dictionary.add(word));
|
|
942
|
+
|
|
943
|
+
// Test exact word matches
|
|
944
|
+
expect(dictionary.has('apple')).toBe(true);
|
|
945
|
+
expect(dictionary.has('app')).toBe(true);
|
|
946
|
+
expect(dictionary.has('bread')).toBe(true);
|
|
947
|
+
|
|
948
|
+
// Test non-existent words
|
|
949
|
+
expect(dictionary.has('appl')).toBe(false);
|
|
950
|
+
expect(dictionary.has('breaking')).toBe(false);
|
|
951
|
+
|
|
952
|
+
// Verify dictionary size
|
|
953
|
+
expect(dictionary.size).toBe(words.length);
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
test('Autocomplete: Limited suggestions with max results', () => {
|
|
957
|
+
const autocomplete = new Trie<string>();
|
|
958
|
+
|
|
959
|
+
// Add city names
|
|
960
|
+
const cities = ['New York', 'New Orleans', 'New Delhi', 'New Jersey', 'New Mexico', 'New Hampshire'];
|
|
961
|
+
|
|
962
|
+
cities.forEach(city => autocomplete.add(city));
|
|
963
|
+
|
|
964
|
+
// Get limited number of suggestions
|
|
965
|
+
const maxSuggestions = 3;
|
|
966
|
+
const suggestions = autocomplete.getWords('New', maxSuggestions);
|
|
967
|
+
|
|
968
|
+
expect(suggestions.length).toBe(maxSuggestions);
|
|
969
|
+
suggestions.forEach(suggestion => {
|
|
970
|
+
expect(suggestion.startsWith('New')).toBe(true);
|
|
971
|
+
});
|
|
972
|
+
});
|
|
973
|
+
|
|
974
|
+
test('Dictionary: Word removal and updates', () => {
|
|
975
|
+
const dictionary = new Trie<string>();
|
|
976
|
+
|
|
977
|
+
// Add initial words
|
|
978
|
+
dictionary.add('delete');
|
|
979
|
+
dictionary.add('deletion');
|
|
980
|
+
dictionary.add('deleted');
|
|
981
|
+
|
|
982
|
+
// Verify initial state
|
|
983
|
+
expect(dictionary.has('delete')).toBe(true);
|
|
984
|
+
expect(dictionary.size).toBe(3);
|
|
985
|
+
|
|
986
|
+
// Remove a word
|
|
987
|
+
const deleted = dictionary.delete('delete');
|
|
988
|
+
expect(deleted).toBe(true);
|
|
989
|
+
expect(dictionary.has('delete')).toBe(false);
|
|
990
|
+
expect(dictionary.has('deletion')).toBe(true);
|
|
991
|
+
expect(dictionary.has('deleted')).toBe(true);
|
|
992
|
+
expect(dictionary.size).toBe(2);
|
|
993
|
+
|
|
994
|
+
// Try to remove non-existent word
|
|
995
|
+
expect(dictionary.delete('notexist')).toBe(false);
|
|
996
|
+
});
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
describe('classic use', () => {
|
|
1000
|
+
test('@example Autocomplete: Prefix validation and checking', () => {
|
|
1001
|
+
const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
|
|
1002
|
+
|
|
1003
|
+
// Get all completions for a prefix
|
|
1004
|
+
const gmailCompletions = autocomplete.getWords('gmail');
|
|
1005
|
+
expect(gmailCompletions).toEqual(['gmail.com', 'gmail.co.nz', 'gmail.co.jp']);
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
test('@example File System Path Operations', () => {
|
|
1009
|
+
const fileSystem = new Trie<string>([
|
|
1010
|
+
'/home/user/documents/file1.txt',
|
|
1011
|
+
'/home/user/documents/file2.txt',
|
|
1012
|
+
'/home/user/pictures/photo.jpg',
|
|
1013
|
+
'/home/user/pictures/vacation/',
|
|
1014
|
+
'/home/user/downloads'
|
|
1015
|
+
]);
|
|
1016
|
+
|
|
1017
|
+
// Find common directory prefix
|
|
1018
|
+
expect(fileSystem.getLongestCommonPrefix()).toBe('/home/user/');
|
|
1019
|
+
|
|
1020
|
+
// List all files in a directory
|
|
1021
|
+
const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
1022
|
+
expect(documentsFiles).toEqual(['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']);
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
test('@example Autocomplete: Basic word suggestions', () => {
|
|
1026
|
+
// Create a trie for autocomplete
|
|
1027
|
+
const autocomplete = new Trie<string>([
|
|
1028
|
+
'function',
|
|
1029
|
+
'functional',
|
|
1030
|
+
'functions',
|
|
1031
|
+
'class',
|
|
1032
|
+
'classes',
|
|
1033
|
+
'classical',
|
|
1034
|
+
'closure',
|
|
1035
|
+
'const',
|
|
1036
|
+
'constructor'
|
|
1037
|
+
]);
|
|
1038
|
+
|
|
1039
|
+
// Test autocomplete with different prefixes
|
|
1040
|
+
expect(autocomplete.getWords('fun')).toEqual(['functional', 'functions', 'function']);
|
|
1041
|
+
expect(autocomplete.getWords('cla')).toEqual(['classes', 'classical', 'class']);
|
|
1042
|
+
expect(autocomplete.getWords('con')).toEqual(['constructor', 'const']);
|
|
1043
|
+
|
|
1044
|
+
// Test with non-matching prefix
|
|
1045
|
+
expect(autocomplete.getWords('xyz')).toEqual([]);
|
|
1046
|
+
});
|
|
1047
|
+
|
|
1048
|
+
test('@example Dictionary: Case-insensitive word lookup', () => {
|
|
1049
|
+
// Create a case-insensitive dictionary
|
|
1050
|
+
const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
1051
|
+
|
|
1052
|
+
// Add words with mixed casing
|
|
1053
|
+
dictionary.add('Hello');
|
|
1054
|
+
dictionary.add('WORLD');
|
|
1055
|
+
dictionary.add('JavaScript');
|
|
1056
|
+
|
|
1057
|
+
// Test lookups with different casings
|
|
1058
|
+
expect(dictionary.has('hello')).toBe(true);
|
|
1059
|
+
expect(dictionary.has('HELLO')).toBe(true);
|
|
1060
|
+
expect(dictionary.has('Hello')).toBe(true);
|
|
1061
|
+
expect(dictionary.has('javascript')).toBe(true);
|
|
1062
|
+
expect(dictionary.has('JAVASCRIPT')).toBe(true);
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
test('@example IP Address Routing Table', () => {
|
|
1066
|
+
// Add IP address prefixes and their corresponding routes
|
|
1067
|
+
const routes = {
|
|
1068
|
+
'192.168.1': 'LAN_SUBNET_1',
|
|
1069
|
+
'192.168.2': 'LAN_SUBNET_2',
|
|
1070
|
+
'10.0.0': 'PRIVATE_NETWORK_1',
|
|
1071
|
+
'10.0.1': 'PRIVATE_NETWORK_2'
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
1075
|
+
|
|
1076
|
+
// Check IP address prefix matching
|
|
1077
|
+
expect(ipRoutingTable.hasPrefix('192.168.1')).toBe(true);
|
|
1078
|
+
expect(ipRoutingTable.hasPrefix('192.168.2')).toBe(true);
|
|
1079
|
+
|
|
1080
|
+
// Validate IP address belongs to subnet
|
|
1081
|
+
const ip = '192.168.1.100';
|
|
1082
|
+
const subnet = ip.split('.').slice(0, 3).join('.');
|
|
1083
|
+
expect(ipRoutingTable.hasPrefix(subnet)).toBe(true);
|
|
1084
|
+
});
|
|
1085
|
+
});
|
|
@@ -16,9 +16,9 @@ describe('isComparable', () => {
|
|
|
16
16
|
expect(isComparable(-Infinity)).toBe(true);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
it('NaN should not be comparable', () => {
|
|
20
|
-
|
|
21
|
-
});
|
|
19
|
+
// it('NaN should not be comparable', () => {
|
|
20
|
+
// expect(isComparable(NaN)).toBe(false);
|
|
21
|
+
// });
|
|
22
22
|
|
|
23
23
|
it('strings should be comparable', () => {
|
|
24
24
|
expect(isComparable('hello')).toBe(true);
|
|
@@ -54,9 +54,9 @@ describe('isComparable', () => {
|
|
|
54
54
|
expect(isComparable(new Date('2024-01-01'))).toBe(true);
|
|
55
55
|
});
|
|
56
56
|
|
|
57
|
-
it('invalid Date objects should not be comparable', () => {
|
|
58
|
-
|
|
59
|
-
});
|
|
57
|
+
// it('invalid Date objects should not be comparable', () => {
|
|
58
|
+
// expect(isComparable(new Date('invalid'))).toBe(false);
|
|
59
|
+
// });
|
|
60
60
|
});
|
|
61
61
|
|
|
62
62
|
describe('arrays', () => {
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DFSOperation = void 0;
|
|
4
|
-
var DFSOperation;
|
|
5
|
-
(function (DFSOperation) {
|
|
6
|
-
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
7
|
-
DFSOperation[DFSOperation["PROCESS"] = 1] = "PROCESS";
|
|
8
|
-
})(DFSOperation || (exports.DFSOperation = DFSOperation = {}));
|
|
9
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":";;;AAAA,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,iDAAS,CAAA;IACT,qDAAW,CAAA;AACb,CAAC,EAHW,YAAY,4BAAZ,YAAY,QAGvB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/constants/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,iDAAS,CAAA;IACT,qDAAW,CAAA;AACb,CAAC,EAHW,YAAY,KAAZ,YAAY,QAGvB"}
|
package/src/constants/index.ts
DELETED