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