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,95 +1,234 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Kirk Qi
|
|
6
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Heap =
|
|
4
|
-
class HeapItem {
|
|
5
|
-
constructor(priority = Number.MAX_SAFE_INTEGER, val = null) {
|
|
6
|
-
this._val = val;
|
|
7
|
-
this._priority = priority;
|
|
8
|
-
}
|
|
9
|
-
get priority() {
|
|
10
|
-
return this._priority;
|
|
11
|
-
}
|
|
12
|
-
set priority(value) {
|
|
13
|
-
this._priority = value;
|
|
14
|
-
}
|
|
15
|
-
get val() {
|
|
16
|
-
return this._val;
|
|
17
|
-
}
|
|
18
|
-
set val(value) {
|
|
19
|
-
this._val = value;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.HeapItem = HeapItem;
|
|
10
|
+
exports.Heap = void 0;
|
|
23
11
|
class Heap {
|
|
24
|
-
constructor(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
12
|
+
constructor(comparator) {
|
|
13
|
+
this.nodes = [];
|
|
14
|
+
this.comparator = comparator;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Insert an element into the heap and maintain the heap properties.
|
|
18
|
+
* @param value - The element to be inserted.
|
|
19
|
+
*/
|
|
20
|
+
add(value) {
|
|
21
|
+
this.nodes.push(value);
|
|
22
|
+
this.bubbleUp(this.nodes.length - 1);
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Remove and return the top element (smallest or largest element) from the heap.
|
|
27
|
+
* @returns The top element or null if the heap is empty.
|
|
28
|
+
*/
|
|
29
|
+
poll() {
|
|
30
|
+
if (this.nodes.length === 0) {
|
|
31
|
+
return null;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
-
this.
|
|
33
|
+
if (this.nodes.length === 1) {
|
|
34
|
+
return this.nodes.pop();
|
|
35
|
+
}
|
|
36
|
+
const topValue = this.nodes[0];
|
|
37
|
+
this.nodes[0] = this.nodes.pop();
|
|
38
|
+
this.sinkDown(0);
|
|
39
|
+
return topValue;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Float operation to maintain heap properties after adding an element.
|
|
43
|
+
* @param index - The index of the newly added element.
|
|
44
|
+
*/
|
|
45
|
+
bubbleUp(index) {
|
|
46
|
+
const element = this.nodes[index];
|
|
47
|
+
while (index > 0) {
|
|
48
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
49
|
+
const parent = this.nodes[parentIndex];
|
|
50
|
+
if (this.comparator(element, parent) < 0) {
|
|
51
|
+
this.nodes[index] = parent;
|
|
52
|
+
this.nodes[parentIndex] = element;
|
|
53
|
+
index = parentIndex;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
34
58
|
}
|
|
35
59
|
}
|
|
36
|
-
|
|
37
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Sinking operation to maintain heap properties after removing the top element.
|
|
62
|
+
* @param index - The index from which to start sinking.
|
|
63
|
+
*/
|
|
64
|
+
sinkDown(index) {
|
|
65
|
+
const leftChildIndex = 2 * index + 1;
|
|
66
|
+
const rightChildIndex = 2 * index + 2;
|
|
67
|
+
const length = this.nodes.length;
|
|
68
|
+
let targetIndex = index;
|
|
69
|
+
if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
|
|
70
|
+
targetIndex = leftChildIndex;
|
|
71
|
+
}
|
|
72
|
+
if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
|
|
73
|
+
targetIndex = rightChildIndex;
|
|
74
|
+
}
|
|
75
|
+
if (targetIndex !== index) {
|
|
76
|
+
const temp = this.nodes[index];
|
|
77
|
+
this.nodes[index] = this.nodes[targetIndex];
|
|
78
|
+
this.nodes[targetIndex] = temp;
|
|
79
|
+
this.sinkDown(targetIndex);
|
|
80
|
+
}
|
|
38
81
|
}
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Fix the entire heap to maintain heap properties.
|
|
84
|
+
*/
|
|
85
|
+
fix() {
|
|
86
|
+
for (let i = Math.floor(this.size / 2); i >= 0; i--)
|
|
87
|
+
this.sinkDown(i);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Peek at the top element of the heap without removing it.
|
|
91
|
+
* @returns The top element or null if the heap is empty.
|
|
92
|
+
*/
|
|
93
|
+
peek() {
|
|
94
|
+
if (this.nodes.length === 0) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return this.nodes[0];
|
|
41
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Get the size (number of elements) of the heap.
|
|
101
|
+
*/
|
|
42
102
|
get size() {
|
|
43
|
-
return this.
|
|
44
|
-
}
|
|
103
|
+
return this.nodes.length;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
107
|
+
* @returns The last element or null if the heap is empty.
|
|
108
|
+
*/
|
|
109
|
+
leaf() {
|
|
110
|
+
var _a;
|
|
111
|
+
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check if the heap is empty.
|
|
115
|
+
* @returns True if the heap is empty, otherwise false.
|
|
116
|
+
*/
|
|
45
117
|
isEmpty() {
|
|
46
|
-
return this.
|
|
47
|
-
}
|
|
48
|
-
peek(isItem) {
|
|
49
|
-
isItem = isItem !== null && isItem !== void 0 ? isItem : false;
|
|
50
|
-
const peeked = this._pq.peek();
|
|
51
|
-
return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
|
|
118
|
+
return this.size === 0;
|
|
52
119
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
120
|
+
clear() {
|
|
121
|
+
this.nodes = [];
|
|
122
|
+
}
|
|
123
|
+
refill(nodes) {
|
|
124
|
+
this.nodes = nodes;
|
|
125
|
+
this.fix();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Use a comparison function to check whether a binary heap contains a specific element.
|
|
129
|
+
* @param value - the element to check.
|
|
130
|
+
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
131
|
+
*/
|
|
132
|
+
has(value) {
|
|
133
|
+
return this.nodes.includes(value);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Use a comparison function to find the index of an element in the heap.
|
|
137
|
+
* @param value - the element to find.
|
|
138
|
+
* @param index - the index currently being searched.
|
|
139
|
+
* @returns The index of the element, or -1 if not found.
|
|
140
|
+
*/
|
|
141
|
+
findIndex(value, index) {
|
|
142
|
+
if (index >= this.size) {
|
|
143
|
+
return -1;
|
|
68
144
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
145
|
+
const compareResult = this.comparator(value, this.nodes[index]);
|
|
146
|
+
if (compareResult === 0) {
|
|
147
|
+
return index; // Element found
|
|
148
|
+
}
|
|
149
|
+
else if (compareResult < 0) {
|
|
150
|
+
// The element should be in the left subtree
|
|
151
|
+
return this.findIndex(value, 2 * index + 1);
|
|
74
152
|
}
|
|
75
153
|
else {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}) !== -1);
|
|
154
|
+
// The element should be in the right subtree
|
|
155
|
+
return this.findIndex(value, 2 * index + 2);
|
|
79
156
|
}
|
|
80
157
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
160
|
+
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
161
|
+
* @returns An array containing elements traversed in the specified order.
|
|
162
|
+
*/
|
|
163
|
+
dfs(order) {
|
|
164
|
+
const result = [];
|
|
165
|
+
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
166
|
+
const dfsHelper = (index) => {
|
|
167
|
+
if (index < this.size) {
|
|
168
|
+
if (order === 'in') {
|
|
169
|
+
dfsHelper(2 * index + 1);
|
|
170
|
+
result.push(this.nodes[index]);
|
|
171
|
+
dfsHelper(2 * index + 2);
|
|
172
|
+
}
|
|
173
|
+
else if (order === 'pre') {
|
|
174
|
+
result.push(this.nodes[index]);
|
|
175
|
+
dfsHelper(2 * index + 1);
|
|
176
|
+
dfsHelper(2 * index + 2);
|
|
177
|
+
}
|
|
178
|
+
else if (order === 'post') {
|
|
179
|
+
dfsHelper(2 * index + 1);
|
|
180
|
+
dfsHelper(2 * index + 2);
|
|
181
|
+
result.push(this.nodes[index]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
dfsHelper(0); // Traverse starting from the root node
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Convert the heap to an array.
|
|
190
|
+
* @returns An array containing the elements of the heap.
|
|
191
|
+
*/
|
|
192
|
+
toArray() {
|
|
193
|
+
return [...this.nodes];
|
|
194
|
+
}
|
|
195
|
+
getNodes() {
|
|
196
|
+
return this.nodes;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Clone the heap, creating a new heap with the same elements.
|
|
200
|
+
* @returns A new Heap instance containing the same elements.
|
|
201
|
+
*/
|
|
202
|
+
clone() {
|
|
203
|
+
const clonedHeap = new Heap(this.comparator);
|
|
204
|
+
clonedHeap.nodes = [...this.nodes];
|
|
205
|
+
return clonedHeap;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Sort the elements in the heap and return them as an array.
|
|
209
|
+
* @returns An array containing the elements sorted in ascending order.
|
|
210
|
+
*/
|
|
211
|
+
sort() {
|
|
212
|
+
const visitedNode = [];
|
|
213
|
+
const cloned = this.clone();
|
|
214
|
+
while (cloned.size !== 0) {
|
|
215
|
+
const top = cloned.poll();
|
|
216
|
+
if (top)
|
|
217
|
+
visitedNode.push(top);
|
|
218
|
+
}
|
|
219
|
+
return visitedNode;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
223
|
+
* @param nodes
|
|
224
|
+
* @param comparator - Comparison function.
|
|
225
|
+
* @returns A new Heap instance.
|
|
226
|
+
*/
|
|
227
|
+
static heapify(nodes, comparator) {
|
|
228
|
+
const binaryHeap = new Heap(comparator);
|
|
229
|
+
binaryHeap.nodes = [...nodes];
|
|
230
|
+
binaryHeap.fix(); // Fix heap properties
|
|
231
|
+
return binaryHeap;
|
|
93
232
|
}
|
|
94
233
|
}
|
|
95
234
|
exports.Heap = Heap;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/heap.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/heap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAIH,MAAa,IAAI;IAIf,YAAY,UAA8B;QAHlC,UAAK,GAAQ,EAAE,CAAC;QAItB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,KAAQ;QACV,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACrC,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;OAGG;IACH,IAAI;QACF,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;YAC3B,OAAO,IAAI,CAAC;SACb;QACD,IAAI,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,IAAI;QACF,MAAM,WAAW,GAAQ,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QAC5B,OAAO,MAAM,CAAC,IAAI,KAAK,CAAC,EAAE;YACxB,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;YAC1B,IAAI,GAAG;gBAAE,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAChC;QACD,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CAAI,KAAU,EAAE,UAA8B;QAC1D,MAAM,UAAU,GAAG,IAAI,IAAI,CAAI,UAAU,CAAC,CAAC;QAC3C,UAAU,CAAC,KAAK,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;QAC9B,UAAU,CAAC,GAAG,EAAE,CAAC,CAAC,sBAAsB;QACxC,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAnPD,oBAmPC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
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>);
|
|
12
|
+
}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Kirk Qi
|
|
6
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.MaxHeap = void 0;
|
|
4
11
|
const heap_1 = require("./heap");
|
|
5
|
-
const priority_queue_1 = require("../priority-queue");
|
|
6
12
|
class MaxHeap extends heap_1.Heap {
|
|
7
|
-
constructor(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
constructor(comparator = (a, b) => {
|
|
14
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
|
+
throw new Error('The a, b params of compare function must be number');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return b - a;
|
|
19
|
+
}
|
|
20
|
+
}) {
|
|
21
|
+
super(comparator);
|
|
12
22
|
}
|
|
13
23
|
}
|
|
14
24
|
exports.MaxHeap = MaxHeap;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"max-heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/max-heap.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"max-heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/max-heap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,iCAA4B;AAG5B,MAAa,OAAiB,SAAQ,WAAO;IAC3C,YACE,aAAiC,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;QAC9C,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;YACrD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;SACvE;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,CAAC;SACd;IACH,CAAC;QAED,KAAK,CAAC,UAAU,CAAC,CAAC;IACpB,CAAC;CACF;AAZD,0BAYC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
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>);
|
|
12
|
+
}
|
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Kirk Qi
|
|
6
|
+
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.MinHeap = void 0;
|
|
4
11
|
const heap_1 = require("./heap");
|
|
5
|
-
const priority_queue_1 = require("../priority-queue");
|
|
6
12
|
class MinHeap extends heap_1.Heap {
|
|
7
|
-
constructor(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
constructor(comparator = (a, b) => {
|
|
14
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
|
+
throw new Error('The a, b params of compare function must be number');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
}) {
|
|
21
|
+
super(comparator);
|
|
12
22
|
}
|
|
13
23
|
}
|
|
14
24
|
exports.MinHeap = MinHeap;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"min-heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/min-heap.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"min-heap.js","sourceRoot":"","sources":["../../../src/data-structures/heap/min-heap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH,iCAA4B;AAG5B,MAAa,OAAiB,SAAQ,WAAO;IAC3C,YACE,aAAiC,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;QAC9C,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,EAAE;YACrD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;SACvE;aAAM;YACL,OAAO,CAAC,GAAG,CAAC,CAAC;SACd;IACH,CAAC;QAED,KAAK,CAAC,UAAU,CAAC,CAAC;IACpB,CAAC;CACF;AAZD,0BAYC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './hash';
|
|
2
|
+
export * from './linked-list';
|
|
3
|
+
export * from './stack';
|
|
4
|
+
export * from './queue';
|
|
5
|
+
export * from './graph';
|
|
6
|
+
export * from './binary-tree';
|
|
7
|
+
export * from './tree';
|
|
8
|
+
export * from './heap';
|
|
9
|
+
export * from './priority-queue';
|
|
10
|
+
export * from './matrix';
|
|
11
|
+
export * from './trie';
|