data-structure-typed 1.47.0 → 1.47.2
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 +140 -203
- package/dist/cjs/data-structures/hash/hash-map.d.ts +27 -0
- package/dist/cjs/data-structures/hash/hash-map.js +27 -0
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +27 -0
- package/dist/mjs/data-structures/hash/hash-map.js +27 -0
- package/dist/umd/data-structure-typed.js +27 -0
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +27 -0
- package/test/integration/index.html +74 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.47.
|
|
3
|
+
"version": "1.47.2",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Heap, Binary Tree, RedBlack Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -53,7 +53,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
53
53
|
return new AVLTreeNode<V, N>(key, value) as N;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
override createTree(options?: AVLTreeOptions) {
|
|
56
|
+
override createTree(options?: AVLTreeOptions): TREE {
|
|
57
57
|
return new AVLTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -305,6 +305,14 @@ export class HashMap<K = any, V = any> {
|
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
/**
|
|
309
|
+
* The `filter` function takes a predicate function and returns a new HashMap containing only the
|
|
310
|
+
* key-value pairs that satisfy the predicate.
|
|
311
|
+
* @param predicate - The `predicate` parameter is a function that takes two arguments: `element` and
|
|
312
|
+
* `map`.
|
|
313
|
+
* @returns a new HashMap object that contains the key-value pairs from the original HashMap that
|
|
314
|
+
* satisfy the given predicate function.
|
|
315
|
+
*/
|
|
308
316
|
filter(predicate: (element: [K, V], map: HashMap<K, V>) => boolean): HashMap<K, V> {
|
|
309
317
|
const filteredMap = new HashMap<K, V>();
|
|
310
318
|
for (const [key, value] of this) {
|
|
@@ -315,6 +323,13 @@ export class HashMap<K = any, V = any> {
|
|
|
315
323
|
return filteredMap;
|
|
316
324
|
}
|
|
317
325
|
|
|
326
|
+
/**
|
|
327
|
+
* The `map` function takes a callback function and returns a new HashMap with the values transformed
|
|
328
|
+
* by the callback.
|
|
329
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `element` and
|
|
330
|
+
* `map`.
|
|
331
|
+
* @returns a new HashMap object with the values mapped according to the provided callback function.
|
|
332
|
+
*/
|
|
318
333
|
map<NV>(callback: (element: [K, V], map: HashMap<K, V>) => NV): HashMap<K, NV> {
|
|
319
334
|
const mappedMap = new HashMap<K, NV>();
|
|
320
335
|
for (const [key, value] of this) {
|
|
@@ -324,6 +339,18 @@ export class HashMap<K = any, V = any> {
|
|
|
324
339
|
return mappedMap;
|
|
325
340
|
}
|
|
326
341
|
|
|
342
|
+
/**
|
|
343
|
+
* The `reduce` function iterates over the elements of a HashMap and applies a callback function to
|
|
344
|
+
* each element, accumulating a single value.
|
|
345
|
+
* @param callback - The callback parameter is a function that takes three arguments: accumulator,
|
|
346
|
+
* element, and map. It is called for each element in the HashMap and is used to accumulate a single
|
|
347
|
+
* result.
|
|
348
|
+
* @param {A} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
|
|
349
|
+
* is the value that will be passed as the first argument to the `callback` function when reducing
|
|
350
|
+
* the elements of the map.
|
|
351
|
+
* @returns The `reduce` function is returning the final value of the accumulator after iterating
|
|
352
|
+
* over all the elements in the HashMap and applying the callback function to each element.
|
|
353
|
+
*/
|
|
327
354
|
reduce<A>(callback: (accumulator: A, element: [K, V], map: HashMap<K, V>) => A, initialValue: A): A {
|
|
328
355
|
let accumulator = initialValue;
|
|
329
356
|
for (const element of this) {
|
|
@@ -97,7 +97,7 @@
|
|
|
97
97
|
}
|
|
98
98
|
console.log((performance.now() - tS).toFixed(2), `RedBlackTree ${n.toLocaleString()} add`);
|
|
99
99
|
console.log(`rbTree.size`, rbTree.size);
|
|
100
|
-
for (let i = 0; i < n -
|
|
100
|
+
for (let i = 0; i < n - 8; i++) {
|
|
101
101
|
rbTree.delete(i)
|
|
102
102
|
}
|
|
103
103
|
rbTree.print(rbTree.root, { isShowRedBlackNIL: true });
|
|
@@ -142,6 +142,79 @@
|
|
|
142
142
|
} catch (e) {
|
|
143
143
|
console.error(e);
|
|
144
144
|
}
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const { BST, RedBlackTree, AVLTree } = dataStructureTyped;
|
|
148
|
+
|
|
149
|
+
const bst = new BST();
|
|
150
|
+
bst.add(11);
|
|
151
|
+
bst.add(3);
|
|
152
|
+
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
153
|
+
bst.size === 16; // true
|
|
154
|
+
bst.has(6); // true
|
|
155
|
+
const node6 = bst.getNode(6); // BSTNode
|
|
156
|
+
bst.getHeight(6) === 2; // true
|
|
157
|
+
bst.getHeight() === 5; // true
|
|
158
|
+
bst.getDepth(6) === 3; // true
|
|
159
|
+
|
|
160
|
+
bst.getLeftMost()?.key === 1; // true
|
|
161
|
+
|
|
162
|
+
bst.delete(6);
|
|
163
|
+
bst.get(6); // undefined
|
|
164
|
+
bst.isAVLBalanced(); // true
|
|
165
|
+
bst.bfs()[0] === 11; // true
|
|
166
|
+
bst.print();
|
|
167
|
+
|
|
168
|
+
const objBST = new BST();
|
|
169
|
+
|
|
170
|
+
objBST.add(11, { "name": "Pablo", "age": 15 });
|
|
171
|
+
objBST.add(3, { "name": "Kirk", "age": 1 });
|
|
172
|
+
|
|
173
|
+
objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [
|
|
174
|
+
{ "name": "Alice", "age": 15 },
|
|
175
|
+
{ "name": "Bob", "age": 1 },
|
|
176
|
+
{ "name": "Charlie", "age": 8 },
|
|
177
|
+
{ "name": "David", "age": 13 },
|
|
178
|
+
{ "name": "Emma", "age": 16 },
|
|
179
|
+
{ "name": "Frank", "age": 2 },
|
|
180
|
+
{ "name": "Grace", "age": 6 },
|
|
181
|
+
{ "name": "Hannah", "age": 9 },
|
|
182
|
+
{ "name": "Isaac", "age": 12 },
|
|
183
|
+
{ "name": "Jack", "age": 14 },
|
|
184
|
+
{ "name": "Katie", "age": 4 },
|
|
185
|
+
{ "name": "Liam", "age": 7 },
|
|
186
|
+
{ "name": "Mia", "age": 10 },
|
|
187
|
+
{ "name": "Noah", "age": 5 }
|
|
188
|
+
]
|
|
189
|
+
);
|
|
190
|
+
objBST.print()
|
|
191
|
+
|
|
192
|
+
objBST.delete(11);
|
|
193
|
+
objBST.print()
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
const rbTree = new RedBlackTree();
|
|
197
|
+
rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
198
|
+
rbTree.isAVLBalanced(); // true
|
|
199
|
+
rbTree.delete(10);
|
|
200
|
+
rbTree.isAVLBalanced(); // true
|
|
201
|
+
console.log(`rbTree.delete(10)`);
|
|
202
|
+
rbTree.print();
|
|
203
|
+
|
|
204
|
+
rbTree.delete(14);
|
|
205
|
+
rbTree.print()
|
|
206
|
+
|
|
207
|
+
const avlTree = new AVLTree();
|
|
208
|
+
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
209
|
+
avlTree.isAVLBalanced(); // true
|
|
210
|
+
avlTree.delete(10);
|
|
211
|
+
avlTree.isAVLBalanced(); // true
|
|
212
|
+
avlTree.print()
|
|
213
|
+
avlTree.delete(14);
|
|
214
|
+
avlTree.print()
|
|
215
|
+
} catch (e) {
|
|
216
|
+
console.error(e);
|
|
217
|
+
}
|
|
145
218
|
</script>
|
|
146
219
|
|
|
147
220
|
</body>
|