data-structure-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/CHANGELOG.md +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +23 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +66 -82
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/cjs/data-structures/binary-tree/bst.js +56 -40
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +26 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +31 -22
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/cjs/types/common.d.ts +2 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +8 -8
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +26 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +65 -28
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +66 -83
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +38 -37
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -41
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +11 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +30 -19
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +34 -23
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -1
- package/dist/mjs/types/common.d.ts +2 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +307 -229
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +100 -0
- package/test/integration/conversion.test.ts +0 -0
|
@@ -6,13 +6,32 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { Comparator, DFSOrderPattern } from '../../types';
|
|
9
|
+
import { HeapOptions } from "../../types";
|
|
9
10
|
|
|
10
11
|
export class Heap<E = any> {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
options: HeapOptions<E>;
|
|
13
|
+
|
|
14
|
+
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
|
|
15
|
+
const defaultComparator = (a: E, b: E) => {
|
|
16
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
|
+
throw new Error('The a, b params of compare function must be number');
|
|
18
|
+
} else {
|
|
19
|
+
return a - b;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (options) {
|
|
23
|
+
this.options = options
|
|
24
|
+
} else {
|
|
25
|
+
this.options = {
|
|
26
|
+
comparator: defaultComparator
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (elements) {
|
|
31
|
+
for (const el of elements) {
|
|
32
|
+
this.push(el);
|
|
33
|
+
}
|
|
34
|
+
// this.fix();
|
|
16
35
|
}
|
|
17
36
|
}
|
|
18
37
|
|
|
@@ -22,12 +41,6 @@ export class Heap<E = any> {
|
|
|
22
41
|
return this._elements;
|
|
23
42
|
}
|
|
24
43
|
|
|
25
|
-
protected _comparator: Comparator<E>;
|
|
26
|
-
|
|
27
|
-
get comparator(): Comparator<E> {
|
|
28
|
-
return this._comparator;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
44
|
/**
|
|
32
45
|
* Get the size (number of elements) of the heap.
|
|
33
46
|
*/
|
|
@@ -46,10 +59,11 @@ export class Heap<E = any> {
|
|
|
46
59
|
/**
|
|
47
60
|
* Static method that creates a binary heap from an array of elements and a comparison function.
|
|
48
61
|
* @returns A new Heap instance.
|
|
62
|
+
* @param elements
|
|
49
63
|
* @param options
|
|
50
64
|
*/
|
|
51
|
-
static heapify<E>(
|
|
52
|
-
return new Heap<E>(options);
|
|
65
|
+
static heapify<E>(elements: Iterable<E>, options: { comparator: Comparator<E> }): Heap<E> {
|
|
66
|
+
return new Heap<E>(elements, options);
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
/**
|
|
@@ -283,7 +297,7 @@ export class Heap<E = any> {
|
|
|
283
297
|
* @returns A new Heap instance containing the same elements.
|
|
284
298
|
*/
|
|
285
299
|
clone(): Heap<E> {
|
|
286
|
-
const clonedHeap = new Heap<E>(
|
|
300
|
+
const clonedHeap = new Heap<E>([], this.options);
|
|
287
301
|
clonedHeap._elements = [...this.elements];
|
|
288
302
|
return clonedHeap;
|
|
289
303
|
}
|
|
@@ -340,7 +354,7 @@ export class Heap<E = any> {
|
|
|
340
354
|
}
|
|
341
355
|
|
|
342
356
|
filter(predicate: (element: E, index: number, heap: Heap<E>) => boolean): Heap<E> {
|
|
343
|
-
const filteredHeap: Heap<E> = new Heap<E>(
|
|
357
|
+
const filteredHeap: Heap<E> = new Heap<E>([], this.options);
|
|
344
358
|
let index = 0;
|
|
345
359
|
for (const el of this) {
|
|
346
360
|
if (predicate(el, index, this)) {
|
|
@@ -353,7 +367,7 @@ export class Heap<E = any> {
|
|
|
353
367
|
|
|
354
368
|
map<T>(callback: (element: E, index: number, heap: Heap<E>) => T, comparator: Comparator<T>): Heap<T> {
|
|
355
369
|
|
|
356
|
-
const mappedHeap: Heap<T> = new Heap<T>({ comparator: comparator });
|
|
370
|
+
const mappedHeap: Heap<T> = new Heap<T>([], { comparator: comparator });
|
|
357
371
|
let index = 0;
|
|
358
372
|
for (const el of this) {
|
|
359
373
|
mappedHeap.add(callback(el, index, this));
|
|
@@ -380,6 +394,15 @@ export class Heap<E = any> {
|
|
|
380
394
|
* Space Complexity: O(1)
|
|
381
395
|
*/
|
|
382
396
|
|
|
397
|
+
print(): void {
|
|
398
|
+
console.log([...this]);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Time Complexity: O(n)
|
|
403
|
+
* Space Complexity: O(1)
|
|
404
|
+
*/
|
|
405
|
+
|
|
383
406
|
/**
|
|
384
407
|
* Time Complexity: O(log n)
|
|
385
408
|
* Space Complexity: O(1)
|
|
@@ -392,18 +415,13 @@ export class Heap<E = any> {
|
|
|
392
415
|
while (index > 0) {
|
|
393
416
|
const parent = (index - 1) >> 1;
|
|
394
417
|
const parentItem = this.elements[parent];
|
|
395
|
-
if (this.
|
|
418
|
+
if (this.options.comparator(parentItem, element) <= 0) break;
|
|
396
419
|
this.elements[index] = parentItem;
|
|
397
420
|
index = parent;
|
|
398
421
|
}
|
|
399
422
|
this.elements[index] = element;
|
|
400
423
|
}
|
|
401
424
|
|
|
402
|
-
/**
|
|
403
|
-
* Time Complexity: O(n)
|
|
404
|
-
* Space Complexity: O(1)
|
|
405
|
-
*/
|
|
406
|
-
|
|
407
425
|
/**
|
|
408
426
|
* Time Complexity: O(log n)
|
|
409
427
|
* Space Complexity: O(1)
|
|
@@ -420,12 +438,12 @@ export class Heap<E = any> {
|
|
|
420
438
|
let minItem = this.elements[left];
|
|
421
439
|
if (
|
|
422
440
|
right < this.elements.length &&
|
|
423
|
-
this.
|
|
441
|
+
this.options.comparator(minItem, this.elements[right]) > 0
|
|
424
442
|
) {
|
|
425
443
|
left = right;
|
|
426
444
|
minItem = this.elements[right];
|
|
427
445
|
}
|
|
428
|
-
if (this.
|
|
446
|
+
if (this.options.comparator(minItem, element) >= 0) break;
|
|
429
447
|
this.elements[index] = minItem;
|
|
430
448
|
index = left;
|
|
431
449
|
}
|
|
@@ -452,7 +470,7 @@ export class FibonacciHeapNode<E> {
|
|
|
452
470
|
export class FibonacciHeap<E> {
|
|
453
471
|
constructor(comparator?: Comparator<E>) {
|
|
454
472
|
this.clear();
|
|
455
|
-
this._comparator = comparator || this.
|
|
473
|
+
this._comparator = comparator || this._defaultComparator;
|
|
456
474
|
|
|
457
475
|
if (typeof this.comparator !== 'function') {
|
|
458
476
|
throw new Error('FibonacciHeap constructor: given comparator should be a function.');
|
|
@@ -653,7 +671,7 @@ export class FibonacciHeap<E> {
|
|
|
653
671
|
this._root = undefined;
|
|
654
672
|
} else {
|
|
655
673
|
this._min = z.right;
|
|
656
|
-
this.
|
|
674
|
+
this._consolidate();
|
|
657
675
|
}
|
|
658
676
|
|
|
659
677
|
this._size--;
|
|
@@ -705,27 +723,27 @@ export class FibonacciHeap<E> {
|
|
|
705
723
|
heapToMerge.clear();
|
|
706
724
|
}
|
|
707
725
|
|
|
726
|
+
/**
|
|
727
|
+
* Create a new node.
|
|
728
|
+
* @param element
|
|
729
|
+
* @protected
|
|
730
|
+
*/
|
|
731
|
+
createNode(element: E): FibonacciHeapNode<E> {
|
|
732
|
+
return new FibonacciHeapNode<E>(element);
|
|
733
|
+
}
|
|
734
|
+
|
|
708
735
|
/**
|
|
709
736
|
* Default comparator function used by the heap.
|
|
710
737
|
* @param {E} a
|
|
711
738
|
* @param {E} b
|
|
712
739
|
* @protected
|
|
713
740
|
*/
|
|
714
|
-
protected
|
|
741
|
+
protected _defaultComparator(a: E, b: E): number {
|
|
715
742
|
if (a < b) return -1;
|
|
716
743
|
if (a > b) return 1;
|
|
717
744
|
return 0;
|
|
718
745
|
}
|
|
719
746
|
|
|
720
|
-
/**
|
|
721
|
-
* Create a new node.
|
|
722
|
-
* @param element
|
|
723
|
-
* @protected
|
|
724
|
-
*/
|
|
725
|
-
protected createNode(element: E): FibonacciHeapNode<E> {
|
|
726
|
-
return new FibonacciHeapNode<E>(element);
|
|
727
|
-
}
|
|
728
|
-
|
|
729
747
|
/**
|
|
730
748
|
* Time Complexity: O(1)
|
|
731
749
|
* Space Complexity: O(1)
|
|
@@ -782,7 +800,7 @@ export class FibonacciHeap<E> {
|
|
|
782
800
|
* @param x
|
|
783
801
|
* @protected
|
|
784
802
|
*/
|
|
785
|
-
protected
|
|
803
|
+
protected _link(y: FibonacciHeapNode<E>, x: FibonacciHeapNode<E>): void {
|
|
786
804
|
this.removeFromRoot(y);
|
|
787
805
|
y.left = y;
|
|
788
806
|
y.right = y;
|
|
@@ -803,7 +821,7 @@ export class FibonacciHeap<E> {
|
|
|
803
821
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
804
822
|
* @protected
|
|
805
823
|
*/
|
|
806
|
-
protected
|
|
824
|
+
protected _consolidate(): void {
|
|
807
825
|
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.size);
|
|
808
826
|
const elements = this.consumeLinkedList(this.root);
|
|
809
827
|
let x: FibonacciHeapNode<E> | undefined,
|
|
@@ -824,7 +842,7 @@ export class FibonacciHeap<E> {
|
|
|
824
842
|
y = t;
|
|
825
843
|
}
|
|
826
844
|
|
|
827
|
-
this.
|
|
845
|
+
this._link(y, x);
|
|
828
846
|
A[d] = undefined;
|
|
829
847
|
d++;
|
|
830
848
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
|
|
14
|
+
elements?: Iterable<E>,
|
|
15
|
+
options: HeapOptions<E> = {
|
|
15
16
|
comparator: (a: E, b: E) => {
|
|
16
17
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
18
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -19,8 +20,7 @@ export class MaxHeap<E = any> extends Heap<E> {
|
|
|
19
20
|
return b - a;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
super(options);
|
|
23
|
+
}) {
|
|
24
|
+
super(elements, options);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -7,11 +7,12 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type { HeapOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
|
|
14
|
+
elements?: Iterable<E>,
|
|
15
|
+
options: HeapOptions<E> = {
|
|
15
16
|
comparator: (a: E, b: E) => {
|
|
16
17
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
18
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -19,8 +20,7 @@ export class MinHeap<E = any> extends Heap<E> {
|
|
|
19
20
|
return a - b;
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
super(options);
|
|
23
|
+
}) {
|
|
24
|
+
super(elements, options);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -26,10 +26,15 @@ export class DoublyLinkedList<E = any> {
|
|
|
26
26
|
/**
|
|
27
27
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
28
28
|
*/
|
|
29
|
-
constructor() {
|
|
29
|
+
constructor(elements?: Iterable<E>) {
|
|
30
30
|
this._head = null;
|
|
31
31
|
this._tail = null;
|
|
32
32
|
this._length = 0;
|
|
33
|
+
if (elements) {
|
|
34
|
+
for (const el of elements) {
|
|
35
|
+
this.push(el);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
protected _head: DoublyLinkedListNode<E> | null;
|
|
@@ -834,4 +839,8 @@ export class DoublyLinkedList<E = any> {
|
|
|
834
839
|
|
|
835
840
|
return accumulator;
|
|
836
841
|
}
|
|
842
|
+
|
|
843
|
+
print(): void {
|
|
844
|
+
console.log([...this]);
|
|
845
|
+
}
|
|
837
846
|
}
|
|
@@ -24,10 +24,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
24
24
|
/**
|
|
25
25
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
26
26
|
*/
|
|
27
|
-
constructor() {
|
|
27
|
+
constructor(elements?: Iterable<E>) {
|
|
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
|
|
|
33
37
|
protected _head: SinglyLinkedListNode<E> | null;
|
|
@@ -781,4 +785,8 @@ export class SinglyLinkedList<E = any> {
|
|
|
781
785
|
|
|
782
786
|
return accumulator;
|
|
783
787
|
}
|
|
788
|
+
|
|
789
|
+
print(): void {
|
|
790
|
+
console.log([...this]);
|
|
791
|
+
}
|
|
784
792
|
}
|
|
@@ -6,11 +6,12 @@
|
|
|
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
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
13
|
+
elements?: Iterable<E>,
|
|
14
|
+
options: PriorityQueueOptions<E> = {
|
|
14
15
|
comparator: (a: E, b: E) => {
|
|
15
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -20,6 +21,6 @@ export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
) {
|
|
23
|
-
super(options);
|
|
24
|
+
super(elements, options);
|
|
24
25
|
}
|
|
25
26
|
}
|
|
@@ -6,20 +6,20 @@
|
|
|
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
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
|
-
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
constructor(elements?: Iterable<E>,
|
|
13
|
+
options: PriorityQueueOptions<E> = {
|
|
14
|
+
comparator: (a: E, b: E) => {
|
|
15
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
|
+
throw new Error('The a, b params of compare function must be number');
|
|
17
|
+
} else {
|
|
18
|
+
return a - b;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
22
|
) {
|
|
23
|
-
super(options);
|
|
23
|
+
super(elements, options);
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import { Heap } from '../heap';
|
|
10
|
-
import {
|
|
10
|
+
import { PriorityQueueOptions } from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(
|
|
14
|
-
super(options);
|
|
13
|
+
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
|
|
14
|
+
super(elements, options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -10,8 +10,13 @@ export 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
|
|
14
|
-
this._elements =
|
|
13
|
+
constructor(elements?: Iterable<E>) {
|
|
14
|
+
this._elements = [];
|
|
15
|
+
if (elements) {
|
|
16
|
+
for (const el of elements) {
|
|
17
|
+
this.push(el);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
protected _elements: E[];
|
|
@@ -153,7 +158,7 @@ export class Stack<E = any> {
|
|
|
153
158
|
* @returns An iterator object.
|
|
154
159
|
*/
|
|
155
160
|
* [Symbol.iterator]() {
|
|
156
|
-
for (let i = this.elements.length
|
|
161
|
+
for (let i = 0; i < this.elements.length; i++) {
|
|
157
162
|
yield this.elements[i];
|
|
158
163
|
}
|
|
159
164
|
}
|
|
@@ -203,4 +208,8 @@ export class Stack<E = any> {
|
|
|
203
208
|
}
|
|
204
209
|
return accumulator;
|
|
205
210
|
}
|
|
211
|
+
|
|
212
|
+
print(): void {
|
|
213
|
+
console.log([...this]);
|
|
214
|
+
}
|
|
206
215
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
BinaryTreeNested,
|
|
4
|
+
BinaryTreeNodeNested,
|
|
5
|
+
BinaryTreeOptions,
|
|
6
|
+
BiTreeDeleteResult,
|
|
7
|
+
BTNCallback,
|
|
8
|
+
BTNKey,
|
|
9
|
+
IterableEntriesOrKeys
|
|
10
|
+
} from '../types';
|
|
3
11
|
|
|
4
12
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
5
13
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
6
14
|
|
|
15
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
16
|
+
|
|
17
|
+
init(elements: IterableEntriesOrKeys<V>): void;
|
|
18
|
+
|
|
7
19
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
8
20
|
|
|
9
21
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
package/src/types/common.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BTNKey } from "./data-structures";
|
|
2
|
+
|
|
1
3
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
2
4
|
|
|
3
5
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
@@ -20,4 +22,6 @@ export interface IterableWithLength<T> extends Iterable<T> {
|
|
|
20
22
|
|
|
21
23
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>
|
|
22
24
|
|
|
23
|
-
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
|
25
|
+
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
|
26
|
+
|
|
27
|
+
export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>
|
|
@@ -30,6 +30,6 @@ export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, Binary
|
|
|
30
30
|
|
|
31
31
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
32
32
|
|
|
33
|
-
export type BinaryTreeOptions = { iterationType
|
|
33
|
+
export type BinaryTreeOptions = { iterationType: IterationType }
|
|
34
34
|
|
|
35
35
|
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
|
-
|
|
4
|
-
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
3
|
+
import { Comparator } from "../../common";
|
|
5
4
|
|
|
6
5
|
// prettier-ignore
|
|
7
6
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
@@ -9,5 +8,5 @@ export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNo
|
|
|
9
8
|
export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
10
9
|
|
|
11
10
|
export type BSTOptions = BinaryTreeOptions & {
|
|
12
|
-
comparator
|
|
11
|
+
comparator: Comparator<BTNKey>
|
|
13
12
|
}
|
|
@@ -23,17 +23,18 @@ const arrHundredThousand = getRandomIntArray(HUNDRED_THOUSAND, 0, HUNDRED_THOUSA
|
|
|
23
23
|
suite
|
|
24
24
|
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
25
25
|
|
|
26
|
-
const pq = new SRCPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
26
|
+
const pq = new SRCPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
27
27
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
28
28
|
})
|
|
29
29
|
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
const pq = new CJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
32
33
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
33
34
|
})
|
|
34
35
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add`, () => {
|
|
35
36
|
|
|
36
|
-
const pq = new MJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
37
|
+
const pq = new MJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
37
38
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
38
39
|
});
|
|
39
40
|
|
|
@@ -47,19 +48,19 @@ if (isCompetitor) {
|
|
|
47
48
|
|
|
48
49
|
suite
|
|
49
50
|
.add(`SRC PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
50
|
-
const pq = new SRCPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
51
|
+
const pq = new SRCPriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
51
52
|
|
|
52
53
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
53
54
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
54
55
|
})
|
|
55
56
|
.add(`CJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
56
|
-
const pq = new CJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
57
|
+
const pq = new CJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
57
58
|
|
|
58
59
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
59
60
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
60
61
|
})
|
|
61
62
|
.add(`MJS PQ ${TEN_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
62
|
-
const pq = new MJSPriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
63
|
+
const pq = new MJSPriorityQueue<number>([],{ comparator: (a, b) => b - a });
|
|
63
64
|
|
|
64
65
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.add(i);
|
|
65
66
|
for (let i = 0; i < TEN_THOUSAND; i++) pq.pop();
|
|
@@ -7,7 +7,7 @@ const { HUNDRED_THOUSAND, TEN_THOUSAND } = magnitude;
|
|
|
7
7
|
|
|
8
8
|
suite
|
|
9
9
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
10
|
-
const heap = new Heap<number>({ comparator: (a, b) => b - a });
|
|
10
|
+
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
11
11
|
|
|
12
12
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
13
13
|
heap.add(i);
|
|
@@ -18,7 +18,7 @@ suite
|
|
|
18
18
|
}
|
|
19
19
|
})
|
|
20
20
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} add & dfs`, () => {
|
|
21
|
-
const heap = new Heap<number>({ comparator: (a, b) => b - a });
|
|
21
|
+
const heap = new Heap<number>([], { comparator: (a, b) => b - a });
|
|
22
22
|
|
|
23
23
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
24
24
|
heap.add(i);
|
|
@@ -8,7 +8,7 @@ const suite = new Benchmark.Suite();
|
|
|
8
8
|
const { HUNDRED_THOUSAND } = magnitude;
|
|
9
9
|
|
|
10
10
|
suite.add(`${HUNDRED_THOUSAND.toLocaleString()} add & pop`, () => {
|
|
11
|
-
const pq = new PriorityQueue<number>({ comparator: (a, b) => b - a });
|
|
11
|
+
const pq = new PriorityQueue<number>([], { comparator: (a, b) => b - a });
|
|
12
12
|
|
|
13
13
|
for (let i = 0; i < HUNDRED_THOUSAND; i++) {
|
|
14
14
|
pq.add(i);
|
|
@@ -112,7 +112,7 @@ describe('AVL Tree Test', () => {
|
|
|
112
112
|
describe('AVL Tree Test recursively', () => {
|
|
113
113
|
it('should perform various operations on a AVL Tree', () => {
|
|
114
114
|
const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
115
|
-
const tree = new AVLTree<number>({ iterationType: IterationType.RECURSIVE });
|
|
115
|
+
const tree = new AVLTree<number>([], { iterationType: IterationType.RECURSIVE });
|
|
116
116
|
|
|
117
117
|
for (const i of arr) tree.add(i, i);
|
|
118
118
|
|
|
@@ -189,9 +189,9 @@ describe('BinaryTree', () => {
|
|
|
189
189
|
tree.add(4);
|
|
190
190
|
tree.add(2);
|
|
191
191
|
expect(tree.getHeight()).toBe(1);
|
|
192
|
-
tree.
|
|
192
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
193
193
|
expect(tree.getHeight()).toBe(1);
|
|
194
|
-
tree.
|
|
194
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
195
195
|
|
|
196
196
|
tree.add(6);
|
|
197
197
|
tree.add(1);
|
|
@@ -381,7 +381,7 @@ describe('BinaryTree', () => {
|
|
|
381
381
|
let tree: BinaryTree<string>;
|
|
382
382
|
|
|
383
383
|
beforeEach(() => {
|
|
384
|
-
tree = new BinaryTree<string>({ iterationType: IterationType.RECURSIVE });
|
|
384
|
+
tree = new BinaryTree<string>([], { iterationType: IterationType.RECURSIVE });
|
|
385
385
|
});
|
|
386
386
|
|
|
387
387
|
afterEach(() => {
|
|
@@ -541,11 +541,11 @@ describe('BinaryTree', () => {
|
|
|
541
541
|
tree.add(3, 'B');
|
|
542
542
|
tree.add(7, 'C');
|
|
543
543
|
|
|
544
|
-
tree.
|
|
544
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
545
545
|
expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
|
|
546
|
-
tree.
|
|
546
|
+
tree.iterationType = IterationType.RECURSIVE;
|
|
547
547
|
expect([...tree]).toEqual([[3, "B"], [5, "A"], [7, "C"]]);
|
|
548
|
-
tree.
|
|
548
|
+
tree.iterationType = IterationType.ITERATIVE;
|
|
549
549
|
|
|
550
550
|
const result = tree.morris();
|
|
551
551
|
expect(result).toEqual([3, 5, 7]);
|
|
@@ -395,7 +395,7 @@ describe('BST operations test', () => {
|
|
|
395
395
|
|
|
396
396
|
describe('BST operations test recursively', () => {
|
|
397
397
|
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
398
|
-
const bst = new BST({ iterationType: IterationType.RECURSIVE });
|
|
398
|
+
const bst = new BST([], { iterationType: IterationType.RECURSIVE });
|
|
399
399
|
expect(bst).toBeInstanceOf(BST);
|
|
400
400
|
bst.add(11, 11);
|
|
401
401
|
bst.add(3, 3);
|