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
|
@@ -6,34 +6,17 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class SinglyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: SinglyLinkedListNode<E> | null;
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
|
-
* @param {E}
|
|
14
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
12
15
|
* will be stored in the node of a linked list.
|
|
13
16
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private _val: E;
|
|
20
|
-
|
|
21
|
-
get val(): E {
|
|
22
|
-
return this._val;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
set val(value: E) {
|
|
26
|
-
this._val = value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private _next: SinglyLinkedListNode<E> | null;
|
|
30
|
-
|
|
31
|
-
get next(): SinglyLinkedListNode<E> | null {
|
|
32
|
-
return this._next;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
set next(value: SinglyLinkedListNode<E> | null) {
|
|
36
|
-
this._next = value;
|
|
17
|
+
constructor(value: E) {
|
|
18
|
+
this.value = value;
|
|
19
|
+
this.next = null;
|
|
37
20
|
}
|
|
38
21
|
}
|
|
39
22
|
|
|
@@ -47,27 +30,19 @@ export class SinglyLinkedList<E = any> {
|
|
|
47
30
|
this._length = 0;
|
|
48
31
|
}
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
protected _head: SinglyLinkedListNode<E> | null;
|
|
51
34
|
|
|
52
35
|
get head(): SinglyLinkedListNode<E> | null {
|
|
53
36
|
return this._head;
|
|
54
37
|
}
|
|
55
38
|
|
|
56
|
-
|
|
57
|
-
this._head = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private _tail: SinglyLinkedListNode<E> | null;
|
|
39
|
+
protected _tail: SinglyLinkedListNode<E> | null;
|
|
61
40
|
|
|
62
41
|
get tail(): SinglyLinkedListNode<E> | null {
|
|
63
42
|
return this._tail;
|
|
64
43
|
}
|
|
65
44
|
|
|
66
|
-
|
|
67
|
-
this._tail = value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private _length: number;
|
|
45
|
+
protected _length: number;
|
|
71
46
|
|
|
72
47
|
get length(): number {
|
|
73
48
|
return this._length;
|
|
@@ -88,29 +63,29 @@ export class SinglyLinkedList<E = any> {
|
|
|
88
63
|
}
|
|
89
64
|
|
|
90
65
|
/**
|
|
91
|
-
* The `push` function adds a new node with the given
|
|
92
|
-
* @param {E}
|
|
66
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
67
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
93
68
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
94
69
|
*/
|
|
95
|
-
push(
|
|
96
|
-
const newNode = new SinglyLinkedListNode(
|
|
70
|
+
push(value: E): void {
|
|
71
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
97
72
|
if (!this.head) {
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
73
|
+
this._head = newNode;
|
|
74
|
+
this._tail = newNode;
|
|
100
75
|
} else {
|
|
101
76
|
this.tail!.next = newNode;
|
|
102
|
-
this.
|
|
77
|
+
this._tail = newNode;
|
|
103
78
|
}
|
|
104
79
|
this._length++;
|
|
105
80
|
}
|
|
106
81
|
|
|
107
82
|
/**
|
|
108
|
-
* The `push` function adds a new node with the given
|
|
109
|
-
* @param {E}
|
|
83
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
84
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
110
85
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
111
86
|
*/
|
|
112
|
-
addLast(
|
|
113
|
-
this.push(
|
|
87
|
+
addLast(value: E): void {
|
|
88
|
+
this.push(value);
|
|
114
89
|
}
|
|
115
90
|
|
|
116
91
|
/**
|
|
@@ -122,22 +97,22 @@ export class SinglyLinkedList<E = any> {
|
|
|
122
97
|
pop(): E | undefined {
|
|
123
98
|
if (!this.head) return undefined;
|
|
124
99
|
if (this.head === this.tail) {
|
|
125
|
-
const
|
|
126
|
-
this.
|
|
127
|
-
this.
|
|
100
|
+
const value = this.head.value;
|
|
101
|
+
this._head = null;
|
|
102
|
+
this._tail = null;
|
|
128
103
|
this._length--;
|
|
129
|
-
return
|
|
104
|
+
return value;
|
|
130
105
|
}
|
|
131
106
|
|
|
132
107
|
let current = this.head;
|
|
133
108
|
while (current.next !== this.tail) {
|
|
134
109
|
current = current.next!;
|
|
135
110
|
}
|
|
136
|
-
const
|
|
111
|
+
const value = this.tail!.value;
|
|
137
112
|
current.next = null;
|
|
138
|
-
this.
|
|
113
|
+
this._tail = current;
|
|
139
114
|
this._length--;
|
|
140
|
-
return
|
|
115
|
+
return value;
|
|
141
116
|
}
|
|
142
117
|
|
|
143
118
|
/**
|
|
@@ -157,9 +132,9 @@ export class SinglyLinkedList<E = any> {
|
|
|
157
132
|
shift(): E | undefined {
|
|
158
133
|
if (!this.head) return undefined;
|
|
159
134
|
const removedNode = this.head;
|
|
160
|
-
this.
|
|
135
|
+
this._head = this.head.next;
|
|
161
136
|
this._length--;
|
|
162
|
-
return removedNode.
|
|
137
|
+
return removedNode.value;
|
|
163
138
|
}
|
|
164
139
|
|
|
165
140
|
/**
|
|
@@ -172,28 +147,28 @@ export class SinglyLinkedList<E = any> {
|
|
|
172
147
|
|
|
173
148
|
/**
|
|
174
149
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
175
|
-
* @param {E}
|
|
150
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
176
151
|
* linked list.
|
|
177
152
|
*/
|
|
178
|
-
unshift(
|
|
179
|
-
const newNode = new SinglyLinkedListNode(
|
|
153
|
+
unshift(value: E): void {
|
|
154
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
180
155
|
if (!this.head) {
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
156
|
+
this._head = newNode;
|
|
157
|
+
this._tail = newNode;
|
|
183
158
|
} else {
|
|
184
159
|
newNode.next = this.head;
|
|
185
|
-
this.
|
|
160
|
+
this._head = newNode;
|
|
186
161
|
}
|
|
187
162
|
this._length++;
|
|
188
163
|
}
|
|
189
164
|
|
|
190
165
|
/**
|
|
191
166
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
192
|
-
* @param {E}
|
|
167
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
193
168
|
* linked list.
|
|
194
169
|
*/
|
|
195
|
-
addFirst(
|
|
196
|
-
this.unshift(
|
|
170
|
+
addFirst(value: E): void {
|
|
171
|
+
this.unshift(value);
|
|
197
172
|
}
|
|
198
173
|
|
|
199
174
|
/**
|
|
@@ -209,7 +184,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
209
184
|
for (let i = 0; i < index; i++) {
|
|
210
185
|
current = current!.next;
|
|
211
186
|
}
|
|
212
|
-
return current!.
|
|
187
|
+
return current!.value;
|
|
213
188
|
}
|
|
214
189
|
|
|
215
190
|
/**
|
|
@@ -243,7 +218,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
243
218
|
const removedNode = prevNode!.next;
|
|
244
219
|
prevNode!.next = removedNode!.next;
|
|
245
220
|
this._length--;
|
|
246
|
-
return removedNode!.
|
|
221
|
+
return removedNode!.value;
|
|
247
222
|
}
|
|
248
223
|
|
|
249
224
|
/**
|
|
@@ -257,7 +232,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
257
232
|
if (!valueOrNode) return false;
|
|
258
233
|
let value: E;
|
|
259
234
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
260
|
-
value = valueOrNode.
|
|
235
|
+
value = valueOrNode.value;
|
|
261
236
|
} else {
|
|
262
237
|
value = valueOrNode;
|
|
263
238
|
}
|
|
@@ -265,16 +240,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
265
240
|
prev = null;
|
|
266
241
|
|
|
267
242
|
while (current) {
|
|
268
|
-
if (current.
|
|
243
|
+
if (current.value === value) {
|
|
269
244
|
if (prev === null) {
|
|
270
|
-
this.
|
|
245
|
+
this._head = current.next;
|
|
271
246
|
if (current === this.tail) {
|
|
272
|
-
this.
|
|
247
|
+
this._tail = null;
|
|
273
248
|
}
|
|
274
249
|
} else {
|
|
275
250
|
prev.next = current.next;
|
|
276
251
|
if (current === this.tail) {
|
|
277
|
-
this.
|
|
252
|
+
this._tail = prev;
|
|
278
253
|
}
|
|
279
254
|
}
|
|
280
255
|
this._length--;
|
|
@@ -291,23 +266,23 @@ export class SinglyLinkedList<E = any> {
|
|
|
291
266
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
292
267
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
293
268
|
* linked list. It is of type number.
|
|
294
|
-
* @param {E}
|
|
269
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
295
270
|
* specified index.
|
|
296
271
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
297
272
|
* if the index is out of bounds.
|
|
298
273
|
*/
|
|
299
|
-
insertAt(index: number,
|
|
274
|
+
insertAt(index: number, value: E): boolean {
|
|
300
275
|
if (index < 0 || index > this.length) return false;
|
|
301
276
|
if (index === 0) {
|
|
302
|
-
this.unshift(
|
|
277
|
+
this.unshift(value);
|
|
303
278
|
return true;
|
|
304
279
|
}
|
|
305
280
|
if (index === this.length) {
|
|
306
|
-
this.push(
|
|
281
|
+
this.push(value);
|
|
307
282
|
return true;
|
|
308
283
|
}
|
|
309
284
|
|
|
310
|
-
const newNode = new SinglyLinkedListNode(
|
|
285
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
311
286
|
const prevNode = this.getNodeAt(index - 1);
|
|
312
287
|
newNode.next = prevNode!.next;
|
|
313
288
|
prevNode!.next = newNode;
|
|
@@ -341,7 +316,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
341
316
|
const array: E[] = [];
|
|
342
317
|
let current = this.head;
|
|
343
318
|
while (current) {
|
|
344
|
-
array.push(current.
|
|
319
|
+
array.push(current.value);
|
|
345
320
|
current = current.next;
|
|
346
321
|
}
|
|
347
322
|
return array;
|
|
@@ -365,7 +340,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
365
340
|
current = next;
|
|
366
341
|
}
|
|
367
342
|
|
|
368
|
-
[this.
|
|
343
|
+
[this._head, this._tail] = [this.tail!, this.head!];
|
|
369
344
|
}
|
|
370
345
|
|
|
371
346
|
/**
|
|
@@ -375,11 +350,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
375
350
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
376
351
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
377
352
|
*/
|
|
378
|
-
find(callback: (
|
|
353
|
+
find(callback: (value: E) => boolean): E | null {
|
|
379
354
|
let current = this.head;
|
|
380
355
|
while (current) {
|
|
381
|
-
if (callback(current.
|
|
382
|
-
return current.
|
|
356
|
+
if (callback(current.value)) {
|
|
357
|
+
return current.value;
|
|
383
358
|
}
|
|
384
359
|
current = current.next;
|
|
385
360
|
}
|
|
@@ -397,7 +372,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
397
372
|
let current = this.head;
|
|
398
373
|
|
|
399
374
|
while (current) {
|
|
400
|
-
if (current.
|
|
375
|
+
if (current.value === value) {
|
|
401
376
|
return index;
|
|
402
377
|
}
|
|
403
378
|
index++;
|
|
@@ -418,7 +393,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
418
393
|
let current = this.head;
|
|
419
394
|
|
|
420
395
|
while (current) {
|
|
421
|
-
if (current.
|
|
396
|
+
if (current.value === value) {
|
|
422
397
|
return current;
|
|
423
398
|
}
|
|
424
399
|
current = current.next;
|
|
@@ -440,18 +415,18 @@ export class SinglyLinkedList<E = any> {
|
|
|
440
415
|
|
|
441
416
|
let existingValue: E;
|
|
442
417
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
443
|
-
existingValue = existingValueOrNode.
|
|
418
|
+
existingValue = existingValueOrNode.value;
|
|
444
419
|
} else {
|
|
445
420
|
existingValue = existingValueOrNode;
|
|
446
421
|
}
|
|
447
|
-
if (this.head.
|
|
422
|
+
if (this.head.value === existingValue) {
|
|
448
423
|
this.unshift(newValue);
|
|
449
424
|
return true;
|
|
450
425
|
}
|
|
451
426
|
|
|
452
427
|
let current = this.head;
|
|
453
428
|
while (current.next) {
|
|
454
|
-
if (current.next.
|
|
429
|
+
if (current.next.value === existingValue) {
|
|
455
430
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
456
431
|
newNode.next = current.next;
|
|
457
432
|
current.next = newNode;
|
|
@@ -486,7 +461,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
486
461
|
newNode.next = existingNode.next;
|
|
487
462
|
existingNode.next = newNode;
|
|
488
463
|
if (existingNode === this.tail) {
|
|
489
|
-
this.
|
|
464
|
+
this._tail = newNode;
|
|
490
465
|
}
|
|
491
466
|
this._length++;
|
|
492
467
|
return true;
|
|
@@ -505,7 +480,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
505
480
|
let current = this.head;
|
|
506
481
|
|
|
507
482
|
while (current) {
|
|
508
|
-
if (current.
|
|
483
|
+
if (current.value === value) {
|
|
509
484
|
count++;
|
|
510
485
|
}
|
|
511
486
|
current = current.next;
|
|
@@ -516,15 +491,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
516
491
|
|
|
517
492
|
/**
|
|
518
493
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
519
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
494
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
520
495
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
521
496
|
* current node in the linked list.
|
|
522
497
|
*/
|
|
523
|
-
forEach(callback: (
|
|
498
|
+
forEach(callback: (value: E, index: number) => void): void {
|
|
524
499
|
let current = this.head;
|
|
525
500
|
let index = 0;
|
|
526
501
|
while (current) {
|
|
527
|
-
callback(current.
|
|
502
|
+
callback(current.value, index);
|
|
528
503
|
current = current.next;
|
|
529
504
|
index++;
|
|
530
505
|
}
|
|
@@ -538,11 +513,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
538
513
|
* SinglyLinkedList).
|
|
539
514
|
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
540
515
|
*/
|
|
541
|
-
map<U>(callback: (
|
|
516
|
+
map<U>(callback: (value: E) => U): SinglyLinkedList<U> {
|
|
542
517
|
const mappedList = new SinglyLinkedList<U>();
|
|
543
518
|
let current = this.head;
|
|
544
519
|
while (current) {
|
|
545
|
-
mappedList.push(callback(current.
|
|
520
|
+
mappedList.push(callback(current.value));
|
|
546
521
|
current = current.next;
|
|
547
522
|
}
|
|
548
523
|
return mappedList;
|
|
@@ -555,12 +530,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
555
530
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
556
531
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
557
532
|
*/
|
|
558
|
-
filter(callback: (
|
|
533
|
+
filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
|
|
559
534
|
const filteredList = new SinglyLinkedList<E>();
|
|
560
535
|
let current = this.head;
|
|
561
536
|
while (current) {
|
|
562
|
-
if (callback(current.
|
|
563
|
-
filteredList.push(current.
|
|
537
|
+
if (callback(current.value)) {
|
|
538
|
+
filteredList.push(current.value);
|
|
564
539
|
}
|
|
565
540
|
current = current.next;
|
|
566
541
|
}
|
|
@@ -570,18 +545,18 @@ export class SinglyLinkedList<E = any> {
|
|
|
570
545
|
/**
|
|
571
546
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
572
547
|
* single value.
|
|
573
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
548
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
574
549
|
* used to perform a specific operation on each element of the linked list.
|
|
575
550
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
576
551
|
* point for the reduction operation.
|
|
577
552
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
578
553
|
* elements in the linked list.
|
|
579
554
|
*/
|
|
580
|
-
reduce<U>(callback: (accumulator: U,
|
|
555
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
|
|
581
556
|
let accumulator = initialValue;
|
|
582
557
|
let current = this.head;
|
|
583
558
|
while (current) {
|
|
584
|
-
accumulator = callback(accumulator, current.
|
|
559
|
+
accumulator = callback(accumulator, current.value);
|
|
585
560
|
current = current.next;
|
|
586
561
|
}
|
|
587
562
|
return accumulator;
|
|
@@ -590,11 +565,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
590
565
|
/**
|
|
591
566
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
567
|
*/
|
|
593
|
-
*[Symbol.iterator]() {
|
|
568
|
+
* [Symbol.iterator]() {
|
|
594
569
|
let current = this.head;
|
|
595
570
|
|
|
596
571
|
while (current) {
|
|
597
|
-
yield current.
|
|
572
|
+
yield current.value;
|
|
598
573
|
current = current.next;
|
|
599
574
|
}
|
|
600
575
|
}
|
|
@@ -33,46 +33,30 @@ export class SkipList<K, V> {
|
|
|
33
33
|
this._probability = probability;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
protected _head: SkipListNode<K, V>;
|
|
37
37
|
|
|
38
38
|
get head(): SkipListNode<K, V> {
|
|
39
39
|
return this._head;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
this._head = value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private _level: number;
|
|
42
|
+
protected _level: number;
|
|
47
43
|
|
|
48
44
|
get level(): number {
|
|
49
45
|
return this._level;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
this._level = value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _maxLevel: number;
|
|
48
|
+
protected _maxLevel: number;
|
|
57
49
|
|
|
58
50
|
get maxLevel(): number {
|
|
59
51
|
return this._maxLevel;
|
|
60
52
|
}
|
|
61
53
|
|
|
62
|
-
|
|
63
|
-
this._maxLevel = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private _probability: number;
|
|
54
|
+
protected _probability: number;
|
|
67
55
|
|
|
68
56
|
get probability(): number {
|
|
69
57
|
return this._probability;
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
set probability(value: number) {
|
|
73
|
-
this._probability = value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
60
|
/**
|
|
77
61
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
78
62
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -80,7 +64,7 @@ export class SkipList<K, V> {
|
|
|
80
64
|
* List.
|
|
81
65
|
*/
|
|
82
66
|
add(key: K, value: V): void {
|
|
83
|
-
const newNode = new SkipListNode(key, value, this.
|
|
67
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
84
68
|
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
|
|
85
69
|
let current = this.head;
|
|
86
70
|
|
|
@@ -97,7 +81,7 @@ export class SkipList<K, V> {
|
|
|
97
81
|
}
|
|
98
82
|
|
|
99
83
|
if (newNode.forward[0] !== null) {
|
|
100
|
-
this.
|
|
84
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
101
85
|
}
|
|
102
86
|
}
|
|
103
87
|
|
|
@@ -124,6 +108,10 @@ export class SkipList<K, V> {
|
|
|
124
108
|
return undefined;
|
|
125
109
|
}
|
|
126
110
|
|
|
111
|
+
has(key: K): boolean {
|
|
112
|
+
return this.get(key) !== undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
127
115
|
/**
|
|
128
116
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
129
117
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -151,7 +139,7 @@ export class SkipList<K, V> {
|
|
|
151
139
|
update[i].forward[i] = current.forward[i];
|
|
152
140
|
}
|
|
153
141
|
while (this.level > 0 && this.head.forward[this.level - 1] === null) {
|
|
154
|
-
this.
|
|
142
|
+
this._level--;
|
|
155
143
|
}
|
|
156
144
|
return true;
|
|
157
145
|
}
|
|
@@ -160,10 +148,70 @@ export class SkipList<K, V> {
|
|
|
160
148
|
}
|
|
161
149
|
|
|
162
150
|
/**
|
|
163
|
-
*
|
|
151
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
152
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
153
|
+
*/
|
|
154
|
+
getFirst(): V | undefined {
|
|
155
|
+
const firstNode = this.head.forward[0];
|
|
156
|
+
return firstNode ? firstNode.value : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
161
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
162
|
+
*/
|
|
163
|
+
getLast(): V | undefined {
|
|
164
|
+
let current = this.head;
|
|
165
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
166
|
+
while (current.forward[i]) {
|
|
167
|
+
current = current.forward[i];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return current.value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
175
|
+
* @param key - the given key.
|
|
176
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
177
|
+
*/
|
|
178
|
+
higher(key: K): V | undefined {
|
|
179
|
+
let current = this.head;
|
|
180
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
181
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
182
|
+
current = current.forward[i];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const nextNode = current.forward[0];
|
|
186
|
+
return nextNode ? nextNode.value : undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
191
|
+
* @param key - the given key.
|
|
192
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
193
|
+
*/
|
|
194
|
+
lower(key: K): V | undefined {
|
|
195
|
+
let current = this.head;
|
|
196
|
+
let lastLess = null;
|
|
197
|
+
|
|
198
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
199
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
200
|
+
current = current.forward[i];
|
|
201
|
+
}
|
|
202
|
+
if (current.key < key) {
|
|
203
|
+
lastLess = current;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return lastLess ? lastLess.value : undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
164
212
|
* @returns the level, which is a number.
|
|
165
213
|
*/
|
|
166
|
-
|
|
214
|
+
protected _randomLevel(): number {
|
|
167
215
|
let level = 1;
|
|
168
216
|
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
169
217
|
level++;
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
// todo need to be improved
|
|
9
9
|
export class MatrixNTI2D<V = any> {
|
|
10
|
-
|
|
10
|
+
protected readonly _matrix: Array<Array<V>>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {Vector2D} from './vector2d';
|
|
9
9
|
|
|
10
10
|
export class Matrix2D {
|
|
11
|
-
|
|
11
|
+
protected readonly _matrix: number[][];
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
@@ -27,10 +27,10 @@ export class Character {
|
|
|
27
27
|
|
|
28
28
|
export class Navigator<T = number> {
|
|
29
29
|
onMove: (cur: [number, number]) => void;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
protected readonly _matrix: T[][];
|
|
31
|
+
protected readonly _cur: [number, number];
|
|
32
|
+
protected _character: Character;
|
|
33
|
+
protected readonly _VISITED: T;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|