min-heap-typed 1.39.0 → 1.39.2
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 +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/map-graph.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/data-structures/matrix/matrix2d.js +3 -7
- package/dist/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/data-structures/matrix/vector2d.js +0 -1
- package/dist/data-structures/queue/deque.d.ts +20 -20
- package/dist/data-structures/queue/deque.js +22 -22
- package/dist/data-structures/queue/queue.d.ts +3 -3
- package/dist/data-structures/queue/queue.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/helpers.d.ts +1 -4
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
|
@@ -147,11 +147,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
147
147
|
}
|
|
148
148
|
|
|
149
149
|
/**
|
|
150
|
-
* The `
|
|
150
|
+
* The `popLast()` function removes and returns the value of the last node in a doubly linked list.
|
|
151
151
|
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
|
|
152
152
|
* list is empty, it returns null.
|
|
153
153
|
*/
|
|
154
|
-
|
|
154
|
+
popLast(): E | undefined {
|
|
155
155
|
return this.pop();
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -175,11 +175,11 @@ export class DoublyLinkedList<E = any> {
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* The `
|
|
178
|
+
* The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
|
|
179
179
|
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
|
|
180
180
|
* list.
|
|
181
181
|
*/
|
|
182
|
-
|
|
182
|
+
popFirst(): E | undefined {
|
|
183
183
|
return this.shift();
|
|
184
184
|
}
|
|
185
185
|
|
|
@@ -211,18 +211,18 @@ export class DoublyLinkedList<E = any> {
|
|
|
211
211
|
}
|
|
212
212
|
|
|
213
213
|
/**
|
|
214
|
-
* The `
|
|
215
|
-
* @returns The method `
|
|
214
|
+
* The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
|
|
215
|
+
* @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
|
|
216
216
|
*/
|
|
217
|
-
|
|
217
|
+
getFirst(): E | undefined {
|
|
218
218
|
return this.head?.val;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
|
-
* The `
|
|
223
|
-
* @returns The method `
|
|
222
|
+
* The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
|
|
223
|
+
* @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
|
|
224
224
|
*/
|
|
225
|
-
|
|
225
|
+
getLast(): E | undefined {
|
|
226
226
|
return this.tail?.val;
|
|
227
227
|
}
|
|
228
228
|
|
|
@@ -266,7 +266,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
266
266
|
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
|
|
267
267
|
* is found in the linked list. If no such node is found, it returns `null`.
|
|
268
268
|
*/
|
|
269
|
-
|
|
269
|
+
getNode(val: E | null): DoublyLinkedListNode<E> | null {
|
|
270
270
|
let current = this.head;
|
|
271
271
|
|
|
272
272
|
while (current) {
|
|
@@ -310,6 +310,43 @@ export class DoublyLinkedList<E = any> {
|
|
|
310
310
|
return true;
|
|
311
311
|
}
|
|
312
312
|
|
|
313
|
+
/**
|
|
314
|
+
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
|
|
315
|
+
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
316
|
+
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
317
|
+
* itself.
|
|
318
|
+
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
319
|
+
* list.
|
|
320
|
+
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
321
|
+
* insertion fails.
|
|
322
|
+
*/
|
|
323
|
+
insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
|
|
324
|
+
let existingNode;
|
|
325
|
+
|
|
326
|
+
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
327
|
+
existingNode = existingValueOrNode;
|
|
328
|
+
} else {
|
|
329
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (existingNode) {
|
|
333
|
+
const newNode = new DoublyLinkedListNode(newValue);
|
|
334
|
+
newNode.prev = existingNode.prev;
|
|
335
|
+
if (existingNode.prev) {
|
|
336
|
+
existingNode.prev.next = newNode;
|
|
337
|
+
}
|
|
338
|
+
newNode.next = existingNode;
|
|
339
|
+
existingNode.prev = newNode;
|
|
340
|
+
if (existingNode === this.head) {
|
|
341
|
+
this.head = newNode;
|
|
342
|
+
}
|
|
343
|
+
this._length++;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
313
350
|
/**
|
|
314
351
|
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
|
|
315
352
|
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
|
|
@@ -331,9 +368,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
331
368
|
return removedNode!.val;
|
|
332
369
|
}
|
|
333
370
|
|
|
334
|
-
delete(valOrNode: E): boolean;
|
|
335
|
-
delete(valOrNode: DoublyLinkedListNode<E>): boolean;
|
|
336
|
-
|
|
337
371
|
/**
|
|
338
372
|
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
|
|
339
373
|
* @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
|
|
@@ -347,7 +381,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
347
381
|
if (valOrNode instanceof DoublyLinkedListNode) {
|
|
348
382
|
node = valOrNode;
|
|
349
383
|
} else {
|
|
350
|
-
node = this.
|
|
384
|
+
node = this.getNode(valOrNode);
|
|
351
385
|
}
|
|
352
386
|
|
|
353
387
|
if (node) {
|
|
@@ -437,14 +471,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
437
471
|
}
|
|
438
472
|
|
|
439
473
|
/**
|
|
440
|
-
* The `
|
|
474
|
+
* The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
|
|
441
475
|
* value that satisfies the given callback function, or null if no value satisfies the callback.
|
|
442
476
|
* @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
|
|
443
477
|
* function is used to determine whether a given value satisfies a certain condition.
|
|
444
|
-
* @returns The method `
|
|
478
|
+
* @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
|
|
445
479
|
* the callback function. If no value satisfies the condition, it returns `null`.
|
|
446
480
|
*/
|
|
447
|
-
|
|
481
|
+
findBackward(callback: (val: E) => boolean): E | null {
|
|
448
482
|
let current = this.tail;
|
|
449
483
|
while (current) {
|
|
450
484
|
if (callback(current.val)) {
|
|
@@ -456,10 +490,10 @@ export class DoublyLinkedList<E = any> {
|
|
|
456
490
|
}
|
|
457
491
|
|
|
458
492
|
/**
|
|
459
|
-
* The `
|
|
460
|
-
* @returns The `
|
|
493
|
+
* The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
|
|
494
|
+
* @returns The `toArrayBackward()` function returns an array of type `E[]`.
|
|
461
495
|
*/
|
|
462
|
-
|
|
496
|
+
toArrayBackward(): E[] {
|
|
463
497
|
const array: E[] = [];
|
|
464
498
|
let current = this.tail;
|
|
465
499
|
while (current) {
|
|
@@ -555,9 +589,6 @@ export class DoublyLinkedList<E = any> {
|
|
|
555
589
|
return accumulator;
|
|
556
590
|
}
|
|
557
591
|
|
|
558
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
559
|
-
insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
|
|
560
|
-
|
|
561
592
|
/**
|
|
562
593
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
|
|
563
594
|
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
@@ -573,7 +604,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
573
604
|
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
574
605
|
existingNode = existingValueOrNode;
|
|
575
606
|
} else {
|
|
576
|
-
existingNode = this.
|
|
607
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
577
608
|
}
|
|
578
609
|
|
|
579
610
|
if (existingNode) {
|
|
@@ -595,39 +626,14 @@ export class DoublyLinkedList<E = any> {
|
|
|
595
626
|
}
|
|
596
627
|
|
|
597
628
|
/**
|
|
598
|
-
* The
|
|
599
|
-
* @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
|
|
600
|
-
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
|
|
601
|
-
* itself.
|
|
602
|
-
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
|
|
603
|
-
* list.
|
|
604
|
-
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
|
|
605
|
-
* insertion fails.
|
|
629
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
606
630
|
*/
|
|
607
|
-
|
|
608
|
-
let
|
|
609
|
-
|
|
610
|
-
if (existingValueOrNode instanceof DoublyLinkedListNode) {
|
|
611
|
-
existingNode = existingValueOrNode;
|
|
612
|
-
} else {
|
|
613
|
-
existingNode = this.findNode(existingValueOrNode);
|
|
614
|
-
}
|
|
631
|
+
* [Symbol.iterator]() {
|
|
632
|
+
let current = this.head;
|
|
615
633
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
if (existingNode.prev) {
|
|
620
|
-
existingNode.prev.next = newNode;
|
|
621
|
-
}
|
|
622
|
-
newNode.next = existingNode;
|
|
623
|
-
existingNode.prev = newNode;
|
|
624
|
-
if (existingNode === this.head) {
|
|
625
|
-
this.head = newNode;
|
|
626
|
-
}
|
|
627
|
-
this._length++;
|
|
628
|
-
return true;
|
|
634
|
+
while (current) {
|
|
635
|
+
yield current.val;
|
|
636
|
+
current = current.next;
|
|
629
637
|
}
|
|
630
|
-
|
|
631
|
-
return false;
|
|
632
638
|
}
|
|
633
639
|
}
|
|
@@ -87,17 +87,13 @@ export class SinglyLinkedList<E = any> {
|
|
|
87
87
|
return singlyLinkedList;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
getLength(): number {
|
|
91
|
-
return this._length;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
90
|
/**
|
|
95
|
-
* The `push` function adds a new node with the given
|
|
96
|
-
* @param {E}
|
|
91
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
92
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
97
93
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
98
94
|
*/
|
|
99
|
-
push(
|
|
100
|
-
const newNode = new SinglyLinkedListNode(
|
|
95
|
+
push(val: E): void {
|
|
96
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
101
97
|
if (!this.head) {
|
|
102
98
|
this.head = newNode;
|
|
103
99
|
this.tail = newNode;
|
|
@@ -108,6 +104,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
108
104
|
this._length++;
|
|
109
105
|
}
|
|
110
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
109
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
110
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
111
|
+
*/
|
|
112
|
+
addLast(val: E): void {
|
|
113
|
+
this.push(val);
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
/**
|
|
112
117
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
113
118
|
* pointers accordingly.
|
|
@@ -135,6 +140,16 @@ export class SinglyLinkedList<E = any> {
|
|
|
135
140
|
return val;
|
|
136
141
|
}
|
|
137
142
|
|
|
143
|
+
/**
|
|
144
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
145
|
+
* pointers accordingly.
|
|
146
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
147
|
+
* the linked list is empty, it returns `null`.
|
|
148
|
+
*/
|
|
149
|
+
popLast(): E | undefined {
|
|
150
|
+
return this.pop();
|
|
151
|
+
}
|
|
152
|
+
|
|
138
153
|
/**
|
|
139
154
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
140
155
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -147,6 +162,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
147
162
|
return removedNode.val;
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
/**
|
|
166
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
167
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
168
|
+
*/
|
|
169
|
+
popFirst(): E | undefined {
|
|
170
|
+
return this.shift();
|
|
171
|
+
}
|
|
172
|
+
|
|
150
173
|
/**
|
|
151
174
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
152
175
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -164,6 +187,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
164
187
|
this._length++;
|
|
165
188
|
}
|
|
166
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
192
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
193
|
+
* linked list.
|
|
194
|
+
*/
|
|
195
|
+
addFirst(val: E): void {
|
|
196
|
+
this.unshift(val);
|
|
197
|
+
}
|
|
198
|
+
|
|
167
199
|
/**
|
|
168
200
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
169
201
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -382,7 +414,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
382
414
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
383
415
|
* the specified value is found, the function returns `null`.
|
|
384
416
|
*/
|
|
385
|
-
|
|
417
|
+
getNode(value: E): SinglyLinkedListNode<E> | null {
|
|
386
418
|
let current = this.head;
|
|
387
419
|
|
|
388
420
|
while (current) {
|
|
@@ -432,9 +464,6 @@ export class SinglyLinkedList<E = any> {
|
|
|
432
464
|
return false;
|
|
433
465
|
}
|
|
434
466
|
|
|
435
|
-
insertAfter(existingValueOrNode: E, newValue: E): boolean;
|
|
436
|
-
insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
|
|
437
|
-
|
|
438
467
|
/**
|
|
439
468
|
* The `insertAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
440
469
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
@@ -449,7 +478,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
449
478
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
450
479
|
existingNode = existingValueOrNode;
|
|
451
480
|
} else {
|
|
452
|
-
existingNode = this.
|
|
481
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
453
482
|
}
|
|
454
483
|
|
|
455
484
|
if (existingNode) {
|
|
@@ -485,6 +514,82 @@ export class SinglyLinkedList<E = any> {
|
|
|
485
514
|
return count;
|
|
486
515
|
}
|
|
487
516
|
|
|
517
|
+
/**
|
|
518
|
+
* 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: val and index. The val argument
|
|
520
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
521
|
+
* current node in the linked list.
|
|
522
|
+
*/
|
|
523
|
+
forEach(callback: (val: E, index: number) => void): void {
|
|
524
|
+
let current = this.head;
|
|
525
|
+
let index = 0;
|
|
526
|
+
while (current) {
|
|
527
|
+
callback(current.val, index);
|
|
528
|
+
current = current.next;
|
|
529
|
+
index++;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
535
|
+
* SinglyLinkedList with the transformed values.
|
|
536
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
537
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
538
|
+
* SinglyLinkedList).
|
|
539
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
540
|
+
*/
|
|
541
|
+
map<U>(callback: (val: E) => U): SinglyLinkedList<U> {
|
|
542
|
+
const mappedList = new SinglyLinkedList<U>();
|
|
543
|
+
let current = this.head;
|
|
544
|
+
while (current) {
|
|
545
|
+
mappedList.push(callback(current.val));
|
|
546
|
+
current = current.next;
|
|
547
|
+
}
|
|
548
|
+
return mappedList;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
553
|
+
* elements that satisfy the given callback function.
|
|
554
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
555
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
556
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
557
|
+
*/
|
|
558
|
+
filter(callback: (val: E) => boolean): SinglyLinkedList<E> {
|
|
559
|
+
const filteredList = new SinglyLinkedList<E>();
|
|
560
|
+
let current = this.head;
|
|
561
|
+
while (current) {
|
|
562
|
+
if (callback(current.val)) {
|
|
563
|
+
filteredList.push(current.val);
|
|
564
|
+
}
|
|
565
|
+
current = current.next;
|
|
566
|
+
}
|
|
567
|
+
return filteredList;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
572
|
+
* single value.
|
|
573
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
574
|
+
* used to perform a specific operation on each element of the linked list.
|
|
575
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
576
|
+
* point for the reduction operation.
|
|
577
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
578
|
+
* elements in the linked list.
|
|
579
|
+
*/
|
|
580
|
+
reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U {
|
|
581
|
+
let accumulator = initialValue;
|
|
582
|
+
let current = this.head;
|
|
583
|
+
while (current) {
|
|
584
|
+
accumulator = callback(accumulator, current.val);
|
|
585
|
+
current = current.next;
|
|
586
|
+
}
|
|
587
|
+
return accumulator;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
|
+
*/
|
|
488
593
|
* [Symbol.iterator]() {
|
|
489
594
|
let current = this.head;
|
|
490
595
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import Vector2D from './vector2d';
|
|
8
|
+
import {Vector2D} from './vector2d';
|
|
9
9
|
|
|
10
10
|
export class Matrix2D {
|
|
11
11
|
private readonly _matrix: number[][];
|
|
@@ -209,5 +209,3 @@ export class Matrix2D {
|
|
|
209
209
|
return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
|
|
213
|
-
export default Matrix2D;
|
|
@@ -96,12 +96,12 @@ export class ObjectDeque<E = number> {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
99
|
-
* The function `
|
|
99
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
100
100
|
* @returns The value of the first element in the data structure.
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
popFirst() {
|
|
103
103
|
if (!this._size) return;
|
|
104
|
-
const value = this.
|
|
104
|
+
const value = this.getFirst();
|
|
105
105
|
delete this._nodes[this._first];
|
|
106
106
|
this._first++;
|
|
107
107
|
this._size--;
|
|
@@ -109,20 +109,20 @@ export class ObjectDeque<E = number> {
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
|
-
* The `
|
|
112
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
113
113
|
* @returns The element at the first position of the `_nodes` array.
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
getFirst() {
|
|
116
116
|
if (this._size) return this._nodes[this._first];
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
|
-
* The `
|
|
120
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
121
121
|
* @returns The value that was removed from the data structure.
|
|
122
122
|
*/
|
|
123
|
-
|
|
123
|
+
popLast() {
|
|
124
124
|
if (!this._size) return;
|
|
125
|
-
const value = this.
|
|
125
|
+
const value = this.getLast();
|
|
126
126
|
delete this._nodes[this._last];
|
|
127
127
|
this._last--;
|
|
128
128
|
this._size--;
|
|
@@ -131,10 +131,10 @@ export class ObjectDeque<E = number> {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
/**
|
|
134
|
-
* The `
|
|
134
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
135
135
|
* @returns The last element in the array "_nodes" is being returned.
|
|
136
136
|
*/
|
|
137
|
-
|
|
137
|
+
getLast() {
|
|
138
138
|
if (this._size) return this._nodes[this._last];
|
|
139
139
|
}
|
|
140
140
|
|
|
@@ -189,19 +189,19 @@ export class ArrayDeque<E> {
|
|
|
189
189
|
}
|
|
190
190
|
|
|
191
191
|
/**
|
|
192
|
-
* The function "
|
|
193
|
-
* @returns The method `
|
|
192
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
193
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
194
194
|
*/
|
|
195
|
-
|
|
195
|
+
popLast(): E | null {
|
|
196
196
|
return this._nodes.pop() ?? null;
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
/**
|
|
200
|
-
* The `
|
|
201
|
-
* @returns The `
|
|
200
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
201
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
202
202
|
* empty.
|
|
203
203
|
*/
|
|
204
|
-
|
|
204
|
+
popFirst(): E | null {
|
|
205
205
|
return this._nodes.shift() ?? null;
|
|
206
206
|
}
|
|
207
207
|
|
|
@@ -220,19 +220,19 @@ export class ArrayDeque<E> {
|
|
|
220
220
|
}
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
|
-
* The `
|
|
224
|
-
* @returns The function `
|
|
223
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
224
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
225
225
|
* empty, it will return `null`.
|
|
226
226
|
*/
|
|
227
|
-
|
|
227
|
+
getFirst(): E | null {
|
|
228
228
|
return this._nodes[0] ?? null;
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
/**
|
|
232
|
-
* The `
|
|
233
|
-
* @returns The method `
|
|
232
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
233
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
234
234
|
*/
|
|
235
|
-
|
|
235
|
+
getLast(): E | null {
|
|
236
236
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
237
237
|
}
|
|
238
238
|
|
|
@@ -123,11 +123,11 @@ export class Queue<E = any> {
|
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
|
-
* The `
|
|
127
|
-
* @returns The method `
|
|
126
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
127
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
128
128
|
* array is empty, it returns `null`.
|
|
129
129
|
*/
|
|
130
|
-
|
|
130
|
+
getLast(): E | undefined {
|
|
131
131
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
132
132
|
}
|
|
133
133
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
5
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
delete<C extends
|
|
9
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
|
@@ -24,10 +24,6 @@ export enum FamilyPosition {
|
|
|
24
24
|
|
|
25
25
|
export type BinaryTreeNodeKey = number;
|
|
26
26
|
|
|
27
|
-
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
28
|
-
|
|
29
|
-
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
30
|
-
|
|
31
27
|
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
|
|
32
28
|
|
|
33
29
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
package/src/types/helpers.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import {BinaryTreeNodeKey} from './data-structures';
|
|
2
|
-
|
|
3
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
4
2
|
|
|
5
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
6
4
|
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
10
|
-
|
|
11
|
-
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
5
|
+
export type OneParamCallback<N, D = any> = (node: N) => D;
|
|
12
6
|
|
|
13
7
|
export enum CP {
|
|
14
8
|
lt = 'lt',
|