heap-typed 1.54.3 → 2.0.1
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/dist/data-structures/base/iterable-element-base.d.ts +14 -40
- package/dist/data-structures/base/iterable-element-base.js +14 -11
- package/dist/data-structures/base/linear-base.d.ts +277 -0
- package/dist/data-structures/base/linear-base.js +552 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
- package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
- package/dist/data-structures/binary-tree/avl-tree.js +64 -0
- package/dist/data-structures/binary-tree/binary-tree.js +5 -5
- package/dist/data-structures/binary-tree/bst.js +11 -11
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
- package/dist/data-structures/graph/abstract-graph.js +16 -16
- package/dist/data-structures/hash/hash-map.d.ts +46 -0
- package/dist/data-structures/hash/hash-map.js +46 -0
- package/dist/data-structures/heap/heap.d.ts +3 -11
- package/dist/data-structures/heap/heap.js +0 -10
- package/dist/data-structures/heap/max-heap.d.ts +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
- package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
- package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
- package/dist/data-structures/queue/deque.d.ts +130 -91
- package/dist/data-structures/queue/deque.js +269 -169
- package/dist/data-structures/queue/queue.d.ts +131 -40
- package/dist/data-structures/queue/queue.js +181 -50
- package/dist/data-structures/stack/stack.d.ts +124 -11
- package/dist/data-structures/stack/stack.js +121 -10
- package/dist/data-structures/trie/trie.d.ts +4 -3
- package/dist/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/base/base.d.ts +9 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/types/data-structures/queue/deque.d.ts +2 -3
- package/dist/types/data-structures/queue/queue.d.ts +2 -2
- package/package.json +2 -2
- 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-multi-map.ts +51 -36
- package/src/data-structures/binary-tree/avl-tree.ts +64 -0
- package/src/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/data-structures/binary-tree/bst.ts +9 -9
- package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
- package/src/data-structures/graph/abstract-graph.ts +16 -16
- package/src/data-structures/hash/hash-map.ts +46 -0
- 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 +307 -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 +196 -63
- package/src/data-structures/stack/stack.ts +124 -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/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
|
@@ -339,7 +339,7 @@ export abstract class AbstractGraph<
|
|
|
339
339
|
|
|
340
340
|
if (isWeight) {
|
|
341
341
|
const allPaths = this.getAllPathsBetween(v1, v2);
|
|
342
|
-
let min =
|
|
342
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
343
343
|
for (const path of allPaths) {
|
|
344
344
|
min = Math.min(this.getPathSumWeight(path), min);
|
|
345
345
|
}
|
|
@@ -356,8 +356,8 @@ export abstract class AbstractGraph<
|
|
|
356
356
|
const queue = new Queue<VO>([vertex1]);
|
|
357
357
|
visited.set(vertex1, true);
|
|
358
358
|
let cost = 0;
|
|
359
|
-
while (queue.
|
|
360
|
-
for (let i = 0; i < queue.
|
|
359
|
+
while (queue.length > 0) {
|
|
360
|
+
for (let i = 0; i < queue.length; i++) {
|
|
361
361
|
const cur = queue.shift();
|
|
362
362
|
if (cur === vertex2) {
|
|
363
363
|
return cost;
|
|
@@ -404,7 +404,7 @@ export abstract class AbstractGraph<
|
|
|
404
404
|
if (isWeight) {
|
|
405
405
|
if (isDFS) {
|
|
406
406
|
const allPaths = this.getAllPathsBetween(v1, v2, 10000);
|
|
407
|
-
let min =
|
|
407
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
408
408
|
let minIndex = -1;
|
|
409
409
|
let index = 0;
|
|
410
410
|
for (const path of allPaths) {
|
|
@@ -475,7 +475,7 @@ export abstract class AbstractGraph<
|
|
|
475
475
|
getMinDist: boolean = false,
|
|
476
476
|
genPaths: boolean = false
|
|
477
477
|
): DijkstraResult<VO> {
|
|
478
|
-
let minDist =
|
|
478
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
479
479
|
let minDest: VO | undefined = undefined;
|
|
480
480
|
let minPath: VO[] = [];
|
|
481
481
|
const paths: VO[][] = [];
|
|
@@ -494,13 +494,13 @@ export abstract class AbstractGraph<
|
|
|
494
494
|
|
|
495
495
|
for (const vertex of vertexMap) {
|
|
496
496
|
const vertexOrKey = vertex[1];
|
|
497
|
-
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey,
|
|
497
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
498
498
|
}
|
|
499
499
|
distMap.set(srcVertex, 0);
|
|
500
500
|
preMap.set(srcVertex, undefined);
|
|
501
501
|
|
|
502
502
|
const getMinOfNoSeen = () => {
|
|
503
|
-
let min =
|
|
503
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
504
504
|
let minV: VO | undefined = undefined;
|
|
505
505
|
for (const [key, value] of distMap) {
|
|
506
506
|
if (!seen.has(key)) {
|
|
@@ -537,7 +537,7 @@ export abstract class AbstractGraph<
|
|
|
537
537
|
seen.add(cur);
|
|
538
538
|
if (destVertex && destVertex === cur) {
|
|
539
539
|
if (getMinDist) {
|
|
540
|
-
minDist = distMap.get(destVertex) ||
|
|
540
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
541
541
|
}
|
|
542
542
|
if (genPaths) {
|
|
543
543
|
getPaths(destVertex);
|
|
@@ -605,7 +605,7 @@ export abstract class AbstractGraph<
|
|
|
605
605
|
getMinDist: boolean = false,
|
|
606
606
|
genPaths: boolean = false
|
|
607
607
|
): DijkstraResult<VO> {
|
|
608
|
-
let minDist =
|
|
608
|
+
let minDist = Number.MAX_SAFE_INTEGER;
|
|
609
609
|
let minDest: VO | undefined = undefined;
|
|
610
610
|
let minPath: VO[] = [];
|
|
611
611
|
const paths: VO[][] = [];
|
|
@@ -621,7 +621,7 @@ export abstract class AbstractGraph<
|
|
|
621
621
|
|
|
622
622
|
for (const vertex of vertexMap) {
|
|
623
623
|
const vertexOrKey = vertex[1];
|
|
624
|
-
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey,
|
|
624
|
+
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Number.MAX_SAFE_INTEGER);
|
|
625
625
|
}
|
|
626
626
|
|
|
627
627
|
const heap = new Heap<{ key: number; value: VO }>([], { comparator: (a, b) => a.key - b.key });
|
|
@@ -661,7 +661,7 @@ export abstract class AbstractGraph<
|
|
|
661
661
|
seen.add(cur);
|
|
662
662
|
if (destVertex && destVertex === cur) {
|
|
663
663
|
if (getMinDist) {
|
|
664
|
-
minDist = distMap.get(destVertex) ||
|
|
664
|
+
minDist = distMap.get(destVertex) || Number.MAX_SAFE_INTEGER;
|
|
665
665
|
}
|
|
666
666
|
if (genPaths) {
|
|
667
667
|
getPaths(destVertex);
|
|
@@ -732,7 +732,7 @@ export abstract class AbstractGraph<
|
|
|
732
732
|
const paths: VO[][] = [];
|
|
733
733
|
const distMap: Map<VO, number> = new Map();
|
|
734
734
|
const preMap: Map<VO, VO> = new Map(); // predecessor
|
|
735
|
-
let min =
|
|
735
|
+
let min = Number.MAX_SAFE_INTEGER;
|
|
736
736
|
let minPath: VO[] = [];
|
|
737
737
|
// TODO
|
|
738
738
|
let hasNegativeCycle: boolean | undefined;
|
|
@@ -745,7 +745,7 @@ export abstract class AbstractGraph<
|
|
|
745
745
|
const numOfEdges = edgeMap.length;
|
|
746
746
|
|
|
747
747
|
this._vertexMap.forEach(vertex => {
|
|
748
|
-
distMap.set(vertex,
|
|
748
|
+
distMap.set(vertex, Number.MAX_SAFE_INTEGER);
|
|
749
749
|
});
|
|
750
750
|
|
|
751
751
|
distMap.set(srcVertex, 0);
|
|
@@ -759,7 +759,7 @@ export abstract class AbstractGraph<
|
|
|
759
759
|
const sWeight = distMap.get(s);
|
|
760
760
|
const dWeight = distMap.get(d);
|
|
761
761
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
762
|
-
if (distMap.get(s) !==
|
|
762
|
+
if (distMap.get(s) !== Number.MAX_SAFE_INTEGER && sWeight + weight < dWeight) {
|
|
763
763
|
distMap.set(d, sWeight + weight);
|
|
764
764
|
if (genPath) preMap.set(d, s);
|
|
765
765
|
}
|
|
@@ -804,7 +804,7 @@ export abstract class AbstractGraph<
|
|
|
804
804
|
const weight = edgeMap[j].weight;
|
|
805
805
|
const sWeight = distMap.get(s);
|
|
806
806
|
if (sWeight) {
|
|
807
|
-
if (sWeight !==
|
|
807
|
+
if (sWeight !== Number.MAX_SAFE_INTEGER && sWeight + weight < sWeight) hasNegativeCycle = true;
|
|
808
808
|
}
|
|
809
809
|
}
|
|
810
810
|
}
|
|
@@ -860,7 +860,7 @@ export abstract class AbstractGraph<
|
|
|
860
860
|
|
|
861
861
|
for (let i = 0; i < n; i++) {
|
|
862
862
|
for (let j = 0; j < n; j++) {
|
|
863
|
-
costs[i][j] = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])?.weight ||
|
|
863
|
+
costs[i][j] = this.getEdge(idAndVertices[i][1], idAndVertices[j][1])?.weight || Number.MAX_SAFE_INTEGER;
|
|
864
864
|
}
|
|
865
865
|
}
|
|
866
866
|
|
|
@@ -21,6 +21,52 @@ import { isWeakKey, rangeCheck } from '../../utils';
|
|
|
21
21
|
* 3. Unique Keys: Keys are unique.
|
|
22
22
|
* If you try to insert another entry with the same key, the new one will replace the old entry.
|
|
23
23
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
24
|
+
* @example
|
|
25
|
+
* // should maintain insertion order
|
|
26
|
+
* const linkedHashMap = new LinkedHashMap<number, string>();
|
|
27
|
+
* linkedHashMap.set(1, 'A');
|
|
28
|
+
* linkedHashMap.set(2, 'B');
|
|
29
|
+
* linkedHashMap.set(3, 'C');
|
|
30
|
+
*
|
|
31
|
+
* const result = Array.from(linkedHashMap);
|
|
32
|
+
* console.log(result); // [
|
|
33
|
+
* // [1, 'A'],
|
|
34
|
+
* // [2, 'B'],
|
|
35
|
+
* // [3, 'C']
|
|
36
|
+
* // ]
|
|
37
|
+
* @example
|
|
38
|
+
* // fast lookup of values by key
|
|
39
|
+
* const hashMap = new HashMap<number, string>();
|
|
40
|
+
* hashMap.set(1, 'A');
|
|
41
|
+
* hashMap.set(2, 'B');
|
|
42
|
+
* hashMap.set(3, 'C');
|
|
43
|
+
*
|
|
44
|
+
* console.log(hashMap.get(1)); // 'A'
|
|
45
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
46
|
+
* console.log(hashMap.get(3)); // 'C'
|
|
47
|
+
* console.log(hashMap.get(99)); // undefined
|
|
48
|
+
* @example
|
|
49
|
+
* // remove duplicates when adding multiple entries
|
|
50
|
+
* const hashMap = new HashMap<number, string>();
|
|
51
|
+
* hashMap.set(1, 'A');
|
|
52
|
+
* hashMap.set(2, 'B');
|
|
53
|
+
* hashMap.set(1, 'C'); // Update value for key 1
|
|
54
|
+
*
|
|
55
|
+
* console.log(hashMap.size); // 2
|
|
56
|
+
* console.log(hashMap.get(1)); // 'C'
|
|
57
|
+
* console.log(hashMap.get(2)); // 'B'
|
|
58
|
+
* @example
|
|
59
|
+
* // count occurrences of keys
|
|
60
|
+
* const data = [1, 2, 1, 3, 2, 1];
|
|
61
|
+
*
|
|
62
|
+
* const countMap = new HashMap<number, number>();
|
|
63
|
+
* for (const key of data) {
|
|
64
|
+
* countMap.set(key, (countMap.get(key) || 0) + 1);
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* console.log(countMap.get(1)); // 3
|
|
68
|
+
* console.log(countMap.get(2)); // 2
|
|
69
|
+
* console.log(countMap.get(3)); // 1
|
|
24
70
|
*/
|
|
25
71
|
export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
|
|
26
72
|
/**
|
|
@@ -185,7 +185,7 @@ import { IterableElementBase } from '../base';
|
|
|
185
185
|
* ]);
|
|
186
186
|
* console.log(scheduleTasks(tasks, 2)); // expectedMap
|
|
187
187
|
*/
|
|
188
|
-
export class Heap<E = any, R = any> extends IterableElementBase<E, R
|
|
188
|
+
export class Heap<E = any, R = any> extends IterableElementBase<E, R> {
|
|
189
189
|
/**
|
|
190
190
|
* The constructor initializes a heap data structure with optional elements and options.
|
|
191
191
|
* @param elements - The `elements` parameter is an iterable object that contains the initial
|
|
@@ -416,17 +416,6 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
416
416
|
return result;
|
|
417
417
|
}
|
|
418
418
|
|
|
419
|
-
/**
|
|
420
|
-
* Time Complexity: O(n)
|
|
421
|
-
* Space Complexity: O(n)
|
|
422
|
-
*
|
|
423
|
-
* Convert the heap to an array.
|
|
424
|
-
* @returns An array containing the elements of the heap.
|
|
425
|
-
*/
|
|
426
|
-
toArray(): E[] {
|
|
427
|
-
return [...this.elements];
|
|
428
|
-
}
|
|
429
|
-
|
|
430
419
|
/**
|
|
431
420
|
* Time Complexity: O(n)
|
|
432
421
|
* Space Complexity: O(n)
|
|
@@ -483,7 +472,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
483
472
|
* @returns The `filter` method is returning a new `Heap` object that contains the elements that pass
|
|
484
473
|
* the filter condition specified by the `callback` function.
|
|
485
474
|
*/
|
|
486
|
-
filter(callback: ElementCallback<E, R, boolean
|
|
475
|
+
filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): Heap<E, R> {
|
|
487
476
|
const filteredList = new Heap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
488
477
|
let index = 0;
|
|
489
478
|
for (const current of this) {
|
|
@@ -517,7 +506,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
|
|
|
517
506
|
* @returns a new instance of the `Heap` class with the mapped elements.
|
|
518
507
|
*/
|
|
519
508
|
map<EM, RM>(
|
|
520
|
-
callback: ElementCallback<E, R, EM
|
|
509
|
+
callback: ElementCallback<E, R, EM>,
|
|
521
510
|
comparator: Comparator<EM>,
|
|
522
511
|
toElementFn?: (rawElement: RM) => EM,
|
|
523
512
|
thisArg?: any
|
|
@@ -61,7 +61,7 @@ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
61
61
|
* @returns The `filter` method is returning a new `MaxHeap` object that contains the elements that pass
|
|
62
62
|
* the filter condition specified by the `callback` function.
|
|
63
63
|
*/
|
|
64
|
-
override filter(callback: ElementCallback<E, R, boolean
|
|
64
|
+
override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MaxHeap<E, R> {
|
|
65
65
|
const filteredList = new MaxHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
66
66
|
let index = 0;
|
|
67
67
|
for (const current of this) {
|
|
@@ -95,7 +95,7 @@ export class MaxHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
95
95
|
* @returns a new instance of the `MaxHeap` class with the mapped elements.
|
|
96
96
|
*/
|
|
97
97
|
override map<EM, RM>(
|
|
98
|
-
callback: ElementCallback<E, R, EM
|
|
98
|
+
callback: ElementCallback<E, R, EM>,
|
|
99
99
|
comparator: Comparator<EM>,
|
|
100
100
|
toElementFn?: (rawElement: RM) => EM,
|
|
101
101
|
thisArg?: any
|
|
@@ -49,7 +49,7 @@ export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
49
49
|
* @returns The `filter` method is returning a new `MinHeap` object that contains the elements that pass
|
|
50
50
|
* the filter condition specified by the `callback` function.
|
|
51
51
|
*/
|
|
52
|
-
override filter(callback: ElementCallback<E, R, boolean
|
|
52
|
+
override filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): MinHeap<E, R> {
|
|
53
53
|
const filteredList = new MinHeap<E, R>([], { toElementFn: this.toElementFn, comparator: this.comparator });
|
|
54
54
|
let index = 0;
|
|
55
55
|
for (const current of this) {
|
|
@@ -83,7 +83,7 @@ export class MinHeap<E = any, R = any> extends Heap<E, R> {
|
|
|
83
83
|
* @returns a new instance of the `MinHeap` class with the mapped elements.
|
|
84
84
|
*/
|
|
85
85
|
override map<EM, RM>(
|
|
86
|
-
callback: ElementCallback<E, R, EM
|
|
86
|
+
callback: ElementCallback<E, R, EM>,
|
|
87
87
|
comparator: Comparator<EM>,
|
|
88
88
|
toElementFn?: (rawElement: RM) => EM,
|
|
89
89
|
thisArg?: any
|