min-heap-typed 1.40.0 → 1.41.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/binary-tree.d.ts +14 -3
- package/dist/data-structures/binary-tree/binary-tree.js +52 -10
- package/dist/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/data-structures/binary-tree/bst.js +3 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +96 -9
- package/dist/data-structures/binary-tree/rb-tree.js +377 -12
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +87 -17
- package/src/data-structures/binary-tree/bst.ts +10 -7
- package/src/data-structures/binary-tree/rb-tree.ts +396 -349
- package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
- package/src/data-structures/graph/abstract-graph.ts +11 -12
- package/src/data-structures/graph/directed-graph.ts +7 -6
- package/src/data-structures/graph/undirected-graph.ts +7 -6
- 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 +2 -2
- 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 +1 -1
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/matrix/vector2d.ts +1 -2
- 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 +3 -4
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -37,7 +37,8 @@ export class TreeMultisetNode<
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
|
39
39
|
extends AVLTree<V, N>
|
|
40
|
-
implements IBinaryTree<V, N>
|
|
40
|
+
implements IBinaryTree<V, N>
|
|
41
|
+
{
|
|
41
42
|
/**
|
|
42
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
43
44
|
* merge duplicated values.
|
|
@@ -169,7 +170,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
169
170
|
} else if (parent.right === undefined) {
|
|
170
171
|
parent.right = newNode;
|
|
171
172
|
if (newNode !== null) {
|
|
172
|
-
this._size =
|
|
173
|
+
this._size = this.size + 1;
|
|
173
174
|
this._setCount(this.count + newNode.count);
|
|
174
175
|
}
|
|
175
176
|
return parent.right;
|
|
@@ -282,7 +283,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|
|
282
283
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
283
284
|
if (!this.root) return bstDeletedResult;
|
|
284
285
|
|
|
285
|
-
const curr: N | null = this.
|
|
286
|
+
const curr: N | null = this.getNode(identifier, callback);
|
|
286
287
|
if (!curr) return bstDeletedResult;
|
|
287
288
|
|
|
288
289
|
const parent: N | null = curr?.parent ? curr.parent : null;
|
|
@@ -26,7 +26,6 @@ export abstract class AbstractVertex<V = any> {
|
|
|
26
26
|
this.key = key;
|
|
27
27
|
this.value = value;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
export abstract class AbstractEdge<E = any> {
|
|
@@ -65,7 +64,8 @@ export abstract class AbstractGraph<
|
|
|
65
64
|
E = any,
|
|
66
65
|
VO extends AbstractVertex<V> = AbstractVertex<V>,
|
|
67
66
|
EO extends AbstractEdge<E> = AbstractEdge<E>
|
|
68
|
-
> implements IGraph<V, E, VO, EO>
|
|
67
|
+
> implements IGraph<V, E, VO, EO>
|
|
68
|
+
{
|
|
69
69
|
protected _vertices: Map<VertexKey, VO> = new Map<VertexKey, VO>();
|
|
70
70
|
|
|
71
71
|
get vertices(): Map<VertexKey, VO> {
|
|
@@ -513,14 +513,14 @@ export abstract class AbstractGraph<
|
|
|
513
513
|
}
|
|
514
514
|
|
|
515
515
|
getMinDist &&
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
516
|
+
distMap.forEach((d, v) => {
|
|
517
|
+
if (v !== srcVertex) {
|
|
518
|
+
if (d < minDist) {
|
|
519
|
+
minDist = d;
|
|
520
|
+
if (genPaths) minDest = v;
|
|
521
|
+
}
|
|
521
522
|
}
|
|
522
|
-
}
|
|
523
|
-
});
|
|
523
|
+
});
|
|
524
524
|
|
|
525
525
|
genPaths && getPaths(minDest);
|
|
526
526
|
|
|
@@ -582,7 +582,7 @@ export abstract class AbstractGraph<
|
|
|
582
582
|
if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
|
|
583
583
|
}
|
|
584
584
|
|
|
585
|
-
const heap = new PriorityQueue<{
|
|
585
|
+
const heap = new PriorityQueue<{key: number; value: VO}>({comparator: (a, b) => a.key - b.key});
|
|
586
586
|
heap.add({key: 0, value: srcVertex});
|
|
587
587
|
|
|
588
588
|
distMap.set(srcVertex, 0);
|
|
@@ -811,7 +811,7 @@ export abstract class AbstractGraph<
|
|
|
811
811
|
* `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
|
|
812
812
|
* path between vertices in the
|
|
813
813
|
*/
|
|
814
|
-
floyd(): {
|
|
814
|
+
floyd(): {costs: number[][]; predecessor: (VO | null)[][]} {
|
|
815
815
|
const idAndVertices = [...this._vertices];
|
|
816
816
|
const n = idAndVertices.length;
|
|
817
817
|
|
|
@@ -996,5 +996,4 @@ export abstract class AbstractGraph<
|
|
|
996
996
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey {
|
|
997
997
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
998
998
|
}
|
|
999
|
-
|
|
1000
999
|
}
|
|
@@ -46,13 +46,14 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export class DirectedGraph<
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
>
|
|
49
|
+
V = any,
|
|
50
|
+
E = any,
|
|
51
|
+
VO extends DirectedVertex<V> = DirectedVertex<V>,
|
|
52
|
+
EO extends DirectedEdge<E> = DirectedEdge<E>
|
|
53
|
+
>
|
|
54
54
|
extends AbstractGraph<V, E, VO, EO>
|
|
55
|
-
implements IGraph<V, E, VO, EO>
|
|
55
|
+
implements IGraph<V, E, VO, EO>
|
|
56
|
+
{
|
|
56
57
|
/**
|
|
57
58
|
* The constructor function initializes an instance of a class.
|
|
58
59
|
*/
|
|
@@ -43,13 +43,14 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class UndirectedGraph<
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
>
|
|
46
|
+
V = any,
|
|
47
|
+
E = any,
|
|
48
|
+
VO extends UndirectedVertex<V> = UndirectedVertex<V>,
|
|
49
|
+
EO extends UndirectedEdge<E> = UndirectedEdge<E>
|
|
50
|
+
>
|
|
51
51
|
extends AbstractGraph<V, E, VO, EO>
|
|
52
|
-
implements IGraph<V, E, VO, EO>
|
|
52
|
+
implements IGraph<V, E, VO, EO>
|
|
53
|
+
{
|
|
53
54
|
/**
|
|
54
55
|
* The constructor initializes a new Map object to store edges.
|
|
55
56
|
*/
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeMap {
|
|
2
|
-
}
|
|
1
|
+
export class TreeMap {}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export class TreeSet {
|
|
2
|
-
}
|
|
1
|
+
export class TreeSet {}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import type {Comparator, DFSOrderPattern} from '../../types';
|
|
9
9
|
|
|
10
10
|
export class Heap<E = any> {
|
|
11
|
-
constructor(options: {
|
|
11
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
12
12
|
this._comparator = options.comparator;
|
|
13
13
|
if (options.nodes && options.nodes.length > 0) {
|
|
14
14
|
this._nodes = options.nodes;
|
|
@@ -48,7 +48,7 @@ export class Heap<E = any> {
|
|
|
48
48
|
* @returns A new Heap instance.
|
|
49
49
|
* @param options
|
|
50
50
|
*/
|
|
51
|
-
static heapify<E>(options: {
|
|
51
|
+
static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
|
|
52
52
|
return new Heap<E>(options);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
|
|
|
11
11
|
|
|
12
12
|
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
options: {
|
|
14
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
15
15
|
comparator: (a: E, b: E) => {
|
|
16
16
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
17
17
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -594,7 +594,7 @@ export class DoublyLinkedList<E = any> {
|
|
|
594
594
|
/**
|
|
595
595
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
596
596
|
*/
|
|
597
|
-
*
|
|
597
|
+
*[Symbol.iterator]() {
|
|
598
598
|
let current = this.head;
|
|
599
599
|
|
|
600
600
|
while (current) {
|
|
@@ -565,7 +565,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
565
565
|
/**
|
|
566
566
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
567
567
|
*/
|
|
568
|
-
*
|
|
568
|
+
*[Symbol.iterator]() {
|
|
569
569
|
let current = this.head;
|
|
570
570
|
|
|
571
571
|
while (current) {
|
|
@@ -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
|
}
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {
|
|
13
|
+
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {
|
|
13
|
+
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
14
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
|
-
protected _nodes: {
|
|
22
|
+
protected _nodes: {[key: number]: E} = {};
|
|
24
23
|
|
|
25
|
-
get nodes(): {
|
|
24
|
+
get nodes(): {[p: number]: E} {
|
|
26
25
|
return this._nodes;
|
|
27
26
|
}
|
|
28
27
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {BinaryTreeOptions} from './binary-tree';
|
|
2
|
-
import {RBTreeNode} from '../../../data-structures';
|
|
1
|
+
// import {BinaryTreeOptions} from './binary-tree';
|
|
2
|
+
// import {RBTreeNode} from '../../../data-structures';
|
|
3
3
|
|
|
4
|
-
export enum
|
|
4
|
+
export enum RBTNColor { RED = 1, BLACK = 0}
|
|
5
5
|
|
|
6
|
-
export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
|
-
|
|
8
|
-
export type RBTreeOptions = BinaryTreeOptions & {}
|
|
6
|
+
// export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
7
|
+
//
|
|
8
|
+
// export type RBTreeOptions = BinaryTreeOptions & {}
|
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
|
|