data-structure-typed 1.36.0 → 1.36.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +510 -5
- package/dist/data-structures/binary-tree/binary-tree.js +1174 -5
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/index.d.ts +0 -5
- package/dist/data-structures/binary-tree/index.js +0 -5
- package/dist/data-structures/binary-tree/index.js.map +1 -1
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +3 -3
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- 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 +2 -2
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/hash/index.js.map +1 -1
- package/dist/data-structures/heap/heap.d.ts +23 -24
- package/dist/data-structures/heap/heap.js +8 -25
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/heap/max-heap.d.ts +3 -3
- package/dist/data-structures/heap/max-heap.js.map +1 -1
- package/dist/data-structures/heap/min-heap.d.ts +3 -3
- package/dist/data-structures/heap/min-heap.js.map +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -3
- package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -3
- package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/interfaces/binary-tree.d.ts +5 -4
- package/dist/interfaces/{abstract-graph.d.ts → graph.d.ts} +1 -1
- package/dist/interfaces/{bst.js → graph.js} +1 -1
- package/dist/interfaces/graph.js.map +1 -0
- package/dist/interfaces/index.d.ts +1 -8
- package/dist/interfaces/index.js +1 -8
- package/dist/interfaces/index.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.d.ts +0 -1
- package/dist/types/data-structures/binary-tree.d.ts +32 -2
- package/dist/types/data-structures/binary-tree.js +22 -0
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/dist/types/data-structures/bst.d.ts +1 -2
- package/dist/types/data-structures/heap.d.ts +2 -1
- package/dist/types/data-structures/index.d.ts +0 -2
- package/dist/types/data-structures/index.js +0 -2
- package/dist/types/data-structures/index.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/binary-tree.d.ts +510 -5
- package/lib/data-structures/binary-tree/binary-tree.js +1174 -5
- package/lib/data-structures/binary-tree/bst.d.ts +3 -3
- package/lib/data-structures/binary-tree/index.d.ts +0 -5
- package/lib/data-structures/binary-tree/index.js +0 -5
- package/lib/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +3 -3
- package/lib/data-structures/graph/abstract-graph.d.ts +2 -2
- package/lib/data-structures/graph/directed-graph.d.ts +2 -2
- package/lib/data-structures/graph/map-graph.d.ts +1 -1
- package/lib/data-structures/graph/map-graph.js +1 -1
- package/lib/data-structures/graph/undirected-graph.d.ts +2 -2
- package/lib/data-structures/hash/index.d.ts +0 -1
- package/lib/data-structures/hash/index.js +0 -1
- package/lib/data-structures/heap/heap.d.ts +23 -24
- package/lib/data-structures/heap/heap.js +8 -25
- package/lib/data-structures/heap/max-heap.d.ts +3 -3
- package/lib/data-structures/heap/min-heap.d.ts +3 -3
- package/lib/data-structures/priority-queue/max-priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/min-priority-queue.d.ts +3 -3
- package/lib/data-structures/priority-queue/priority-queue.d.ts +3 -3
- package/lib/interfaces/binary-tree.d.ts +5 -4
- package/lib/interfaces/{abstract-graph.d.ts → graph.d.ts} +1 -1
- package/lib/interfaces/index.d.ts +1 -8
- package/lib/interfaces/index.js +1 -8
- package/lib/types/data-structures/abstract-graph.d.ts +0 -1
- package/lib/types/data-structures/binary-tree.d.ts +32 -2
- package/lib/types/data-structures/binary-tree.js +21 -1
- package/lib/types/data-structures/bst.d.ts +1 -2
- package/lib/types/data-structures/heap.d.ts +2 -1
- package/lib/types/data-structures/index.d.ts +0 -2
- package/lib/types/data-structures/index.js +0 -2
- package/package.json +4 -4
- package/src/data-structures/binary-tree/avl-tree.ts +6 -6
- package/src/data-structures/binary-tree/binary-tree.ts +1523 -14
- package/src/data-structures/binary-tree/bst.ts +3 -6
- package/src/data-structures/binary-tree/index.ts +0 -5
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/tree-multiset.ts +6 -6
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +30 -48
- package/src/data-structures/heap/max-heap.ts +3 -3
- package/src/data-structures/heap/min-heap.ts +3 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -3
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +7 -4
- package/src/interfaces/{abstract-graph.ts → graph.ts} +1 -1
- package/src/interfaces/index.ts +1 -8
- package/src/types/data-structures/abstract-graph.ts +1 -1
- package/src/types/data-structures/binary-tree.ts +42 -2
- package/src/types/data-structures/bst.ts +2 -2
- package/src/types/data-structures/heap.ts +3 -1
- package/src/types/data-structures/index.ts +0 -2
- package/src/types/data-structures/navigator.ts +1 -0
- package/test/unit/data-structures/heap/max-heap.test.ts +2 -2
- package/test/unit/data-structures/heap/min-heap.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -1
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.LICENSE.txt +7 -0
- package/umd/bundle.min.js.map +1 -1
- package/dist/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/data-structures/binary-tree/aa-tree.js +0 -7
- package/dist/data-structures/binary-tree/aa-tree.js.map +0 -1
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +0 -527
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +0 -1199
- package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +0 -1
- package/dist/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/data-structures/binary-tree/b-tree.js +0 -7
- package/dist/data-structures/binary-tree/b-tree.js.map +0 -1
- package/dist/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/data-structures/binary-tree/splay-tree.js +0 -7
- package/dist/data-structures/binary-tree/splay-tree.js.map +0 -1
- package/dist/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/data-structures/binary-tree/two-three-tree.js +0 -7
- package/dist/data-structures/binary-tree/two-three-tree.js.map +0 -1
- package/dist/data-structures/hash/pair.d.ts +0 -2
- package/dist/data-structures/hash/pair.js +0 -7
- package/dist/data-structures/hash/pair.js.map +0 -1
- package/dist/interfaces/abstract-binary-tree.d.ts +0 -7
- package/dist/interfaces/abstract-binary-tree.js +0 -3
- package/dist/interfaces/abstract-binary-tree.js.map +0 -1
- package/dist/interfaces/abstract-graph.js +0 -3
- package/dist/interfaces/abstract-graph.js.map +0 -1
- package/dist/interfaces/avl-tree.d.ts +0 -7
- package/dist/interfaces/avl-tree.js +0 -3
- package/dist/interfaces/avl-tree.js.map +0 -1
- package/dist/interfaces/bst.d.ts +0 -6
- package/dist/interfaces/bst.js.map +0 -1
- package/dist/interfaces/directed-graph.d.ts +0 -3
- package/dist/interfaces/directed-graph.js +0 -3
- package/dist/interfaces/directed-graph.js.map +0 -1
- package/dist/interfaces/rb-tree.d.ts +0 -6
- package/dist/interfaces/rb-tree.js +0 -3
- package/dist/interfaces/rb-tree.js.map +0 -1
- package/dist/interfaces/tree-multiset.d.ts +0 -6
- package/dist/interfaces/tree-multiset.js +0 -3
- package/dist/interfaces/tree-multiset.js.map +0 -1
- package/dist/interfaces/undirected-graph.d.ts +0 -3
- package/dist/interfaces/undirected-graph.js +0 -3
- package/dist/interfaces/undirected-graph.js.map +0 -1
- package/dist/types/data-structures/abstract-binary-tree.d.ts +0 -34
- package/dist/types/data-structures/abstract-binary-tree.js +0 -25
- package/dist/types/data-structures/abstract-binary-tree.js.map +0 -1
- package/dist/types/data-structures/priority-queue.d.ts +0 -7
- package/dist/types/data-structures/priority-queue.js +0 -3
- package/dist/types/data-structures/priority-queue.js.map +0 -1
- package/lib/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/lib/data-structures/binary-tree/aa-tree.js +0 -2
- package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +0 -527
- package/lib/data-structures/binary-tree/abstract-binary-tree.js +0 -1193
- package/lib/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/lib/data-structures/binary-tree/b-tree.js +0 -2
- package/lib/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/lib/data-structures/binary-tree/splay-tree.js +0 -2
- package/lib/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/lib/data-structures/binary-tree/two-three-tree.js +0 -2
- package/lib/data-structures/hash/pair.d.ts +0 -2
- package/lib/data-structures/hash/pair.js +0 -2
- package/lib/interfaces/abstract-binary-tree.d.ts +0 -7
- package/lib/interfaces/abstract-graph.js +0 -1
- package/lib/interfaces/avl-tree.d.ts +0 -7
- package/lib/interfaces/avl-tree.js +0 -1
- package/lib/interfaces/bst.d.ts +0 -6
- package/lib/interfaces/bst.js +0 -1
- package/lib/interfaces/directed-graph.d.ts +0 -3
- package/lib/interfaces/directed-graph.js +0 -1
- package/lib/interfaces/rb-tree.d.ts +0 -6
- package/lib/interfaces/rb-tree.js +0 -1
- package/lib/interfaces/tree-multiset.d.ts +0 -6
- package/lib/interfaces/tree-multiset.js +0 -1
- package/lib/interfaces/undirected-graph.d.ts +0 -3
- package/lib/interfaces/undirected-graph.js +0 -1
- package/lib/types/data-structures/abstract-binary-tree.d.ts +0 -34
- package/lib/types/data-structures/abstract-binary-tree.js +0 -21
- package/lib/types/data-structures/priority-queue.d.ts +0 -7
- package/lib/types/data-structures/priority-queue.js +0 -1
- package/src/data-structures/binary-tree/aa-tree.ts +0 -1
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1556
- package/src/data-structures/binary-tree/b-tree.ts +0 -1
- package/src/data-structures/binary-tree/splay-tree.ts +0 -1
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
- package/src/data-structures/hash/pair.ts +0 -1
- package/src/interfaces/abstract-binary-tree.ts +0 -8
- package/src/interfaces/avl-tree.ts +0 -8
- package/src/interfaces/bst.ts +0 -6
- package/src/interfaces/directed-graph.ts +0 -3
- package/src/interfaces/rb-tree.ts +0 -6
- package/src/interfaces/tree-multiset.ts +0 -7
- package/src/interfaces/undirected-graph.ts +0 -3
- package/src/types/data-structures/abstract-binary-tree.ts +0 -49
- package/src/types/data-structures/priority-queue.ts +0 -9
- /package/lib/interfaces/{abstract-binary-tree.js → graph.js} +0 -0
|
@@ -14,18 +14,15 @@ import type {
|
|
|
14
14
|
} from '../../types';
|
|
15
15
|
import {CP, LoopType} from '../../types';
|
|
16
16
|
import {BinaryTree, BinaryTreeNode} from './binary-tree';
|
|
17
|
-
import {
|
|
17
|
+
import {IBinaryTree} from '../../interfaces';
|
|
18
18
|
|
|
19
|
-
export class BSTNode<V = any,
|
|
20
|
-
extends BinaryTreeNode<V, NEIGHBOR>
|
|
21
|
-
implements IBSTNode<V, NEIGHBOR>
|
|
22
|
-
{
|
|
19
|
+
export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
|
|
23
20
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
24
21
|
super(key, val);
|
|
25
22
|
}
|
|
26
23
|
}
|
|
27
24
|
|
|
28
|
-
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements
|
|
25
|
+
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
|
|
29
26
|
/**
|
|
30
27
|
* The constructor function initializes a binary search tree object with an optional comparator function.
|
|
31
28
|
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
export * from './abstract-binary-tree';
|
|
2
1
|
export * from './binary-tree';
|
|
3
2
|
export * from './bst';
|
|
4
3
|
export * from './binary-indexed-tree';
|
|
5
4
|
export * from './segment-tree';
|
|
6
5
|
export * from './avl-tree';
|
|
7
|
-
export * from './b-tree';
|
|
8
6
|
export * from './rb-tree';
|
|
9
|
-
export * from './splay-tree';
|
|
10
|
-
export * from './aa-tree';
|
|
11
7
|
export * from './tree-multiset';
|
|
12
|
-
export * from './two-three-tree';
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
|
|
2
|
-
import {
|
|
2
|
+
import {IBinaryTree} from '../../interfaces';
|
|
3
3
|
import {BST, BSTNode} from './bst';
|
|
4
4
|
|
|
5
|
-
export class RBTreeNode<V = any,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
{
|
|
5
|
+
export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<
|
|
6
|
+
V,
|
|
7
|
+
FAMILY
|
|
8
|
+
> {
|
|
9
9
|
private _color: RBColor;
|
|
10
10
|
|
|
11
11
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
@@ -22,7 +22,7 @@ export class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTr
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements
|
|
25
|
+
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
|
|
26
26
|
constructor(options?: RBTreeOptions) {
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
|
|
9
9
|
import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
|
|
10
|
-
import {
|
|
10
|
+
import {IBinaryTree} from '../../interfaces';
|
|
11
11
|
import {AVLTree, AVLTreeNode} from './avl-tree';
|
|
12
12
|
|
|
13
|
-
export class TreeMultisetNode<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
{
|
|
13
|
+
export class TreeMultisetNode<
|
|
14
|
+
V = any,
|
|
15
|
+
FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
|
|
16
|
+
> extends AVLTreeNode<V, FAMILY> {
|
|
17
17
|
/**
|
|
18
18
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
19
19
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
@@ -37,7 +37,7 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|
|
37
37
|
*/
|
|
38
38
|
export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
|
|
39
39
|
extends AVLTree<N>
|
|
40
|
-
implements
|
|
40
|
+
implements IBinaryTree<N>
|
|
41
41
|
{
|
|
42
42
|
/**
|
|
43
43
|
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {arrayRemove, uuidV4} from '../../utils';
|
|
9
9
|
import {PriorityQueue} from '../priority-queue';
|
|
10
10
|
import type {DijkstraResult, VertexKey} from '../../types';
|
|
11
|
-
import {
|
|
11
|
+
import {IGraph} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export abstract class AbstractVertex<V = any> {
|
|
14
14
|
/**
|
|
@@ -104,7 +104,7 @@ export abstract class AbstractEdge<V = any> {
|
|
|
104
104
|
export abstract class AbstractGraph<
|
|
105
105
|
V extends AbstractVertex<any> = AbstractVertex<any>,
|
|
106
106
|
E extends AbstractEdge<any> = AbstractEdge<any>
|
|
107
|
-
> implements
|
|
107
|
+
> implements IGraph<V, E>
|
|
108
108
|
{
|
|
109
109
|
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
|
|
110
110
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {arrayRemove} from '../../utils';
|
|
9
9
|
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
10
|
import type {TopologicalStatus, VertexKey} from '../../types';
|
|
11
|
-
import {
|
|
11
|
+
import {IGraph} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -64,7 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
|
|
|
64
64
|
|
|
65
65
|
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
|
|
66
66
|
extends AbstractGraph<V, E>
|
|
67
|
-
implements
|
|
67
|
+
implements IGraph<V, E>
|
|
68
68
|
{
|
|
69
69
|
/**
|
|
70
70
|
* The constructor function initializes an instance of a class.
|
|
@@ -126,7 +126,7 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
|
|
|
126
126
|
* If the weight is not provided, it can be set to a default value or left undefined.
|
|
127
127
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type,
|
|
128
128
|
* depending on the specific implementation of the `MapEdge` class.
|
|
129
|
-
* @returns a new instance of the `MapEdge` class,
|
|
129
|
+
* @returns a new instance of the `MapEdge` class, cast as type `E`.
|
|
130
130
|
*/
|
|
131
131
|
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
|
|
132
132
|
return new MapEdge(src, dest, weight, val) as E;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {arrayRemove} from '../../utils';
|
|
9
9
|
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
|
|
10
10
|
import type {VertexKey} from '../../types';
|
|
11
|
-
import {
|
|
11
|
+
import {IGraph} from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
|
14
14
|
/**
|
|
@@ -55,7 +55,7 @@ export class UndirectedGraph<
|
|
|
55
55
|
E extends UndirectedEdge<any> = UndirectedEdge
|
|
56
56
|
>
|
|
57
57
|
extends AbstractGraph<V, E>
|
|
58
|
-
implements
|
|
58
|
+
implements IGraph<V, E>
|
|
59
59
|
{
|
|
60
60
|
/**
|
|
61
61
|
* The constructor initializes a new Map object to store edges.
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
|
-
*
|
|
4
3
|
* @author Kirk Qi
|
|
5
4
|
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
|
6
5
|
* @license MIT License
|
|
7
6
|
*/
|
|
8
7
|
|
|
9
|
-
import type {
|
|
8
|
+
import type {HeapComparator, HeapDFSOrderPattern} from '../../types';
|
|
10
9
|
|
|
11
|
-
export class Heap<
|
|
12
|
-
|
|
13
|
-
private readonly comparator:
|
|
10
|
+
export class Heap<E> {
|
|
11
|
+
protected nodes: E[] = [];
|
|
12
|
+
private readonly comparator: HeapComparator<E>;
|
|
14
13
|
|
|
15
|
-
constructor(comparator:
|
|
14
|
+
constructor(comparator: HeapComparator<E>) {
|
|
16
15
|
this.comparator = comparator;
|
|
17
16
|
}
|
|
18
17
|
|
|
@@ -20,7 +19,7 @@ export class Heap<T> {
|
|
|
20
19
|
* Insert an element into the heap and maintain the heap properties.
|
|
21
20
|
* @param value - The element to be inserted.
|
|
22
21
|
*/
|
|
23
|
-
add(value:
|
|
22
|
+
add(value: E): Heap<E> {
|
|
24
23
|
this.nodes.push(value);
|
|
25
24
|
this.bubbleUp(this.nodes.length - 1);
|
|
26
25
|
return this;
|
|
@@ -30,16 +29,16 @@ export class Heap<T> {
|
|
|
30
29
|
* Remove and return the top element (smallest or largest element) from the heap.
|
|
31
30
|
* @returns The top element or null if the heap is empty.
|
|
32
31
|
*/
|
|
33
|
-
poll():
|
|
32
|
+
poll(): E | null {
|
|
34
33
|
if (this.nodes.length === 0) {
|
|
35
34
|
return null;
|
|
36
35
|
}
|
|
37
36
|
if (this.nodes.length === 1) {
|
|
38
|
-
return this.nodes.pop() as
|
|
37
|
+
return this.nodes.pop() as E;
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
const topValue = this.nodes[0];
|
|
42
|
-
this.nodes[0] = this.nodes.pop() as
|
|
41
|
+
this.nodes[0] = this.nodes.pop() as E;
|
|
43
42
|
this.sinkDown(0);
|
|
44
43
|
return topValue;
|
|
45
44
|
}
|
|
@@ -99,7 +98,7 @@ export class Heap<T> {
|
|
|
99
98
|
* Peek at the top element of the heap without removing it.
|
|
100
99
|
* @returns The top element or null if the heap is empty.
|
|
101
100
|
*/
|
|
102
|
-
peek():
|
|
101
|
+
peek(): E | null {
|
|
103
102
|
if (this.nodes.length === 0) {
|
|
104
103
|
return null;
|
|
105
104
|
}
|
|
@@ -117,7 +116,7 @@ export class Heap<T> {
|
|
|
117
116
|
* Get the last element in the heap, which is not necessarily a leaf node.
|
|
118
117
|
* @returns The last element or null if the heap is empty.
|
|
119
118
|
*/
|
|
120
|
-
leaf():
|
|
119
|
+
get leaf(): E | null {
|
|
121
120
|
return this.nodes[this.size - 1] ?? null;
|
|
122
121
|
}
|
|
123
122
|
|
|
@@ -129,11 +128,18 @@ export class Heap<T> {
|
|
|
129
128
|
return this.size === 0;
|
|
130
129
|
}
|
|
131
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Reset the nodes of the heap. Make the nodes empty.
|
|
133
|
+
*/
|
|
132
134
|
clear() {
|
|
133
135
|
this.nodes = [];
|
|
134
136
|
}
|
|
135
137
|
|
|
136
|
-
|
|
138
|
+
/**
|
|
139
|
+
* Clear and add nodes of the heap
|
|
140
|
+
* @param nodes
|
|
141
|
+
*/
|
|
142
|
+
refill(nodes: E[]) {
|
|
137
143
|
this.nodes = nodes;
|
|
138
144
|
this.fix();
|
|
139
145
|
}
|
|
@@ -143,41 +149,17 @@ export class Heap<T> {
|
|
|
143
149
|
* @param value - the element to check.
|
|
144
150
|
* @returns Returns true if the specified element is contained; otherwise, returns false.
|
|
145
151
|
*/
|
|
146
|
-
has(value:
|
|
152
|
+
has(value: E): boolean {
|
|
147
153
|
return this.nodes.includes(value);
|
|
148
154
|
}
|
|
149
155
|
|
|
150
|
-
/**
|
|
151
|
-
* Use a comparison function to find the index of an element in the heap.
|
|
152
|
-
* @param value - the element to find.
|
|
153
|
-
* @param index - the index currently being searched.
|
|
154
|
-
* @returns The index of the element, or -1 if not found.
|
|
155
|
-
*/
|
|
156
|
-
private findIndex(value: T, index: number): number {
|
|
157
|
-
if (index >= this.size) {
|
|
158
|
-
return -1;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const compareResult = this.comparator(value, this.nodes[index]);
|
|
162
|
-
|
|
163
|
-
if (compareResult === 0) {
|
|
164
|
-
return index; // Element found
|
|
165
|
-
} else if (compareResult < 0) {
|
|
166
|
-
// The element should be in the left subtree
|
|
167
|
-
return this.findIndex(value, 2 * index + 1);
|
|
168
|
-
} else {
|
|
169
|
-
// The element should be in the right subtree
|
|
170
|
-
return this.findIndex(value, 2 * index + 2);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
156
|
/**
|
|
175
157
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
176
158
|
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
177
159
|
* @returns An array containing elements traversed in the specified order.
|
|
178
160
|
*/
|
|
179
|
-
dfs(order:
|
|
180
|
-
const result:
|
|
161
|
+
dfs(order: HeapDFSOrderPattern): E[] {
|
|
162
|
+
const result: E[] = [];
|
|
181
163
|
|
|
182
164
|
// Auxiliary recursive function, traverses the binary heap according to the traversal order
|
|
183
165
|
const dfsHelper = (index: number) => {
|
|
@@ -207,11 +189,11 @@ export class Heap<T> {
|
|
|
207
189
|
* Convert the heap to an array.
|
|
208
190
|
* @returns An array containing the elements of the heap.
|
|
209
191
|
*/
|
|
210
|
-
toArray():
|
|
192
|
+
toArray(): E[] {
|
|
211
193
|
return [...this.nodes];
|
|
212
194
|
}
|
|
213
195
|
|
|
214
|
-
getNodes():
|
|
196
|
+
getNodes(): E[] {
|
|
215
197
|
return this.nodes;
|
|
216
198
|
}
|
|
217
199
|
|
|
@@ -219,8 +201,8 @@ export class Heap<T> {
|
|
|
219
201
|
* Clone the heap, creating a new heap with the same elements.
|
|
220
202
|
* @returns A new Heap instance containing the same elements.
|
|
221
203
|
*/
|
|
222
|
-
clone(): Heap<
|
|
223
|
-
const clonedHeap = new Heap<
|
|
204
|
+
clone(): Heap<E> {
|
|
205
|
+
const clonedHeap = new Heap<E>(this.comparator);
|
|
224
206
|
clonedHeap.nodes = [...this.nodes];
|
|
225
207
|
return clonedHeap;
|
|
226
208
|
}
|
|
@@ -229,8 +211,8 @@ export class Heap<T> {
|
|
|
229
211
|
* Sort the elements in the heap and return them as an array.
|
|
230
212
|
* @returns An array containing the elements sorted in ascending order.
|
|
231
213
|
*/
|
|
232
|
-
sort():
|
|
233
|
-
const visitedNode:
|
|
214
|
+
sort(): E[] {
|
|
215
|
+
const visitedNode: E[] = [];
|
|
234
216
|
const cloned = this.clone();
|
|
235
217
|
while (cloned.size !== 0) {
|
|
236
218
|
const top = cloned.poll();
|
|
@@ -245,8 +227,8 @@ export class Heap<T> {
|
|
|
245
227
|
* @param comparator - Comparison function.
|
|
246
228
|
* @returns A new Heap instance.
|
|
247
229
|
*/
|
|
248
|
-
static heapify<
|
|
249
|
-
const binaryHeap = new Heap<
|
|
230
|
+
static heapify<E>(nodes: E[], comparator: HeapComparator<E>): Heap<E> {
|
|
231
|
+
const binaryHeap = new Heap<E>(comparator);
|
|
250
232
|
binaryHeap.nodes = [...nodes];
|
|
251
233
|
binaryHeap.fix(); // Fix heap properties
|
|
252
234
|
return binaryHeap;
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {Heap} from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type {HeapComparator} from '../../types';
|
|
11
11
|
|
|
12
|
-
export class MaxHeap<
|
|
12
|
+
export class MaxHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator:
|
|
14
|
+
comparator: HeapComparator<E> = (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');
|
|
17
17
|
} else {
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {Heap} from './heap';
|
|
10
|
-
import type {
|
|
10
|
+
import type {HeapComparator} from '../../types';
|
|
11
11
|
|
|
12
|
-
export class MinHeap<
|
|
12
|
+
export class MinHeap<E = any> extends Heap<E> {
|
|
13
13
|
constructor(
|
|
14
|
-
comparator:
|
|
14
|
+
comparator: HeapComparator<E> = (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');
|
|
17
17
|
} else {
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type {HeapComparator} from '../../types';
|
|
10
10
|
|
|
11
|
-
export class MaxPriorityQueue<
|
|
11
|
+
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
compare:
|
|
13
|
+
compare: HeapComparator<E> = (a: E, b: E) => {
|
|
14
14
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
15
|
throw new Error('The a, b params of compare function must be number');
|
|
16
16
|
} else {
|
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import {PriorityQueue} from './priority-queue';
|
|
9
|
-
import type {
|
|
9
|
+
import type {HeapComparator} from '../../types';
|
|
10
10
|
|
|
11
|
-
export class MinPriorityQueue<
|
|
11
|
+
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
compare:
|
|
13
|
+
compare: HeapComparator<E> = (a: E, b: E) => {
|
|
14
14
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
15
15
|
throw new Error('The a, b params of compare function must be number');
|
|
16
16
|
} else {
|
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import {Heap} from '../heap';
|
|
10
|
-
import {
|
|
10
|
+
import {HeapComparator} from '../../types';
|
|
11
11
|
|
|
12
|
-
export class PriorityQueue<
|
|
13
|
-
constructor(comparator:
|
|
12
|
+
export class PriorityQueue<E> extends Heap<E> {
|
|
13
|
+
constructor(comparator: HeapComparator<E>) {
|
|
14
14
|
super(comparator);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../data-structures';
|
|
2
|
-
import {
|
|
2
|
+
import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
|
|
3
3
|
|
|
4
|
-
export interface
|
|
5
|
-
|
|
4
|
+
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
|
|
5
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
8
|
+
|
|
9
|
+
remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
10
|
+
}
|
package/src/interfaces/index.ts
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './abstract-graph';
|
|
3
|
-
export * from './avl-tree';
|
|
1
|
+
export * from './graph';
|
|
4
2
|
export * from './binary-tree';
|
|
5
|
-
export * from './bst';
|
|
6
|
-
export * from './directed-graph';
|
|
7
3
|
export * from './doubly-linked-list';
|
|
8
4
|
export * from './heap';
|
|
9
5
|
export * from './navigator';
|
|
10
6
|
export * from './priority-queue';
|
|
11
|
-
export * from './rb-tree';
|
|
12
7
|
export * from './segment-tree';
|
|
13
8
|
export * from './singly-linked-list';
|
|
14
|
-
export * from './tree-multiset';
|
|
15
|
-
export * from './undirected-graph';
|
|
@@ -1,5 +1,45 @@
|
|
|
1
1
|
import {BinaryTreeNode} from '../../data-structures/binary-tree';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Enum representing different loop types.
|
|
5
|
+
*
|
|
6
|
+
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
7
|
+
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export enum LoopType {
|
|
11
|
+
ITERATIVE = 'ITERATIVE',
|
|
12
|
+
RECURSIVE = 'RECURSIVE'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export enum FamilyPosition {
|
|
16
|
+
ROOT = 'ROOT',
|
|
17
|
+
LEFT = 'LEFT',
|
|
18
|
+
RIGHT = 'RIGHT',
|
|
19
|
+
ROOT_LEFT = 'ROOT_LEFT',
|
|
20
|
+
ROOT_RIGHT = 'ROOT_RIGHT',
|
|
21
|
+
ISOLATED = 'ISOLATED',
|
|
22
|
+
MAL_NODE = 'MAL_NODE'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type BinaryTreeNodePropertyName = 'key' | 'val';
|
|
26
|
+
|
|
27
|
+
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
|
|
28
|
+
|
|
29
|
+
export type DFSOrderPattern = 'in' | 'pre' | 'post';
|
|
30
|
+
|
|
31
|
+
export type BinaryTreeNodeKey = number;
|
|
32
|
+
|
|
33
|
+
export type BinaryTreeNodeProperty<N extends BinaryTreeNode<N['val'], N>> =
|
|
34
|
+
| N['val']
|
|
35
|
+
| N
|
|
36
|
+
| number
|
|
37
|
+
| BinaryTreeNodeKey;
|
|
38
|
+
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
|
|
39
|
+
|
|
40
|
+
export type BinaryTreeNodeProperties<N extends BinaryTreeNode<N['val'], N>> =
|
|
41
|
+
BinaryTreeNodeProperty<N>[];
|
|
3
42
|
|
|
4
43
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
5
|
-
|
|
44
|
+
|
|
45
|
+
export type BinaryTreeOptions = { loopType?: LoopType }
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {BSTNode} from '../../data-structures/binary-tree';
|
|
2
|
-
import type {BinaryTreeOptions} from './binary-tree';
|
|
3
|
-
import {BinaryTreeNodeKey} from './abstract-binary-tree';
|
|
2
|
+
import type {BinaryTreeNodeKey, BinaryTreeOptions} from './binary-tree';
|
|
4
3
|
|
|
5
4
|
export type BSTComparator = (a: BinaryTreeNodeKey, b: BinaryTreeNodeKey) => number;
|
|
6
5
|
|
|
7
6
|
// prettier-ignore
|
|
8
7
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
|
+
|
|
9
9
|
export type BSTOptions = BinaryTreeOptions & {
|
|
10
10
|
comparator?: BSTComparator,
|
|
11
11
|
}
|
|
@@ -5,10 +5,8 @@ export * from './segment-tree';
|
|
|
5
5
|
export * from './tree-multiset';
|
|
6
6
|
export * from './abstract-graph';
|
|
7
7
|
export * from './map-graph';
|
|
8
|
-
export * from './abstract-binary-tree';
|
|
9
8
|
export * from './rb-tree';
|
|
10
9
|
export * from './directed-graph';
|
|
11
|
-
export * from './priority-queue';
|
|
12
10
|
export * from './heap';
|
|
13
11
|
export * from './singly-linked-list';
|
|
14
12
|
export * from './doubly-linked-list';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {HeapComparator, MaxHeap} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MaxHeap', () => {
|
|
4
|
-
const numberComparator:
|
|
4
|
+
const numberComparator: HeapComparator<number> = (a, b) => b - a;
|
|
5
5
|
let maxHeap: MaxHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {HeapComparator, MinHeap} from '../../../../src';
|
|
2
2
|
|
|
3
3
|
describe('MinHeap', () => {
|
|
4
|
-
const numberComparator:
|
|
4
|
+
const numberComparator: HeapComparator<number> = (a, b) => a - b;
|
|
5
5
|
let minHeap: MinHeap<number>;
|
|
6
6
|
|
|
7
7
|
beforeEach(() => {
|
|
@@ -89,7 +89,8 @@ describe('MaxPriorityQueue Performance Test', () => {
|
|
|
89
89
|
prev = polled;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
|
|
92
|
+
const cost = performance.now() - startTime;
|
|
93
|
+
expect(cost).toBeLessThan(bigO.LINEAR * 20);
|
|
93
94
|
expect(prev).toBeGreaterThan(0);
|
|
94
95
|
});
|
|
95
96
|
|