data-structure-typed 1.35.1 → 1.36.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/CHANGELOG.md +8 -1
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +21 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +13 -864
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
- package/dist/data-structures/binary-tree/avl-tree.js +90 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +536 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1194 -2
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +133 -0
- package/dist/data-structures/binary-tree/bst.js +114 -0
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +12 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +45 -0
- package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +270 -7
- package/dist/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +167 -0
- package/dist/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +54 -0
- package/dist/data-structures/graph/map-graph.js.map +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
- package/dist/data-structures/graph/undirected-graph.js +105 -0
- package/dist/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +35 -0
- package/dist/data-structures/hash/coordinate-map.js.map +1 -1
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +28 -0
- package/dist/data-structures/hash/coordinate-set.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +29 -1
- package/dist/data-structures/hash/hash-map.js.map +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +88 -6
- package/dist/data-structures/hash/hash-table.js.map +1 -1
- package/dist/data-structures/hash/index.d.ts +7 -0
- package/dist/data-structures/hash/pair.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/heap/heap.d.ts +99 -0
- package/dist/data-structures/heap/heap.js +200 -78
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/max-heap.d.ts +12 -0
- package/dist/data-structures/heap/max-heap.js +16 -6
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +12 -0
- package/dist/data-structures/heap/min-heap.js +16 -6
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
- package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
- package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +15 -0
- package/dist/data-structures/matrix/matrix.js.map +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +91 -2
- package/dist/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +28 -0
- package/dist/data-structures/matrix/navigator.js.map +1 -1
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +188 -1
- package/dist/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/priority-queue.js +11 -174
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +124 -0
- package/dist/data-structures/queue/deque.js.map +1 -1
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +80 -0
- package/dist/data-structures/queue/queue.js.map +1 -1
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +50 -0
- package/dist/data-structures/stack/stack.js.map +1 -1
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +1 -0
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/trie.d.ts +61 -0
- package/dist/data-structures/trie/trie.js +36 -0
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
- package/dist/interfaces/abstract-graph.d.ts +5 -0
- package/dist/interfaces/avl-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +6 -0
- package/dist/interfaces/directed-graph.d.ts +3 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/index.d.ts +15 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/rb-tree.d.ts +6 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/tree-multiset.d.ts +6 -0
- package/dist/interfaces/undirected-graph.d.ts +3 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +32 -0
- package/dist/types/data-structures/abstract-binary-tree.js +6 -0
- package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.d.ts +6 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/hash.d.ts +1 -0
- package/dist/types/data-structures/heap.d.ts +1 -0
- package/dist/types/data-structures/index.d.ts +16 -0
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/navigator.d.ts +14 -0
- package/dist/types/data-structures/priority-queue.d.ts +7 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/utils.d.ts +19 -0
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +3 -509
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +1 -1175
- package/lib/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/binary-tree.d.ts +506 -1
- package/lib/data-structures/binary-tree/binary-tree.js +1173 -2
- package/lib/data-structures/binary-tree/bst.d.ts +1 -1
- package/lib/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/lib/data-structures/graph/abstract-graph.js +3 -5
- package/lib/data-structures/heap/heap.d.ts +84 -68
- package/lib/data-structures/heap/heap.js +168 -107
- package/lib/data-structures/heap/max-heap.d.ts +6 -17
- package/lib/data-structures/heap/max-heap.js +11 -17
- package/lib/data-structures/heap/min-heap.d.ts +6 -18
- package/lib/data-structures/heap/min-heap.js +11 -18
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -7
- package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -7
- package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
- package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
- package/lib/data-structures/priority-queue/priority-queue.js +11 -315
- package/lib/types/data-structures/abstract-binary-tree.d.ts +1 -3
- package/lib/types/data-structures/binary-tree.d.ts +4 -2
- package/lib/types/data-structures/heap.d.ts +1 -3
- package/package.json +10 -6
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +4 -1527
- package/src/data-structures/binary-tree/avl-tree.ts +3 -3
- package/src/data-structures/binary-tree/binary-tree.ts +1524 -5
- package/src/data-structures/binary-tree/bst.ts +3 -3
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
- package/src/data-structures/graph/abstract-graph.ts +3 -5
- package/src/data-structures/heap/heap.ts +167 -143
- package/src/data-structures/heap/max-heap.ts +15 -22
- package/src/data-structures/heap/min-heap.ts +15 -23
- package/src/data-structures/priority-queue/max-priority-queue.ts +13 -46
- package/src/data-structures/priority-queue/min-priority-queue.ts +13 -47
- package/src/data-structures/priority-queue/priority-queue.ts +7 -350
- package/src/types/data-structures/abstract-binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/src/types/data-structures/heap.ts +1 -5
- package/test/unit/data-structures/heap/heap.test.ts +26 -18
- package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
- package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +9 -10
- 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 +14 -30
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.LICENSE.txt +15 -0
- package/umd/bundle.min.js.map +1 -1
|
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, BinaryTreeNodePropertyName, BSTComparator, BSTN
|
|
|
9
9
|
import { CP } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBST, IBSTNode } from '../../interfaces';
|
|
12
|
-
export declare class BSTNode<V = any,
|
|
12
|
+
export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> implements IBSTNode<V, FAMILY> {
|
|
13
13
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
2
|
import { IRBTree, IRBTreeNode } from '../../interfaces';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
|
-
export declare class RBTreeNode<V = any,
|
|
4
|
+
export declare class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<V, FAMILY> implements IRBTreeNode<V, FAMILY> {
|
|
5
5
|
private _color;
|
|
6
6
|
constructor(key: BinaryTreeNodeKey, val?: V);
|
|
7
7
|
get color(): RBColor;
|
|
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } f
|
|
|
9
9
|
import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
|
|
10
10
|
import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class TreeMultisetNode<V = any,
|
|
12
|
+
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> implements ITreeMultisetNode<V, FAMILY> {
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
15
15
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Kirk Qi
|
|
5
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { arrayRemove, uuidV4 } from '../../utils';
|
|
@@ -523,9 +523,7 @@ export class AbstractGraph {
|
|
|
523
523
|
if (vertexOrKey instanceof AbstractVertex)
|
|
524
524
|
distMap.set(vertexOrKey, Infinity);
|
|
525
525
|
}
|
|
526
|
-
const heap = new PriorityQueue(
|
|
527
|
-
comparator: (a, b) => a.key - b.key
|
|
528
|
-
});
|
|
526
|
+
const heap = new PriorityQueue((a, b) => a.key - b.key);
|
|
529
527
|
heap.add({ key: 0, val: srcVertex });
|
|
530
528
|
distMap.set(srcVertex, 0);
|
|
531
529
|
preMap.set(srcVertex, null);
|
|
@@ -1,83 +1,99 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @author Kirk Qi
|
|
4
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
5
|
* @license MIT License
|
|
7
6
|
*/
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
* @param
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
import type { CompareFunction } from '../../types';
|
|
8
|
+
export declare class Heap<E> {
|
|
9
|
+
protected nodes: E[];
|
|
10
|
+
private readonly comparator;
|
|
11
|
+
constructor(comparator: CompareFunction<E>);
|
|
12
|
+
/**
|
|
13
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
14
|
+
* @param value - The element to be inserted.
|
|
15
|
+
*/
|
|
16
|
+
add(value: E): Heap<E>;
|
|
17
|
+
/**
|
|
18
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
19
|
+
* @returns The top element or null if the heap is empty.
|
|
20
|
+
*/
|
|
21
|
+
poll(): E | null;
|
|
22
|
+
/**
|
|
23
|
+
* Float operation to maintain heap properties after adding an element.
|
|
24
|
+
* @param index - The index of the newly added element.
|
|
25
|
+
*/
|
|
26
|
+
protected bubbleUp(index: number): void;
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
28
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
29
|
+
* @param index - The index from which to start sinking.
|
|
31
30
|
*/
|
|
32
|
-
protected
|
|
33
|
-
protected abstract _pq: PriorityQueue<HeapItem<V>>;
|
|
34
|
-
get pq(): PriorityQueue<HeapItem<V>>;
|
|
35
|
-
protected _priorityExtractor: (val: V) => number;
|
|
36
|
-
get priorityExtractor(): (val: V) => number;
|
|
31
|
+
protected sinkDown(index: number): void;
|
|
37
32
|
/**
|
|
38
|
-
*
|
|
39
|
-
|
|
33
|
+
* Fix the entire heap to maintain heap properties.
|
|
34
|
+
*/
|
|
35
|
+
protected fix(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Peek at the top element of the heap without removing it.
|
|
38
|
+
* @returns The top element or null if the heap is empty.
|
|
39
|
+
*/
|
|
40
|
+
peek(): E | null;
|
|
41
|
+
/**
|
|
42
|
+
* Get the size (number of elements) of the heap.
|
|
40
43
|
*/
|
|
41
44
|
get size(): number;
|
|
42
45
|
/**
|
|
43
|
-
*
|
|
44
|
-
* @returns
|
|
46
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
47
|
+
* @returns The last element or null if the heap is empty.
|
|
48
|
+
*/
|
|
49
|
+
get leaf(): E | null;
|
|
50
|
+
/**
|
|
51
|
+
* Check if the heap is empty.
|
|
52
|
+
* @returns True if the heap is empty, otherwise false.
|
|
45
53
|
*/
|
|
46
54
|
isEmpty(): boolean;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
peek(isItem: true): HeapItem<V> | null;
|
|
50
|
-
peekLast(isItem?: undefined): V | undefined;
|
|
51
|
-
peekLast(isItem: false): V | undefined;
|
|
52
|
-
peekLast(isItem: true): HeapItem<V> | null;
|
|
53
|
-
/**
|
|
54
|
-
* The `add` function adds an val to a priority queue with an optional priority value.
|
|
55
|
-
* @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
56
|
-
* type.
|
|
57
|
-
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
58
|
-
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
59
|
-
* the value of `val`. If the `val` parameter is not a number, then the
|
|
60
|
-
* @returns The `add` method returns the instance of the `Heap` class.
|
|
61
|
-
* @throws {Error} if priority is not a valid number
|
|
62
|
-
*/
|
|
63
|
-
add(priority: number, val?: V): Heap<V>;
|
|
64
|
-
poll(isItem?: undefined): V | undefined;
|
|
65
|
-
poll(isItem: false): V | undefined;
|
|
66
|
-
poll(isItem: true): HeapItem<V> | null;
|
|
67
|
-
/**
|
|
68
|
-
* The function checks if a given node or value exists in the priority queue.
|
|
69
|
-
* @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
|
|
70
|
-
* @returns a boolean value.
|
|
71
|
-
*/
|
|
72
|
-
has(node: V | HeapItem<V>): boolean;
|
|
73
|
-
toArray(isItem?: undefined): (V | undefined)[];
|
|
74
|
-
toArray(isItem: false): (V | undefined)[];
|
|
75
|
-
toArray(isItem: true): (HeapItem<V> | null)[];
|
|
76
|
-
sort(isItem?: undefined): (V | undefined)[];
|
|
77
|
-
sort(isItem: false): (V | undefined)[];
|
|
78
|
-
sort(isItem: true): (HeapItem<V> | null)[];
|
|
79
|
-
/**
|
|
80
|
-
* The clear function clears the priority queue.
|
|
55
|
+
/**
|
|
56
|
+
* Reset the nodes of the heap. Make the nodes empty.
|
|
81
57
|
*/
|
|
82
58
|
clear(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Clear and add nodes of the heap
|
|
61
|
+
* @param nodes
|
|
62
|
+
*/
|
|
63
|
+
refill(nodes: E[]): void;
|
|
64
|
+
/**
|
|
65
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
66
|
+
* @param value - the element to check.
|
|
67
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
68
|
+
*/
|
|
69
|
+
has(value: E): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
72
|
+
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
73
|
+
* @returns An array containing elements traversed in the specified order.
|
|
74
|
+
*/
|
|
75
|
+
dfs(order: 'in' | 'pre' | 'post'): E[];
|
|
76
|
+
/**
|
|
77
|
+
* Convert the heap to an array.
|
|
78
|
+
* @returns An array containing the elements of the heap.
|
|
79
|
+
*/
|
|
80
|
+
toArray(): E[];
|
|
81
|
+
getNodes(): E[];
|
|
82
|
+
/**
|
|
83
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
84
|
+
* @returns A new Heap instance containing the same elements.
|
|
85
|
+
*/
|
|
86
|
+
clone(): Heap<E>;
|
|
87
|
+
/**
|
|
88
|
+
* Sort the elements in the heap and return them as an array.
|
|
89
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
90
|
+
*/
|
|
91
|
+
sort(): E[];
|
|
92
|
+
/**
|
|
93
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
94
|
+
* @param nodes
|
|
95
|
+
* @param comparator - Comparison function.
|
|
96
|
+
* @returns A new Heap instance.
|
|
97
|
+
*/
|
|
98
|
+
static heapify<E>(nodes: E[], comparator: CompareFunction<E>): Heap<E>;
|
|
83
99
|
}
|
|
@@ -1,152 +1,213 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
* @author Kirk Qi
|
|
4
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
5
|
+
* @license MIT License
|
|
6
|
+
*/
|
|
7
|
+
export class Heap {
|
|
8
|
+
constructor(comparator) {
|
|
9
|
+
this.nodes = [];
|
|
10
|
+
this.comparator = comparator;
|
|
11
|
+
}
|
|
2
12
|
/**
|
|
3
|
-
*
|
|
4
|
-
* @param
|
|
5
|
-
* optional and has a default value of `NaN`.
|
|
6
|
-
* @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
|
|
7
|
-
* `V` or `null`.
|
|
13
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
14
|
+
* @param value - The element to be inserted.
|
|
8
15
|
*/
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
12
|
-
|
|
13
|
-
get priority() {
|
|
14
|
-
return this._priority;
|
|
15
|
-
}
|
|
16
|
-
set priority(value) {
|
|
17
|
-
this._priority = value;
|
|
18
|
-
}
|
|
19
|
-
get val() {
|
|
20
|
-
return this._val;
|
|
16
|
+
add(value) {
|
|
17
|
+
this.nodes.push(value);
|
|
18
|
+
this.bubbleUp(this.nodes.length - 1);
|
|
19
|
+
return this;
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
23
|
+
* @returns The top element or null if the heap is empty.
|
|
24
|
+
*/
|
|
25
|
+
poll() {
|
|
26
|
+
if (this.nodes.length === 0) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
if (this.nodes.length === 1) {
|
|
30
|
+
return this.nodes.pop();
|
|
31
|
+
}
|
|
32
|
+
const topValue = this.nodes[0];
|
|
33
|
+
this.nodes[0] = this.nodes.pop();
|
|
34
|
+
this.sinkDown(0);
|
|
35
|
+
return topValue;
|
|
24
36
|
}
|
|
25
|
-
}
|
|
26
|
-
export class Heap {
|
|
27
37
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
38
|
+
* Float operation to maintain heap properties after adding an element.
|
|
39
|
+
* @param index - The index of the newly added element.
|
|
31
40
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
bubbleUp(index) {
|
|
42
|
+
const element = this.nodes[index];
|
|
43
|
+
while (index > 0) {
|
|
44
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
45
|
+
const parent = this.nodes[parentIndex];
|
|
46
|
+
if (this.comparator(element, parent) < 0) {
|
|
47
|
+
this.nodes[index] = parent;
|
|
48
|
+
this.nodes[parentIndex] = element;
|
|
49
|
+
index = parentIndex;
|
|
37
50
|
}
|
|
38
|
-
|
|
51
|
+
else {
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
58
|
+
* @param index - The index from which to start sinking.
|
|
59
|
+
*/
|
|
60
|
+
sinkDown(index) {
|
|
61
|
+
const leftChildIndex = 2 * index + 1;
|
|
62
|
+
const rightChildIndex = 2 * index + 2;
|
|
63
|
+
const length = this.nodes.length;
|
|
64
|
+
let targetIndex = index;
|
|
65
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
66
|
+
targetIndex = leftChildIndex;
|
|
67
|
+
}
|
|
68
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
69
|
+
targetIndex = rightChildIndex;
|
|
39
70
|
}
|
|
40
|
-
|
|
41
|
-
|
|
71
|
+
if (targetIndex !== index) {
|
|
72
|
+
const temp = this.nodes[index];
|
|
73
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
74
|
+
this.nodes[targetIndex] = temp;
|
|
75
|
+
this.sinkDown(targetIndex);
|
|
42
76
|
}
|
|
43
77
|
}
|
|
44
|
-
|
|
45
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Fix the entire heap to maintain heap properties.
|
|
80
|
+
*/
|
|
81
|
+
fix() {
|
|
82
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
83
|
+
this.sinkDown(i);
|
|
46
84
|
}
|
|
47
|
-
|
|
48
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Peek at the top element of the heap without removing it.
|
|
87
|
+
* @returns The top element or null if the heap is empty.
|
|
88
|
+
*/
|
|
89
|
+
peek() {
|
|
90
|
+
if (this.nodes.length === 0) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return this.nodes[0];
|
|
49
94
|
}
|
|
50
95
|
/**
|
|
51
|
-
*
|
|
52
|
-
* @returns The size of the priority queue.
|
|
96
|
+
* Get the size (number of elements) of the heap.
|
|
53
97
|
*/
|
|
54
98
|
get size() {
|
|
55
|
-
return this.
|
|
99
|
+
return this.nodes.length;
|
|
56
100
|
}
|
|
57
101
|
/**
|
|
58
|
-
*
|
|
59
|
-
* @returns
|
|
102
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
103
|
+
* @returns The last element or null if the heap is empty.
|
|
104
|
+
*/
|
|
105
|
+
get leaf() {
|
|
106
|
+
var _a;
|
|
107
|
+
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if the heap is empty.
|
|
111
|
+
* @returns True if the heap is empty, otherwise false.
|
|
60
112
|
*/
|
|
61
113
|
isEmpty() {
|
|
62
|
-
return this.
|
|
114
|
+
return this.size === 0;
|
|
63
115
|
}
|
|
64
116
|
/**
|
|
65
|
-
*
|
|
66
|
-
* @returns The `peek()` method is returning either a `HeapItem<V>` object or `null`.Returns an val with the highest priority in the queue
|
|
117
|
+
* Reset the nodes of the heap. Make the nodes empty.
|
|
67
118
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const peeked = this._pq.peek();
|
|
71
|
-
return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
|
|
119
|
+
clear() {
|
|
120
|
+
this.nodes = [];
|
|
72
121
|
}
|
|
73
122
|
/**
|
|
74
|
-
*
|
|
75
|
-
* @
|
|
123
|
+
* Clear and add nodes of the heap
|
|
124
|
+
* @param nodes
|
|
76
125
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return isItem ? leafItem : leafItem === null || leafItem === void 0 ? void 0 : leafItem.val;
|
|
126
|
+
refill(nodes) {
|
|
127
|
+
this.nodes = nodes;
|
|
128
|
+
this.fix();
|
|
81
129
|
}
|
|
82
130
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @param
|
|
85
|
-
*
|
|
86
|
-
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
87
|
-
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
88
|
-
* the value of `val`. If the `val` parameter is not a number, then the
|
|
89
|
-
* @returns The `add` method returns the instance of the `Heap` class.
|
|
90
|
-
* @throws {Error} if priority is not a valid number
|
|
131
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
132
|
+
* @param value - the element to check.
|
|
133
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
91
134
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this._pq.add(new HeapItem(priority, val));
|
|
95
|
-
return this;
|
|
135
|
+
has(value) {
|
|
136
|
+
return this.nodes.includes(value);
|
|
96
137
|
}
|
|
97
138
|
/**
|
|
98
|
-
*
|
|
99
|
-
* @
|
|
139
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
140
|
+
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
141
|
+
* @returns An array containing elements traversed in the specified order.
|
|
100
142
|
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
143
|
+
dfs(order) {
|
|
144
|
+
const result = [];
|
|
145
|
+
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
146
|
+
const dfsHelper = (index) => {
|
|
147
|
+
if (index < this.size) {
|
|
148
|
+
if (order === 'in') {
|
|
149
|
+
dfsHelper(2 * index + 1);
|
|
150
|
+
result.push(this.nodes[index]);
|
|
151
|
+
dfsHelper(2 * index + 2);
|
|
152
|
+
}
|
|
153
|
+
else if (order === 'pre') {
|
|
154
|
+
result.push(this.nodes[index]);
|
|
155
|
+
dfsHelper(2 * index + 1);
|
|
156
|
+
dfsHelper(2 * index + 2);
|
|
157
|
+
}
|
|
158
|
+
else if (order === 'post') {
|
|
159
|
+
dfsHelper(2 * index + 1);
|
|
160
|
+
dfsHelper(2 * index + 2);
|
|
161
|
+
result.push(this.nodes[index]);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
dfsHelper(0); // Traverse starting from the root node
|
|
166
|
+
return result;
|
|
108
167
|
}
|
|
109
168
|
/**
|
|
110
|
-
*
|
|
111
|
-
* @
|
|
112
|
-
* @returns a boolean value.
|
|
169
|
+
* Convert the heap to an array.
|
|
170
|
+
* @returns An array containing the elements of the heap.
|
|
113
171
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return (this.pq.getNodes().findIndex(item => {
|
|
120
|
-
return item.val === node;
|
|
121
|
-
}) !== -1);
|
|
122
|
-
}
|
|
172
|
+
toArray() {
|
|
173
|
+
return [...this.nodes];
|
|
174
|
+
}
|
|
175
|
+
getNodes() {
|
|
176
|
+
return this.nodes;
|
|
123
177
|
}
|
|
124
178
|
/**
|
|
125
|
-
*
|
|
126
|
-
* @returns
|
|
179
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
180
|
+
* @returns A new Heap instance containing the same elements.
|
|
127
181
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return
|
|
182
|
+
clone() {
|
|
183
|
+
const clonedHeap = new Heap(this.comparator);
|
|
184
|
+
clonedHeap.nodes = [...this.nodes];
|
|
185
|
+
return clonedHeap;
|
|
132
186
|
}
|
|
133
187
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
|
|
137
|
-
* be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
|
|
138
|
-
* sorted result will be an array of `HeapItem
|
|
139
|
-
* @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
|
|
188
|
+
* Sort the elements in the heap and return them as an array.
|
|
189
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
140
190
|
*/
|
|
141
|
-
sort(
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
191
|
+
sort() {
|
|
192
|
+
const visitedNode = [];
|
|
193
|
+
const cloned = this.clone();
|
|
194
|
+
while (cloned.size !== 0) {
|
|
195
|
+
const top = cloned.poll();
|
|
196
|
+
if (top)
|
|
197
|
+
visitedNode.push(top);
|
|
198
|
+
}
|
|
199
|
+
return visitedNode;
|
|
145
200
|
}
|
|
146
201
|
/**
|
|
147
|
-
*
|
|
202
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
203
|
+
* @param nodes
|
|
204
|
+
* @param comparator - Comparison function.
|
|
205
|
+
* @returns A new Heap instance.
|
|
148
206
|
*/
|
|
149
|
-
|
|
150
|
-
|
|
207
|
+
static heapify(nodes, comparator) {
|
|
208
|
+
const binaryHeap = new Heap(comparator);
|
|
209
|
+
binaryHeap.nodes = [...nodes];
|
|
210
|
+
binaryHeap.fix(); // Fix heap properties
|
|
211
|
+
return binaryHeap;
|
|
151
212
|
}
|
|
152
213
|
}
|
|
@@ -1,23 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Kirk Qi
|
|
5
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @class MaxHeap
|
|
13
|
-
* @extends Heap
|
|
14
|
-
*/
|
|
15
|
-
export declare class MaxHeap<V = any> extends Heap<V> {
|
|
16
|
-
protected _pq: PriorityQueue<HeapItem<V>>;
|
|
17
|
-
/**
|
|
18
|
-
* The constructor initializes a PriorityQueue with a custom comparator function.
|
|
19
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
20
|
-
* type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
|
|
21
|
-
*/
|
|
22
|
-
constructor(options?: HeapOptions<V>);
|
|
8
|
+
import { Heap } from './heap';
|
|
9
|
+
import type { CompareFunction } from '../../types';
|
|
10
|
+
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
+
constructor(comparator?: CompareFunction<E>);
|
|
23
12
|
}
|
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Kirk Qi
|
|
5
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import { PriorityQueue } from '../priority-queue';
|
|
10
|
-
/**
|
|
11
|
-
* @class MaxHeap
|
|
12
|
-
* @extends Heap
|
|
13
|
-
*/
|
|
14
9
|
export class MaxHeap extends Heap {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
10
|
+
constructor(comparator = (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 b - a;
|
|
16
|
+
}
|
|
17
|
+
}) {
|
|
18
|
+
super(comparator);
|
|
25
19
|
}
|
|
26
20
|
}
|
|
@@ -1,24 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Kirk Qi
|
|
5
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { Heap
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @class MinHeap
|
|
13
|
-
* @extends Heap
|
|
14
|
-
*/
|
|
15
|
-
export declare class MinHeap<V = any> extends Heap<V> {
|
|
16
|
-
protected _pq: PriorityQueue<HeapItem<V>>;
|
|
17
|
-
/**
|
|
18
|
-
* The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
|
|
19
|
-
* objects.
|
|
20
|
-
* @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
|
|
21
|
-
* type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
|
|
22
|
-
*/
|
|
23
|
-
constructor(options?: HeapOptions<V>);
|
|
8
|
+
import { Heap } from './heap';
|
|
9
|
+
import type { CompareFunction } from '../../types';
|
|
10
|
+
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
+
constructor(comparator?: CompareFunction<E>);
|
|
24
12
|
}
|