min-heap-typed 1.38.9 → 1.39.1
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 +2 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +86 -32
- package/dist/data-structures/binary-tree/binary-tree.js +8 -8
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +4 -4
- package/dist/data-structures/graph/abstract-graph.js +5 -5
- package/dist/data-structures/graph/directed-graph.d.ts +4 -4
- package/dist/data-structures/graph/directed-graph.js +6 -6
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/map-graph.js +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
- package/dist/data-structures/graph/undirected-graph.js +4 -4
- package/dist/data-structures/heap/heap.d.ts +10 -5
- package/dist/data-structures/heap/heap.js +10 -10
- package/dist/data-structures/heap/max-heap.d.ts +4 -1
- package/dist/data-structures/heap/max-heap.js +9 -7
- package/dist/data-structures/heap/min-heap.d.ts +4 -1
- package/dist/data-structures/heap/min-heap.js +9 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/data-structures/matrix/matrix2d.js +3 -7
- package/dist/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/data-structures/matrix/vector2d.js +0 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +9 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +5 -2
- package/dist/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/helpers.d.ts +1 -4
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +5 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +21 -1
- package/src/data-structures/binary-tree/binary-tree.ts +37 -93
- package/src/data-structures/binary-tree/bst.ts +11 -17
- package/src/data-structures/binary-tree/rb-tree.ts +2 -1
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +16 -15
- package/src/data-structures/graph/directed-graph.ts +8 -7
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/graph/undirected-graph.ts +9 -8
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/tree-map.ts +1 -2
- package/src/data-structures/hash/tree-set.ts +1 -2
- package/src/data-structures/heap/heap.ts +12 -12
- package/src/data-structures/heap/max-heap.ts +8 -6
- package/src/data-structures/heap/min-heap.ts +8 -6
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +1 -4
- package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -5
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/helpers.ts +1 -7
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -7,12 +7,16 @@
|
|
|
7
7
|
|
|
8
8
|
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
9
9
|
|
|
10
|
-
export class Heap<E> {
|
|
10
|
+
export class Heap<E = any> {
|
|
11
11
|
protected nodes: E[] = [];
|
|
12
12
|
protected readonly comparator: Comparator<E>;
|
|
13
13
|
|
|
14
|
-
constructor(comparator: Comparator<E
|
|
15
|
-
this.comparator = comparator;
|
|
14
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
15
|
+
this.comparator = options.comparator;
|
|
16
|
+
if (options.nodes && options.nodes.length > 0) {
|
|
17
|
+
this.nodes = options.nodes;
|
|
18
|
+
this.fix();
|
|
19
|
+
}
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
/**
|
|
@@ -32,15 +36,11 @@ export class Heap<E> {
|
|
|
32
36
|
|
|
33
37
|
/**
|
|
34
38
|
* Static method that creates a binary heap from an array of nodes and a comparison function.
|
|
35
|
-
* @param nodes
|
|
36
|
-
* @param comparator - Comparison function.
|
|
37
39
|
* @returns A new Heap instance.
|
|
40
|
+
* @param options
|
|
38
41
|
*/
|
|
39
|
-
static heapify<E>(nodes: E[]
|
|
40
|
-
|
|
41
|
-
binaryHeap.nodes = [...nodes];
|
|
42
|
-
binaryHeap.fix(); // Fix heap properties
|
|
43
|
-
return binaryHeap;
|
|
42
|
+
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
|
43
|
+
return new Heap<E>(options);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -180,7 +180,7 @@ export class Heap<E> {
|
|
|
180
180
|
* @returns A new Heap instance containing the same elements.
|
|
181
181
|
*/
|
|
182
182
|
clone(): Heap<E> {
|
|
183
|
-
const clonedHeap = new Heap<E>(this.comparator);
|
|
183
|
+
const clonedHeap = new Heap<E>({comparator: this.comparator});
|
|
184
184
|
clonedHeap.nodes = [...this.nodes];
|
|
185
185
|
return clonedHeap;
|
|
186
186
|
}
|
|
@@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
|
|
|
269
269
|
|
|
270
270
|
export class FibonacciHeap<E> {
|
|
271
271
|
root?: FibonacciHeapNode<E>;
|
|
272
|
-
size
|
|
272
|
+
size = 0;
|
|
273
273
|
protected min?: FibonacciHeapNode<E>;
|
|
274
274
|
protected readonly comparator: Comparator<E>;
|
|
275
275
|
|
|
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator: Comparator<E
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
|
+
comparator: (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 b - a;
|
|
20
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
) {
|
|
22
|
-
super(
|
|
24
|
+
super(options);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator: Comparator<E
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
|
+
comparator: (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
|
+
}
|
|
19
21
|
}
|
|
20
22
|
}
|
|
21
23
|
) {
|
|
22
|
-
super(
|
|
24
|
+
super(options);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
|
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {
|
|
17
|
+
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import Vector2D from './vector2d';
|
|
8
|
+
import {Vector2D} from './vector2d';
|
|
9
9
|
|
|
10
10
|
export class Matrix2D {
|
|
11
11
|
private readonly _matrix: number[][];
|
|
@@ -209,5 +209,3 @@ export class Matrix2D {
|
|
|
209
209
|
return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
210
210
|
}
|
|
211
211
|
}
|
|
212
|
-
|
|
213
|
-
export default Matrix2D;
|
|
@@ -10,8 +10,7 @@ export class Vector2D {
|
|
|
10
10
|
public x: number = 0,
|
|
11
11
|
public y: number = 0,
|
|
12
12
|
public w: number = 1 // needed for matrix multiplication
|
|
13
|
-
) {
|
|
14
|
-
}
|
|
13
|
+
) {}
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* The function checks if the x and y values of a point are both zero.
|
|
@@ -313,5 +312,3 @@ export class Vector2D {
|
|
|
313
312
|
this.y = 0;
|
|
314
313
|
}
|
|
315
314
|
}
|
|
316
|
-
|
|
317
|
-
export default Vector2D;
|
|
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: 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 b - a;
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: 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
|
+
}
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
) {
|
|
21
|
-
super(
|
|
23
|
+
super(options);
|
|
22
24
|
}
|
|
23
25
|
}
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
import {Heap} from '../heap';
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
|
-
export class PriorityQueue<E> extends Heap<E> {
|
|
13
|
-
constructor(comparator: Comparator<E
|
|
14
|
-
super(
|
|
12
|
+
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
14
|
+
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
-
}
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {}
|
|
14
13
|
|
|
15
14
|
// O(1) time complexity of obtaining the value
|
|
16
15
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
|
|
|
20
19
|
if (capacity !== undefined) this._capacity = capacity;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
private _nodes: {
|
|
22
|
+
private _nodes: {[key: number]: E} = {};
|
|
24
23
|
|
|
25
|
-
get nodes(): {
|
|
24
|
+
get nodes(): {[p: number]: E} {
|
|
26
25
|
return this._nodes;
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -157,7 +156,7 @@ export class ObjectDeque<E = number> {
|
|
|
157
156
|
return this._size <= 0;
|
|
158
157
|
}
|
|
159
158
|
|
|
160
|
-
protected _seNodes(value: {
|
|
159
|
+
protected _seNodes(value: {[p: number]: E}) {
|
|
161
160
|
this._nodes = value;
|
|
162
161
|
}
|
|
163
162
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
5
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
7
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
8
|
|
|
9
|
-
delete<C extends
|
|
9
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
|
@@ -24,10 +24,6 @@ export enum FamilyPosition {
|
|
|
24
24
|
|
|
25
25
|
export type BinaryTreeNodeKey = number;
|
|
26
26
|
|
|
27
|
-
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
28
|
-
|
|
29
|
-
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
30
|
-
|
|
31
27
|
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
|
|
32
28
|
|
|
33
29
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
package/src/types/helpers.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import {BinaryTreeNodeKey} from './data-structures';
|
|
2
|
-
|
|
3
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
4
2
|
|
|
5
3
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
6
4
|
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
10
|
-
|
|
11
|
-
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
5
|
+
export type OneParamCallback<N, D = any> = (node: N) => D;
|
|
12
6
|
|
|
13
7
|
export enum CP {
|
|
14
8
|
lt = 'lt',
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ToThunkFn = () => ReturnType<TrlFn>;
|
|
2
|
-
export type Thunk = () => ReturnType<ToThunkFn> & {
|
|
2
|
+
export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
|
|
3
3
|
export type TrlFn = (...args: any[]) => any;
|
|
4
4
|
export type TrlAsyncFn = (...args: any[]) => any;
|
|
5
5
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type KeyValueObject = {
|
|
1
|
+
export type KeyValueObject = {[key: string]: any};
|
|
2
2
|
|
|
3
|
-
export type KeyValueObjectWithKey = {
|
|
3
|
+
export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
|
|
4
4
|
|
|
5
5
|
export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
|
|
6
6
|
|