graph-typed 1.47.5 → 1.47.6
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 +8 -8
- package/dist/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/data-structures/binary-tree/bst.js +56 -40
- package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/heap/heap.d.ts +19 -21
- package/dist/data-structures/heap/heap.js +52 -34
- package/dist/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/data-structures/heap/max-heap.js +2 -2
- package/dist/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/data-structures/heap/min-heap.js +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/data-structures/queue/deque.d.ts +1 -0
- package/dist/data-structures/queue/deque.js +3 -0
- package/dist/data-structures/queue/queue.d.ts +1 -0
- package/dist/data-structures/queue/queue.js +3 -0
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -2
- package/dist/interfaces/binary-tree.d.ts +3 -1
- package/dist/types/common.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +27 -17
- package/src/data-structures/binary-tree/binary-tree.ts +114 -97
- package/src/data-structures/binary-tree/bst.ts +67 -47
- package/src/data-structures/binary-tree/rb-tree.ts +34 -20
- package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/interfaces/binary-tree.ts +13 -1
- package/src/types/common.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -5,15 +5,12 @@
|
|
|
5
5
|
* @license MIT License
|
|
6
6
|
*/
|
|
7
7
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
8
|
+
import { HeapOptions } from "../../types";
|
|
8
9
|
export declare class Heap<E = any> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
elements?: E[];
|
|
12
|
-
});
|
|
10
|
+
options: HeapOptions<E>;
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
13
12
|
protected _elements: E[];
|
|
14
13
|
get elements(): E[];
|
|
15
|
-
protected _comparator: Comparator<E>;
|
|
16
|
-
get comparator(): Comparator<E>;
|
|
17
14
|
/**
|
|
18
15
|
* Get the size (number of elements) of the heap.
|
|
19
16
|
*/
|
|
@@ -26,10 +23,10 @@ export declare class Heap<E = any> {
|
|
|
26
23
|
/**
|
|
27
24
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
28
25
|
* @returns A new Heap instance.
|
|
26
|
+
* @param elements
|
|
29
27
|
* @param options
|
|
30
28
|
*/
|
|
31
|
-
static heapify<E>(options: {
|
|
32
|
-
elements: E[];
|
|
29
|
+
static heapify<E>(elements: Iterable<E>, options: {
|
|
33
30
|
comparator: Comparator<E>;
|
|
34
31
|
}): Heap<E>;
|
|
35
32
|
/**
|
|
@@ -204,6 +201,11 @@ export declare class Heap<E = any> {
|
|
|
204
201
|
* Time Complexity: O(log n)
|
|
205
202
|
* Space Complexity: O(1)
|
|
206
203
|
*/
|
|
204
|
+
print(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Time Complexity: O(n)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
*/
|
|
207
209
|
/**
|
|
208
210
|
* Time Complexity: O(log n)
|
|
209
211
|
* Space Complexity: O(1)
|
|
@@ -212,10 +214,6 @@ export declare class Heap<E = any> {
|
|
|
212
214
|
* @param index - The index of the newly added element.
|
|
213
215
|
*/
|
|
214
216
|
protected _bubbleUp(index: number): void;
|
|
215
|
-
/**
|
|
216
|
-
* Time Complexity: O(n)
|
|
217
|
-
* Space Complexity: O(1)
|
|
218
|
-
*/
|
|
219
217
|
/**
|
|
220
218
|
* Time Complexity: O(log n)
|
|
221
219
|
* Space Complexity: O(1)
|
|
@@ -349,18 +347,18 @@ export declare class FibonacciHeap<E> {
|
|
|
349
347
|
*/
|
|
350
348
|
merge(heapToMerge: FibonacciHeap<E>): void;
|
|
351
349
|
/**
|
|
352
|
-
*
|
|
353
|
-
* @param
|
|
354
|
-
* @param {E} b
|
|
350
|
+
* Create a new node.
|
|
351
|
+
* @param element
|
|
355
352
|
* @protected
|
|
356
353
|
*/
|
|
357
|
-
|
|
354
|
+
createNode(element: E): FibonacciHeapNode<E>;
|
|
358
355
|
/**
|
|
359
|
-
*
|
|
360
|
-
* @param
|
|
356
|
+
* Default comparator function used by the heap.
|
|
357
|
+
* @param {E} a
|
|
358
|
+
* @param {E} b
|
|
361
359
|
* @protected
|
|
362
360
|
*/
|
|
363
|
-
protected
|
|
361
|
+
protected _defaultComparator(a: E, b: E): number;
|
|
364
362
|
/**
|
|
365
363
|
* Time Complexity: O(1)
|
|
366
364
|
* Space Complexity: O(1)
|
|
@@ -399,7 +397,7 @@ export declare class FibonacciHeap<E> {
|
|
|
399
397
|
* @param x
|
|
400
398
|
* @protected
|
|
401
399
|
*/
|
|
402
|
-
protected
|
|
400
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void;
|
|
403
401
|
/**
|
|
404
402
|
* Time Complexity: O(n log n), where n is the number of elements in the heap.
|
|
405
403
|
* Space Complexity: O(n)
|
|
@@ -411,5 +409,5 @@ export declare class FibonacciHeap<E> {
|
|
|
411
409
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
412
410
|
* @protected
|
|
413
411
|
*/
|
|
414
|
-
protected
|
|
412
|
+
protected _consolidate(): void;
|
|
415
413
|
}
|
|
@@ -8,20 +8,34 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
|
|
10
10
|
class Heap {
|
|
11
|
-
constructor(options) {
|
|
11
|
+
constructor(elements, options) {
|
|
12
12
|
this._elements = [];
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const defaultComparator = (a, b) => {
|
|
14
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
|
+
throw new Error('The a, b params of compare function must be number');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
if (options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.options = {
|
|
26
|
+
comparator: defaultComparator
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (elements) {
|
|
30
|
+
for (const el of elements) {
|
|
31
|
+
this.push(el);
|
|
32
|
+
}
|
|
33
|
+
// this.fix();
|
|
17
34
|
}
|
|
18
35
|
}
|
|
19
36
|
get elements() {
|
|
20
37
|
return this._elements;
|
|
21
38
|
}
|
|
22
|
-
get comparator() {
|
|
23
|
-
return this._comparator;
|
|
24
|
-
}
|
|
25
39
|
/**
|
|
26
40
|
* Get the size (number of elements) of the heap.
|
|
27
41
|
*/
|
|
@@ -39,10 +53,11 @@ class Heap {
|
|
|
39
53
|
/**
|
|
40
54
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
41
55
|
* @returns A new Heap instance.
|
|
56
|
+
* @param elements
|
|
42
57
|
* @param options
|
|
43
58
|
*/
|
|
44
|
-
static heapify(options) {
|
|
45
|
-
return new Heap(options);
|
|
59
|
+
static heapify(elements, options) {
|
|
60
|
+
return new Heap(elements, options);
|
|
46
61
|
}
|
|
47
62
|
/**
|
|
48
63
|
* Time Complexity: O(log n), where n is the number of elements in the heap.
|
|
@@ -256,7 +271,7 @@ class Heap {
|
|
|
256
271
|
* @returns A new Heap instance containing the same elements.
|
|
257
272
|
*/
|
|
258
273
|
clone() {
|
|
259
|
-
const clonedHeap = new Heap(
|
|
274
|
+
const clonedHeap = new Heap([], this.options);
|
|
260
275
|
clonedHeap._elements = [...this.elements];
|
|
261
276
|
return clonedHeap;
|
|
262
277
|
}
|
|
@@ -308,7 +323,7 @@ class Heap {
|
|
|
308
323
|
}
|
|
309
324
|
}
|
|
310
325
|
filter(predicate) {
|
|
311
|
-
const filteredHeap = new Heap(
|
|
326
|
+
const filteredHeap = new Heap([], this.options);
|
|
312
327
|
let index = 0;
|
|
313
328
|
for (const el of this) {
|
|
314
329
|
if (predicate(el, index, this)) {
|
|
@@ -319,7 +334,7 @@ class Heap {
|
|
|
319
334
|
return filteredHeap;
|
|
320
335
|
}
|
|
321
336
|
map(callback, comparator) {
|
|
322
|
-
const mappedHeap = new Heap({ comparator: comparator });
|
|
337
|
+
const mappedHeap = new Heap([], { comparator: comparator });
|
|
323
338
|
let index = 0;
|
|
324
339
|
for (const el of this) {
|
|
325
340
|
mappedHeap.add(callback(el, index, this));
|
|
@@ -340,6 +355,13 @@ class Heap {
|
|
|
340
355
|
* Time Complexity: O(log n)
|
|
341
356
|
* Space Complexity: O(1)
|
|
342
357
|
*/
|
|
358
|
+
print() {
|
|
359
|
+
console.log([...this]);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Time Complexity: O(n)
|
|
363
|
+
* Space Complexity: O(1)
|
|
364
|
+
*/
|
|
343
365
|
/**
|
|
344
366
|
* Time Complexity: O(log n)
|
|
345
367
|
* Space Complexity: O(1)
|
|
@@ -352,17 +374,13 @@ class Heap {
|
|
|
352
374
|
while (index > 0) {
|
|
353
375
|
const parent = (index - 1) >> 1;
|
|
354
376
|
const parentItem = this.elements[parent];
|
|
355
|
-
if (this.
|
|
377
|
+
if (this.options.comparator(parentItem, element) <= 0)
|
|
356
378
|
break;
|
|
357
379
|
this.elements[index] = parentItem;
|
|
358
380
|
index = parent;
|
|
359
381
|
}
|
|
360
382
|
this.elements[index] = element;
|
|
361
383
|
}
|
|
362
|
-
/**
|
|
363
|
-
* Time Complexity: O(n)
|
|
364
|
-
* Space Complexity: O(1)
|
|
365
|
-
*/
|
|
366
384
|
/**
|
|
367
385
|
* Time Complexity: O(log n)
|
|
368
386
|
* Space Complexity: O(1)
|
|
@@ -378,11 +396,11 @@ class Heap {
|
|
|
378
396
|
const right = left + 1;
|
|
379
397
|
let minItem = this.elements[left];
|
|
380
398
|
if (right < this.elements.length &&
|
|
381
|
-
this.
|
|
399
|
+
this.options.comparator(minItem, this.elements[right]) > 0) {
|
|
382
400
|
left = right;
|
|
383
401
|
minItem = this.elements[right];
|
|
384
402
|
}
|
|
385
|
-
if (this.
|
|
403
|
+
if (this.options.comparator(minItem, element) >= 0)
|
|
386
404
|
break;
|
|
387
405
|
this.elements[index] = minItem;
|
|
388
406
|
index = left;
|
|
@@ -403,7 +421,7 @@ class FibonacciHeap {
|
|
|
403
421
|
constructor(comparator) {
|
|
404
422
|
this._size = 0;
|
|
405
423
|
this.clear();
|
|
406
|
-
this._comparator = comparator || this.
|
|
424
|
+
this._comparator = comparator || this._defaultComparator;
|
|
407
425
|
if (typeof this.comparator !== 'function') {
|
|
408
426
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
409
427
|
}
|
|
@@ -574,7 +592,7 @@ class FibonacciHeap {
|
|
|
574
592
|
}
|
|
575
593
|
else {
|
|
576
594
|
this._min = z.right;
|
|
577
|
-
this.
|
|
595
|
+
this._consolidate();
|
|
578
596
|
}
|
|
579
597
|
this._size--;
|
|
580
598
|
return z.element;
|
|
@@ -614,27 +632,27 @@ class FibonacciHeap {
|
|
|
614
632
|
// Clear the heap that was merged
|
|
615
633
|
heapToMerge.clear();
|
|
616
634
|
}
|
|
635
|
+
/**
|
|
636
|
+
* Create a new node.
|
|
637
|
+
* @param element
|
|
638
|
+
* @protected
|
|
639
|
+
*/
|
|
640
|
+
createNode(element) {
|
|
641
|
+
return new FibonacciHeapNode(element);
|
|
642
|
+
}
|
|
617
643
|
/**
|
|
618
644
|
* Default comparator function used by the heap.
|
|
619
645
|
* @param {E} a
|
|
620
646
|
* @param {E} b
|
|
621
647
|
* @protected
|
|
622
648
|
*/
|
|
623
|
-
|
|
649
|
+
_defaultComparator(a, b) {
|
|
624
650
|
if (a < b)
|
|
625
651
|
return -1;
|
|
626
652
|
if (a > b)
|
|
627
653
|
return 1;
|
|
628
654
|
return 0;
|
|
629
655
|
}
|
|
630
|
-
/**
|
|
631
|
-
* Create a new node.
|
|
632
|
-
* @param element
|
|
633
|
-
* @protected
|
|
634
|
-
*/
|
|
635
|
-
createNode(element) {
|
|
636
|
-
return new FibonacciHeapNode(element);
|
|
637
|
-
}
|
|
638
656
|
/**
|
|
639
657
|
* Time Complexity: O(1)
|
|
640
658
|
* Space Complexity: O(1)
|
|
@@ -690,7 +708,7 @@ class FibonacciHeap {
|
|
|
690
708
|
* @param x
|
|
691
709
|
* @protected
|
|
692
710
|
*/
|
|
693
|
-
|
|
711
|
+
_link(y, x) {
|
|
694
712
|
this.removeFromRoot(y);
|
|
695
713
|
y.left = y;
|
|
696
714
|
y.right = y;
|
|
@@ -709,7 +727,7 @@ class FibonacciHeap {
|
|
|
709
727
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
710
728
|
* @protected
|
|
711
729
|
*/
|
|
712
|
-
|
|
730
|
+
_consolidate() {
|
|
713
731
|
const A = new Array(this.size);
|
|
714
732
|
const elements = this.consumeLinkedList(this.root);
|
|
715
733
|
let x, y, d, t;
|
|
@@ -723,7 +741,7 @@ class FibonacciHeap {
|
|
|
723
741
|
x = y;
|
|
724
742
|
y = t;
|
|
725
743
|
}
|
|
726
|
-
this.
|
|
744
|
+
this._link(y, x);
|
|
727
745
|
A[d] = undefined;
|
|
728
746
|
d++;
|
|
729
747
|
}
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MaxHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MaxHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MaxHeap extends heap_1.Heap {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MaxHeap extends heap_1.Heap {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MaxHeap = MaxHeap;
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from './heap';
|
|
9
|
-
import type {
|
|
9
|
+
import type { HeapOptions } from '../../types';
|
|
10
10
|
export declare class MinHeap<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.MinHeap = void 0;
|
|
11
11
|
const heap_1 = require("./heap");
|
|
12
12
|
class MinHeap extends heap_1.Heap {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MinHeap extends heap_1.Heap {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MinHeap = MinHeap;
|
|
@@ -20,7 +20,7 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
20
20
|
/**
|
|
21
21
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
22
22
|
*/
|
|
23
|
-
constructor();
|
|
23
|
+
constructor(elements?: Iterable<E>);
|
|
24
24
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
25
25
|
get head(): DoublyLinkedListNode<E> | null;
|
|
26
26
|
protected _tail: DoublyLinkedListNode<E> | null;
|
|
@@ -453,4 +453,5 @@ export declare class DoublyLinkedList<E = any> {
|
|
|
453
453
|
* elements in the linked list.
|
|
454
454
|
*/
|
|
455
455
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: DoublyLinkedList<E>) => T, initialValue: T): T;
|
|
456
|
+
print(): void;
|
|
456
457
|
}
|
|
@@ -25,10 +25,15 @@ class DoublyLinkedList {
|
|
|
25
25
|
/**
|
|
26
26
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
27
27
|
*/
|
|
28
|
-
constructor() {
|
|
28
|
+
constructor(elements) {
|
|
29
29
|
this._head = null;
|
|
30
30
|
this._tail = null;
|
|
31
31
|
this._length = 0;
|
|
32
|
+
if (elements) {
|
|
33
|
+
for (const el of elements) {
|
|
34
|
+
this.push(el);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
32
37
|
}
|
|
33
38
|
get head() {
|
|
34
39
|
return this._head;
|
|
@@ -766,5 +771,8 @@ class DoublyLinkedList {
|
|
|
766
771
|
}
|
|
767
772
|
return accumulator;
|
|
768
773
|
}
|
|
774
|
+
print() {
|
|
775
|
+
console.log([...this]);
|
|
776
|
+
}
|
|
769
777
|
}
|
|
770
778
|
exports.DoublyLinkedList = DoublyLinkedList;
|
|
@@ -19,7 +19,7 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
19
19
|
/**
|
|
20
20
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
21
21
|
*/
|
|
22
|
-
constructor();
|
|
22
|
+
constructor(elements?: Iterable<E>);
|
|
23
23
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
24
24
|
get head(): SinglyLinkedListNode<E> | null;
|
|
25
25
|
protected _tail: SinglyLinkedListNode<E> | null;
|
|
@@ -411,4 +411,5 @@ export declare class SinglyLinkedList<E = any> {
|
|
|
411
411
|
* elements in the linked list.
|
|
412
412
|
*/
|
|
413
413
|
reduce<T>(callback: (accumulator: T, value: E, index: number, list: SinglyLinkedList<E>) => T, initialValue: T): T;
|
|
414
|
+
print(): void;
|
|
414
415
|
}
|
|
@@ -24,10 +24,14 @@ class SinglyLinkedList {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
|
-
constructor() {
|
|
27
|
+
constructor(elements) {
|
|
28
28
|
this._head = null;
|
|
29
29
|
this._tail = null;
|
|
30
30
|
this._length = 0;
|
|
31
|
+
if (elements) {
|
|
32
|
+
for (const el of elements)
|
|
33
|
+
this.push(el);
|
|
34
|
+
}
|
|
31
35
|
}
|
|
32
36
|
get head() {
|
|
33
37
|
return this._head;
|
|
@@ -709,5 +713,8 @@ class SinglyLinkedList {
|
|
|
709
713
|
}
|
|
710
714
|
return accumulator;
|
|
711
715
|
}
|
|
716
|
+
print() {
|
|
717
|
+
console.log([...this]);
|
|
718
|
+
}
|
|
712
719
|
}
|
|
713
720
|
exports.SinglyLinkedList = SinglyLinkedList;
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ exports.MaxPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MaxPriorityQueue = MaxPriorityQueue;
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { PriorityQueue } from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
11
|
-
constructor(options?:
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,7 +10,7 @@ exports.MinPriorityQueue = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
const priority_queue_1 = require("./priority-queue");
|
|
12
12
|
class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
13
|
-
constructor(options = {
|
|
13
|
+
constructor(elements, options = {
|
|
14
14
|
comparator: (a, b) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,7 +20,7 @@ class MinPriorityQueue extends priority_queue_1.PriorityQueue {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
}) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
exports.MinPriorityQueue = MinPriorityQueue;
|
|
@@ -6,10 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { Heap } from '../heap';
|
|
9
|
-
import {
|
|
9
|
+
import { PriorityQueueOptions } from '../../types';
|
|
10
10
|
export declare class PriorityQueue<E = any> extends Heap<E> {
|
|
11
|
-
constructor(options
|
|
12
|
-
comparator: Comparator<E>;
|
|
13
|
-
nodes?: E[];
|
|
14
|
-
});
|
|
11
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>);
|
|
15
12
|
}
|
|
@@ -10,8 +10,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.PriorityQueue = void 0;
|
|
11
11
|
const heap_1 = require("../heap");
|
|
12
12
|
class PriorityQueue extends heap_1.Heap {
|
|
13
|
-
constructor(options) {
|
|
14
|
-
super(options);
|
|
13
|
+
constructor(elements, options) {
|
|
14
|
+
super(elements, options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
exports.PriorityQueue = PriorityQueue;
|
|
@@ -428,6 +428,7 @@ export declare class Deque<E> {
|
|
|
428
428
|
* applying the callback function to each element.
|
|
429
429
|
*/
|
|
430
430
|
reduce<T>(callback: (accumulator: T, element: E, index: number, deque: this) => T, initialValue: T): T;
|
|
431
|
+
print(): void;
|
|
431
432
|
/**
|
|
432
433
|
* Time Complexity: O(n)
|
|
433
434
|
* Space Complexity: O(n)
|
|
@@ -205,6 +205,7 @@ export declare class Queue<E = any> {
|
|
|
205
205
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
206
206
|
*/
|
|
207
207
|
clone(): Queue<E>;
|
|
208
|
+
print(): void;
|
|
208
209
|
[Symbol.iterator](): Generator<E, void, unknown>;
|
|
209
210
|
/**
|
|
210
211
|
* Time Complexity: O(n)
|
|
@@ -10,7 +10,7 @@ export declare class Stack<E = any> {
|
|
|
10
10
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
11
11
|
* is provided and is an array, it is assigned to the `_elements
|
|
12
12
|
*/
|
|
13
|
-
constructor(elements?: E
|
|
13
|
+
constructor(elements?: Iterable<E>);
|
|
14
14
|
protected _elements: E[];
|
|
15
15
|
get elements(): E[];
|
|
16
16
|
/**
|
|
@@ -116,4 +116,5 @@ export declare class Stack<E = any> {
|
|
|
116
116
|
filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
|
|
117
117
|
map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
|
|
118
118
|
reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
|
|
119
|
+
print(): void;
|
|
119
120
|
}
|
|
@@ -14,7 +14,12 @@ class Stack {
|
|
|
14
14
|
* is provided and is an array, it is assigned to the `_elements
|
|
15
15
|
*/
|
|
16
16
|
constructor(elements) {
|
|
17
|
-
this._elements =
|
|
17
|
+
this._elements = [];
|
|
18
|
+
if (elements) {
|
|
19
|
+
for (const el of elements) {
|
|
20
|
+
this.push(el);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
18
23
|
}
|
|
19
24
|
get elements() {
|
|
20
25
|
return this._elements;
|
|
@@ -137,7 +142,7 @@ class Stack {
|
|
|
137
142
|
* @returns An iterator object.
|
|
138
143
|
*/
|
|
139
144
|
*[Symbol.iterator]() {
|
|
140
|
-
for (let i = this.elements.length
|
|
145
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
141
146
|
yield this.elements[i];
|
|
142
147
|
}
|
|
143
148
|
}
|
|
@@ -181,5 +186,8 @@ class Stack {
|
|
|
181
186
|
}
|
|
182
187
|
return accumulator;
|
|
183
188
|
}
|
|
189
|
+
print() {
|
|
190
|
+
console.log([...this]);
|
|
191
|
+
}
|
|
184
192
|
}
|
|
185
193
|
exports.Stack = Stack;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
5
7
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
6
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
7
9
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BTNKey } from "./data-structures";
|
|
1
2
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
3
4
|
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
@@ -18,3 +19,4 @@ export type BinaryTreePrintOptions = {
|
|
|
18
19
|
isShowNull?: boolean;
|
|
19
20
|
isShowRedBlackNIL?: boolean;
|
|
20
21
|
};
|
|
22
|
+
export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
|
|
@@ -26,6 +26,6 @@ export type BiTreeDeleteResult<N> = {
|
|
|
26
26
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
27
|
export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
28
28
|
export type BinaryTreeOptions = {
|
|
29
|
-
iterationType
|
|
29
|
+
iterationType: IterationType;
|
|
30
30
|
};
|
|
31
31
|
export type NodeDisplayLayout = [string[], number, number, number];
|