priority-queue-typed 1.47.5 → 1.47.7
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 +36 -18
- package/dist/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/data-structures/binary-tree/bst.js +168 -169
- package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/data-structures/graph/abstract-graph.js +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- 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/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +4 -2
- package/dist/types/common.d.ts +7 -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/hash/hash-map.d.ts +1 -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 +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- 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/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -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/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
|
@@ -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
|
}
|
|
@@ -29,13 +29,20 @@ export class Trie {
|
|
|
29
29
|
constructor(words?: string[], caseSensitive = true) {
|
|
30
30
|
this._root = new TrieNode('');
|
|
31
31
|
this._caseSensitive = caseSensitive;
|
|
32
|
+
this._size = 0;
|
|
32
33
|
if (words) {
|
|
33
|
-
for (const
|
|
34
|
-
this.add(
|
|
34
|
+
for (const word of words) {
|
|
35
|
+
this.add(word);
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
protected _size: number;
|
|
41
|
+
|
|
42
|
+
get size(): number {
|
|
43
|
+
return this._size;
|
|
44
|
+
}
|
|
45
|
+
|
|
39
46
|
protected _caseSensitive: boolean;
|
|
40
47
|
|
|
41
48
|
get caseSensitive(): boolean {
|
|
@@ -64,6 +71,7 @@ export class Trie {
|
|
|
64
71
|
add(word: string): boolean {
|
|
65
72
|
word = this._caseProcess(word);
|
|
66
73
|
let cur = this.root;
|
|
74
|
+
let isNewWord = false;
|
|
67
75
|
for (const c of word) {
|
|
68
76
|
let nodeC = cur.children.get(c);
|
|
69
77
|
if (!nodeC) {
|
|
@@ -72,8 +80,12 @@ export class Trie {
|
|
|
72
80
|
}
|
|
73
81
|
cur = nodeC;
|
|
74
82
|
}
|
|
75
|
-
cur.isEnd
|
|
76
|
-
|
|
83
|
+
if (!cur.isEnd) {
|
|
84
|
+
isNewWord = true;
|
|
85
|
+
cur.isEnd = true;
|
|
86
|
+
this._size++;
|
|
87
|
+
}
|
|
88
|
+
return isNewWord;
|
|
77
89
|
}
|
|
78
90
|
|
|
79
91
|
/**
|
|
@@ -143,6 +155,9 @@ export class Trie {
|
|
|
143
155
|
};
|
|
144
156
|
|
|
145
157
|
dfs(this.root, 0);
|
|
158
|
+
if (isDeleted) {
|
|
159
|
+
this._size--;
|
|
160
|
+
}
|
|
146
161
|
return isDeleted;
|
|
147
162
|
}
|
|
148
163
|
|
|
@@ -377,6 +392,10 @@ export class Trie {
|
|
|
377
392
|
return accumulator;
|
|
378
393
|
}
|
|
379
394
|
|
|
395
|
+
print() {
|
|
396
|
+
console.log([...this]);
|
|
397
|
+
}
|
|
398
|
+
|
|
380
399
|
/**
|
|
381
400
|
* Time Complexity: O(M), where M is the length of the input string.
|
|
382
401
|
* Space Complexity: O(1) - Constant space.
|
|
@@ -1,10 +1,22 @@
|
|
|
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
|
+
BTNodeExemplar,
|
|
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
|
|
|
7
|
-
|
|
15
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
16
|
+
|
|
17
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
|
|
18
|
+
|
|
19
|
+
addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
|
|
8
20
|
|
|
9
21
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
|
10
22
|
}
|
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,16 @@ 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 BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
|
|
28
|
+
|
|
29
|
+
export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
|
|
30
|
+
|
|
31
|
+
export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>
|
|
32
|
+
|
|
33
|
+
export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>
|
|
34
|
+
|
|
35
|
+
export type BTNodePureKeyOrNode<N> = BTNKey | N;
|
|
36
|
+
|
|
37
|
+
export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
|
|
@@ -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
|
}
|