directed-graph-typed 1.39.5 → 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.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -14
- 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 +12 -27
- package/dist/data-structures/binary-tree/binary-tree.js +25 -46
- package/dist/data-structures/binary-tree/bst.d.ts +7 -7
- package/dist/data-structures/binary-tree/bst.js +16 -16
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -5
- package/dist/data-structures/binary-tree/rb-tree.js +5 -11
- package/dist/data-structures/binary-tree/segment-tree.d.ts +14 -30
- package/dist/data-structures/binary-tree/segment-tree.js +20 -68
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +7 -7
- package/dist/data-structures/binary-tree/tree-multiset.js +24 -24
- package/dist/data-structures/graph/abstract-graph.d.ts +16 -35
- package/dist/data-structures/graph/abstract-graph.js +18 -57
- package/dist/data-structures/graph/directed-graph.d.ts +16 -22
- package/dist/data-structures/graph/directed-graph.js +17 -35
- package/dist/data-structures/graph/map-graph.d.ts +13 -19
- package/dist/data-structures/graph/map-graph.js +15 -33
- package/dist/data-structures/graph/undirected-graph.d.ts +12 -19
- package/dist/data-structures/graph/undirected-graph.js +15 -28
- 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 +10 -13
- package/dist/data-structures/hash/hash-table.js +8 -17
- 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 +34 -42
- package/dist/data-structures/linked-list/doubly-linked-list.js +67 -91
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -32
- package/dist/data-structures/linked-list/singly-linked-list.js +64 -82
- 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 +3 -3
- package/dist/interfaces/graph.d.ts +2 -2
- 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 +15 -17
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +35 -60
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/rb-tree.ts +19 -27
- package/src/data-structures/binary-tree/segment-tree.ts +25 -92
- package/src/data-structures/binary-tree/tree-multiset.ts +26 -27
- package/src/data-structures/graph/abstract-graph.ts +42 -88
- package/src/data-structures/graph/directed-graph.ts +29 -55
- package/src/data-structures/graph/map-graph.ts +20 -45
- package/src/data-structures/graph/undirected-graph.ts +24 -41
- 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 +15 -27
- 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 +81 -115
- package/src/data-structures/linked-list/singly-linked-list.ts +76 -101
- 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 +3 -3
- package/src/interfaces/graph.ts +2 -2
- 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
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,43 +20,31 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
protected _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
29
30
|
|
|
30
31
|
get capacity(): number {
|
|
31
32
|
return this._capacity;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
this._capacity = value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private _first = -1;
|
|
35
|
+
protected _first = -1;
|
|
39
36
|
|
|
40
37
|
get first(): number {
|
|
41
38
|
return this._first;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
this._first = value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private _last = -1;
|
|
41
|
+
protected _last = -1;
|
|
49
42
|
|
|
50
43
|
get last(): number {
|
|
51
44
|
return this._last;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
|
|
55
|
-
this._last = value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private _size = 0;
|
|
47
|
+
protected _size = 0;
|
|
59
48
|
|
|
60
49
|
get size(): number {
|
|
61
50
|
return this._size;
|
|
@@ -67,14 +56,14 @@ export class ObjectDeque<E = number> {
|
|
|
67
56
|
* structure.
|
|
68
57
|
*/
|
|
69
58
|
addFirst(value: E) {
|
|
70
|
-
if (this.
|
|
71
|
-
const mid = Math.floor(this.
|
|
59
|
+
if (this.size === 0) {
|
|
60
|
+
const mid = Math.floor(this.capacity / 2);
|
|
72
61
|
this._first = mid;
|
|
73
62
|
this._last = mid;
|
|
74
63
|
} else {
|
|
75
64
|
this._first--;
|
|
76
65
|
}
|
|
77
|
-
this.
|
|
66
|
+
this.nodes[this.first] = value;
|
|
78
67
|
this._size++;
|
|
79
68
|
}
|
|
80
69
|
|
|
@@ -83,14 +72,14 @@ export class ObjectDeque<E = number> {
|
|
|
83
72
|
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
84
73
|
*/
|
|
85
74
|
addLast(value: E) {
|
|
86
|
-
if (this.
|
|
87
|
-
const mid = Math.floor(this.
|
|
75
|
+
if (this.size === 0) {
|
|
76
|
+
const mid = Math.floor(this.capacity / 2);
|
|
88
77
|
this._first = mid;
|
|
89
78
|
this._last = mid;
|
|
90
79
|
} else {
|
|
91
80
|
this._last++;
|
|
92
81
|
}
|
|
93
|
-
this.
|
|
82
|
+
this.nodes[this.last] = value;
|
|
94
83
|
this._size++;
|
|
95
84
|
}
|
|
96
85
|
|
|
@@ -99,9 +88,9 @@ export class ObjectDeque<E = number> {
|
|
|
99
88
|
* @returns The value of the first element in the data structure.
|
|
100
89
|
*/
|
|
101
90
|
popFirst() {
|
|
102
|
-
if (!this.
|
|
91
|
+
if (!this.size) return;
|
|
103
92
|
const value = this.getFirst();
|
|
104
|
-
delete this.
|
|
93
|
+
delete this.nodes[this.first];
|
|
105
94
|
this._first++;
|
|
106
95
|
this._size--;
|
|
107
96
|
return value;
|
|
@@ -112,7 +101,7 @@ export class ObjectDeque<E = number> {
|
|
|
112
101
|
* @returns The element at the first position of the `_nodes` array.
|
|
113
102
|
*/
|
|
114
103
|
getFirst() {
|
|
115
|
-
if (this.
|
|
104
|
+
if (this.size) return this.nodes[this.first];
|
|
116
105
|
}
|
|
117
106
|
|
|
118
107
|
/**
|
|
@@ -120,9 +109,9 @@ export class ObjectDeque<E = number> {
|
|
|
120
109
|
* @returns The value that was removed from the data structure.
|
|
121
110
|
*/
|
|
122
111
|
popLast() {
|
|
123
|
-
if (!this.
|
|
112
|
+
if (!this.size) return;
|
|
124
113
|
const value = this.getLast();
|
|
125
|
-
delete this.
|
|
114
|
+
delete this.nodes[this.last];
|
|
126
115
|
this._last--;
|
|
127
116
|
this._size--;
|
|
128
117
|
|
|
@@ -134,7 +123,7 @@ export class ObjectDeque<E = number> {
|
|
|
134
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
135
124
|
*/
|
|
136
125
|
getLast() {
|
|
137
|
-
if (this.
|
|
126
|
+
if (this.size) return this.nodes[this.last];
|
|
138
127
|
}
|
|
139
128
|
|
|
140
129
|
/**
|
|
@@ -145,7 +134,7 @@ export class ObjectDeque<E = number> {
|
|
|
145
134
|
* index, `null` is returned.
|
|
146
135
|
*/
|
|
147
136
|
get(index: number) {
|
|
148
|
-
return this.
|
|
137
|
+
return this.nodes[this.first + index] || null;
|
|
149
138
|
}
|
|
150
139
|
|
|
151
140
|
/**
|
|
@@ -153,15 +142,7 @@ export class ObjectDeque<E = number> {
|
|
|
153
142
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
154
143
|
*/
|
|
155
144
|
isEmpty() {
|
|
156
|
-
return this.
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
-
this._nodes = value;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
protected _setSize(value: number) {
|
|
164
|
-
this._size = value;
|
|
145
|
+
return this.size <= 0;
|
|
165
146
|
}
|
|
166
147
|
}
|
|
167
148
|
|
|
@@ -170,8 +151,12 @@ export class ObjectDeque<E = number> {
|
|
|
170
151
|
export class ArrayDeque<E> {
|
|
171
152
|
protected _nodes: E[] = [];
|
|
172
153
|
|
|
154
|
+
get nodes(): E[] {
|
|
155
|
+
return this._nodes;
|
|
156
|
+
}
|
|
157
|
+
|
|
173
158
|
get size() {
|
|
174
|
-
return this.
|
|
159
|
+
return this.nodes.length;
|
|
175
160
|
}
|
|
176
161
|
|
|
177
162
|
/**
|
|
@@ -184,7 +169,7 @@ export class ArrayDeque<E> {
|
|
|
184
169
|
* @returns The return value is the new length of the array after the value has been added.
|
|
185
170
|
*/
|
|
186
171
|
addLast(value: E) {
|
|
187
|
-
return this.
|
|
172
|
+
return this.nodes.push(value);
|
|
188
173
|
}
|
|
189
174
|
|
|
190
175
|
/**
|
|
@@ -192,7 +177,7 @@ export class ArrayDeque<E> {
|
|
|
192
177
|
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
193
178
|
*/
|
|
194
179
|
popLast(): E | null {
|
|
195
|
-
return this.
|
|
180
|
+
return this.nodes.pop() ?? null;
|
|
196
181
|
}
|
|
197
182
|
|
|
198
183
|
/**
|
|
@@ -201,7 +186,7 @@ export class ArrayDeque<E> {
|
|
|
201
186
|
* empty.
|
|
202
187
|
*/
|
|
203
188
|
popFirst(): E | null {
|
|
204
|
-
return this.
|
|
189
|
+
return this.nodes.shift() ?? null;
|
|
205
190
|
}
|
|
206
191
|
|
|
207
192
|
/**
|
|
@@ -215,7 +200,7 @@ export class ArrayDeque<E> {
|
|
|
215
200
|
* `value` at the beginning.
|
|
216
201
|
*/
|
|
217
202
|
addFirst(value: E) {
|
|
218
|
-
return this.
|
|
203
|
+
return this.nodes.unshift(value);
|
|
219
204
|
}
|
|
220
205
|
|
|
221
206
|
/**
|
|
@@ -224,7 +209,7 @@ export class ArrayDeque<E> {
|
|
|
224
209
|
* empty, it will return `null`.
|
|
225
210
|
*/
|
|
226
211
|
getFirst(): E | null {
|
|
227
|
-
return this.
|
|
212
|
+
return this.nodes[0] ?? null;
|
|
228
213
|
}
|
|
229
214
|
|
|
230
215
|
/**
|
|
@@ -232,7 +217,7 @@ export class ArrayDeque<E> {
|
|
|
232
217
|
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
233
218
|
*/
|
|
234
219
|
getLast(): E | null {
|
|
235
|
-
return this.
|
|
220
|
+
return this.nodes[this.nodes.length - 1] ?? null;
|
|
236
221
|
}
|
|
237
222
|
|
|
238
223
|
/**
|
|
@@ -247,7 +232,7 @@ export class ArrayDeque<E> {
|
|
|
247
232
|
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
248
233
|
*/
|
|
249
234
|
get(index: number): E | null {
|
|
250
|
-
return this.
|
|
235
|
+
return this.nodes[index] ?? null;
|
|
251
236
|
}
|
|
252
237
|
|
|
253
238
|
/**
|
|
@@ -259,7 +244,7 @@ export class ArrayDeque<E> {
|
|
|
259
244
|
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
260
245
|
*/
|
|
261
246
|
set(index: number, value: E) {
|
|
262
|
-
return (this.
|
|
247
|
+
return (this.nodes[index] = value);
|
|
263
248
|
}
|
|
264
249
|
|
|
265
250
|
/**
|
|
@@ -273,7 +258,7 @@ export class ArrayDeque<E> {
|
|
|
273
258
|
* are being removed, an empty array will be returned.
|
|
274
259
|
*/
|
|
275
260
|
insert(index: number, value: E) {
|
|
276
|
-
return this.
|
|
261
|
+
return this.nodes.splice(index, 0, value);
|
|
277
262
|
}
|
|
278
263
|
|
|
279
264
|
/**
|
|
@@ -283,7 +268,7 @@ export class ArrayDeque<E> {
|
|
|
283
268
|
* @returns The method is returning an array containing the removed element.
|
|
284
269
|
*/
|
|
285
270
|
delete(index: number) {
|
|
286
|
-
return this.
|
|
271
|
+
return this.nodes.splice(index, 1);
|
|
287
272
|
}
|
|
288
273
|
|
|
289
274
|
/**
|
|
@@ -292,6 +277,6 @@ export class ArrayDeque<E> {
|
|
|
292
277
|
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
293
278
|
*/
|
|
294
279
|
isEmpty() {
|
|
295
|
-
return this.
|
|
280
|
+
return this.nodes.length === 0;
|
|
296
281
|
}
|
|
297
282
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import {SinglyLinkedList} from '../linked-list';
|
|
7
7
|
|
|
8
|
-
export class
|
|
8
|
+
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
9
|
/**
|
|
10
10
|
* The enqueue function adds a value to the end of an array.
|
|
11
11
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -22,12 +22,20 @@ export class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
22
22
|
return this.shift();
|
|
23
23
|
}
|
|
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(): E | undefined {
|
|
30
|
+
return this.head?.value;
|
|
31
|
+
}
|
|
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(): E | undefined {
|
|
30
|
-
return this.
|
|
38
|
+
return this.getFirst();
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
|
|
@@ -43,26 +51,18 @@ export class Queue<E = any> {
|
|
|
43
51
|
this._offset = 0;
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
protected _nodes: E[];
|
|
47
55
|
|
|
48
56
|
get nodes(): E[] {
|
|
49
57
|
return this._nodes;
|
|
50
58
|
}
|
|
51
59
|
|
|
52
|
-
|
|
53
|
-
this._nodes = value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _offset: number;
|
|
60
|
+
protected _offset: number;
|
|
57
61
|
|
|
58
62
|
get offset(): number {
|
|
59
63
|
return this._offset;
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
set offset(value: number) {
|
|
63
|
-
this._offset = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
66
|
/**
|
|
67
67
|
* The size function returns the number of elements in an array.
|
|
68
68
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
@@ -101,25 +101,34 @@ export class Queue<E = any> {
|
|
|
101
101
|
shift(): E | undefined {
|
|
102
102
|
if (this.size === 0) return undefined;
|
|
103
103
|
|
|
104
|
-
const first = this.
|
|
105
|
-
this.
|
|
104
|
+
const first = this.getFirst();
|
|
105
|
+
this._offset += 1;
|
|
106
106
|
|
|
107
107
|
if (this.offset * 2 < this.nodes.length) return first;
|
|
108
108
|
|
|
109
109
|
// only delete dequeued elements when reaching half size
|
|
110
110
|
// to decrease latency of shifting elements.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
111
|
+
this._nodes = this.nodes.slice(this.offset);
|
|
112
|
+
this._offset = 0;
|
|
113
113
|
return first;
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The `getFirst` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
118
|
+
* @returns The `getFirst()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
119
|
+
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
120
|
+
*/
|
|
121
|
+
getFirst(): E | undefined {
|
|
122
|
+
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
116
125
|
/**
|
|
117
126
|
* The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
|
|
118
127
|
* @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
|
|
119
128
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
|
|
120
129
|
*/
|
|
121
130
|
peek(): E | undefined {
|
|
122
|
-
return this.
|
|
131
|
+
return this.getFirst();
|
|
123
132
|
}
|
|
124
133
|
|
|
125
134
|
/**
|
|
@@ -131,6 +140,15 @@ export class Queue<E = any> {
|
|
|
131
140
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
132
141
|
}
|
|
133
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
145
|
+
* @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
146
|
+
* array is empty, it returns `null`.
|
|
147
|
+
*/
|
|
148
|
+
peekLast(): E | undefined {
|
|
149
|
+
return this.getLast();
|
|
150
|
+
}
|
|
151
|
+
|
|
134
152
|
/**
|
|
135
153
|
* The enqueue function adds a value to the end of a queue.
|
|
136
154
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -171,8 +189,8 @@ export class Queue<E = any> {
|
|
|
171
189
|
* The clear function resets the nodes array and offset to their initial values.
|
|
172
190
|
*/
|
|
173
191
|
clear(): void {
|
|
174
|
-
this.
|
|
175
|
-
this.
|
|
192
|
+
this._nodes = [];
|
|
193
|
+
this._offset = 0;
|
|
176
194
|
}
|
|
177
195
|
|
|
178
196
|
/**
|
|
@@ -183,7 +201,7 @@ export class Queue<E = any> {
|
|
|
183
201
|
return new Queue(this.nodes.slice(this.offset));
|
|
184
202
|
}
|
|
185
203
|
|
|
186
|
-
*[Symbol.iterator]() {
|
|
204
|
+
* [Symbol.iterator]() {
|
|
187
205
|
for (const item of this.nodes) {
|
|
188
206
|
yield item;
|
|
189
207
|
}
|
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
* @class
|
|
5
5
|
*/
|
|
6
6
|
export class Stack<E = any> {
|
|
7
|
-
protected _elements: E[];
|
|
8
|
-
|
|
9
7
|
/**
|
|
10
8
|
* The constructor initializes an array of elements, which can be provided as an optional parameter.
|
|
11
9
|
* @param {E[]} [elements] - The `elements` parameter is an optional parameter of type `E[]`, which represents an array
|
|
@@ -16,6 +14,12 @@ export class Stack<E = any> {
|
|
|
16
14
|
this._elements = Array.isArray(elements) ? elements : [];
|
|
17
15
|
}
|
|
18
16
|
|
|
17
|
+
protected _elements: E[];
|
|
18
|
+
|
|
19
|
+
get elements(): E[] {
|
|
20
|
+
return this._elements;
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
/**
|
|
20
24
|
* The function "fromArray" creates a new Stack object from an array of elements.
|
|
21
25
|
* @param {E[]} elements - The `elements` parameter is an array of elements of type `E`.
|
|
@@ -31,7 +35,7 @@ export class Stack<E = any> {
|
|
|
31
35
|
* @returns A boolean value indicating whether the `_elements` array is empty or not.
|
|
32
36
|
*/
|
|
33
37
|
isEmpty(): boolean {
|
|
34
|
-
return this.
|
|
38
|
+
return this.elements.length === 0;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
/**
|
|
@@ -39,7 +43,7 @@ export class Stack<E = any> {
|
|
|
39
43
|
* @returns The size of the elements array.
|
|
40
44
|
*/
|
|
41
45
|
size(): number {
|
|
42
|
-
return this.
|
|
46
|
+
return this.elements.length;
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
/**
|
|
@@ -49,7 +53,7 @@ export class Stack<E = any> {
|
|
|
49
53
|
peek(): E | null {
|
|
50
54
|
if (this.isEmpty()) return null;
|
|
51
55
|
|
|
52
|
-
return this.
|
|
56
|
+
return this.elements[this.elements.length - 1];
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
/**
|
|
@@ -58,7 +62,7 @@ export class Stack<E = any> {
|
|
|
58
62
|
* @returns The `push` method is returning the updated `Stack<E>` object.
|
|
59
63
|
*/
|
|
60
64
|
push(element: E): Stack<E> {
|
|
61
|
-
this.
|
|
65
|
+
this.elements.push(element);
|
|
62
66
|
return this;
|
|
63
67
|
}
|
|
64
68
|
|
|
@@ -70,7 +74,7 @@ export class Stack<E = any> {
|
|
|
70
74
|
pop(): E | null {
|
|
71
75
|
if (this.isEmpty()) return null;
|
|
72
76
|
|
|
73
|
-
return this.
|
|
77
|
+
return this.elements.pop() || null;
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
/**
|
|
@@ -78,7 +82,7 @@ export class Stack<E = any> {
|
|
|
78
82
|
* @returns An array of type E.
|
|
79
83
|
*/
|
|
80
84
|
toArray(): E[] {
|
|
81
|
-
return this.
|
|
85
|
+
return this.elements.slice();
|
|
82
86
|
}
|
|
83
87
|
|
|
84
88
|
/**
|
|
@@ -93,6 +97,6 @@ export class Stack<E = any> {
|
|
|
93
97
|
* @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
|
|
94
98
|
*/
|
|
95
99
|
clone(): Stack<E> {
|
|
96
|
-
return new Stack(this.
|
|
100
|
+
return new Stack(this.elements.slice());
|
|
97
101
|
}
|
|
98
102
|
}
|
|
@@ -1,38 +1,12 @@
|
|
|
1
1
|
export class TreeNode<V = any> {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
this._children = children || [];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
private _key: string;
|
|
9
|
-
|
|
10
|
-
get key(): string {
|
|
11
|
-
return this._key;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
set key(value: string) {
|
|
15
|
-
this._key = value;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
private _value?: V | undefined;
|
|
2
|
+
key: string;
|
|
3
|
+
value?: V | undefined;
|
|
4
|
+
children?: TreeNode<V>[] | undefined;
|
|
19
5
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
set value(value: V | undefined) {
|
|
25
|
-
this._value = value;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private _children?: TreeNode<V>[] | undefined;
|
|
29
|
-
|
|
30
|
-
get children(): TreeNode<V>[] | undefined {
|
|
31
|
-
return this._children;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
set children(value: TreeNode<V>[] | undefined) {
|
|
35
|
-
this._children = value;
|
|
6
|
+
constructor(key: string, value?: V, children?: TreeNode<V>[]) {
|
|
7
|
+
this.key = key;
|
|
8
|
+
this.value = value || undefined;
|
|
9
|
+
this.children = children || [];
|
|
36
10
|
}
|
|
37
11
|
|
|
38
12
|
addChildren(children: TreeNode<V> | TreeNode<V>[]) {
|
|
@@ -11,40 +11,14 @@
|
|
|
11
11
|
* and a flag indicating whether it's the end of a word.
|
|
12
12
|
*/
|
|
13
13
|
export class TrieNode {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this._children = new Map<string, TrieNode>();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _key;
|
|
21
|
-
|
|
22
|
-
get key(): string {
|
|
23
|
-
return this._key;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
set key(v: string) {
|
|
27
|
-
this._key = v;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected _children: Map<string, TrieNode>;
|
|
31
|
-
|
|
32
|
-
get children(): Map<string, TrieNode> {
|
|
33
|
-
return this._children;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set children(v: Map<string, TrieNode>) {
|
|
37
|
-
this._children = v;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
protected _isEnd: boolean;
|
|
41
|
-
|
|
42
|
-
get isEnd(): boolean {
|
|
43
|
-
return this._isEnd;
|
|
44
|
-
}
|
|
14
|
+
key: string;
|
|
15
|
+
children: Map<string, TrieNode>;
|
|
16
|
+
isEnd: boolean;
|
|
45
17
|
|
|
46
|
-
|
|
47
|
-
this.
|
|
18
|
+
constructor(key: string) {
|
|
19
|
+
this.key = key;
|
|
20
|
+
this.isEnd = false;
|
|
21
|
+
this.children = new Map<string, TrieNode>();
|
|
48
22
|
}
|
|
49
23
|
}
|
|
50
24
|
|
|
@@ -52,8 +26,6 @@ export class TrieNode {
|
|
|
52
26
|
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
53
27
|
*/
|
|
54
28
|
export class Trie {
|
|
55
|
-
private readonly _caseSensitive: boolean;
|
|
56
|
-
|
|
57
29
|
constructor(words?: string[], caseSensitive = true) {
|
|
58
30
|
this._root = new TrieNode('');
|
|
59
31
|
this._caseSensitive = caseSensitive;
|
|
@@ -64,16 +36,18 @@ export class Trie {
|
|
|
64
36
|
}
|
|
65
37
|
}
|
|
66
38
|
|
|
39
|
+
protected _caseSensitive: boolean;
|
|
40
|
+
|
|
41
|
+
get caseSensitive(): boolean {
|
|
42
|
+
return this._caseSensitive;
|
|
43
|
+
}
|
|
44
|
+
|
|
67
45
|
protected _root: TrieNode;
|
|
68
46
|
|
|
69
47
|
get root() {
|
|
70
48
|
return this._root;
|
|
71
49
|
}
|
|
72
50
|
|
|
73
|
-
set root(v: TrieNode) {
|
|
74
|
-
this._root = v;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
51
|
/**
|
|
78
52
|
* Add a word to the Trie structure.
|
|
79
53
|
* @param {string} word - The word to add.
|
|
@@ -277,7 +251,7 @@ export class Trie {
|
|
|
277
251
|
return words;
|
|
278
252
|
}
|
|
279
253
|
|
|
280
|
-
|
|
254
|
+
protected _caseProcess(str: string) {
|
|
281
255
|
if (!this._caseSensitive) {
|
|
282
256
|
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
283
257
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult,
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
|
-
createNode(key: BTNKey,
|
|
5
|
+
createNode(key: BTNKey, value?: N['value']): N;
|
|
6
6
|
|
|
7
|
-
add(keyOrNode: BTNKey | N | null,
|
|
7
|
+
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
8
8
|
|
|
9
9
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
package/src/interfaces/graph.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {VertexKey} from '../types';
|
|
2
2
|
|
|
3
3
|
export interface IGraph<V, E, VO, EO> {
|
|
4
|
-
createVertex(key: VertexKey,
|
|
4
|
+
createVertex(key: VertexKey, value?: V): VO;
|
|
5
5
|
|
|
6
|
-
createEdge(srcOrV1: VertexKey
|
|
6
|
+
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
7
7
|
}
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
|
|
3
3
|
export type TrlFn = (...args: any[]) => any;
|
|
4
4
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type KeyValueObject = {[key: string]: any};
|
|
1
|
+
export type KeyValueObject = { [key: string]: any };
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
|
|
3
|
+
export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
|
|
4
4
|
|
|
5
5
|
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
|
6
6
|
|