data-structure-typed 1.47.5 → 1.47.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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +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 -5
- package/dist/cjs/data-structures/heap/min-heap.js +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 +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- 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 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- 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 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +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 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +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 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/cjs/types/common.d.ts +7 -0
- package/dist/cjs/types/common.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 +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/mjs/types/common.d.ts +7 -0
- 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 +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +629 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- 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 +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +156 -0
- package/tsconfig-cjs.json +1 -1
- package/test/integration/conversion.test.ts +0 -0
|
@@ -589,7 +589,7 @@ export class AbstractGraph {
|
|
|
589
589
|
if (vertexOrKey instanceof AbstractVertex)
|
|
590
590
|
distMap.set(vertexOrKey, Infinity);
|
|
591
591
|
}
|
|
592
|
-
const heap = new PriorityQueue({ comparator: (a, b) => a.key - b.key });
|
|
592
|
+
const heap = new PriorityQueue([], { comparator: (a, b) => a.key - b.key });
|
|
593
593
|
heap.add({ key: 0, value: srcVertex });
|
|
594
594
|
distMap.set(srcVertex, 0);
|
|
595
595
|
preMap.set(srcVertex, null);
|
|
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
14
14
|
protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
|
|
15
15
|
protected _hashFn: (key: K) => string;
|
|
16
16
|
protected _objHashFn: (key: K) => object;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options?: HashMapOptions<K, V>);
|
|
17
|
+
constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
|
|
23
18
|
protected _size: number;
|
|
24
19
|
get size(): number;
|
|
25
20
|
/**
|
|
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
|
|
|
172
167
|
* The above function is an iterator that yields key-value pairs from a linked list.
|
|
173
168
|
*/
|
|
174
169
|
[Symbol.iterator](): Generator<[K, V], void, unknown>;
|
|
170
|
+
print(): void;
|
|
175
171
|
/**
|
|
176
172
|
* Time Complexity: O(1)
|
|
177
173
|
* Space Complexity: O(1)
|
|
@@ -14,19 +14,13 @@ export class HashMap {
|
|
|
14
14
|
_sentinel;
|
|
15
15
|
_hashFn;
|
|
16
16
|
_objHashFn;
|
|
17
|
-
|
|
18
|
-
* The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
|
|
19
|
-
* @param options - The `options` parameter is an object that contains the `elements` property. The
|
|
20
|
-
* `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options = {
|
|
23
|
-
elements: [],
|
|
17
|
+
constructor(elements, options = {
|
|
24
18
|
hashFn: (key) => String(key),
|
|
25
19
|
objHashFn: (key) => key
|
|
26
20
|
}) {
|
|
27
21
|
this._sentinel = {};
|
|
28
22
|
this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
|
|
29
|
-
const {
|
|
23
|
+
const { hashFn, objHashFn } = options;
|
|
30
24
|
this._hashFn = hashFn;
|
|
31
25
|
this._objHashFn = objHashFn;
|
|
32
26
|
if (elements) {
|
|
@@ -349,6 +343,9 @@ export class HashMap {
|
|
|
349
343
|
node = node.next;
|
|
350
344
|
}
|
|
351
345
|
}
|
|
346
|
+
print() {
|
|
347
|
+
console.log([...this]);
|
|
348
|
+
}
|
|
352
349
|
/**
|
|
353
350
|
* Time Complexity: O(1)
|
|
354
351
|
* Space Complexity: O(1)
|
|
@@ -5,15 +5,12 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
+
import { HeapOptions } from "../../types";
|
|
8
9
|
export declare class Heap<E = any> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
elements?: E[];
|
|
12
|
-
});
|
|
10
|
+
options: HeapOptions<E>;
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
13
12
|
protected _elements: E[];
|
|
14
13
|
get elements(): E[];
|
|
15
|
-
protected _comparator: Comparator<E>;
|
|
16
|
-
get comparator(): Comparator<E>;
|
|
17
14
|
/**
|
|
18
15
|
* Get the size (number of elements) of the heap.
|
|
19
16
|
*/
|
|
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
|
|
|
26
23
|
/**
|
|
27
24
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
28
25
|
* @returns A new Heap instance.
|
|
26
|
+
* @param elements
|
|
29
27
|
* @param options
|
|
30
28
|
*/
|
|
31
|
-
static heapify<E>(options: {
|
|
32
|
-
elements: E[];
|
|
29
|
+
static heapify<E>(elements: Iterable<E>, options: {
|
|
33
30
|
comparator: Comparator<E>;
|
|
34
31
|
}): Heap<E>;
|
|
35
32
|
/**
|
|
@@ -204,6 +201,11 @@ export declare class Heap<E = any> {
|
|
|
204
201
|
* Time Complexity: O(log n)
|
|
205
202
|
* Space Complexity: O(1)
|
|
206
203
|
*/
|
|
204
|
+
print(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(n)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
207
209
|
/**
|
|
208
210
|
* Time Complexity: O(log n)
|
|
209
211
|
* Space Complexity: O(1)
|
|
@@ -212,10 +214,6 @@ export declare class Heap<E = any> {
|
|
|
212
214
|
* @param index - The index of the newly added element.
|
|
213
215
|
*/
|
|
214
216
|
protected _bubbleUp(index: number): void;
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(n)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
217
|
/**
|
|
220
218
|
* Time Complexity: O(log n)
|
|
221
219
|
* Space Complexity: O(1)
|
|
@@ -349,18 +347,18 @@ export declare class FibonacciHeap<E> {
|
|
|
349
347
|
*/
|
|
350
348
|
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
351
349
|
/**
|
|
352
|
-
*
|
|
353
|
-
* @param
|
|
354
|
-
* @param {E} b
|
|
350
|
+
* Create a new node.
|
|
351
|
+
* @param element
|
|
355
352
|
* @protected
|
|
356
353
|
*/
|
|
357
|
-
|
|
354
|
+
createNode(element: E): FibonacciHeapNode<E>;
|
|
358
355
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param
|
|
356
|
+
* Default comparator function used by the heap.
|
|
357
|
+
* @param {E} a
|
|
358
|
+
* @param {E} b
|
|
361
359
|
* @protected
|
|
362
360
|
*/
|
|
363
|
-
protected
|
|
361
|
+
protected _defaultComparator(a: E, b: E): number;
|
|
364
362
|
/**
|
|
365
363
|
* Time Complexity: O(1)
|
|
366
364
|
* Space Complexity: O(1)
|
|
@@ -399,7 +397,7 @@ export declare class FibonacciHeap<E> {
|
|
|
399
397
|
* @param x
|
|
400
398
|
* @protected
|
|
401
399
|
*/
|
|
402
|
-
protected
|
|
400
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
403
401
|
/**
|
|
404
402
|
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
405
403
|
* Space Complexity: O(n)
|
|
@@ -411,5 +409,5 @@ export declare class FibonacciHeap<E> {
|
|
|
411
409
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
412
410
|
* @protected
|
|
413
411
|
*/
|
|
414
|
-
protected
|
|
412
|
+
protected _consolidate(): void;
|
|
415
413
|
}
|
|
@@ -5,21 +5,35 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
export class Heap {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
options;
|
|
9
|
+
constructor(elements, options) {
|
|
10
|
+
const defaultComparator = (a, b) => {
|
|
11
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
12
|
+
throw new Error('The a, b params of compare function must be number');
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
return a - b;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
if (options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.options = {
|
|
23
|
+
comparator: defaultComparator
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (elements) {
|
|
27
|
+
for (const el of elements) {
|
|
28
|
+
this.push(el);
|
|
29
|
+
}
|
|
30
|
+
// this.fix();
|
|
13
31
|
}
|
|
14
32
|
}
|
|
15
33
|
_elements = [];
|
|
16
34
|
get elements() {
|
|
17
35
|
return this._elements;
|
|
18
36
|
}
|
|
19
|
-
_comparator;
|
|
20
|
-
get comparator() {
|
|
21
|
-
return this._comparator;
|
|
22
|
-
}
|
|
23
37
|
/**
|
|
24
38
|
* Get the size (number of elements) of the heap.
|
|
25
39
|
*/
|
|
@@ -36,10 +50,11 @@ export class Heap {
|
|
|
36
50
|
/**
|
|
37
51
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
38
52
|
* @returns A new Heap instance.
|
|
53
|
+
* @param elements
|
|
39
54
|
* @param options
|
|
40
55
|
*/
|
|
41
|
-
static heapify(options) {
|
|
42
|
-
return new Heap(options);
|
|
56
|
+
static heapify(elements, options) {
|
|
57
|
+
return new Heap(elements, options);
|
|
43
58
|
}
|
|
44
59
|
/**
|
|
45
60
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -253,7 +268,7 @@ export class Heap {
|
|
|
253
268
|
* @returns A new Heap instance containing the same elements.
|
|
254
269
|
*/
|
|
255
270
|
clone() {
|
|
256
|
-
const clonedHeap = new Heap(
|
|
271
|
+
const clonedHeap = new Heap([], this.options);
|
|
257
272
|
clonedHeap._elements = [...this.elements];
|
|
258
273
|
return clonedHeap;
|
|
259
274
|
}
|
|
@@ -305,7 +320,7 @@ export class Heap {
|
|
|
305
320
|
}
|
|
306
321
|
}
|
|
307
322
|
filter(predicate) {
|
|
308
|
-
const filteredHeap = new Heap(
|
|
323
|
+
const filteredHeap = new Heap([], this.options);
|
|
309
324
|
let index = 0;
|
|
310
325
|
for (const el of this) {
|
|
311
326
|
if (predicate(el, index, this)) {
|
|
@@ -316,7 +331,7 @@ export class Heap {
|
|
|
316
331
|
return filteredHeap;
|
|
317
332
|
}
|
|
318
333
|
map(callback, comparator) {
|
|
319
|
-
const mappedHeap = new Heap({ comparator: comparator });
|
|
334
|
+
const mappedHeap = new Heap([], { comparator: comparator });
|
|
320
335
|
let index = 0;
|
|
321
336
|
for (const el of this) {
|
|
322
337
|
mappedHeap.add(callback(el, index, this));
|
|
@@ -337,6 +352,13 @@ export class Heap {
|
|
|
337
352
|
* Time Complexity: O(log n)
|
|
338
353
|
* Space Complexity: O(1)
|
|
339
354
|
*/
|
|
355
|
+
print() {
|
|
356
|
+
console.log([...this]);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Time Complexity: O(n)
|
|
360
|
+
* Space Complexity: O(1)
|
|
361
|
+
*/
|
|
340
362
|
/**
|
|
341
363
|
* Time Complexity: O(log n)
|
|
342
364
|
* Space Complexity: O(1)
|
|
@@ -349,17 +371,13 @@ export class Heap {
|
|
|
349
371
|
while (index > 0) {
|
|
350
372
|
const parent = (index - 1) >> 1;
|
|
351
373
|
const parentItem = this.elements[parent];
|
|
352
|
-
if (this.
|
|
374
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
353
375
|
break;
|
|
354
376
|
this.elements[index] = parentItem;
|
|
355
377
|
index = parent;
|
|
356
378
|
}
|
|
357
379
|
this.elements[index] = element;
|
|
358
380
|
}
|
|
359
|
-
/**
|
|
360
|
-
* Time Complexity: O(n)
|
|
361
|
-
* Space Complexity: O(1)
|
|
362
|
-
*/
|
|
363
381
|
/**
|
|
364
382
|
* Time Complexity: O(log n)
|
|
365
383
|
* Space Complexity: O(1)
|
|
@@ -375,11 +393,11 @@ export class Heap {
|
|
|
375
393
|
const right = left + 1;
|
|
376
394
|
let minItem = this.elements[left];
|
|
377
395
|
if (right < this.elements.length &&
|
|
378
|
-
this.
|
|
396
|
+
this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
379
397
|
left = right;
|
|
380
398
|
minItem = this.elements[right];
|
|
381
399
|
}
|
|
382
|
-
if (this.
|
|
400
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
383
401
|
break;
|
|
384
402
|
this.elements[index] = minItem;
|
|
385
403
|
index = left;
|
|
@@ -404,7 +422,7 @@ export class FibonacciHeapNode {
|
|
|
404
422
|
export class FibonacciHeap {
|
|
405
423
|
constructor(comparator) {
|
|
406
424
|
this.clear();
|
|
407
|
-
this._comparator = comparator || this.
|
|
425
|
+
this._comparator = comparator || this._defaultComparator;
|
|
408
426
|
if (typeof this.comparator !== 'function') {
|
|
409
427
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
410
428
|
}
|
|
@@ -579,7 +597,7 @@ export class FibonacciHeap {
|
|
|
579
597
|
}
|
|
580
598
|
else {
|
|
581
599
|
this._min = z.right;
|
|
582
|
-
this.
|
|
600
|
+
this._consolidate();
|
|
583
601
|
}
|
|
584
602
|
this._size--;
|
|
585
603
|
return z.element;
|
|
@@ -619,27 +637,27 @@ export class FibonacciHeap {
|
|
|
619
637
|
// Clear the heap that was merged
|
|
620
638
|
heapToMerge.clear();
|
|
621
639
|
}
|
|
640
|
+
/**
|
|
641
|
+
* Create a new node.
|
|
642
|
+
* @param element
|
|
643
|
+
* @protected
|
|
644
|
+
*/
|
|
645
|
+
createNode(element) {
|
|
646
|
+
return new FibonacciHeapNode(element);
|
|
647
|
+
}
|
|
622
648
|
/**
|
|
623
649
|
* Default comparator function used by the heap.
|
|
624
650
|
* @param {E} a
|
|
625
651
|
* @param {E} b
|
|
626
652
|
* @protected
|
|
627
653
|
*/
|
|
628
|
-
|
|
654
|
+
_defaultComparator(a, b) {
|
|
629
655
|
if (a < b)
|
|
630
656
|
return -1;
|
|
631
657
|
if (a > b)
|
|
632
658
|
return 1;
|
|
633
659
|
return 0;
|
|
634
660
|
}
|
|
635
|
-
/**
|
|
636
|
-
* Create a new node.
|
|
637
|
-
* @param element
|
|
638
|
-
* @protected
|
|
639
|
-
*/
|
|
640
|
-
createNode(element) {
|
|
641
|
-
return new FibonacciHeapNode(element);
|
|
642
|
-
}
|
|
643
661
|
/**
|
|
644
662
|
* Time Complexity: O(1)
|
|
645
663
|
* Space Complexity: O(1)
|
|
@@ -695,7 +713,7 @@ export class FibonacciHeap {
|
|
|
695
713
|
* @param x
|
|
696
714
|
* @protected
|
|
697
715
|
*/
|
|
698
|
-
|
|
716
|
+
_link(y, x) {
|
|
699
717
|
this.removeFromRoot(y);
|
|
700
718
|
y.left = y;
|
|
701
719
|
y.right = y;
|
|
@@ -714,7 +732,7 @@ export class FibonacciHeap {
|
|
|
714
732
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
715
733
|
* @protected
|
|
716
734
|
*/
|
|
717
|
-
|
|
735
|
+
_consolidate() {
|
|
718
736
|
const A = new Array(this.size);
|
|
719
737
|
const elements = this.consumeLinkedList(this.root);
|
|
720
738
|
let x, y, d, t;
|
|
@@ -728,7 +746,7 @@ export class FibonacciHeap {
|
|
|
728
746
|
x = y;
|
|
729
747
|
y = t;
|
|
730
748
|
}
|
|
731
|
-
this.
|
|
749
|
+
this._link(y, x);
|
|
732
750
|
A[d] = undefined;
|
|
733
751
|
d++;
|
|
734
752
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
export class MaxHeap extends Heap {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MaxHeap extends Heap {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
9
|
export class MinHeap extends Heap {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MinHeap extends Heap {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
20
20
|
/**
|
|
21
21
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
22
22
|
*/
|
|
23
|
-
constructor();
|
|
23
|
+
constructor(elements?: Iterable<E>);
|
|
24
24
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
25
25
|
get head(): DoublyLinkedListNode<E> | null;
|
|
26
26
|
protected _tail: DoublyLinkedListNode<E> | null;
|
|
@@ -453,4 +453,5 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
453
453
|
* elements in the linked list.
|
|
454
454
|
*/
|
|
455
455
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
|
|
456
|
+
print(): void;
|
|
456
457
|
}
|
|
@@ -24,10 +24,15 @@ export class DoublyLinkedList {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
|
-
constructor() {
|
|
27
|
+
constructor(elements) {
|
|
28
28
|
this._head = null;
|
|
29
29
|
this._tail = null;
|
|
30
30
|
this._length = 0;
|
|
31
|
+
if (elements) {
|
|
32
|
+
for (const el of elements) {
|
|
33
|
+
this.push(el);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
31
36
|
}
|
|
32
37
|
_head;
|
|
33
38
|
get head() {
|
|
@@ -766,4 +771,7 @@ export class DoublyLinkedList {
|
|
|
766
771
|
}
|
|
767
772
|
return accumulator;
|
|
768
773
|
}
|
|
774
|
+
print() {
|
|
775
|
+
console.log([...this]);
|
|
776
|
+
}
|
|
769
777
|
}
|
|
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
19
19
|
/**
|
|
20
20
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
21
21
|
*/
|
|
22
|
-
constructor();
|
|
22
|
+
constructor(elements?: Iterable<E>);
|
|
23
23
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
24
24
|
get head(): SinglyLinkedListNode<E> | null;
|
|
25
25
|
protected _tail: SinglyLinkedListNode<E> | null;
|
|
@@ -411,4 +411,5 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
411
411
|
* elements in the linked list.
|
|
412
412
|
*/
|
|
413
413
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
|
|
414
|
+
print(): void;
|
|
414
415
|
}
|
|
@@ -22,10 +22,14 @@ export class SinglyLinkedList {
|
|
|
22
22
|
/**
|
|
23
23
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
24
24
|
*/
|
|
25
|
-
constructor() {
|
|
25
|
+
constructor(elements) {
|
|
26
26
|
this._head = null;
|
|
27
27
|
this._tail = null;
|
|
28
28
|
this._length = 0;
|
|
29
|
+
if (elements) {
|
|
30
|
+
for (const el of elements)
|
|
31
|
+
this.push(el);
|
|
32
|
+
}
|
|
29
33
|
}
|
|
30
34
|
_head;
|
|
31
35
|
get head() {
|
|
@@ -710,4 +714,7 @@ export class SinglyLinkedList {
|
|
|
710
714
|
}
|
|
711
715
|
return accumulator;
|
|
712
716
|
}
|
|
717
|
+
print() {
|
|
718
|
+
console.log([...this]);
|
|
719
|
+
}
|
|
713
720
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
export class MaxPriorityQueue extends PriorityQueue {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MaxPriorityQueue extends PriorityQueue {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
9
|
export class MinPriorityQueue extends PriorityQueue {
|
|
10
|
-
constructor(options = {
|
|
10
|
+
constructor(elements, options = {
|
|
11
11
|
comparator: (a, b) => {
|
|
12
12
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
13
13
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -17,6 +17,6 @@ export class MinPriorityQueue extends PriorityQueue {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
}) {
|
|
20
|
-
super(options);
|
|
20
|
+
super(elements, options);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from '../heap';
|
|
9
|
-
import {
|
|
9
|
+
import { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -428,6 +428,7 @@ export declare class Deque<E> {
|
|
|
428
428
|
* applying the callback function to each element.
|
|
429
429
|
*/
|
|
430
430
|
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
|
|
431
|
+
print(): void;
|
|
431
432
|
/**
|
|
432
433
|
* Time Complexity: O(n)
|
|
433
434
|
* Space Complexity: O(n)
|
|
@@ -205,6 +205,7 @@ export declare class Queue<E = any> {
|
|
|
205
205
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
206
206
|
*/
|
|
207
207
|
clone(): Queue<E>;
|
|
208
|
+
print(): void;
|
|
208
209
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
209
210
|
/**
|
|
210
211
|
* Time Complexity: O(n)
|
|
@@ -10,7 +10,7 @@ export declare class Stack<E = any> {
|
|
|
10
10
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
11
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
12
|
*/
|
|
13
|
-
constructor(elements?: E
|
|
13
|
+
constructor(elements?: Iterable<E>);
|
|
14
14
|
protected _elements: E[];
|
|
15
15
|
get elements(): E[];
|
|
16
16
|
/**
|
|
@@ -116,4 +116,5 @@ export declare class Stack<E = any> {
|
|
|
116
116
|
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
|
|
117
117
|
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
|
|
118
118
|
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
|
|
119
|
+
print(): void;
|
|
119
120
|
}
|