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
|
@@ -16,9 +16,9 @@ import {CP, LoopType} from '../../types';
|
|
|
16
16
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
17
17
|
import {IBST, IBSTNode} from '../../interfaces';
|
|
18
18
|
|
|
19
|
-
export class BSTNode<V = any,
|
|
20
|
-
extends BinaryTreeNode<V,
|
|
21
|
-
implements IBSTNode<V,
|
|
19
|
+
export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>>
|
|
20
|
+
extends BinaryTreeNode<V, FAMILY>
|
|
21
|
+
implements IBSTNode<V, FAMILY>
|
|
22
22
|
{
|
|
23
23
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
24
24
|
super(key, val);
|
|
@@ -2,9 +2,9 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
|
|
|
2
2
|
import {IRBTree, IRBTreeNode} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
|
-
export class RBTreeNode<V = any,
|
|
6
|
-
extends BSTNode<V,
|
|
7
|
-
implements IRBTreeNode<V,
|
|
5
|
+
export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>>
|
|
6
|
+
extends BSTNode<V, FAMILY>
|
|
7
|
+
implements IRBTreeNode<V, FAMILY>
|
|
8
8
|
{
|
|
9
9
|
private _color: RBColor;
|
|
10
10
|
|
|
@@ -10,9 +10,9 @@ import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType}
|
|
|
10
10
|
import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
13
|
-
export class TreeMultisetNode<V = any,
|
|
14
|
-
extends AVLTreeNode<V,
|
|
15
|
-
implements ITreeMultisetNode<V,
|
|
13
|
+
export class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>>
|
|
14
|
+
extends AVLTreeNode<V, FAMILY>
|
|
15
|
+
implements ITreeMultisetNode<V, FAMILY>
|
|
16
16
|
{
|
|
17
17
|
/**
|
|
18
18
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
@@ -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';
|
|
@@ -622,9 +622,7 @@ export abstract class AbstractGraph<
|
|
|
622
622
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
623
623
|
}
|
|
624
624
|
|
|
625
|
-
const heap = new PriorityQueue<{key: number; val: V}>(
|
|
626
|
-
comparator: (a, b) => a.key - b.key
|
|
627
|
-
});
|
|
625
|
+
const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
|
|
628
626
|
heap.add({key: 0, val: srcVertex});
|
|
629
627
|
|
|
630
628
|
distMap.set(srcVertex, 0);
|
|
@@ -1,212 +1,236 @@
|
|
|
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 {PriorityQueue} from '../priority-queue';
|
|
9
|
-
import type {HeapOptions} from '../../types';
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* The constructor function initializes an instance of a class with a priority and a value.
|
|
14
|
-
* @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
|
|
15
|
-
* optional and has a default value of `NaN`.
|
|
16
|
-
* @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
|
|
17
|
-
* `V` or `null`.
|
|
18
|
-
*/
|
|
19
|
-
constructor(priority: number = Number.MAX_SAFE_INTEGER, val: V | null = null) {
|
|
20
|
-
this._val = val;
|
|
21
|
-
this._priority = priority;
|
|
22
|
-
}
|
|
8
|
+
import type {CompareFunction} from '../../types';
|
|
23
9
|
|
|
24
|
-
|
|
10
|
+
export class Heap<E> {
|
|
11
|
+
protected nodes: E[] = [];
|
|
12
|
+
private readonly comparator: CompareFunction<E>;
|
|
25
13
|
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
constructor(comparator: CompareFunction<E>) {
|
|
15
|
+
this.comparator = comparator;
|
|
28
16
|
}
|
|
29
17
|
|
|
30
|
-
|
|
31
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
20
|
+
* @param value - The element to be inserted.
|
|
21
|
+
*/
|
|
22
|
+
add(value: E): Heap<E> {
|
|
23
|
+
this.nodes.push(value);
|
|
24
|
+
this.bubbleUp(this.nodes.length - 1);
|
|
25
|
+
return this;
|
|
32
26
|
}
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
30
|
+
* @returns The top element or null if the heap is empty.
|
|
31
|
+
*/
|
|
32
|
+
poll(): E | null {
|
|
33
|
+
if (this.nodes.length === 0) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (this.nodes.length === 1) {
|
|
37
|
+
return this.nodes.pop() as E;
|
|
38
|
+
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
this.
|
|
40
|
+
const topValue = this.nodes[0];
|
|
41
|
+
this.nodes[0] = this.nodes.pop() as E;
|
|
42
|
+
this.sinkDown(0);
|
|
43
|
+
return topValue;
|
|
42
44
|
}
|
|
43
|
-
}
|
|
44
45
|
|
|
45
|
-
export abstract class Heap<V = any> {
|
|
46
46
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
47
|
+
* Float operation to maintain heap properties after adding an element.
|
|
48
|
+
* @param index - The index of the newly added element.
|
|
50
49
|
*/
|
|
51
|
-
protected
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
protected bubbleUp(index: number): void {
|
|
51
|
+
const element = this.nodes[index];
|
|
52
|
+
while (index > 0) {
|
|
53
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
54
|
+
const parent = this.nodes[parentIndex];
|
|
55
|
+
if (this.comparator(element, parent) < 0) {
|
|
56
|
+
this.nodes[index] = parent;
|
|
57
|
+
this.nodes[parentIndex] = element;
|
|
58
|
+
index = parentIndex;
|
|
59
|
+
} else {
|
|
60
|
+
break;
|
|
56
61
|
}
|
|
57
|
-
this._priorityExtractor = priorityExtractor || (el => +el);
|
|
58
|
-
} else {
|
|
59
|
-
this._priorityExtractor = el => +el;
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
67
|
+
* @param index - The index from which to start sinking.
|
|
68
|
+
*/
|
|
69
|
+
protected sinkDown(index: number): void {
|
|
70
|
+
const leftChildIndex = 2 * index + 1;
|
|
71
|
+
const rightChildIndex = 2 * index + 2;
|
|
72
|
+
const length = this.nodes.length;
|
|
73
|
+
let targetIndex = index;
|
|
74
|
+
|
|
75
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
76
|
+
targetIndex = leftChildIndex;
|
|
77
|
+
}
|
|
78
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
79
|
+
targetIndex = rightChildIndex;
|
|
80
|
+
}
|
|
64
81
|
|
|
65
|
-
|
|
66
|
-
|
|
82
|
+
if (targetIndex !== index) {
|
|
83
|
+
const temp = this.nodes[index];
|
|
84
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
85
|
+
this.nodes[targetIndex] = temp;
|
|
86
|
+
this.sinkDown(targetIndex);
|
|
87
|
+
}
|
|
67
88
|
}
|
|
68
89
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Fix the entire heap to maintain heap properties.
|
|
92
|
+
*/
|
|
93
|
+
protected fix() {
|
|
94
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
|
|
72
95
|
}
|
|
73
96
|
|
|
74
97
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @returns The
|
|
98
|
+
* Peek at the top element of the heap without removing it.
|
|
99
|
+
* @returns The top element or null if the heap is empty.
|
|
77
100
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
101
|
+
peek(): E | null {
|
|
102
|
+
if (this.nodes.length === 0) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return this.nodes[0];
|
|
80
106
|
}
|
|
81
107
|
|
|
82
108
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
109
|
+
* Get the size (number of elements) of the heap.
|
|
85
110
|
*/
|
|
86
|
-
|
|
87
|
-
return this.
|
|
111
|
+
get size(): number {
|
|
112
|
+
return this.nodes.length;
|
|
88
113
|
}
|
|
89
114
|
|
|
90
|
-
peek(isItem?: undefined): V | undefined;
|
|
91
|
-
peek(isItem: false): V | undefined;
|
|
92
|
-
peek(isItem: true): HeapItem<V> | null;
|
|
93
|
-
|
|
94
115
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @returns The
|
|
116
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
117
|
+
* @returns The last element or null if the heap is empty.
|
|
97
118
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const peeked = this._pq.peek();
|
|
101
|
-
|
|
102
|
-
return isItem ? peeked : peeked?.val;
|
|
119
|
+
get leaf(): E | null {
|
|
120
|
+
return this.nodes[this.size - 1] ?? null;
|
|
103
121
|
}
|
|
104
122
|
|
|
105
|
-
peekLast(isItem?: undefined): V | undefined;
|
|
106
|
-
peekLast(isItem: false): V | undefined;
|
|
107
|
-
peekLast(isItem: true): HeapItem<V> | null;
|
|
108
|
-
|
|
109
123
|
/**
|
|
110
|
-
*
|
|
111
|
-
* @returns
|
|
124
|
+
* Check if the heap is empty.
|
|
125
|
+
* @returns True if the heap is empty, otherwise false.
|
|
112
126
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const leafItem = this._pq.leaf();
|
|
116
|
-
|
|
117
|
-
return isItem ? leafItem : leafItem?.val;
|
|
127
|
+
isEmpty() {
|
|
128
|
+
return this.size === 0;
|
|
118
129
|
}
|
|
119
130
|
|
|
120
131
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
|
|
123
|
-
* type.
|
|
124
|
-
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
|
|
125
|
-
* val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
|
|
126
|
-
* the value of `val`. If the `val` parameter is not a number, then the
|
|
127
|
-
* @returns The `add` method returns the instance of the `Heap` class.
|
|
128
|
-
* @throws {Error} if priority is not a valid number
|
|
132
|
+
* Reset the nodes of the heap. Make the nodes empty.
|
|
129
133
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
134
|
+
clear() {
|
|
135
|
+
this.nodes = [];
|
|
136
|
+
}
|
|
133
137
|
|
|
134
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Clear and add nodes of the heap
|
|
140
|
+
* @param nodes
|
|
141
|
+
*/
|
|
142
|
+
refill(nodes: E[]) {
|
|
143
|
+
this.nodes = nodes;
|
|
144
|
+
this.fix();
|
|
135
145
|
}
|
|
136
146
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
149
|
+
* @param value - the element to check.
|
|
150
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
151
|
+
*/
|
|
152
|
+
has(value: E): boolean {
|
|
153
|
+
return this.nodes.includes(value);
|
|
154
|
+
}
|
|
140
155
|
|
|
141
156
|
/**
|
|
142
|
-
*
|
|
143
|
-
* @
|
|
157
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
158
|
+
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
159
|
+
* @returns An array containing elements traversed in the specified order.
|
|
144
160
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
dfs(order: 'in' | 'pre' | 'post'): E[] {
|
|
162
|
+
const result: E[] = [];
|
|
163
|
+
|
|
164
|
+
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
165
|
+
const dfsHelper = (index: number) => {
|
|
166
|
+
if (index < this.size) {
|
|
167
|
+
if (order === 'in') {
|
|
168
|
+
dfsHelper(2 * index + 1);
|
|
169
|
+
result.push(this.nodes[index]);
|
|
170
|
+
dfsHelper(2 * index + 2);
|
|
171
|
+
} else if (order === 'pre') {
|
|
172
|
+
result.push(this.nodes[index]);
|
|
173
|
+
dfsHelper(2 * index + 1);
|
|
174
|
+
dfsHelper(2 * index + 2);
|
|
175
|
+
} else if (order === 'post') {
|
|
176
|
+
dfsHelper(2 * index + 1);
|
|
177
|
+
dfsHelper(2 * index + 2);
|
|
178
|
+
result.push(this.nodes[index]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
dfsHelper(0); // Traverse starting from the root node
|
|
151
184
|
|
|
152
|
-
return
|
|
185
|
+
return result;
|
|
153
186
|
}
|
|
154
187
|
|
|
155
188
|
/**
|
|
156
|
-
*
|
|
157
|
-
* @
|
|
158
|
-
* @returns a boolean value.
|
|
189
|
+
* Convert the heap to an array.
|
|
190
|
+
* @returns An array containing the elements of the heap.
|
|
159
191
|
*/
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return this.pq.getNodes().includes(node);
|
|
163
|
-
} else {
|
|
164
|
-
return (
|
|
165
|
-
this.pq.getNodes().findIndex(item => {
|
|
166
|
-
return item.val === node;
|
|
167
|
-
}) !== -1
|
|
168
|
-
);
|
|
169
|
-
}
|
|
192
|
+
toArray(): E[] {
|
|
193
|
+
return [...this.nodes];
|
|
170
194
|
}
|
|
171
195
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
196
|
+
getNodes(): E[] {
|
|
197
|
+
return this.nodes;
|
|
198
|
+
}
|
|
175
199
|
|
|
176
200
|
/**
|
|
177
|
-
*
|
|
178
|
-
* @returns
|
|
201
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
202
|
+
* @returns A new Heap instance containing the same elements.
|
|
179
203
|
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
204
|
+
clone(): Heap<E> {
|
|
205
|
+
const clonedHeap = new Heap<E>(this.comparator);
|
|
206
|
+
clonedHeap.nodes = [...this.nodes];
|
|
207
|
+
return clonedHeap;
|
|
185
208
|
}
|
|
186
209
|
|
|
187
|
-
sort(isItem?: undefined): (V | undefined)[];
|
|
188
|
-
sort(isItem: false): (V | undefined)[];
|
|
189
|
-
sort(isItem: true): (HeapItem<V> | null)[];
|
|
190
|
-
|
|
191
210
|
/**
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
* @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
|
|
195
|
-
* be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
|
|
196
|
-
* sorted result will be an array of `HeapItem
|
|
197
|
-
* @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
|
|
211
|
+
* Sort the elements in the heap and return them as an array.
|
|
212
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
198
213
|
*/
|
|
199
|
-
sort(
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
214
|
+
sort(): E[] {
|
|
215
|
+
const visitedNode: E[] = [];
|
|
216
|
+
const cloned = this.clone();
|
|
217
|
+
while (cloned.size !== 0) {
|
|
218
|
+
const top = cloned.poll();
|
|
219
|
+
if (top) visitedNode.push(top);
|
|
220
|
+
}
|
|
221
|
+
return visitedNode;
|
|
204
222
|
}
|
|
205
223
|
|
|
206
224
|
/**
|
|
207
|
-
*
|
|
225
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
226
|
+
* @param nodes
|
|
227
|
+
* @param comparator - Comparison function.
|
|
228
|
+
* @returns A new Heap instance.
|
|
208
229
|
*/
|
|
209
|
-
|
|
210
|
-
|
|
230
|
+
static heapify<E>(nodes: E[], comparator: CompareFunction<E>): Heap<E> {
|
|
231
|
+
const binaryHeap = new Heap<E>(comparator);
|
|
232
|
+
binaryHeap.nodes = [...nodes];
|
|
233
|
+
binaryHeap.fix(); // Fix heap properties
|
|
234
|
+
return binaryHeap;
|
|
211
235
|
}
|
|
212
236
|
}
|
|
@@ -1,31 +1,24 @@
|
|
|
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
|
|
|
9
|
-
import {Heap
|
|
10
|
-
import {
|
|
11
|
-
import type {HeapOptions} from '../../types';
|
|
9
|
+
import {Heap} from './heap';
|
|
10
|
+
import type {CompareFunction} from '../../types';
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*/
|
|
25
|
-
constructor(options?: HeapOptions<V>) {
|
|
26
|
-
super(options);
|
|
27
|
-
this._pq = new PriorityQueue<HeapItem<V>>({
|
|
28
|
-
comparator: (a, b) => b.priority - a.priority
|
|
29
|
-
});
|
|
12
|
+
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
|
+
constructor(
|
|
14
|
+
comparator: CompareFunction<E> = (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return b - a;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
) {
|
|
22
|
+
super(comparator);
|
|
30
23
|
}
|
|
31
24
|
}
|
|
@@ -1,32 +1,24 @@
|
|
|
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
|
|
|
9
|
-
import {Heap
|
|
10
|
-
import {
|
|
11
|
-
import type {HeapOptions} from '../../types';
|
|
9
|
+
import {Heap} from './heap';
|
|
10
|
+
import type {CompareFunction} from '../../types';
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
|
|
25
|
-
*/
|
|
26
|
-
constructor(options?: HeapOptions<V>) {
|
|
27
|
-
super(options);
|
|
28
|
-
this._pq = new PriorityQueue<HeapItem<V>>({
|
|
29
|
-
comparator: (a, b) => a.priority - b.priority
|
|
30
|
-
});
|
|
12
|
+
export class MinHeap<E = any> extends Heap<E> {
|
|
13
|
+
constructor(
|
|
14
|
+
comparator: CompareFunction<E> = (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
) {
|
|
22
|
+
super(comparator);
|
|
31
23
|
}
|
|
32
24
|
}
|
|
@@ -1,56 +1,23 @@
|
|
|
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 {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type {CompareFunction} from '../../types';
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
super(
|
|
22
|
-
...options,
|
|
23
|
-
comparator: options?.comparator
|
|
24
|
-
? options.comparator
|
|
25
|
-
: (a: E, b: E) => {
|
|
26
|
-
const aKey = a as unknown as number,
|
|
27
|
-
bKey = b as unknown as number;
|
|
28
|
-
return bKey - aKey;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static override heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MaxPriorityQueue<E>;
|
|
34
|
-
static override heapify<E>(options: PriorityQueueOptions<E>): MaxPriorityQueue<E>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The function `heapify` creates a max priority queue from the given options and returns it.
|
|
38
|
-
* @param options - The `options` parameter is an object that contains configuration options for creating a priority
|
|
39
|
-
* queue. It can have the following properties:
|
|
40
|
-
* @returns a MaxPriorityQueue object.
|
|
41
|
-
*/
|
|
42
|
-
static override heapify<E>(options: PriorityQueueOptions<E>): MaxPriorityQueue<E> {
|
|
43
|
-
const maxPQ = new MaxPriorityQueue<E>({
|
|
44
|
-
...options,
|
|
45
|
-
comparator: options?.comparator
|
|
46
|
-
? options.comparator
|
|
47
|
-
: (a: E, b: E) => {
|
|
48
|
-
const aKey = a as unknown as number,
|
|
49
|
-
bKey = b as unknown as number;
|
|
50
|
-
return bKey - aKey;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
maxPQ._fix();
|
|
54
|
-
return maxPQ;
|
|
12
|
+
constructor(
|
|
13
|
+
compare: CompareFunction<E> = (a: E, b: E) => {
|
|
14
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
|
+
throw new Error('The a, b params of compare function must be number');
|
|
16
|
+
} else {
|
|
17
|
+
return b - a;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
) {
|
|
21
|
+
super(compare);
|
|
55
22
|
}
|
|
56
23
|
}
|