graph-typed 1.49.0 → 1.49.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/base/iterable-base.d.ts +11 -0
- package/dist/data-structures/base/iterable-base.js +21 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +10 -12
- package/dist/data-structures/binary-tree/avl-tree.js +3 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +58 -58
- package/dist/data-structures/binary-tree/binary-tree.js +6 -6
- package/dist/data-structures/binary-tree/bst.d.ts +15 -15
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -11
- package/dist/data-structures/binary-tree/rb-tree.js +5 -6
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +3 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +22 -10
- package/dist/data-structures/hash/hash-map.js +28 -16
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +20 -38
- package/dist/data-structures/heap/heap.js +22 -42
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +95 -95
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -136
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -23
- package/dist/data-structures/linked-list/singly-linked-list.js +42 -49
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +76 -80
- package/dist/data-structures/queue/deque.js +106 -122
- package/dist/data-structures/queue/queue.d.ts +30 -16
- package/dist/data-structures/queue/queue.js +31 -24
- package/dist/data-structures/stack/stack.d.ts +17 -8
- package/dist/data-structures/stack/stack.js +9 -9
- package/dist/data-structures/trie/trie.d.ts +14 -5
- package/dist/data-structures/trie/trie.js +13 -13
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +24 -0
- package/src/data-structures/binary-tree/avl-tree.ts +14 -14
- package/src/data-structures/binary-tree/binary-tree.ts +74 -77
- package/src/data-structures/binary-tree/bst.ts +18 -17
- package/src/data-structures/binary-tree/rb-tree.ts +17 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +22 -20
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +40 -24
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +33 -60
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +147 -145
- package/src/data-structures/linked-list/singly-linked-list.ts +52 -52
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- 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 +9 -2
- package/src/data-structures/queue/deque.ts +129 -144
- package/src/data-structures/queue/queue.ts +37 -26
- package/src/data-structures/stack/stack.ts +20 -14
- package/src/data-structures/trie/trie.ts +18 -13
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { IterableElementBase } from "../base";
|
|
2
|
-
import { ElementCallback } from "../../types";
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* data-structure-typed
|
|
6
3
|
*
|
|
@@ -8,6 +5,9 @@ import { ElementCallback } from "../../types";
|
|
|
8
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
6
|
* @license MIT License
|
|
10
7
|
*/
|
|
8
|
+
import type { ElementCallback } from "../../types";
|
|
9
|
+
import { IterableElementBase } from "../base";
|
|
10
|
+
|
|
11
11
|
export class SinglyLinkedListNode<E = any> {
|
|
12
12
|
value: E;
|
|
13
13
|
next: SinglyLinkedListNode<E> | undefined;
|
|
@@ -31,7 +31,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
31
31
|
super();
|
|
32
32
|
this._head = undefined;
|
|
33
33
|
this._tail = undefined;
|
|
34
|
-
this.
|
|
34
|
+
this._size = 0;
|
|
35
35
|
if (elements) {
|
|
36
36
|
for (const el of elements)
|
|
37
37
|
this.push(el);
|
|
@@ -50,10 +50,10 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
50
50
|
return this._tail;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
protected
|
|
53
|
+
protected _size: number;
|
|
54
54
|
|
|
55
|
-
get
|
|
56
|
-
return this.
|
|
55
|
+
get size(): number {
|
|
56
|
+
return this._size;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -91,7 +91,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
91
91
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
92
92
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
93
93
|
*/
|
|
94
|
-
push(value: E):
|
|
94
|
+
push(value: E): boolean {
|
|
95
95
|
const newNode = new SinglyLinkedListNode(value);
|
|
96
96
|
if (!this.head) {
|
|
97
97
|
this._head = newNode;
|
|
@@ -100,7 +100,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
100
100
|
this.tail!.next = newNode;
|
|
101
101
|
this._tail = newNode;
|
|
102
102
|
}
|
|
103
|
-
this.
|
|
103
|
+
this._size++;
|
|
104
|
+
return true;
|
|
104
105
|
}
|
|
105
106
|
|
|
106
107
|
/**
|
|
@@ -116,8 +117,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
116
117
|
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
117
118
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
118
119
|
*/
|
|
119
|
-
addLast(value: E):
|
|
120
|
-
this.push(value);
|
|
120
|
+
addLast(value: E): boolean {
|
|
121
|
+
return this.push(value);
|
|
121
122
|
}
|
|
122
123
|
|
|
123
124
|
/**
|
|
@@ -140,7 +141,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
140
141
|
const value = this.head.value;
|
|
141
142
|
this._head = undefined;
|
|
142
143
|
this._tail = undefined;
|
|
143
|
-
this.
|
|
144
|
+
this._size--;
|
|
144
145
|
return value;
|
|
145
146
|
}
|
|
146
147
|
|
|
@@ -151,7 +152,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
151
152
|
const value = this.tail!.value;
|
|
152
153
|
current.next = undefined;
|
|
153
154
|
this._tail = current;
|
|
154
|
-
this.
|
|
155
|
+
this._size--;
|
|
155
156
|
return value;
|
|
156
157
|
}
|
|
157
158
|
|
|
@@ -189,7 +190,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
189
190
|
if (!this.head) return undefined;
|
|
190
191
|
const removedNode = this.head;
|
|
191
192
|
this._head = this.head.next;
|
|
192
|
-
this.
|
|
193
|
+
this._size--;
|
|
193
194
|
return removedNode.value;
|
|
194
195
|
}
|
|
195
196
|
|
|
@@ -222,7 +223,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
222
223
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
223
224
|
* linked list.
|
|
224
225
|
*/
|
|
225
|
-
unshift(value: E):
|
|
226
|
+
unshift(value: E): boolean {
|
|
226
227
|
const newNode = new SinglyLinkedListNode(value);
|
|
227
228
|
if (!this.head) {
|
|
228
229
|
this._head = newNode;
|
|
@@ -231,7 +232,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
231
232
|
newNode.next = this.head;
|
|
232
233
|
this._head = newNode;
|
|
233
234
|
}
|
|
234
|
-
this.
|
|
235
|
+
this._size++;
|
|
236
|
+
return true;
|
|
235
237
|
}
|
|
236
238
|
|
|
237
239
|
/**
|
|
@@ -247,8 +249,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
247
249
|
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
248
250
|
* linked list.
|
|
249
251
|
*/
|
|
250
|
-
addFirst(value: E):
|
|
251
|
-
this.unshift(value);
|
|
252
|
+
addFirst(value: E): boolean {
|
|
253
|
+
return this.unshift(value);
|
|
252
254
|
}
|
|
253
255
|
|
|
254
256
|
/**
|
|
@@ -267,7 +269,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
267
269
|
* `undefined` if the index is out of bounds.
|
|
268
270
|
*/
|
|
269
271
|
getAt(index: number): E | undefined {
|
|
270
|
-
if (index < 0 || index >= this.
|
|
272
|
+
if (index < 0 || index >= this.size) return undefined;
|
|
271
273
|
let current = this.head;
|
|
272
274
|
for (let i = 0; i < index; i++) {
|
|
273
275
|
current = current!.next;
|
|
@@ -313,16 +315,22 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
313
315
|
* @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
|
|
314
316
|
* bounds.
|
|
315
317
|
*/
|
|
316
|
-
deleteAt(index: number):
|
|
317
|
-
if (index < 0 || index >= this.
|
|
318
|
-
if (index === 0)
|
|
319
|
-
|
|
318
|
+
deleteAt(index: number): boolean {
|
|
319
|
+
if (index < 0 || index >= this.size) return false;
|
|
320
|
+
if (index === 0) {
|
|
321
|
+
this.shift();
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
if (index === this.size - 1) {
|
|
325
|
+
this.pop();
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
320
328
|
|
|
321
329
|
const prevNode = this.getNodeAt(index - 1);
|
|
322
330
|
const removedNode = prevNode!.next;
|
|
323
331
|
prevNode!.next = removedNode!.next;
|
|
324
|
-
this.
|
|
325
|
-
return
|
|
332
|
+
this._size--;
|
|
333
|
+
return true;
|
|
326
334
|
}
|
|
327
335
|
|
|
328
336
|
/**
|
|
@@ -340,7 +348,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
340
348
|
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
|
|
341
349
|
* successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
|
|
342
350
|
*/
|
|
343
|
-
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined
|
|
351
|
+
delete(valueOrNode: E | SinglyLinkedListNode<E> | undefined ): boolean {
|
|
344
352
|
if (!valueOrNode) return false;
|
|
345
353
|
let value: E;
|
|
346
354
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -364,7 +372,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
364
372
|
this._tail = prev;
|
|
365
373
|
}
|
|
366
374
|
}
|
|
367
|
-
this.
|
|
375
|
+
this._size--;
|
|
368
376
|
return true;
|
|
369
377
|
}
|
|
370
378
|
prev = current;
|
|
@@ -383,7 +391,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
383
391
|
* Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node.
|
|
384
392
|
* Space Complexity: O(1) - Constant space.
|
|
385
393
|
*
|
|
386
|
-
* The `
|
|
394
|
+
* The `addAt` function inserts a value at a specified index in a singly linked list.
|
|
387
395
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
388
396
|
* linked list. It is of type number.
|
|
389
397
|
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
@@ -391,13 +399,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
391
399
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
392
400
|
* if the index is out of bounds.
|
|
393
401
|
*/
|
|
394
|
-
|
|
395
|
-
if (index < 0 || index > this.
|
|
402
|
+
addAt(index: number, value: E): boolean {
|
|
403
|
+
if (index < 0 || index > this.size) return false;
|
|
396
404
|
if (index === 0) {
|
|
397
405
|
this.unshift(value);
|
|
398
406
|
return true;
|
|
399
407
|
}
|
|
400
|
-
if (index === this.
|
|
408
|
+
if (index === this.size) {
|
|
401
409
|
this.push(value);
|
|
402
410
|
return true;
|
|
403
411
|
}
|
|
@@ -406,7 +414,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
406
414
|
const prevNode = this.getNodeAt(index - 1);
|
|
407
415
|
newNode.next = prevNode!.next;
|
|
408
416
|
prevNode!.next = newNode;
|
|
409
|
-
this.
|
|
417
|
+
this._size++;
|
|
410
418
|
return true;
|
|
411
419
|
}
|
|
412
420
|
|
|
@@ -416,7 +424,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
416
424
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
417
425
|
*/
|
|
418
426
|
isEmpty(): boolean {
|
|
419
|
-
return this.
|
|
427
|
+
return this.size === 0;
|
|
420
428
|
}
|
|
421
429
|
|
|
422
430
|
/**
|
|
@@ -425,7 +433,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
425
433
|
clear(): void {
|
|
426
434
|
this._head = undefined;
|
|
427
435
|
this._tail = undefined;
|
|
428
|
-
this.
|
|
436
|
+
this._size = 0;
|
|
429
437
|
}
|
|
430
438
|
|
|
431
439
|
/**
|
|
@@ -462,8 +470,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
462
470
|
* The `reverse` function reverses the order of the nodes in a singly linked list.
|
|
463
471
|
* @returns The reverse() method does not return anything. It has a return type of void.
|
|
464
472
|
*/
|
|
465
|
-
reverse():
|
|
466
|
-
if (!this.head || this.head === this.tail) return;
|
|
473
|
+
reverse(): this {
|
|
474
|
+
if (!this.head || this.head === this.tail) return this;
|
|
467
475
|
|
|
468
476
|
let prev: SinglyLinkedListNode<E> | undefined = undefined;
|
|
469
477
|
let current: SinglyLinkedListNode<E> | undefined = this.head;
|
|
@@ -477,6 +485,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
477
485
|
}
|
|
478
486
|
|
|
479
487
|
[this._head, this._tail] = [this.tail!, this.head!];
|
|
488
|
+
return this;
|
|
480
489
|
}
|
|
481
490
|
|
|
482
491
|
/**
|
|
@@ -571,14 +580,14 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
571
580
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
572
581
|
* Space Complexity: O(1) - Constant space.
|
|
573
582
|
*
|
|
574
|
-
* The `
|
|
583
|
+
* The `addBefore` function inserts a new value before an existing value in a singly linked list.
|
|
575
584
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
|
|
576
585
|
* new value before. It can be either the value itself or a node containing the value in the linked list.
|
|
577
586
|
* @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
|
|
578
|
-
* @returns The method `
|
|
587
|
+
* @returns The method `addBefore` returns a boolean value. It returns `true` if the new value was successfully
|
|
579
588
|
* inserted before the existing value, and `false` otherwise.
|
|
580
589
|
*/
|
|
581
|
-
|
|
590
|
+
addBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
|
|
582
591
|
if (!this.head) return false;
|
|
583
592
|
|
|
584
593
|
let existingValue: E;
|
|
@@ -598,7 +607,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
598
607
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
599
608
|
newNode.next = current.next;
|
|
600
609
|
current.next = newNode;
|
|
601
|
-
this.
|
|
610
|
+
this._size++;
|
|
602
611
|
return true;
|
|
603
612
|
}
|
|
604
613
|
current = current.next;
|
|
@@ -616,14 +625,14 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
616
625
|
* Time Complexity: O(n) - Linear time, where n is the length of the list, as it needs to reverse the pointers of each node.
|
|
617
626
|
* Space Complexity: O(1) - Constant space.
|
|
618
627
|
*
|
|
619
|
-
* The `
|
|
628
|
+
* The `addAfter` function inserts a new node with a given value after an existing node in a singly linked list.
|
|
620
629
|
* @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node in the linked list after which
|
|
621
630
|
* the new value will be inserted. It can be either the value of the existing node or the existing node itself.
|
|
622
631
|
* @param {E} newValue - The value that you want to insert into the linked list after the existing value or node.
|
|
623
632
|
* @returns The method returns a boolean value. It returns true if the new value was successfully inserted after the
|
|
624
633
|
* existing value or node, and false if the existing value or node was not found in the linked list.
|
|
625
634
|
*/
|
|
626
|
-
|
|
635
|
+
addAfter(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean {
|
|
627
636
|
let existingNode: E | SinglyLinkedListNode<E> | undefined;
|
|
628
637
|
|
|
629
638
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
@@ -639,7 +648,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
639
648
|
if (existingNode === this.tail) {
|
|
640
649
|
this._tail = newNode;
|
|
641
650
|
}
|
|
642
|
-
this.
|
|
651
|
+
this._size++;
|
|
643
652
|
return true;
|
|
644
653
|
}
|
|
645
654
|
|
|
@@ -737,15 +746,6 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
737
746
|
return mappedList;
|
|
738
747
|
}
|
|
739
748
|
|
|
740
|
-
/**
|
|
741
|
-
* Time Complexity: O(n), where n is the number of elements in the linked list.
|
|
742
|
-
* Space Complexity: O(n)
|
|
743
|
-
*/
|
|
744
|
-
|
|
745
|
-
print(): void {
|
|
746
|
-
console.log([...this]);
|
|
747
|
-
}
|
|
748
|
-
|
|
749
749
|
protected* _getIterator(): IterableIterator<E> {
|
|
750
750
|
let current = this.head;
|
|
751
751
|
|
|
@@ -193,7 +193,7 @@ export class SkipList<K, V> {
|
|
|
193
193
|
* Get the value of the first element (the smallest element) in the Skip List.
|
|
194
194
|
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
195
195
|
*/
|
|
196
|
-
|
|
196
|
+
get first(): V | undefined {
|
|
197
197
|
const firstNode = this.head.forward[0];
|
|
198
198
|
return firstNode ? firstNode.value : undefined;
|
|
199
199
|
}
|
|
@@ -210,7 +210,7 @@ export class SkipList<K, V> {
|
|
|
210
210
|
* Get the value of the last element (the largest element) in the Skip List.
|
|
211
211
|
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
212
212
|
*/
|
|
213
|
-
|
|
213
|
+
get last(): V | undefined {
|
|
214
214
|
let current = this.head;
|
|
215
215
|
for (let i = this.level - 1; i >= 0; i--) {
|
|
216
216
|
while (current.forward[i]) {
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { PriorityQueue } from './priority-queue';
|
|
9
8
|
import type { PriorityQueueOptions } from '../../types';
|
|
9
|
+
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { PriorityQueue } from './priority-queue';
|
|
9
8
|
import type { PriorityQueueOptions } from '../../types';
|
|
9
|
+
import { PriorityQueue } from './priority-queue';
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(elements?: Iterable<E>,
|
|
@@ -5,10 +5,17 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
9
9
|
import { Heap } from '../heap';
|
|
10
|
-
import { PriorityQueueOptions } from '../../types';
|
|
11
10
|
|
|
11
|
+
/**
|
|
12
|
+
* 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
|
|
13
|
+
* 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
|
|
14
|
+
* 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
|
|
15
|
+
* 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
|
|
16
|
+
* 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
|
|
17
|
+
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
|
|
18
|
+
*/
|
|
12
19
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
20
|
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
|
|
14
21
|
super(elements, options);
|