min-heap-typed 1.49.5 → 1.49.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 +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- 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 +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -21,23 +21,12 @@ import { IterableElementBase } from '../base';
|
|
|
21
21
|
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
|
22
22
|
*/
|
|
23
23
|
export class Heap<E = any> extends IterableElementBase<E> {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
constructor(elements?: Iterable<E>, options?: HeapOptions<E>) {
|
|
24
|
+
constructor(elements: Iterable<E> = [], options?: HeapOptions<E>) {
|
|
27
25
|
super();
|
|
28
|
-
|
|
29
|
-
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
30
|
-
throw new Error('The a, b params of compare function must be number');
|
|
31
|
-
} else {
|
|
32
|
-
return a - b;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
26
|
+
|
|
35
27
|
if (options) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.options = {
|
|
39
|
-
comparator: defaultComparator
|
|
40
|
-
};
|
|
28
|
+
const { comparator } = options;
|
|
29
|
+
if (comparator) this._comparator = comparator;
|
|
41
30
|
}
|
|
42
31
|
|
|
43
32
|
if (elements) {
|
|
@@ -48,6 +37,18 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
48
37
|
}
|
|
49
38
|
}
|
|
50
39
|
|
|
40
|
+
protected _comparator = (a: E, b: E) => {
|
|
41
|
+
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
42
|
+
throw new Error('The a, b params of compare function must be number');
|
|
43
|
+
} else {
|
|
44
|
+
return a - b;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
get comparator() {
|
|
49
|
+
return this._comparator;
|
|
50
|
+
}
|
|
51
|
+
|
|
51
52
|
protected _elements: E[] = [];
|
|
52
53
|
|
|
53
54
|
get elements(): E[] {
|
|
@@ -278,7 +279,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
278
279
|
* @returns A new Heap instance containing the same elements.
|
|
279
280
|
*/
|
|
280
281
|
clone(): Heap<E> {
|
|
281
|
-
const clonedHeap = new Heap<E>([], this.
|
|
282
|
+
const clonedHeap = new Heap<E>([], { comparator: this.comparator });
|
|
282
283
|
clonedHeap._elements = [...this.elements];
|
|
283
284
|
return clonedHeap;
|
|
284
285
|
}
|
|
@@ -413,7 +414,7 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
413
414
|
while (index > 0) {
|
|
414
415
|
const parent = (index - 1) >> 1;
|
|
415
416
|
const parentItem = this.elements[parent];
|
|
416
|
-
if (this.
|
|
417
|
+
if (this.comparator(parentItem, element) <= 0) break;
|
|
417
418
|
this.elements[index] = parentItem;
|
|
418
419
|
index = parent;
|
|
419
420
|
}
|
|
@@ -435,11 +436,11 @@ export class Heap<E = any> extends IterableElementBase<E> {
|
|
|
435
436
|
let left = (index << 1) | 1;
|
|
436
437
|
const right = left + 1;
|
|
437
438
|
let minItem = this.elements[left];
|
|
438
|
-
if (right < this.elements.length && this.
|
|
439
|
+
if (right < this.elements.length && this.comparator(minItem, this.elements[right]) > 0) {
|
|
439
440
|
left = right;
|
|
440
441
|
minItem = this.elements[right];
|
|
441
442
|
}
|
|
442
|
-
if (this.
|
|
443
|
+
if (this.comparator(minItem, element) >= 0) break;
|
|
443
444
|
this.elements[index] = minItem;
|
|
444
445
|
index = left;
|
|
445
446
|
}
|
|
@@ -20,7 +20,7 @@ import { Heap } from './heap';
|
|
|
20
20
|
*/
|
|
21
21
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
22
22
|
constructor(
|
|
23
|
-
elements
|
|
23
|
+
elements: Iterable<E> = [],
|
|
24
24
|
options: HeapOptions<E> = {
|
|
25
25
|
comparator: (a: E, b: E) => {
|
|
26
26
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
@@ -20,7 +20,7 @@ import { Heap } from './heap';
|
|
|
20
20
|
*/
|
|
21
21
|
export class MinHeap<E = any> extends Heap<E> {
|
|
22
22
|
constructor(
|
|
23
|
-
elements
|
|
23
|
+
elements: Iterable<E> = [],
|
|
24
24
|
options: HeapOptions<E> = {
|
|
25
25
|
comparator: (a: E, b: E) => {
|
|
26
26
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
@@ -35,7 +35,7 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
35
35
|
/**
|
|
36
36
|
* The constructor initializes the linked list with an empty head, tail, and size.
|
|
37
37
|
*/
|
|
38
|
-
constructor(elements
|
|
38
|
+
constructor(elements: Iterable<E> = []) {
|
|
39
39
|
super();
|
|
40
40
|
this._head = undefined;
|
|
41
41
|
this._tail = undefined;
|
|
@@ -27,11 +27,8 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
27
27
|
/**
|
|
28
28
|
* The constructor initializes the linked list with an empty head, tail, and length.
|
|
29
29
|
*/
|
|
30
|
-
constructor(elements
|
|
30
|
+
constructor(elements: Iterable<E> = []) {
|
|
31
31
|
super();
|
|
32
|
-
this._head = undefined;
|
|
33
|
-
this._tail = undefined;
|
|
34
|
-
this._size = 0;
|
|
35
32
|
if (elements) {
|
|
36
33
|
for (const el of elements) this.push(el);
|
|
37
34
|
}
|
|
@@ -49,7 +46,7 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
|
|
|
49
46
|
return this._tail;
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
protected _size: number;
|
|
49
|
+
protected _size: number = 0;
|
|
53
50
|
|
|
54
51
|
get size(): number {
|
|
55
52
|
return this._size;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { SkipLinkedListOptions } from '../../types';
|
|
8
9
|
|
|
9
10
|
export class SkipListNode<K, V> {
|
|
10
11
|
key: K;
|
|
@@ -19,39 +20,37 @@ export class SkipListNode<K, V> {
|
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export class SkipList<K, V> {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this._maxLevel = maxLevel;
|
|
33
|
-
this._probability = probability;
|
|
23
|
+
constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
|
|
24
|
+
if (options) {
|
|
25
|
+
const { maxLevel, probability } = options;
|
|
26
|
+
if (typeof maxLevel === 'number') this._maxLevel = maxLevel;
|
|
27
|
+
if (typeof probability === 'number') this._probability = probability;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (elements) {
|
|
31
|
+
for (const [key, value] of elements) this.add(key, value);
|
|
32
|
+
}
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
protected _head: SkipListNode<K, V
|
|
35
|
+
protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
|
|
37
36
|
|
|
38
37
|
get head(): SkipListNode<K, V> {
|
|
39
38
|
return this._head;
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
protected _level: number;
|
|
41
|
+
protected _level: number = 0;
|
|
43
42
|
|
|
44
43
|
get level(): number {
|
|
45
44
|
return this._level;
|
|
46
45
|
}
|
|
47
46
|
|
|
48
|
-
protected _maxLevel: number;
|
|
47
|
+
protected _maxLevel: number = 16;
|
|
49
48
|
|
|
50
49
|
get maxLevel(): number {
|
|
51
50
|
return this._maxLevel;
|
|
52
51
|
}
|
|
53
52
|
|
|
54
|
-
protected _probability: number;
|
|
53
|
+
protected _probability: number = 0.5;
|
|
55
54
|
|
|
56
55
|
get probability(): number {
|
|
57
56
|
return this._probability;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
import type { MatrixOptions } from '../../types';
|
|
8
9
|
|
|
9
10
|
export class Matrix {
|
|
10
11
|
/**
|
|
@@ -14,16 +15,7 @@ export class Matrix {
|
|
|
14
15
|
* @param [options] - The `options` parameter is an optional object that can contain the following
|
|
15
16
|
* properties:
|
|
16
17
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
data: number[][],
|
|
19
|
-
options?: {
|
|
20
|
-
rows?: number;
|
|
21
|
-
cols?: number;
|
|
22
|
-
addFn?: (a: number, b: number) => any;
|
|
23
|
-
subtractFn?: (a: number, b: number) => any;
|
|
24
|
-
multiplyFn?: (a: number, b: number) => any;
|
|
25
|
-
}
|
|
26
|
-
) {
|
|
18
|
+
constructor(data: number[][], options?: MatrixOptions) {
|
|
27
19
|
if (options) {
|
|
28
20
|
const { rows, cols, addFn, subtractFn, multiplyFn } = options;
|
|
29
21
|
if (typeof rows === 'number' && rows > 0) this._rows = rows;
|
|
@@ -10,7 +10,7 @@ import { PriorityQueue } from './priority-queue';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
elements
|
|
13
|
+
elements: Iterable<E> = [],
|
|
14
14
|
options: PriorityQueueOptions<E> = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
@@ -10,7 +10,7 @@ import { PriorityQueue } from './priority-queue';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
elements
|
|
13
|
+
elements: Iterable<E> = [],
|
|
14
14
|
options: PriorityQueueOptions<E> = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
@@ -17,7 +17,7 @@ import { Heap } from '../heap';
|
|
|
17
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
18
|
*/
|
|
19
19
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
20
|
-
constructor(elements
|
|
20
|
+
constructor(elements: Iterable<E> = [], options?: PriorityQueueOptions<E>) {
|
|
21
21
|
super(elements, options);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback, IterableWithSizeOrLength } from '../../types';
|
|
8
|
+
import type { DequeOptions, ElementCallback, IterableWithSizeOrLength } from '../../types';
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
import { calcMinUnitsRequired, rangeCheck } from '../../utils';
|
|
11
11
|
|
|
@@ -22,19 +22,16 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
22
22
|
protected _bucketLast = 0;
|
|
23
23
|
protected _lastInBucket = 0;
|
|
24
24
|
protected _bucketCount = 0;
|
|
25
|
-
protected readonly _bucketSize: number;
|
|
25
|
+
protected readonly _bucketSize: number = 1 << 12;
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
* The constructor initializes a data structure with a specified bucket size and populates it with
|
|
29
|
-
* elements from an iterable.
|
|
30
|
-
* @param elements - The `elements` parameter is an iterable object (such as an array or a Set) that
|
|
31
|
-
* contains the initial elements to be stored in the data structure. It can also be an object with a
|
|
32
|
-
* `length` property or a `size` property, which represents the number of elements in the iterable.
|
|
33
|
-
* @param bucketSize - The `bucketSize` parameter is the maximum number of elements that can be
|
|
34
|
-
* stored in each bucket. It determines the size of each bucket in the data structure.
|
|
35
|
-
*/
|
|
36
|
-
constructor(elements: IterableWithSizeOrLength<E> = [], bucketSize = 1 << 12) {
|
|
27
|
+
constructor(elements: IterableWithSizeOrLength<E> = [], options?: DequeOptions) {
|
|
37
28
|
super();
|
|
29
|
+
|
|
30
|
+
if (options) {
|
|
31
|
+
const { bucketSize } = options;
|
|
32
|
+
if (typeof bucketSize === 'number') this._bucketSize = bucketSize;
|
|
33
|
+
}
|
|
34
|
+
|
|
38
35
|
let _size: number;
|
|
39
36
|
if ('length' in elements) {
|
|
40
37
|
if (elements.length instanceof Function) _size = elements.length();
|
|
@@ -44,7 +41,6 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
44
41
|
else _size = elements.size;
|
|
45
42
|
}
|
|
46
43
|
|
|
47
|
-
this._bucketSize = bucketSize;
|
|
48
44
|
this._bucketCount = calcMinUnitsRequired(_size, this._bucketSize) || 1;
|
|
49
45
|
for (let i = 0; i < this._bucketCount; ++i) {
|
|
50
46
|
this._buckets.push(new Array(this._bucketSize));
|
|
@@ -637,7 +633,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
637
633
|
* satisfy the given predicate function.
|
|
638
634
|
*/
|
|
639
635
|
filter(predicate: ElementCallback<E, boolean>, thisArg?: any): Deque<E> {
|
|
640
|
-
const newDeque = new Deque<E>([], this._bucketSize);
|
|
636
|
+
const newDeque = new Deque<E>([], { bucketSize: this._bucketSize });
|
|
641
637
|
let index = 0;
|
|
642
638
|
for (const el of this) {
|
|
643
639
|
if (predicate.call(thisArg, el, index, this)) {
|
|
@@ -666,7 +662,7 @@ export class Deque<E> extends IterableElementBase<E> {
|
|
|
666
662
|
* @returns a new Deque object with the mapped values.
|
|
667
663
|
*/
|
|
668
664
|
map<T>(callback: ElementCallback<E, T>, thisArg?: any): Deque<T> {
|
|
669
|
-
const newDeque = new Deque<T>([], this._bucketSize);
|
|
665
|
+
const newDeque = new Deque<T>([], { bucketSize: this._bucketSize });
|
|
670
666
|
let index = 0;
|
|
671
667
|
for (const el of this) {
|
|
672
668
|
newDeque.push(callback.call(thisArg, el, index, this));
|
|
@@ -13,29 +13,30 @@ import { SinglyLinkedList } from '../linked-list';
|
|
|
13
13
|
* 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
|
|
14
14
|
* 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
|
|
15
15
|
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
|
16
|
-
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store
|
|
16
|
+
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
|
|
17
17
|
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
18
18
|
*/
|
|
19
19
|
export class Queue<E = any> extends IterableElementBase<E> {
|
|
20
20
|
/**
|
|
21
21
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
22
22
|
* @param {E[]} [elements] - The `elements` parameter is an optional array of elements of type `E`. If provided, it
|
|
23
|
-
* will be used to initialize the `
|
|
23
|
+
* will be used to initialize the `_elements` property of the class. If not provided, the `_elements` property will be
|
|
24
24
|
* initialized as an empty array.
|
|
25
25
|
*/
|
|
26
|
-
constructor(elements
|
|
26
|
+
constructor(elements: Iterable<E> = []) {
|
|
27
27
|
super();
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
if (elements) {
|
|
29
|
+
for (const el of elements) this.push(el);
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
protected
|
|
33
|
+
protected _elements: E[] = [];
|
|
33
34
|
|
|
34
|
-
get
|
|
35
|
-
return this.
|
|
35
|
+
get elements(): E[] {
|
|
36
|
+
return this._elements;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
protected _offset: number;
|
|
39
|
+
protected _offset: number = 0;
|
|
39
40
|
|
|
40
41
|
get offset(): number {
|
|
41
42
|
return this._offset;
|
|
@@ -46,19 +47,19 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
46
47
|
* @returns {number} The size of the array, which is the difference between the length of the array and the offset.
|
|
47
48
|
*/
|
|
48
49
|
get size(): number {
|
|
49
|
-
return this.
|
|
50
|
+
return this.elements.length - this.offset;
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
/**
|
|
53
54
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
54
55
|
* Space Complexity: O(1) - no additional space is used.
|
|
55
56
|
*
|
|
56
|
-
* The `first` function returns the first element of the array `
|
|
57
|
-
* @returns The `get first()` method returns the first element of the data structure, represented by the `
|
|
57
|
+
* The `first` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
58
|
+
* @returns The `get first()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
58
59
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
59
60
|
*/
|
|
60
61
|
get first(): E | undefined {
|
|
61
|
-
return this.size > 0 ? this.
|
|
62
|
+
return this.size > 0 ? this.elements[this.offset] : undefined;
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
|
@@ -71,11 +72,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
71
72
|
* Space Complexity: O(1) - no additional space is used.
|
|
72
73
|
*
|
|
73
74
|
* The `last` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
74
|
-
* @returns The method `get last()` returns the last element of the `
|
|
75
|
+
* @returns The method `get last()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
75
76
|
* array is empty, it returns `undefined`.
|
|
76
77
|
*/
|
|
77
78
|
get last(): E | undefined {
|
|
78
|
-
return this.size > 0 ? this.
|
|
79
|
+
return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
/**
|
|
@@ -109,7 +110,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
109
110
|
* @returns The `add` method is returning a `Queue<E>` object.
|
|
110
111
|
*/
|
|
111
112
|
push(element: E): boolean {
|
|
112
|
-
this.
|
|
113
|
+
this.elements.push(element);
|
|
113
114
|
return true;
|
|
114
115
|
}
|
|
115
116
|
|
|
@@ -132,11 +133,11 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
132
133
|
const first = this.first;
|
|
133
134
|
this._offset += 1;
|
|
134
135
|
|
|
135
|
-
if (this.offset * 2 < this.
|
|
136
|
+
if (this.offset * 2 < this.elements.length) return first;
|
|
136
137
|
|
|
137
138
|
// only delete dequeued elements when reaching half size
|
|
138
139
|
// to decrease latency of shifting elements.
|
|
139
|
-
this.
|
|
140
|
+
this._elements = this.elements.slice(this.offset);
|
|
140
141
|
this._offset = 0;
|
|
141
142
|
return first;
|
|
142
143
|
}
|
|
@@ -150,8 +151,8 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
150
151
|
* Time Complexity: O(1) - constant time as it retrieves the value at the current offset.
|
|
151
152
|
* Space Complexity: O(1) - no additional space is used.
|
|
152
153
|
*
|
|
153
|
-
* The `peek` function returns the first element of the array `
|
|
154
|
-
* @returns The `peek()` method returns the first element of the data structure, represented by the `
|
|
154
|
+
* The `peek` function returns the first element of the array `_elements` if it exists, otherwise it returns `undefined`.
|
|
155
|
+
* @returns The `peek()` method returns the first element of the data structure, represented by the `_elements` array at
|
|
155
156
|
* the `_offset` index. If the data structure is empty (size is 0), it returns `undefined`.
|
|
156
157
|
*/
|
|
157
158
|
peek(): E | undefined {
|
|
@@ -168,7 +169,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
168
169
|
* Space Complexity: O(1) - no additional space is used.
|
|
169
170
|
*
|
|
170
171
|
* The `peekLast` function returns the last element in an array-like data structure, or undefined if the structure is empty.
|
|
171
|
-
* @returns The method `peekLast()` returns the last element of the `
|
|
172
|
+
* @returns The method `peekLast()` returns the last element of the `_elements` array if the array is not empty. If the
|
|
172
173
|
* array is empty, it returns `undefined`.
|
|
173
174
|
*/
|
|
174
175
|
peekLast(): E | undefined {
|
|
@@ -219,7 +220,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
219
220
|
* @param index
|
|
220
221
|
*/
|
|
221
222
|
getAt(index: number): E | undefined {
|
|
222
|
-
return this.
|
|
223
|
+
return this.elements[index];
|
|
223
224
|
}
|
|
224
225
|
|
|
225
226
|
/**
|
|
@@ -247,18 +248,18 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
247
248
|
* Time Complexity: O(1) - constant time as it returns a shallow copy of the internal array.
|
|
248
249
|
* Space Complexity: O(n) - where n is the number of elements in the queue.
|
|
249
250
|
*
|
|
250
|
-
* The toArray() function returns an array of elements from the current offset to the end of the
|
|
251
|
+
* The toArray() function returns an array of elements from the current offset to the end of the _elements array.
|
|
251
252
|
* @returns An array of type E is being returned.
|
|
252
253
|
*/
|
|
253
254
|
toArray(): E[] {
|
|
254
|
-
return this.
|
|
255
|
+
return this.elements.slice(this.offset);
|
|
255
256
|
}
|
|
256
257
|
|
|
257
258
|
/**
|
|
258
|
-
* The clear function resets the
|
|
259
|
+
* The clear function resets the elements array and offset to their initial values.
|
|
259
260
|
*/
|
|
260
261
|
clear(): void {
|
|
261
|
-
this.
|
|
262
|
+
this._elements = [];
|
|
262
263
|
this._offset = 0;
|
|
263
264
|
}
|
|
264
265
|
|
|
@@ -275,7 +276,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
275
276
|
* @returns The `clone()` method is returning a new instance of the `Queue` class.
|
|
276
277
|
*/
|
|
277
278
|
clone(): Queue<E> {
|
|
278
|
-
return new Queue(this.
|
|
279
|
+
return new Queue(this.elements.slice(this.offset));
|
|
279
280
|
}
|
|
280
281
|
|
|
281
282
|
/**
|
|
@@ -345,7 +346,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|
|
345
346
|
*/
|
|
346
347
|
|
|
347
348
|
protected* _getIterator(): IterableIterator<E> {
|
|
348
|
-
for (const item of this.
|
|
349
|
+
for (const item of this.elements) {
|
|
349
350
|
yield item;
|
|
350
351
|
}
|
|
351
352
|
}
|
|
@@ -23,17 +23,14 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|
|
23
23
|
* of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
|
|
24
24
|
* is provided and is an array, it is assigned to the `_elements
|
|
25
25
|
*/
|
|
26
|
-
constructor(elements
|
|
26
|
+
constructor(elements: Iterable<E> = []) {
|
|
27
27
|
super();
|
|
28
|
-
this._elements = [];
|
|
29
28
|
if (elements) {
|
|
30
|
-
for (const el of elements)
|
|
31
|
-
this.push(el);
|
|
32
|
-
}
|
|
29
|
+
for (const el of elements) this.push(el);
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
|
|
36
|
-
protected _elements: E[];
|
|
33
|
+
protected _elements: E[] = [];
|
|
37
34
|
|
|
38
35
|
get elements(): E[] {
|
|
39
36
|
return this._elements;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElementCallback } from '../../types';
|
|
8
|
+
import type { ElementCallback, TrieOptions } from '../../types';
|
|
9
9
|
import { IterableElementBase } from '../base';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -38,31 +38,30 @@ export class TrieNode {
|
|
|
38
38
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
|
|
39
39
|
*/
|
|
40
40
|
export class Trie extends IterableElementBase<string> {
|
|
41
|
-
constructor(words
|
|
41
|
+
constructor(words: Iterable<string> = [], options?: TrieOptions) {
|
|
42
42
|
super();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
if (options) {
|
|
44
|
+
const { caseSensitive } = options;
|
|
45
|
+
if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
|
|
46
|
+
}
|
|
46
47
|
if (words) {
|
|
47
|
-
for (const word of words)
|
|
48
|
-
this.add(word);
|
|
49
|
-
}
|
|
48
|
+
for (const word of words) this.add(word);
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
protected _size: number;
|
|
52
|
+
protected _size: number = 0;
|
|
54
53
|
|
|
55
54
|
get size(): number {
|
|
56
55
|
return this._size;
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
protected _caseSensitive: boolean;
|
|
58
|
+
protected _caseSensitive: boolean = true;
|
|
60
59
|
|
|
61
60
|
get caseSensitive(): boolean {
|
|
62
61
|
return this._caseSensitive;
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
protected _root: TrieNode;
|
|
64
|
+
protected _root: TrieNode = new TrieNode('');
|
|
66
65
|
|
|
67
66
|
get root() {
|
|
68
67
|
return this._root;
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
BinaryTreeNodeNested,
|
|
6
6
|
BinaryTreeOptions,
|
|
7
7
|
BTNCallback,
|
|
8
|
-
|
|
8
|
+
KeyOrNodeOrEntry
|
|
9
9
|
} from '../types';
|
|
10
10
|
|
|
11
11
|
export interface IBinaryTree<
|
|
@@ -18,9 +18,9 @@ export interface IBinaryTree<
|
|
|
18
18
|
|
|
19
19
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
20
20
|
|
|
21
|
-
add(keyOrNodeOrEntry:
|
|
21
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
22
22
|
|
|
23
|
-
addMany(nodes: Iterable<
|
|
23
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
24
24
|
|
|
25
25
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
26
26
|
}
|
package/src/types/common.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export enum BSTVariant {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
STANDARD = 'STANDARD',
|
|
3
|
+
INVERSE = 'INVERSE'
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export enum CP {
|
|
@@ -54,7 +54,7 @@ export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
|
54
54
|
|
|
55
55
|
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
56
56
|
|
|
57
|
-
export type
|
|
57
|
+
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
58
58
|
|
|
59
59
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
60
60
|
|
|
@@ -6,6 +6,6 @@ export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K,
|
|
|
6
6
|
export type BinaryTreeNested<K, V, N extends BinaryTreeNode<K, V, N>> = BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
7
|
|
|
8
8
|
export type BinaryTreeOptions<K> = {
|
|
9
|
-
iterationType
|
|
10
|
-
extractor
|
|
9
|
+
iterationType?: IterationType,
|
|
10
|
+
extractor?: (key: K) => number
|
|
11
11
|
}
|
|
@@ -7,5 +7,5 @@ export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTN
|
|
|
7
7
|
export type BSTNested<K, V, N extends BSTNode<K, V, N>> = BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
9
|
export type BSTOptions<K> = BinaryTreeOptions<K> & {
|
|
10
|
-
variant
|
|
10
|
+
variant?: BSTVariant
|
|
11
11
|
}
|
|
@@ -5,4 +5,4 @@ export type TreeMultimapNodeNested<K, V> = TreeMultimapNode<K, V, TreeMultimapNo
|
|
|
5
5
|
|
|
6
6
|
export type TreeMultimapNested<K, V, N extends TreeMultimapNode<K, V, N>> = TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
7
|
|
|
8
|
-
export type TreeMultimapOptions<K> =
|
|
8
|
+
export type TreeMultimapOptions<K> = AVLTreeOptions<K> & {}
|
|
@@ -5,9 +5,13 @@ export type HashMapLinkedNode<K, V> = {
|
|
|
5
5
|
prev: HashMapLinkedNode<K, V>;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
+
export type LinkedHashMapOptions<K> = {
|
|
9
|
+
hashFn?: (key: K) => string;
|
|
10
|
+
objHashFn?: (key: K) => object;
|
|
11
|
+
};
|
|
12
|
+
|
|
8
13
|
export type HashMapOptions<K> = {
|
|
9
|
-
hashFn
|
|
10
|
-
objHashFn: (key: K) => object;
|
|
14
|
+
hashFn?: (key: K) => string;
|
|
11
15
|
};
|
|
12
16
|
|
|
13
17
|
export type HashMapStoreItem<K, V> = { key: K; value: V };
|