min-heap-typed 1.39.6 → 1.40.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/dist/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -20
- package/dist/data-structures/binary-tree/binary-tree.js +8 -29
- package/dist/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/data-structures/binary-tree/rb-tree.js +1 -7
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +17 -42
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/rb-tree.ts +13 -21
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -33,21 +33,12 @@ class ObjectDeque {
|
|
|
33
33
|
get capacity() {
|
|
34
34
|
return this._capacity;
|
|
35
35
|
}
|
|
36
|
-
set capacity(value) {
|
|
37
|
-
this._capacity = value;
|
|
38
|
-
}
|
|
39
36
|
get first() {
|
|
40
37
|
return this._first;
|
|
41
38
|
}
|
|
42
|
-
set first(value) {
|
|
43
|
-
this._first = value;
|
|
44
|
-
}
|
|
45
39
|
get last() {
|
|
46
40
|
return this._last;
|
|
47
41
|
}
|
|
48
|
-
set last(value) {
|
|
49
|
-
this._last = value;
|
|
50
|
-
}
|
|
51
42
|
get size() {
|
|
52
43
|
return this._size;
|
|
53
44
|
}
|
|
@@ -57,15 +48,15 @@ class ObjectDeque {
|
|
|
57
48
|
* structure.
|
|
58
49
|
*/
|
|
59
50
|
addFirst(value) {
|
|
60
|
-
if (this.
|
|
61
|
-
const mid = Math.floor(this.
|
|
51
|
+
if (this.size === 0) {
|
|
52
|
+
const mid = Math.floor(this.capacity / 2);
|
|
62
53
|
this._first = mid;
|
|
63
54
|
this._last = mid;
|
|
64
55
|
}
|
|
65
56
|
else {
|
|
66
57
|
this._first--;
|
|
67
58
|
}
|
|
68
|
-
this.
|
|
59
|
+
this.nodes[this.first] = value;
|
|
69
60
|
this._size++;
|
|
70
61
|
}
|
|
71
62
|
/**
|
|
@@ -73,15 +64,15 @@ class ObjectDeque {
|
|
|
73
64
|
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
74
65
|
*/
|
|
75
66
|
addLast(value) {
|
|
76
|
-
if (this.
|
|
77
|
-
const mid = Math.floor(this.
|
|
67
|
+
if (this.size === 0) {
|
|
68
|
+
const mid = Math.floor(this.capacity / 2);
|
|
78
69
|
this._first = mid;
|
|
79
70
|
this._last = mid;
|
|
80
71
|
}
|
|
81
72
|
else {
|
|
82
73
|
this._last++;
|
|
83
74
|
}
|
|
84
|
-
this.
|
|
75
|
+
this.nodes[this.last] = value;
|
|
85
76
|
this._size++;
|
|
86
77
|
}
|
|
87
78
|
/**
|
|
@@ -89,10 +80,10 @@ class ObjectDeque {
|
|
|
89
80
|
* @returns The value of the first element in the data structure.
|
|
90
81
|
*/
|
|
91
82
|
popFirst() {
|
|
92
|
-
if (!this.
|
|
83
|
+
if (!this.size)
|
|
93
84
|
return;
|
|
94
85
|
const value = this.getFirst();
|
|
95
|
-
delete this.
|
|
86
|
+
delete this.nodes[this.first];
|
|
96
87
|
this._first++;
|
|
97
88
|
this._size--;
|
|
98
89
|
return value;
|
|
@@ -102,18 +93,18 @@ class ObjectDeque {
|
|
|
102
93
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
94
|
*/
|
|
104
95
|
getFirst() {
|
|
105
|
-
if (this.
|
|
106
|
-
return this.
|
|
96
|
+
if (this.size)
|
|
97
|
+
return this.nodes[this.first];
|
|
107
98
|
}
|
|
108
99
|
/**
|
|
109
100
|
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
101
|
* @returns The value that was removed from the data structure.
|
|
111
102
|
*/
|
|
112
103
|
popLast() {
|
|
113
|
-
if (!this.
|
|
104
|
+
if (!this.size)
|
|
114
105
|
return;
|
|
115
106
|
const value = this.getLast();
|
|
116
|
-
delete this.
|
|
107
|
+
delete this.nodes[this.last];
|
|
117
108
|
this._last--;
|
|
118
109
|
this._size--;
|
|
119
110
|
return value;
|
|
@@ -123,8 +114,8 @@ class ObjectDeque {
|
|
|
123
114
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
115
|
*/
|
|
125
116
|
getLast() {
|
|
126
|
-
if (this.
|
|
127
|
-
return this.
|
|
117
|
+
if (this.size)
|
|
118
|
+
return this.nodes[this.last];
|
|
128
119
|
}
|
|
129
120
|
/**
|
|
130
121
|
* The get function returns the element at the specified index in an array-like data structure.
|
|
@@ -134,20 +125,14 @@ class ObjectDeque {
|
|
|
134
125
|
* index, `null` is returned.
|
|
135
126
|
*/
|
|
136
127
|
get(index) {
|
|
137
|
-
return this.
|
|
128
|
+
return this.nodes[this.first + index] || null;
|
|
138
129
|
}
|
|
139
130
|
/**
|
|
140
131
|
* The function checks if the size of a data structure is less than or equal to zero.
|
|
141
132
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
142
133
|
*/
|
|
143
134
|
isEmpty() {
|
|
144
|
-
return this.
|
|
145
|
-
}
|
|
146
|
-
_seNodes(value) {
|
|
147
|
-
this._nodes = value;
|
|
148
|
-
}
|
|
149
|
-
_setSize(value) {
|
|
150
|
-
this._size = value;
|
|
135
|
+
return this.size <= 0;
|
|
151
136
|
}
|
|
152
137
|
}
|
|
153
138
|
exports.ObjectDeque = ObjectDeque;
|
|
@@ -157,8 +142,11 @@ class ArrayDeque {
|
|
|
157
142
|
constructor() {
|
|
158
143
|
this._nodes = [];
|
|
159
144
|
}
|
|
145
|
+
get nodes() {
|
|
146
|
+
return this._nodes;
|
|
147
|
+
}
|
|
160
148
|
get size() {
|
|
161
|
-
return this.
|
|
149
|
+
return this.nodes.length;
|
|
162
150
|
}
|
|
163
151
|
/**
|
|
164
152
|
* O(n) time complexity of adding at the beginning and the end
|
|
@@ -169,7 +157,7 @@ class ArrayDeque {
|
|
|
169
157
|
* @returns The return value is the new length of the array after the value has been added.
|
|
170
158
|
*/
|
|
171
159
|
addLast(value) {
|
|
172
|
-
return this.
|
|
160
|
+
return this.nodes.push(value);
|
|
173
161
|
}
|
|
174
162
|
/**
|
|
175
163
|
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
@@ -177,7 +165,7 @@ class ArrayDeque {
|
|
|
177
165
|
*/
|
|
178
166
|
popLast() {
|
|
179
167
|
var _a;
|
|
180
|
-
return (_a = this.
|
|
168
|
+
return (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
|
|
181
169
|
}
|
|
182
170
|
/**
|
|
183
171
|
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
@@ -186,7 +174,7 @@ class ArrayDeque {
|
|
|
186
174
|
*/
|
|
187
175
|
popFirst() {
|
|
188
176
|
var _a;
|
|
189
|
-
return (_a = this.
|
|
177
|
+
return (_a = this.nodes.shift()) !== null && _a !== void 0 ? _a : null;
|
|
190
178
|
}
|
|
191
179
|
/**
|
|
192
180
|
* O(n) time complexity of adding at the beginning and the end
|
|
@@ -198,7 +186,7 @@ class ArrayDeque {
|
|
|
198
186
|
* `value` at the beginning.
|
|
199
187
|
*/
|
|
200
188
|
addFirst(value) {
|
|
201
|
-
return this.
|
|
189
|
+
return this.nodes.unshift(value);
|
|
202
190
|
}
|
|
203
191
|
/**
|
|
204
192
|
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
@@ -207,7 +195,7 @@ class ArrayDeque {
|
|
|
207
195
|
*/
|
|
208
196
|
getFirst() {
|
|
209
197
|
var _a;
|
|
210
|
-
return (_a = this.
|
|
198
|
+
return (_a = this.nodes[0]) !== null && _a !== void 0 ? _a : null;
|
|
211
199
|
}
|
|
212
200
|
/**
|
|
213
201
|
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
@@ -215,7 +203,7 @@ class ArrayDeque {
|
|
|
215
203
|
*/
|
|
216
204
|
getLast() {
|
|
217
205
|
var _a;
|
|
218
|
-
return (_a = this.
|
|
206
|
+
return (_a = this.nodes[this.nodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
219
207
|
}
|
|
220
208
|
/**
|
|
221
209
|
* O(1) time complexity of obtaining the value
|
|
@@ -229,7 +217,7 @@ class ArrayDeque {
|
|
|
229
217
|
*/
|
|
230
218
|
get(index) {
|
|
231
219
|
var _a;
|
|
232
|
-
return (_a = this.
|
|
220
|
+
return (_a = this.nodes[index]) !== null && _a !== void 0 ? _a : null;
|
|
233
221
|
}
|
|
234
222
|
/**
|
|
235
223
|
* The set function assigns a value to a specific index in an array.
|
|
@@ -240,7 +228,7 @@ class ArrayDeque {
|
|
|
240
228
|
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
241
229
|
*/
|
|
242
230
|
set(index, value) {
|
|
243
|
-
return (this.
|
|
231
|
+
return (this.nodes[index] = value);
|
|
244
232
|
}
|
|
245
233
|
/**
|
|
246
234
|
* The insert function adds a value at a specified index in an array.
|
|
@@ -253,7 +241,7 @@ class ArrayDeque {
|
|
|
253
241
|
* are being removed, an empty array will be returned.
|
|
254
242
|
*/
|
|
255
243
|
insert(index, value) {
|
|
256
|
-
return this.
|
|
244
|
+
return this.nodes.splice(index, 0, value);
|
|
257
245
|
}
|
|
258
246
|
/**
|
|
259
247
|
* The delete function removes an element from an array at a specified index.
|
|
@@ -262,7 +250,7 @@ class ArrayDeque {
|
|
|
262
250
|
* @returns The method is returning an array containing the removed element.
|
|
263
251
|
*/
|
|
264
252
|
delete(index) {
|
|
265
|
-
return this.
|
|
253
|
+
return this.nodes.splice(index, 1);
|
|
266
254
|
}
|
|
267
255
|
/**
|
|
268
256
|
* The function checks if an array called "_nodes" is empty.
|
|
@@ -270,7 +258,7 @@ class ArrayDeque {
|
|
|
270
258
|
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
271
259
|
*/
|
|
272
260
|
isEmpty() {
|
|
273
|
-
return this.
|
|
261
|
+
return this.nodes.length === 0;
|
|
274
262
|
}
|
|
275
263
|
}
|
|
276
264
|
exports.ArrayDeque = ArrayDeque;
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
import { SinglyLinkedList } from '../linked-list';
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
8
8
|
/**
|
|
9
9
|
* The enqueue function adds a value to the end of an array.
|
|
10
10
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -15,6 +15,11 @@ export declare class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
15
15
|
* @returns The method is returning the element at the front of the queue, or null if the queue is empty.
|
|
16
16
|
*/
|
|
17
17
|
dequeue(): E | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
21
|
+
*/
|
|
22
|
+
getFirst(): E | undefined;
|
|
18
23
|
/**
|
|
19
24
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
20
25
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
@@ -29,12 +34,10 @@ export declare class Queue<E = any> {
|
|
|
29
34
|
* initialized as an empty array.
|
|
30
35
|
*/
|
|
31
36
|
constructor(elements?: E[]);
|
|
32
|
-
|
|
37
|
+
protected _nodes: E[];
|
|
33
38
|
get nodes(): E[];
|
|
34
|
-
|
|
35
|
-
private _offset;
|
|
39
|
+
protected _offset: number;
|
|
36
40
|
get offset(): number;
|
|
37
|
-
set offset(value: number);
|
|
38
41
|
/**
|
|
39
42
|
* The size function returns the number of elements in an array.
|
|
40
43
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -61,6 +64,12 @@ export declare class Queue<E = any> {
|
|
|
61
64
|
* @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
|
|
62
65
|
*/
|
|
63
66
|
shift(): E | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
69
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
70
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
71
|
+
*/
|
|
72
|
+
getFirst(): E | undefined;
|
|
64
73
|
/**
|
|
65
74
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
66
75
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
@@ -73,6 +82,12 @@ export declare class Queue<E = any> {
|
|
|
73
82
|
* array is empty, it returns `null`.
|
|
74
83
|
*/
|
|
75
84
|
getLast(): E | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
87
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
88
|
+
* array is empty, it returns `null`.
|
|
89
|
+
*/
|
|
90
|
+
peekLast(): E | undefined;
|
|
76
91
|
/**
|
|
77
92
|
* The enqueue function adds a value to the end of a queue.
|
|
78
93
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Queue = exports.
|
|
3
|
+
exports.Queue = exports.LinkedListQueue = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* @license MIT
|
|
6
6
|
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
9
|
const linked_list_1 = require("../linked-list");
|
|
10
|
-
class
|
|
10
|
+
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
11
11
|
/**
|
|
12
12
|
* The enqueue function adds a value to the end of an array.
|
|
13
13
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -22,16 +22,23 @@ class SkipQueue extends linked_list_1.SinglyLinkedList {
|
|
|
22
22
|
dequeue() {
|
|
23
23
|
return this.shift();
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The `getFirst` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
|
+
* @returns The `getFirst()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
|
+
*/
|
|
29
|
+
getFirst() {
|
|
30
|
+
var _a;
|
|
31
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
32
|
+
}
|
|
25
33
|
/**
|
|
26
34
|
* The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
|
|
27
35
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
36
|
*/
|
|
29
37
|
peek() {
|
|
30
|
-
|
|
31
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
38
|
+
return this.getFirst();
|
|
32
39
|
}
|
|
33
40
|
}
|
|
34
|
-
exports.
|
|
41
|
+
exports.LinkedListQueue = LinkedListQueue;
|
|
35
42
|
class Queue {
|
|
36
43
|
/**
|
|
37
44
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
@@ -46,15 +53,9 @@ class Queue {
|
|
|
46
53
|
get nodes() {
|
|
47
54
|
return this._nodes;
|
|
48
55
|
}
|
|
49
|
-
set nodes(value) {
|
|
50
|
-
this._nodes = value;
|
|
51
|
-
}
|
|
52
56
|
get offset() {
|
|
53
57
|
return this._offset;
|
|
54
58
|
}
|
|
55
|
-
set offset(value) {
|
|
56
|
-
this._offset = value;
|
|
57
|
-
}
|
|
58
59
|
/**
|
|
59
60
|
* The size function returns the number of elements in an array.
|
|
60
61
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -90,23 +91,31 @@ class Queue {
|
|
|
90
91
|
shift() {
|
|
91
92
|
if (this.size === 0)
|
|
92
93
|
return undefined;
|
|
93
|
-
const first = this.
|
|
94
|
-
this.
|
|
94
|
+
const first = this.getFirst();
|
|
95
|
+
this._offset += 1;
|
|
95
96
|
if (this.offset * 2 < this.nodes.length)
|
|
96
97
|
return first;
|
|
97
98
|
// only delete dequeued elements when reaching half size
|
|
98
99
|
// to decrease latency of shifting elements.
|
|
99
|
-
this.
|
|
100
|
-
this.
|
|
100
|
+
this._nodes = this.nodes.slice(this.offset);
|
|
101
|
+
this._offset = 0;
|
|
101
102
|
return first;
|
|
102
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
106
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
107
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
108
|
+
*/
|
|
109
|
+
getFirst() {
|
|
110
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
111
|
+
}
|
|
103
112
|
/**
|
|
104
113
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
105
114
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
106
115
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
107
116
|
*/
|
|
108
117
|
peek() {
|
|
109
|
-
return this.
|
|
118
|
+
return this.getFirst();
|
|
110
119
|
}
|
|
111
120
|
/**
|
|
112
121
|
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
@@ -116,6 +125,14 @@ class Queue {
|
|
|
116
125
|
getLast() {
|
|
117
126
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
118
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
130
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
131
|
+
* array is empty, it returns `null`.
|
|
132
|
+
*/
|
|
133
|
+
peekLast() {
|
|
134
|
+
return this.getLast();
|
|
135
|
+
}
|
|
119
136
|
/**
|
|
120
137
|
* The enqueue function adds a value to the end of a queue.
|
|
121
138
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -151,8 +168,8 @@ class Queue {
|
|
|
151
168
|
* The clear function resets the nodes array and offset to their initial values.
|
|
152
169
|
*/
|
|
153
170
|
clear() {
|
|
154
|
-
this.
|
|
155
|
-
this.
|
|
171
|
+
this._nodes = [];
|
|
172
|
+
this._offset = 0;
|
|
156
173
|
}
|
|
157
174
|
/**
|
|
158
175
|
* The `clone()` function returns a new Queue object with the same elements as the original Queue.
|
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export declare class Stack<E = any> {
|
|
7
|
-
protected _elements: E[];
|
|
8
7
|
/**
|
|
9
8
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
10
9
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -12,6 +11,8 @@ export declare class Stack<E = any> {
|
|
|
12
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
13
12
|
*/
|
|
14
13
|
constructor(elements?: E[]);
|
|
14
|
+
protected _elements: E[];
|
|
15
|
+
get elements(): E[];
|
|
15
16
|
/**
|
|
16
17
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
17
18
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -16,6 +16,9 @@ class Stack {
|
|
|
16
16
|
constructor(elements) {
|
|
17
17
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
18
18
|
}
|
|
19
|
+
get elements() {
|
|
20
|
+
return this._elements;
|
|
21
|
+
}
|
|
19
22
|
/**
|
|
20
23
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
24
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -30,14 +33,14 @@ class Stack {
|
|
|
30
33
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
31
34
|
*/
|
|
32
35
|
isEmpty() {
|
|
33
|
-
return this.
|
|
36
|
+
return this.elements.length === 0;
|
|
34
37
|
}
|
|
35
38
|
/**
|
|
36
39
|
* The size() function returns the number of elements in an array.
|
|
37
40
|
* @returns The size of the elements array.
|
|
38
41
|
*/
|
|
39
42
|
size() {
|
|
40
|
-
return this.
|
|
43
|
+
return this.elements.length;
|
|
41
44
|
}
|
|
42
45
|
/**
|
|
43
46
|
* The `peek` function returns the last element of an array, or null if the array is empty.
|
|
@@ -46,7 +49,7 @@ class Stack {
|
|
|
46
49
|
peek() {
|
|
47
50
|
if (this.isEmpty())
|
|
48
51
|
return null;
|
|
49
|
-
return this.
|
|
52
|
+
return this.elements[this.elements.length - 1];
|
|
50
53
|
}
|
|
51
54
|
/**
|
|
52
55
|
* The push function adds an element to the stack and returns the updated stack.
|
|
@@ -54,7 +57,7 @@ class Stack {
|
|
|
54
57
|
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
55
58
|
*/
|
|
56
59
|
push(element) {
|
|
57
|
-
this.
|
|
60
|
+
this.elements.push(element);
|
|
58
61
|
return this;
|
|
59
62
|
}
|
|
60
63
|
/**
|
|
@@ -65,14 +68,14 @@ class Stack {
|
|
|
65
68
|
pop() {
|
|
66
69
|
if (this.isEmpty())
|
|
67
70
|
return null;
|
|
68
|
-
return this.
|
|
71
|
+
return this.elements.pop() || null;
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* The toArray function returns a copy of the elements in an array.
|
|
72
75
|
* @returns An array of type E.
|
|
73
76
|
*/
|
|
74
77
|
toArray() {
|
|
75
|
-
return this.
|
|
78
|
+
return this.elements.slice();
|
|
76
79
|
}
|
|
77
80
|
/**
|
|
78
81
|
* The clear function clears the elements array.
|
|
@@ -85,7 +88,7 @@ class Stack {
|
|
|
85
88
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
86
89
|
*/
|
|
87
90
|
clone() {
|
|
88
|
-
return new Stack(this.
|
|
91
|
+
return new Stack(this.elements.slice());
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
exports.Stack = Stack;
|
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
export declare class TreeNode<V = any> {
|
|
2
|
+
key: string;
|
|
3
|
+
value?: V | undefined;
|
|
4
|
+
children?: TreeNode<V>[] | undefined;
|
|
2
5
|
constructor(key: string, value?: V, children?: TreeNode<V>[]);
|
|
3
|
-
private _key;
|
|
4
|
-
get key(): string;
|
|
5
|
-
set key(value: string);
|
|
6
|
-
private _value?;
|
|
7
|
-
get value(): V | undefined;
|
|
8
|
-
set value(value: V | undefined);
|
|
9
|
-
private _children?;
|
|
10
|
-
get children(): TreeNode<V>[] | undefined;
|
|
11
|
-
set children(value: TreeNode<V>[] | undefined);
|
|
12
6
|
addChildren(children: TreeNode<V> | TreeNode<V>[]): void;
|
|
13
7
|
getHeight(): number;
|
|
14
8
|
}
|
|
@@ -3,27 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.TreeNode = void 0;
|
|
4
4
|
class TreeNode {
|
|
5
5
|
constructor(key, value, children) {
|
|
6
|
-
this.
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
}
|
|
10
|
-
get key() {
|
|
11
|
-
return this._key;
|
|
12
|
-
}
|
|
13
|
-
set key(value) {
|
|
14
|
-
this._key = value;
|
|
15
|
-
}
|
|
16
|
-
get value() {
|
|
17
|
-
return this._value;
|
|
18
|
-
}
|
|
19
|
-
set value(value) {
|
|
20
|
-
this._value = value;
|
|
21
|
-
}
|
|
22
|
-
get children() {
|
|
23
|
-
return this._children;
|
|
24
|
-
}
|
|
25
|
-
set children(value) {
|
|
26
|
-
this._children = value;
|
|
6
|
+
this.key = key;
|
|
7
|
+
this.value = value || undefined;
|
|
8
|
+
this.children = children || [];
|
|
27
9
|
}
|
|
28
10
|
addChildren(children) {
|
|
29
11
|
if (!this.children) {
|
|
@@ -10,26 +10,20 @@
|
|
|
10
10
|
* and a flag indicating whether it's the end of a word.
|
|
11
11
|
*/
|
|
12
12
|
export declare class TrieNode {
|
|
13
|
+
key: string;
|
|
14
|
+
children: Map<string, TrieNode>;
|
|
15
|
+
isEnd: boolean;
|
|
13
16
|
constructor(key: string);
|
|
14
|
-
private _key;
|
|
15
|
-
get key(): string;
|
|
16
|
-
set key(v: string);
|
|
17
|
-
protected _children: Map<string, TrieNode>;
|
|
18
|
-
get children(): Map<string, TrieNode>;
|
|
19
|
-
set children(v: Map<string, TrieNode>);
|
|
20
|
-
protected _isEnd: boolean;
|
|
21
|
-
get isEnd(): boolean;
|
|
22
|
-
set isEnd(v: boolean);
|
|
23
17
|
}
|
|
24
18
|
/**
|
|
25
19
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
26
20
|
*/
|
|
27
21
|
export declare class Trie {
|
|
28
|
-
private readonly _caseSensitive;
|
|
29
22
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
23
|
+
protected _caseSensitive: boolean;
|
|
24
|
+
get caseSensitive(): boolean;
|
|
30
25
|
protected _root: TrieNode;
|
|
31
26
|
get root(): TrieNode;
|
|
32
|
-
set root(v: TrieNode);
|
|
33
27
|
/**
|
|
34
28
|
* Add a word to the Trie structure.
|
|
35
29
|
* @param {string} word - The word to add.
|
|
@@ -81,5 +75,5 @@ export declare class Trie {
|
|
|
81
75
|
* @returns {string[]} an array of strings.
|
|
82
76
|
*/
|
|
83
77
|
getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
|
|
84
|
-
|
|
78
|
+
protected _caseProcess(str: string): string;
|
|
85
79
|
}
|
|
@@ -14,27 +14,9 @@ exports.Trie = exports.TrieNode = void 0;
|
|
|
14
14
|
*/
|
|
15
15
|
class TrieNode {
|
|
16
16
|
constructor(key) {
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
}
|
|
21
|
-
get key() {
|
|
22
|
-
return this._key;
|
|
23
|
-
}
|
|
24
|
-
set key(v) {
|
|
25
|
-
this._key = v;
|
|
26
|
-
}
|
|
27
|
-
get children() {
|
|
28
|
-
return this._children;
|
|
29
|
-
}
|
|
30
|
-
set children(v) {
|
|
31
|
-
this._children = v;
|
|
32
|
-
}
|
|
33
|
-
get isEnd() {
|
|
34
|
-
return this._isEnd;
|
|
35
|
-
}
|
|
36
|
-
set isEnd(v) {
|
|
37
|
-
this._isEnd = v;
|
|
17
|
+
this.key = key;
|
|
18
|
+
this.isEnd = false;
|
|
19
|
+
this.children = new Map();
|
|
38
20
|
}
|
|
39
21
|
}
|
|
40
22
|
exports.TrieNode = TrieNode;
|
|
@@ -51,12 +33,12 @@ class Trie {
|
|
|
51
33
|
}
|
|
52
34
|
}
|
|
53
35
|
}
|
|
36
|
+
get caseSensitive() {
|
|
37
|
+
return this._caseSensitive;
|
|
38
|
+
}
|
|
54
39
|
get root() {
|
|
55
40
|
return this._root;
|
|
56
41
|
}
|
|
57
|
-
set root(v) {
|
|
58
|
-
this._root = v;
|
|
59
|
-
}
|
|
60
42
|
/**
|
|
61
43
|
* Add a word to the Trie structure.
|
|
62
44
|
* @param {string} word - The word to add.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult,
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
5
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BSTNode } from '../../../data-structures';
|
|
2
|
-
import type {
|
|
2
|
+
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
3
|
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type BSTOptions = BinaryTreeOptions & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.40.0",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -131,6 +131,6 @@
|
|
|
131
131
|
"typescript": "^4.9.5"
|
|
132
132
|
},
|
|
133
133
|
"dependencies": {
|
|
134
|
-
"data-structure-typed": "^1.
|
|
134
|
+
"data-structure-typed": "^1.40.0"
|
|
135
135
|
}
|
|
136
136
|
}
|