data-structure-typed 1.35.0 → 1.36.0
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/workflows/ci.yml +3 -0
- package/CHANGELOG.md +8 -1
- package/CONTRIBUTING.md +18 -0
- package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
- 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 +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +21 -0
- 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/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 +100 -0
- package/dist/data-structures/heap/heap.js +215 -76
- 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 +34 -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 +4 -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/graph/abstract-graph.js +3 -5
- package/lib/data-structures/heap/heap.d.ts +85 -68
- package/lib/data-structures/heap/heap.js +186 -108
- 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 +5 -8
- package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +5 -8
- 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/heap.d.ts +1 -3
- package/package.json +11 -7
- package/src/data-structures/graph/abstract-graph.ts +3 -5
- package/src/data-structures/heap/heap.ts +182 -140
- 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 +14 -47
- package/src/data-structures/priority-queue/min-priority-queue.ts +14 -48
- package/src/data-structures/priority-queue/priority-queue.ts +7 -350
- package/src/types/data-structures/heap.ts +1 -5
- package/test/integration/avl-tree.test.ts +4 -4
- package/test/integration/bst.test.ts +8 -8
- package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
- package/test/unit/data-structures/graph/overall.test.ts +2 -2
- 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/linked-list/singly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -9
- 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 +8 -0
- package/umd/bundle.min.js.map +1 -1
- package/test/unit/data-structures/graph/index.ts +0 -2
- package/test/unit/data-structures/linked-list/index.ts +0 -4
|
@@ -1,212 +1,254 @@
|
|
|
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 {PriorityQueue} from '../priority-queue';
|
|
9
|
-
import type {HeapOptions} from '../../types';
|
|
10
8
|
|
|
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
|
-
}
|
|
9
|
+
import type {CompareFunction} from '../../types';
|
|
23
10
|
|
|
24
|
-
|
|
11
|
+
export class Heap<T> {
|
|
12
|
+
private nodes: T[] = [];
|
|
13
|
+
private readonly comparator: CompareFunction<T>;
|
|
25
14
|
|
|
26
|
-
|
|
27
|
-
|
|
15
|
+
constructor(comparator: CompareFunction<T>) {
|
|
16
|
+
this.comparator = comparator;
|
|
28
17
|
}
|
|
29
18
|
|
|
30
|
-
|
|
31
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
21
|
+
* @param value - The element to be inserted.
|
|
22
|
+
*/
|
|
23
|
+
add(value: T): Heap<T> {
|
|
24
|
+
this.nodes.push(value);
|
|
25
|
+
this.bubbleUp(this.nodes.length - 1);
|
|
26
|
+
return this;
|
|
32
27
|
}
|
|
33
28
|
|
|
34
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
31
|
+
* @returns The top element or null if the heap is empty.
|
|
32
|
+
*/
|
|
33
|
+
poll(): T | null {
|
|
34
|
+
if (this.nodes.length === 0) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
if (this.nodes.length === 1) {
|
|
38
|
+
return this.nodes.pop() as T;
|
|
39
|
+
}
|
|
35
40
|
|
|
36
|
-
|
|
37
|
-
|
|
41
|
+
const topValue = this.nodes[0];
|
|
42
|
+
this.nodes[0] = this.nodes.pop() as T;
|
|
43
|
+
this.sinkDown(0);
|
|
44
|
+
return topValue;
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
set val(value: V | null) {
|
|
41
|
-
this._val = value;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export abstract class Heap<V = any> {
|
|
46
47
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @param [options] - An optional object that contains configuration options for the Heap.
|
|
48
|
+
* Float operation to maintain heap properties after adding an element.
|
|
49
|
+
* @param index - The index of the newly added element.
|
|
50
50
|
*/
|
|
51
|
-
protected
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
51
|
+
protected bubbleUp(index: number): void {
|
|
52
|
+
const element = this.nodes[index];
|
|
53
|
+
while (index > 0) {
|
|
54
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
55
|
+
const parent = this.nodes[parentIndex];
|
|
56
|
+
if (this.comparator(element, parent) < 0) {
|
|
57
|
+
this.nodes[index] = parent;
|
|
58
|
+
this.nodes[parentIndex] = element;
|
|
59
|
+
index = parentIndex;
|
|
60
|
+
} else {
|
|
61
|
+
break;
|
|
56
62
|
}
|
|
57
|
-
this._priorityExtractor = priorityExtractor || (el => +el);
|
|
58
|
-
} else {
|
|
59
|
-
this._priorityExtractor = el => +el;
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
68
|
+
* @param index - The index from which to start sinking.
|
|
69
|
+
*/
|
|
70
|
+
protected sinkDown(index: number): void {
|
|
71
|
+
const leftChildIndex = 2 * index + 1;
|
|
72
|
+
const rightChildIndex = 2 * index + 2;
|
|
73
|
+
const length = this.nodes.length;
|
|
74
|
+
let targetIndex = index;
|
|
75
|
+
|
|
76
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
77
|
+
targetIndex = leftChildIndex;
|
|
78
|
+
}
|
|
79
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
80
|
+
targetIndex = rightChildIndex;
|
|
81
|
+
}
|
|
64
82
|
|
|
65
|
-
|
|
66
|
-
|
|
83
|
+
if (targetIndex !== index) {
|
|
84
|
+
const temp = this.nodes[index];
|
|
85
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
86
|
+
this.nodes[targetIndex] = temp;
|
|
87
|
+
this.sinkDown(targetIndex);
|
|
88
|
+
}
|
|
67
89
|
}
|
|
68
90
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Fix the entire heap to maintain heap properties.
|
|
93
|
+
*/
|
|
94
|
+
protected fix() {
|
|
95
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
|
|
72
96
|
}
|
|
73
97
|
|
|
74
98
|
/**
|
|
75
|
-
*
|
|
76
|
-
* @returns The
|
|
99
|
+
* Peek at the top element of the heap without removing it.
|
|
100
|
+
* @returns The top element or null if the heap is empty.
|
|
77
101
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
102
|
+
peek(): T | null {
|
|
103
|
+
if (this.nodes.length === 0) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return this.nodes[0];
|
|
80
107
|
}
|
|
81
108
|
|
|
82
109
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
|
|
110
|
+
* Get the size (number of elements) of the heap.
|
|
85
111
|
*/
|
|
86
|
-
|
|
87
|
-
return this.
|
|
112
|
+
get size(): number {
|
|
113
|
+
return this.nodes.length;
|
|
88
114
|
}
|
|
89
115
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
118
|
+
* @returns The last element or null if the heap is empty.
|
|
119
|
+
*/
|
|
120
|
+
leaf(): T | null {
|
|
121
|
+
return this.nodes[this.size - 1] ?? null;
|
|
122
|
+
}
|
|
93
123
|
|
|
94
124
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @returns
|
|
125
|
+
* Check if the heap is empty.
|
|
126
|
+
* @returns True if the heap is empty, otherwise false.
|
|
97
127
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
128
|
+
isEmpty() {
|
|
129
|
+
return this.size === 0;
|
|
130
|
+
}
|
|
101
131
|
|
|
102
|
-
|
|
132
|
+
clear() {
|
|
133
|
+
this.nodes = [];
|
|
103
134
|
}
|
|
104
135
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
136
|
+
refill(nodes: T[]) {
|
|
137
|
+
this.nodes = nodes;
|
|
138
|
+
this.fix();
|
|
139
|
+
}
|
|
108
140
|
|
|
109
141
|
/**
|
|
110
|
-
*
|
|
111
|
-
* @
|
|
142
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
143
|
+
* @param value - the element to check.
|
|
144
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
112
145
|
*/
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const leafItem = this._pq.leaf();
|
|
116
|
-
|
|
117
|
-
return isItem ? leafItem : leafItem?.val;
|
|
146
|
+
has(value: T): boolean {
|
|
147
|
+
return this.nodes.includes(value);
|
|
118
148
|
}
|
|
119
149
|
|
|
120
150
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param
|
|
123
|
-
*
|
|
124
|
-
* @
|
|
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
|
|
151
|
+
* Use a comparison function to find the index of an element in the heap.
|
|
152
|
+
* @param value - the element to find.
|
|
153
|
+
* @param index - the index currently being searched.
|
|
154
|
+
* @returns The index of the element, or -1 if not found.
|
|
129
155
|
*/
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
156
|
+
private findIndex(value: T, index: number): number {
|
|
157
|
+
if (index >= this.size) {
|
|
158
|
+
return -1;
|
|
159
|
+
}
|
|
133
160
|
|
|
134
|
-
|
|
135
|
-
}
|
|
161
|
+
const compareResult = this.comparator(value, this.nodes[index]);
|
|
136
162
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
163
|
+
if (compareResult === 0) {
|
|
164
|
+
return index; // Element found
|
|
165
|
+
} else if (compareResult < 0) {
|
|
166
|
+
// The element should be in the left subtree
|
|
167
|
+
return this.findIndex(value, 2 * index + 1);
|
|
168
|
+
} else {
|
|
169
|
+
// The element should be in the right subtree
|
|
170
|
+
return this.findIndex(value, 2 * index + 2);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
140
173
|
|
|
141
174
|
/**
|
|
142
|
-
*
|
|
143
|
-
* @
|
|
175
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
176
|
+
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
177
|
+
* @returns An array containing elements traversed in the specified order.
|
|
144
178
|
*/
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
179
|
+
dfs(order: 'in' | 'pre' | 'post'): T[] {
|
|
180
|
+
const result: T[] = [];
|
|
181
|
+
|
|
182
|
+
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
183
|
+
const dfsHelper = (index: number) => {
|
|
184
|
+
if (index < this.size) {
|
|
185
|
+
if (order === 'in') {
|
|
186
|
+
dfsHelper(2 * index + 1);
|
|
187
|
+
result.push(this.nodes[index]);
|
|
188
|
+
dfsHelper(2 * index + 2);
|
|
189
|
+
} else if (order === 'pre') {
|
|
190
|
+
result.push(this.nodes[index]);
|
|
191
|
+
dfsHelper(2 * index + 1);
|
|
192
|
+
dfsHelper(2 * index + 2);
|
|
193
|
+
} else if (order === 'post') {
|
|
194
|
+
dfsHelper(2 * index + 1);
|
|
195
|
+
dfsHelper(2 * index + 2);
|
|
196
|
+
result.push(this.nodes[index]);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
dfsHelper(0); // Traverse starting from the root node
|
|
151
202
|
|
|
152
|
-
return
|
|
203
|
+
return result;
|
|
153
204
|
}
|
|
154
205
|
|
|
155
206
|
/**
|
|
156
|
-
*
|
|
157
|
-
* @
|
|
158
|
-
* @returns a boolean value.
|
|
207
|
+
* Convert the heap to an array.
|
|
208
|
+
* @returns An array containing the elements of the heap.
|
|
159
209
|
*/
|
|
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
|
-
}
|
|
210
|
+
toArray(): T[] {
|
|
211
|
+
return [...this.nodes];
|
|
170
212
|
}
|
|
171
213
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
214
|
+
getNodes(): T[] {
|
|
215
|
+
return this.nodes;
|
|
216
|
+
}
|
|
175
217
|
|
|
176
218
|
/**
|
|
177
|
-
*
|
|
178
|
-
* @returns
|
|
219
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
220
|
+
* @returns A new Heap instance containing the same elements.
|
|
179
221
|
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return isItem ? itemArray : itemArray.map(item => item.val);
|
|
222
|
+
clone(): Heap<T> {
|
|
223
|
+
const clonedHeap = new Heap<T>(this.comparator);
|
|
224
|
+
clonedHeap.nodes = [...this.nodes];
|
|
225
|
+
return clonedHeap;
|
|
185
226
|
}
|
|
186
227
|
|
|
187
|
-
sort(isItem?: undefined): (V | undefined)[];
|
|
188
|
-
sort(isItem: false): (V | undefined)[];
|
|
189
|
-
sort(isItem: true): (HeapItem<V> | null)[];
|
|
190
|
-
|
|
191
228
|
/**
|
|
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.
|
|
229
|
+
* Sort the elements in the heap and return them as an array.
|
|
230
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
198
231
|
*/
|
|
199
|
-
sort(
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
232
|
+
sort(): T[] {
|
|
233
|
+
const visitedNode: T[] = [];
|
|
234
|
+
const cloned = this.clone();
|
|
235
|
+
while (cloned.size !== 0) {
|
|
236
|
+
const top = cloned.poll();
|
|
237
|
+
if (top) visitedNode.push(top);
|
|
238
|
+
}
|
|
239
|
+
return visitedNode;
|
|
204
240
|
}
|
|
205
241
|
|
|
206
242
|
/**
|
|
207
|
-
*
|
|
243
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
244
|
+
* @param nodes
|
|
245
|
+
* @param comparator - Comparison function.
|
|
246
|
+
* @returns A new Heap instance.
|
|
208
247
|
*/
|
|
209
|
-
|
|
210
|
-
|
|
248
|
+
static heapify<T>(nodes: T[], comparator: CompareFunction<T>): Heap<T> {
|
|
249
|
+
const binaryHeap = new Heap<T>(comparator);
|
|
250
|
+
binaryHeap.nodes = [...nodes];
|
|
251
|
+
binaryHeap.fix(); // Fix heap properties
|
|
252
|
+
return binaryHeap;
|
|
211
253
|
}
|
|
212
254
|
}
|
|
@@ -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<T = any> extends Heap<T> {
|
|
13
|
+
constructor(
|
|
14
|
+
comparator: CompareFunction<T> = (a: T, b: T) => {
|
|
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<T = any> extends Heap<T> {
|
|
13
|
+
constructor(
|
|
14
|
+
comparator: CompareFunction<T> = (a: T, b: T) => {
|
|
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
|
-
export class MaxPriorityQueue<
|
|
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;
|
|
11
|
+
export class MaxPriorityQueue<T = any> extends PriorityQueue<T> {
|
|
12
|
+
constructor(
|
|
13
|
+
compare: CompareFunction<T> = (a: T, b: T) => {
|
|
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
|
}
|
|
@@ -1,57 +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
|
-
export class MinPriorityQueue<
|
|
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 aKey - bKey;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
static override heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MinPriorityQueue<E>;
|
|
34
|
-
static override heapify<E>(options: PriorityQueueOptions<E>): MinPriorityQueue<E>;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
|
|
38
|
-
* provided, and then fixes the heap structure of the queue.
|
|
39
|
-
* @param options - The `options` parameter is an object that contains configuration options for creating a priority
|
|
40
|
-
* queue. It can have the following properties:
|
|
41
|
-
* @returns a MinPriorityQueue object.
|
|
42
|
-
*/
|
|
43
|
-
static override heapify<E>(options: PriorityQueueOptions<E>): MinPriorityQueue<E> {
|
|
44
|
-
const minPQ = new MinPriorityQueue<E>({
|
|
45
|
-
...options,
|
|
46
|
-
comparator: options?.comparator
|
|
47
|
-
? options.comparator
|
|
48
|
-
: (a: E, b: E) => {
|
|
49
|
-
const aKey = a as unknown as number,
|
|
50
|
-
bKey = b as unknown as number;
|
|
51
|
-
return aKey - bKey;
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
minPQ._fix();
|
|
55
|
-
return minPQ;
|
|
11
|
+
export class MinPriorityQueue<T = any> extends PriorityQueue<T> {
|
|
12
|
+
constructor(
|
|
13
|
+
compare: CompareFunction<T> = (a: T, b: T) => {
|
|
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 a - b;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
) {
|
|
21
|
+
super(compare);
|
|
56
22
|
}
|
|
57
23
|
}
|