data-structure-typed 1.19.2 → 1.19.4
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/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -1,76 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
14
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
15
|
-
if (!m) return o;
|
|
16
|
-
var i = m.call(o), r, ar = [], e;
|
|
17
|
-
try {
|
|
18
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
19
|
-
}
|
|
20
|
-
catch (error) { e = { error: error }; }
|
|
21
|
-
finally {
|
|
22
|
-
try {
|
|
23
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
24
|
-
}
|
|
25
|
-
finally { if (e) throw e.error; }
|
|
26
|
-
}
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
-
if (ar || !(i in from)) {
|
|
32
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
-
ar[i] = from[i];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.PriorityQueue = void 0;
|
|
40
|
-
|
|
4
|
+
class PriorityQueue {
|
|
41
5
|
/**
|
|
42
6
|
* The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
|
|
43
7
|
* function.
|
|
44
8
|
* @param options - The `options` parameter is an object that contains the following properties:
|
|
45
9
|
*/
|
|
46
|
-
|
|
10
|
+
constructor(options) {
|
|
47
11
|
this._nodes = [];
|
|
48
|
-
this._comparator =
|
|
49
|
-
|
|
12
|
+
this._comparator = (a, b) => {
|
|
13
|
+
const aKey = a, bKey = b;
|
|
50
14
|
return aKey - bKey;
|
|
51
15
|
};
|
|
52
|
-
|
|
16
|
+
const { nodes, comparator, isFix = true } = options;
|
|
53
17
|
this._comparator = comparator;
|
|
54
18
|
if (nodes && Array.isArray(nodes) && nodes.length > 0) {
|
|
55
19
|
// TODO support distinct
|
|
56
|
-
this._nodes =
|
|
20
|
+
this._nodes = [...nodes];
|
|
57
21
|
isFix && this._fix();
|
|
58
22
|
}
|
|
59
23
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(PriorityQueue.prototype, "size", {
|
|
68
|
-
get: function () {
|
|
69
|
-
return this.nodes.length;
|
|
70
|
-
},
|
|
71
|
-
enumerable: false,
|
|
72
|
-
configurable: true
|
|
73
|
-
});
|
|
24
|
+
get nodes() {
|
|
25
|
+
return this._nodes;
|
|
26
|
+
}
|
|
27
|
+
get size() {
|
|
28
|
+
return this.nodes.length;
|
|
29
|
+
}
|
|
74
30
|
/**
|
|
75
31
|
* The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
|
|
76
32
|
* @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
|
|
@@ -78,11 +34,11 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
78
34
|
* the priority queue, and "initialValues" which is an array of initial values to be added to the priority
|
|
79
35
|
* @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
|
|
80
36
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
37
|
+
static heapify(options) {
|
|
38
|
+
const heap = new PriorityQueue(options);
|
|
83
39
|
heap._fix();
|
|
84
40
|
return heap;
|
|
85
|
-
}
|
|
41
|
+
}
|
|
86
42
|
/**
|
|
87
43
|
* The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
|
|
88
44
|
* the isValid method.
|
|
@@ -90,48 +46,48 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
90
46
|
* following properties:
|
|
91
47
|
* @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
|
|
92
48
|
*/
|
|
93
|
-
|
|
94
|
-
return new PriorityQueue(
|
|
95
|
-
}
|
|
49
|
+
static isPriorityQueueified(options) {
|
|
50
|
+
return new PriorityQueue(Object.assign(Object.assign({}, options), { isFix: false })).isValid();
|
|
51
|
+
}
|
|
96
52
|
/**
|
|
97
53
|
* Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
|
|
98
54
|
*/
|
|
99
|
-
|
|
55
|
+
getNodes() {
|
|
100
56
|
return this._nodes;
|
|
101
|
-
}
|
|
57
|
+
}
|
|
102
58
|
/**
|
|
103
59
|
* The "add" function adds a node to the heap and ensures that the heap property is maintained.
|
|
104
60
|
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
|
|
105
61
|
* that needs to be added to the heap.
|
|
106
62
|
*/
|
|
107
|
-
|
|
63
|
+
add(node) {
|
|
108
64
|
this.nodes.push(node);
|
|
109
65
|
this._heapifyUp(this.size - 1);
|
|
110
|
-
}
|
|
66
|
+
}
|
|
111
67
|
/**
|
|
112
68
|
* The "has" function checks if a given node is present in the list of nodes.
|
|
113
69
|
* @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
|
|
114
70
|
* we want to check if it exists in the `nodes` array.
|
|
115
71
|
* @returns a boolean value indicating whether the given node is included in the array of nodes.
|
|
116
72
|
*/
|
|
117
|
-
|
|
73
|
+
has(node) {
|
|
118
74
|
return this.nodes.includes(node);
|
|
119
|
-
}
|
|
75
|
+
}
|
|
120
76
|
/**
|
|
121
77
|
* The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
|
|
122
78
|
* @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
|
|
123
79
|
* Otherwise, it returns `null`.
|
|
124
80
|
*/
|
|
125
|
-
|
|
81
|
+
peek() {
|
|
126
82
|
return this.size ? this.nodes[0] : null;
|
|
127
|
-
}
|
|
83
|
+
}
|
|
128
84
|
/**
|
|
129
85
|
* The `poll` function removes and returns the top element from a heap data structure.
|
|
130
86
|
* @returns The `poll()` method returns a value of type `T` or `null`.
|
|
131
87
|
*/
|
|
132
|
-
|
|
88
|
+
poll() {
|
|
133
89
|
var _a, _b;
|
|
134
|
-
|
|
90
|
+
let res = null;
|
|
135
91
|
if (this.size > 1) {
|
|
136
92
|
this._swap(0, this.nodes.length - 1);
|
|
137
93
|
res = (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
@@ -141,56 +97,56 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
141
97
|
res = (_b = this.nodes.pop()) !== null && _b !== void 0 ? _b : null;
|
|
142
98
|
}
|
|
143
99
|
return res;
|
|
144
|
-
}
|
|
100
|
+
}
|
|
145
101
|
/**
|
|
146
102
|
* The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
|
|
147
103
|
* @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
|
|
148
104
|
* empty or the last element is `null`, then it returns `null`.
|
|
149
105
|
*/
|
|
150
|
-
|
|
106
|
+
leaf() {
|
|
151
107
|
var _a;
|
|
152
108
|
return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
|
|
153
|
-
}
|
|
109
|
+
}
|
|
154
110
|
/**
|
|
155
111
|
* The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
|
|
156
112
|
* object is empty or not.
|
|
157
113
|
* @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
|
|
158
114
|
* 0.
|
|
159
115
|
*/
|
|
160
|
-
|
|
116
|
+
isEmpty() {
|
|
161
117
|
return this.size === 0;
|
|
162
|
-
}
|
|
118
|
+
}
|
|
163
119
|
/**
|
|
164
120
|
* The clear function clears the nodes array.
|
|
165
121
|
*/
|
|
166
|
-
|
|
122
|
+
clear() {
|
|
167
123
|
this._setNodes([]);
|
|
168
|
-
}
|
|
124
|
+
}
|
|
169
125
|
/**
|
|
170
126
|
* The toArray function returns an array containing all the elements in the nodes property.
|
|
171
127
|
* @returns An array of type T, which is the elements of the nodes property.
|
|
172
128
|
*/
|
|
173
|
-
|
|
174
|
-
return
|
|
175
|
-
}
|
|
129
|
+
toArray() {
|
|
130
|
+
return [...this.nodes];
|
|
131
|
+
}
|
|
176
132
|
/**
|
|
177
133
|
* The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
|
|
178
134
|
* original instance.
|
|
179
135
|
* @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
|
|
180
136
|
* `comparator` properties as the original instance.
|
|
181
137
|
*/
|
|
182
|
-
|
|
138
|
+
clone() {
|
|
183
139
|
return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
|
|
184
|
-
}
|
|
140
|
+
}
|
|
185
141
|
// --- start additional methods ---
|
|
186
142
|
/**
|
|
187
143
|
* The `isValid` function recursively checks if a binary tree satisfies a certain condition.
|
|
188
144
|
* @returns The function `isValid()` returns a boolean value.
|
|
189
145
|
*/
|
|
190
|
-
|
|
191
|
-
for (
|
|
192
|
-
|
|
193
|
-
|
|
146
|
+
isValid() {
|
|
147
|
+
for (let i = 0; i < this.nodes.length; i++) {
|
|
148
|
+
const leftChildIndex = this._getLeft(i);
|
|
149
|
+
const rightChildIndex = this._getRight(i);
|
|
194
150
|
if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
|
|
195
151
|
return false;
|
|
196
152
|
}
|
|
@@ -199,7 +155,7 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
199
155
|
}
|
|
200
156
|
}
|
|
201
157
|
return true;
|
|
202
|
-
}
|
|
158
|
+
}
|
|
203
159
|
/**
|
|
204
160
|
* Plan to support sorting of duplicate elements.
|
|
205
161
|
*/
|
|
@@ -208,16 +164,16 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
208
164
|
* Plan to support sorting of duplicate elements.
|
|
209
165
|
* @returns The `sort()` method is returning an array of type `T[]`.
|
|
210
166
|
*/
|
|
211
|
-
|
|
167
|
+
sort() {
|
|
212
168
|
// TODO Plan to support sorting of duplicate elements.
|
|
213
|
-
|
|
169
|
+
const visitedNode = [];
|
|
214
170
|
while (this.size !== 0) {
|
|
215
|
-
|
|
171
|
+
const top = this.poll();
|
|
216
172
|
if (top)
|
|
217
173
|
visitedNode.push(top);
|
|
218
174
|
}
|
|
219
175
|
return visitedNode;
|
|
220
|
-
}
|
|
176
|
+
}
|
|
221
177
|
/**
|
|
222
178
|
* The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
|
|
223
179
|
* based on the specified traversal order.
|
|
@@ -225,37 +181,36 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
225
181
|
* the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
|
|
226
182
|
* @returns an array of type `(T | null)[]`.
|
|
227
183
|
*/
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
var traverse = function (cur) {
|
|
184
|
+
DFS(dfsMode) {
|
|
185
|
+
const visitedNode = [];
|
|
186
|
+
const traverse = (cur) => {
|
|
232
187
|
var _a, _b, _c;
|
|
233
|
-
|
|
234
|
-
|
|
188
|
+
const leftChildIndex = this._getLeft(cur);
|
|
189
|
+
const rightChildIndex = this._getRight(cur);
|
|
235
190
|
switch (dfsMode) {
|
|
236
191
|
case 'in':
|
|
237
|
-
|
|
238
|
-
visitedNode.push((_a =
|
|
239
|
-
|
|
192
|
+
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
193
|
+
visitedNode.push((_a = this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
|
|
194
|
+
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
240
195
|
break;
|
|
241
196
|
case 'pre':
|
|
242
|
-
visitedNode.push((_b =
|
|
243
|
-
|
|
244
|
-
|
|
197
|
+
visitedNode.push((_b = this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
|
|
198
|
+
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
199
|
+
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
245
200
|
break;
|
|
246
201
|
case 'post':
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
visitedNode.push((_c =
|
|
202
|
+
this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
|
|
203
|
+
this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
|
|
204
|
+
visitedNode.push((_c = this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
|
|
250
205
|
break;
|
|
251
206
|
}
|
|
252
207
|
};
|
|
253
208
|
this._isValidIndex(0) && traverse(0);
|
|
254
209
|
return visitedNode;
|
|
255
|
-
}
|
|
256
|
-
|
|
210
|
+
}
|
|
211
|
+
_setNodes(value) {
|
|
257
212
|
this._nodes = value;
|
|
258
|
-
}
|
|
213
|
+
}
|
|
259
214
|
/**
|
|
260
215
|
* The function compares two numbers using a custom comparator function.
|
|
261
216
|
* @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
|
|
@@ -264,61 +219,61 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
264
219
|
* comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
|
|
265
220
|
* indicating that the element at index `a` is greater than the element at index `b`.
|
|
266
221
|
*/
|
|
267
|
-
|
|
222
|
+
_compare(a, b) {
|
|
268
223
|
return this._comparator(this.nodes[a], this.nodes[b]) > 0;
|
|
269
|
-
}
|
|
224
|
+
}
|
|
270
225
|
/**
|
|
271
226
|
* The function swaps two elements in an array.
|
|
272
227
|
* @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
|
|
273
228
|
* @param {number} b - The parameter "b" is a number.
|
|
274
229
|
*/
|
|
275
|
-
|
|
276
|
-
|
|
230
|
+
_swap(a, b) {
|
|
231
|
+
const temp = this.nodes[a];
|
|
277
232
|
this.nodes[a] = this.nodes[b];
|
|
278
233
|
this.nodes[b] = temp;
|
|
279
|
-
}
|
|
234
|
+
}
|
|
280
235
|
/**
|
|
281
236
|
* The function checks if a given index is valid within an array.
|
|
282
237
|
* @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
|
|
283
238
|
* checked for validity.
|
|
284
239
|
* @returns A boolean value indicating whether the given index is valid or not.
|
|
285
240
|
*/
|
|
286
|
-
|
|
241
|
+
_isValidIndex(index) {
|
|
287
242
|
return index > -1 && index < this.nodes.length;
|
|
288
|
-
}
|
|
243
|
+
}
|
|
289
244
|
/**
|
|
290
245
|
* The function returns the index of the parent node given the index of a child node in a binary tree.
|
|
291
246
|
* @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
|
|
292
247
|
* @returns the parent of the given child node.
|
|
293
248
|
*/
|
|
294
|
-
|
|
249
|
+
_getParent(child) {
|
|
295
250
|
return Math.floor((child - 1) / 2);
|
|
296
|
-
}
|
|
251
|
+
}
|
|
297
252
|
/**
|
|
298
253
|
* The function returns the index of the left child node in a binary tree given the index of its parent node.
|
|
299
254
|
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
300
255
|
* @returns the left child of a given parent node in a binary tree.
|
|
301
256
|
*/
|
|
302
|
-
|
|
257
|
+
_getLeft(parent) {
|
|
303
258
|
return (2 * parent) + 1;
|
|
304
|
-
}
|
|
259
|
+
}
|
|
305
260
|
/**
|
|
306
261
|
* The function returns the index of the right child node in a binary tree given the index of its parent node.
|
|
307
262
|
* @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
|
|
308
263
|
* @returns the right child of a given parent node in a binary tree.
|
|
309
264
|
*/
|
|
310
|
-
|
|
265
|
+
_getRight(parent) {
|
|
311
266
|
return (2 * parent) + 2;
|
|
312
|
-
}
|
|
267
|
+
}
|
|
313
268
|
/**
|
|
314
269
|
* The function returns the index of the smallest child node of a given parent node.
|
|
315
270
|
* @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
|
|
316
271
|
* tree.
|
|
317
272
|
* @returns the minimum value between the parent node and its left and right child nodes.
|
|
318
273
|
*/
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
274
|
+
_getComparedChild(parent) {
|
|
275
|
+
let min = parent;
|
|
276
|
+
const left = this._getLeft(parent), right = this._getRight(parent);
|
|
322
277
|
if (left < this.size && this._compare(min, left)) {
|
|
323
278
|
min = left;
|
|
324
279
|
}
|
|
@@ -326,40 +281,39 @@ var PriorityQueue = /** @class */ (function () {
|
|
|
326
281
|
min = right;
|
|
327
282
|
}
|
|
328
283
|
return min;
|
|
329
|
-
}
|
|
284
|
+
}
|
|
330
285
|
/**
|
|
331
286
|
* The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
|
|
332
287
|
* correct position.
|
|
333
288
|
* @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
|
|
334
289
|
*/
|
|
335
|
-
|
|
290
|
+
_heapifyUp(start) {
|
|
336
291
|
while (start > 0 && this._compare(this._getParent(start), start)) {
|
|
337
|
-
|
|
292
|
+
const parent = this._getParent(start);
|
|
338
293
|
this._swap(start, parent);
|
|
339
294
|
start = parent;
|
|
340
295
|
}
|
|
341
|
-
}
|
|
296
|
+
}
|
|
342
297
|
/**
|
|
343
298
|
* The function performs a heapify operation by comparing and swapping elements in a binary heap.
|
|
344
299
|
* @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
|
|
345
300
|
* operation should start.
|
|
346
301
|
*/
|
|
347
|
-
|
|
348
|
-
|
|
302
|
+
_heapifyDown(start) {
|
|
303
|
+
let min = this._getComparedChild(start);
|
|
349
304
|
while (this._compare(start, min)) {
|
|
350
305
|
this._swap(min, start);
|
|
351
306
|
start = min;
|
|
352
307
|
min = this._getComparedChild(start);
|
|
353
308
|
}
|
|
354
|
-
}
|
|
309
|
+
}
|
|
355
310
|
/**
|
|
356
311
|
* The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
|
|
357
312
|
* towards the root.
|
|
358
313
|
*/
|
|
359
|
-
|
|
360
|
-
for (
|
|
314
|
+
_fix() {
|
|
315
|
+
for (let i = Math.floor(this.size / 2); i > -1; i--)
|
|
361
316
|
this._heapifyDown(i);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
}());
|
|
317
|
+
}
|
|
318
|
+
}
|
|
365
319
|
exports.PriorityQueue = PriorityQueue;
|