Merge pull request #7 from zrwusa/optimization1.36.2

[binary-tree, graph] In order to optimize the design of Binary Trees,…
This commit is contained in:
zrwusa 2023-10-17 21:11:36 +08:00 committed by GitHub
commit 5e770e1444
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 135 additions and 255 deletions

View file

@ -1,6 +1,5 @@
src/types/data-structures/abstract-binary-tree.ts
src/types/data-structures/binary-tree.ts
src/types/data-structures/bst.ts
src/types/data-structures/avl-tree.ts
src/types/data-structures/tree-multiset.ts
src/types/data-structures/rb-tree.ts
src/types/data-structures/tree-multiset.ts

View file

@ -1 +0,0 @@
export class AaTree {}

View file

@ -1,33 +0,0 @@
/**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
import type {AbstractBinaryTreeNodeNested, BinaryTreeNodeKey} from '../../types';
import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../../interfaces';
export abstract class AbstractBinaryTreeNode<
V = any,
FAMILY extends AbstractBinaryTreeNode<V, FAMILY> = AbstractBinaryTreeNodeNested<V>
> implements IAbstractBinaryTreeNode<V, FAMILY>
{
/**
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
* stored in the binary tree node. If no value is provided, it will be set to undefined.
*/
protected constructor(val?: V) {
this.val = val;
}
val: V | undefined;
}
export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N> = AbstractBinaryTreeNode>
implements IAbstractBinaryTree<N>
{
abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null;
}

View file

@ -7,12 +7,12 @@
*/
import {BST, BSTNode} from './bst';
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
import {IAVLTree, IAVLTreeNode} from '../../interfaces';
import {IBinaryTree} from '../../interfaces';
export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>>
extends BSTNode<V, FAMILY>
implements IAVLTreeNode<V, FAMILY>
{
export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<
V,
FAMILY
> {
height: number;
constructor(key: BinaryTreeNodeKey, val?: V) {
@ -21,7 +21,7 @@ export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTre
}
}
export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IBinaryTree<N> {
/**
* This is a constructor function for an AVL tree data structure in TypeScript.
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the

View file

@ -1 +0,0 @@
export class BTree {}

View file

@ -6,12 +6,15 @@
* @license MIT License
*/
import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
import {IBinaryTree, IBinaryTreeNode} from '../../interfaces';
import type {
BinaryTreeNodeKey,
BinaryTreeNodeNested,
BinaryTreeNodeProperties,
BinaryTreeNodeProperty,
BinaryTreeOptions
} from '../../types';
import {IBinaryTree} from '../../interfaces';
import {
AbstractBinaryTreeNodeProperties,
AbstractBinaryTreeNodeProperty,
BinaryTreeDeletedResult,
BinaryTreeNodePropertyName,
DFSOrderPattern,
@ -21,10 +24,7 @@ import {
} from '../../types';
import {trampoline} from '../../utils';
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>>
extends AbstractBinaryTreeNode<V, FAMILY>
implements IBinaryTreeNode<V, FAMILY>
{
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
/**
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
@ -33,12 +33,14 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
* stored in the binary tree node. If no value is provided, it will be set to undefined.
*/
constructor(key: BinaryTreeNodeKey, val?: V) {
super(val);
this.key = key;
this.val = val;
}
key: BinaryTreeNodeKey;
val: V | undefined;
private _left: FAMILY | null | undefined;
get left(): FAMILY | null | undefined {
@ -99,10 +101,7 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
}
}
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
extends AbstractBinaryTree<N>
implements IBinaryTree<N>
{
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
/**
* This is a constructor function for a binary tree class that takes an optional options parameter.
* @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@ -110,7 +109,6 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* different configuration options.
*/
constructor(options?: BinaryTreeOptions) {
super();
if (options !== undefined) {
const {loopType = LoopType.ITERATIVE} = options;
this._loopType = loopType;
@ -228,7 +226,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
needInsert = null;
} else if (typeof keyOrNode === 'number') {
needInsert = this.createNode(keyOrNode, val);
} else if (keyOrNode instanceof AbstractBinaryTreeNode) {
} else if (keyOrNode instanceof BinaryTreeNode) {
needInsert = keyOrNode;
} else {
return;
@ -271,7 +269,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
for (let i = 0; i < keysOrNodes.length; i++) {
const keyOrNode = keysOrNodes[i];
if (keyOrNode instanceof AbstractBinaryTreeNode) {
if (keyOrNode instanceof BinaryTreeNode) {
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
continue;
}
@ -894,9 +892,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name.
* If a node is provided, the bfs algorithm will be performed starting from that node.
* If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property.
* @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
* @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`.
*/
bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties<N> {
bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
this._clearResults();
const queue: Array<N | null | undefined> = [this.root];
@ -954,12 +952,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* each node based on the specified pattern and property name.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`.
* @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
* @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name.
*/
dfs(
pattern: DFSOrderPattern = 'in',
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperties<N> {
dfs(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
this._clearResults();
const _traverse = (node: N) => {
switch (pattern) {
@ -1029,12 +1024,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* specify the traversal pattern and the property name to accumulate results by.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`.
* @returns An object of type AbstractBinaryTreeNodeProperties<N>.
* @returns An object of type BinaryTreeNodeProperties<N>.
*/
dfsIterative(
pattern: DFSOrderPattern = 'in',
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperties<N> {
): BinaryTreeNodeProperties<N> {
this._clearResults();
if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
// 0: visit, 1: print
@ -1121,12 +1116,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function
* will accumulate results based on that property. If no property name is provided, the function will default to
* accumulating results based on the 'key' property.
* @returns An object of type `AbstractBinaryTreeNodeProperties<N>`.
* @returns An object of type `BinaryTreeNodeProperties<N>`.
*/
levelIterative(
node: N | null = this.root,
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperties<N> {
): BinaryTreeNodeProperties<N> {
if (!node) return [];
this._clearResults();
@ -1194,10 +1189,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
listLevels(
node: N | null = this.root,
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperty<N>[][] {
): BinaryTreeNodeProperty<N>[][] {
if (!node) return [];
const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
const levelsNodes: BinaryTreeNodeProperty<N>[][] = [];
const collectByProperty = (node: N, level: number) => {
switch (nodeOrPropertyName) {
@ -1307,12 +1302,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'.
* @returns An array of AbstractBinaryTreeNodeProperties<N> objects.
* @returns An array of BinaryTreeNodeProperties<N> objects.
*/
morris(
pattern: DFSOrderPattern = 'in',
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperties<N> {
morris(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
if (this.root === null) return [];
this._clearResults();
@ -1545,11 +1537,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
* name.
* @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
* can accept either a `NodeOrPropertyName` type or be undefined.
* @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
* @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties<N>`.
*/
protected _getResultByPropertyName(
nodeOrPropertyName: NodeOrPropertyName = 'key'
): AbstractBinaryTreeNodeProperties<N> {
protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties<N> {
switch (nodeOrPropertyName) {
case 'key':
return this.visitedKey;

View file

@ -14,18 +14,15 @@ import type {
} from '../../types';
import {CP, LoopType} from '../../types';
import {BinaryTree, BinaryTreeNode} from './binary-tree';
import {IBST, IBSTNode} from '../../interfaces';
import {IBinaryTree} from '../../interfaces';
export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>>
extends BinaryTreeNode<V, FAMILY>
implements IBSTNode<V, FAMILY>
{
export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
constructor(key: BinaryTreeNodeKey, val?: V) {
super(key, val);
}
}
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
/**
* The constructor function initializes a binary search tree object with an optional comparator function.
* @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.

View file

@ -1,12 +1,7 @@
export * from './abstract-binary-tree';
export * from './binary-tree';
export * from './bst';
export * from './binary-indexed-tree';
export * from './segment-tree';
export * from './avl-tree';
export * from './b-tree';
export * from './rb-tree';
export * from './splay-tree';
export * from './aa-tree';
export * from './tree-multiset';
export * from './two-three-tree';

View file

@ -1,11 +1,11 @@
import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types';
import {IRBTree, IRBTreeNode} from '../../interfaces';
import {IBinaryTree} from '../../interfaces';
import {BST, BSTNode} from './bst';
export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>>
extends BSTNode<V, FAMILY>
implements IRBTreeNode<V, FAMILY>
{
export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<
V,
FAMILY
> {
private _color: RBColor;
constructor(key: BinaryTreeNodeKey, val?: V) {
@ -22,7 +22,7 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
}
}
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
constructor(options?: RBTreeOptions) {
super(options);
}

View file

@ -1 +0,0 @@
export class SplayTree {}

View file

@ -7,13 +7,13 @@
*/
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
import {IBinaryTree} from '../../interfaces';
import {AVLTree, AVLTreeNode} from './avl-tree';
export class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>>
extends AVLTreeNode<V, FAMILY>
implements ITreeMultisetNode<V, FAMILY>
{
export class TreeMultisetNode<
V = any,
FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
> extends AVLTreeNode<V, FAMILY> {
/**
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
@ -37,7 +37,7 @@ export class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY
*/
export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
extends AVLTree<N>
implements ITreeMultiset<N>
implements IBinaryTree<N>
{
/**
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to

View file

@ -1 +0,0 @@
export class TwoThreeTree {}

View file

@ -8,7 +8,7 @@
import {arrayRemove, uuidV4} from '../../utils';
import {PriorityQueue} from '../priority-queue';
import type {DijkstraResult, VertexKey} from '../../types';
import {IAbstractGraph} from '../../interfaces';
import {IGraph} from '../../interfaces';
export abstract class AbstractVertex<V = any> {
/**
@ -104,7 +104,7 @@ export abstract class AbstractEdge<V = any> {
export abstract class AbstractGraph<
V extends AbstractVertex<any> = AbstractVertex<any>,
E extends AbstractEdge<any> = AbstractEdge<any>
> implements IAbstractGraph<V, E>
> implements IGraph<V, E>
{
private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();

View file

@ -8,7 +8,7 @@
import {arrayRemove} from '../../utils';
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
import type {TopologicalStatus, VertexKey} from '../../types';
import {IDirectedGraph} from '../../interfaces';
import {IGraph} from '../../interfaces';
export class DirectedVertex<V = any> extends AbstractVertex<V> {
/**
@ -64,7 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
extends AbstractGraph<V, E>
implements IDirectedGraph<V, E>
implements IGraph<V, E>
{
/**
* The constructor function initializes an instance of a class.

View file

@ -126,7 +126,7 @@ export class MapGraph<V extends MapVertex<V['val']> = MapVertex, E extends MapEd
* If the weight is not provided, it can be set to a default value or left undefined.
* @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type,
* depending on the specific implementation of the `MapEdge` class.
* @returns a new instance of the `MapEdge` class, casted as type `E`.
* @returns a new instance of the `MapEdge` class, cast as type `E`.
*/
override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E {
return new MapEdge(src, dest, weight, val) as E;

View file

@ -8,7 +8,7 @@
import {arrayRemove} from '../../utils';
import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
import type {VertexKey} from '../../types';
import {IUNDirectedGraph} from '../../interfaces';
import {IGraph} from '../../interfaces';
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
/**
@ -55,7 +55,7 @@ export class UndirectedGraph<
E extends UndirectedEdge<any> = UndirectedEdge
>
extends AbstractGraph<V, E>
implements IUNDirectedGraph<V, E>
implements IGraph<V, E>
{
/**
* The constructor initializes a new Map object to store edges.

View file

@ -1,7 +1,6 @@
export * from './hash-table';
export * from './coordinate-map';
export * from './coordinate-set';
export * from './pair';
export * from './tree-map';
export * from './tree-set';
export * from './hash-map';

View file

@ -1 +0,0 @@
export class Pair {}

View file

@ -5,13 +5,13 @@
* @license MIT License
*/
import type {CompareFunction} from '../../types';
import type {HeapComparator, HeapDFSOrderPattern} from '../../types';
export class Heap<E> {
protected nodes: E[] = [];
private readonly comparator: CompareFunction<E>;
private readonly comparator: HeapComparator<E>;
constructor(comparator: CompareFunction<E>) {
constructor(comparator: HeapComparator<E>) {
this.comparator = comparator;
}
@ -158,7 +158,7 @@ export class Heap<E> {
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
* @returns An array containing elements traversed in the specified order.
*/
dfs(order: 'in' | 'pre' | 'post'): E[] {
dfs(order: HeapDFSOrderPattern): E[] {
const result: E[] = [];
// Auxiliary recursive function, traverses the binary heap according to the traversal order
@ -227,7 +227,7 @@ export class Heap<E> {
* @param comparator - Comparison function.
* @returns A new Heap instance.
*/
static heapify<E>(nodes: E[], comparator: CompareFunction<E>): Heap<E> {
static heapify<E>(nodes: E[], comparator: HeapComparator<E>): Heap<E> {
const binaryHeap = new Heap<E>(comparator);
binaryHeap.nodes = [...nodes];
binaryHeap.fix(); // Fix heap properties

View file

@ -7,11 +7,11 @@
*/
import {Heap} from './heap';
import type {CompareFunction} from '../../types';
import type {HeapComparator} from '../../types';
export class MaxHeap<E = any> extends Heap<E> {
constructor(
comparator: CompareFunction<E> = (a: E, b: E) => {
comparator: HeapComparator<E> = (a: E, b: E) => {
if (!(typeof a === 'number' && typeof b === 'number')) {
throw new Error('The a, b params of compare function must be number');
} else {

View file

@ -7,11 +7,11 @@
*/
import {Heap} from './heap';
import type {CompareFunction} from '../../types';
import type {HeapComparator} from '../../types';
export class MinHeap<E = any> extends Heap<E> {
constructor(
comparator: CompareFunction<E> = (a: E, b: E) => {
comparator: HeapComparator<E> = (a: E, b: E) => {
if (!(typeof a === 'number' && typeof b === 'number')) {
throw new Error('The a, b params of compare function must be number');
} else {

View file

@ -6,11 +6,11 @@
* @license MIT License
*/
import {PriorityQueue} from './priority-queue';
import type {CompareFunction} from '../../types';
import type {HeapComparator} from '../../types';
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: CompareFunction<E> = (a: E, b: E) => {
compare: HeapComparator<E> = (a: E, b: E) => {
if (!(typeof a === 'number' && typeof b === 'number')) {
throw new Error('The a, b params of compare function must be number');
} else {

View file

@ -6,11 +6,11 @@
* @license MIT License
*/
import {PriorityQueue} from './priority-queue';
import type {CompareFunction} from '../../types';
import type {HeapComparator} from '../../types';
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: CompareFunction<E> = (a: E, b: E) => {
compare: HeapComparator<E> = (a: E, b: E) => {
if (!(typeof a === 'number' && typeof b === 'number')) {
throw new Error('The a, b params of compare function must be number');
} else {

View file

@ -7,10 +7,10 @@
*/
import {Heap} from '../heap';
import {CompareFunction} from '../../types';
import {HeapComparator} from '../../types';
export class PriorityQueue<E> extends Heap<E> {
constructor(comparator: CompareFunction<E>) {
constructor(comparator: HeapComparator<E>) {
super(comparator);
}
}

View file

@ -1,8 +0,0 @@
import {BinaryTreeNodeKey} from '../types';
import {AbstractBinaryTreeNode} from '../data-structures';
export interface IAbstractBinaryTreeNode<T, NEIGHBOR extends IAbstractBinaryTreeNode<T, NEIGHBOR>> {}
export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N>> {
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N | null;
}

View file

@ -1,8 +0,0 @@
import {AVLTreeNode} from '../data-structures';
import {IBST, IBSTNode} from './bst';
export interface IAVLTreeNode<T, NEIGHBOR extends IAVLTreeNode<T, NEIGHBOR>> extends IBSTNode<T, NEIGHBOR> {
height: number;
}
export interface IAVLTree<N extends AVLTreeNode<N['val'], N>> extends IBST<N> {}

View file

@ -1,7 +1,10 @@
import {BinaryTreeNode} from '../data-structures';
import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from './abstract-binary-tree';
import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
export interface IBinaryTreeNode<T, NEIGHBOR extends IBinaryTreeNode<T, NEIGHBOR>>
extends IAbstractBinaryTreeNode<T, NEIGHBOR> {}
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> extends IAbstractBinaryTree<N> {}
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
}

View file

@ -1,6 +0,0 @@
import {BSTNode} from '../data-structures';
import {IBinaryTree, IBinaryTreeNode} from './binary-tree';
export interface IBSTNode<T, NEIGHBOR extends IBSTNode<T, NEIGHBOR>> extends IBinaryTreeNode<T, NEIGHBOR> {}
export interface IBST<N extends BSTNode<N['val'], N>> extends IBinaryTree<N> {}

View file

@ -1,3 +0,0 @@
import {IAbstractGraph} from './abstract-graph';
export interface IDirectedGraph<V, E> extends IAbstractGraph<V, E> {}

View file

@ -1,6 +1,6 @@
import {VertexKey} from '../types';
export interface IAbstractGraph<V, E> {
export interface IGraph<V, E> {
createVertex(key: VertexKey, val?: V): V;
createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E;

View file

@ -1,15 +1,8 @@
export * from './abstract-binary-tree';
export * from './abstract-graph';
export * from './avl-tree';
export * from './graph';
export * from './binary-tree';
export * from './bst';
export * from './directed-graph';
export * from './doubly-linked-list';
export * from './heap';
export * from './navigator';
export * from './priority-queue';
export * from './rb-tree';
export * from './segment-tree';
export * from './singly-linked-list';
export * from './tree-multiset';
export * from './undirected-graph';

View file

@ -1,6 +0,0 @@
import {RBTreeNode} from '../data-structures';
import {IBST, IBSTNode} from './bst';
export interface IRBTreeNode<T, NEIGHBOR extends IRBTreeNode<T, NEIGHBOR>> extends IBSTNode<T, NEIGHBOR> {}
export interface IRBTree<N extends RBTreeNode<N['val'], N>> extends IBST<N> {}

View file

@ -1,7 +0,0 @@
import {TreeMultisetNode} from '../data-structures';
import {IAVLTree, IAVLTreeNode} from './avl-tree';
export interface ITreeMultisetNode<T, NEIGHBOR extends ITreeMultisetNode<T, NEIGHBOR>>
extends IAVLTreeNode<T, NEIGHBOR> {}
export interface ITreeMultiset<N extends TreeMultisetNode<N['val'], N>> extends IAVLTree<N> {}

View file

@ -1,3 +0,0 @@
import {IAbstractGraph} from './abstract-graph';
export interface IUNDirectedGraph<V, E> extends IAbstractGraph<V, E> {}

View file

@ -1,49 +0,0 @@
import {AbstractBinaryTreeNode} from '../../data-structures';
/**
* Enum representing different loop types.
*
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
*/
export enum LoopType {
ITERATIVE = 'ITERATIVE',
RECURSIVE = 'RECURSIVE'
}
export enum FamilyPosition {
ROOT = 'ROOT',
LEFT = 'LEFT',
RIGHT = 'RIGHT',
ROOT_LEFT = 'ROOT_LEFT',
ROOT_RIGHT = 'ROOT_RIGHT',
ISOLATED = 'ISOLATED',
MAL_NODE = 'MAL_NODE'
}
export type BinaryTreeNodePropertyName = 'key' | 'val';
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
export type DFSOrderPattern = 'in' | 'pre' | 'post';
export type BinaryTreeNodeKey = number;
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
export type AbstractBinaryTreeNodeProperty<N extends AbstractBinaryTreeNode<N['val'], N>> =
| N['val']
| N
| number
| BinaryTreeNodeKey;
export type AbstractBinaryTreeNodeProperties<N extends AbstractBinaryTreeNode<N['val'], N>> =
AbstractBinaryTreeNodeProperty<N>[];
export type AbstractBinaryTreeNodeNested<T> = AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
export type AbstractBinaryTreeOptions = {
};

View file

@ -1,5 +1,5 @@
export type VertexKey = string | number;
export type EdgeKey = string;
export type DijkstraResult<V> = {
distMap: Map<V, number>;
distPaths?: Map<V, V[]>;

View file

@ -1,5 +1,45 @@
import {BinaryTreeNode} from '../../data-structures/binary-tree';
import {AbstractBinaryTreeOptions, LoopType} from './abstract-binary-tree';
/**
* Enum representing different loop types.
*
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
*/
export enum LoopType {
ITERATIVE = 'ITERATIVE',
RECURSIVE = 'RECURSIVE'
}
export enum FamilyPosition {
ROOT = 'ROOT',
LEFT = 'LEFT',
RIGHT = 'RIGHT',
ROOT_LEFT = 'ROOT_LEFT',
ROOT_RIGHT = 'ROOT_RIGHT',
ISOLATED = 'ISOLATED',
MAL_NODE = 'MAL_NODE'
}
export type BinaryTreeNodePropertyName = 'key' | 'val';
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
export type DFSOrderPattern = 'in' | 'pre' | 'post';
export type BinaryTreeNodeKey = number;
export type BinaryTreeNodeProperty<N extends BinaryTreeNode<N['val'], N>> =
| N['val']
| N
| number
| BinaryTreeNodeKey;
export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
export type BinaryTreeNodeProperties<N extends BinaryTreeNode<N['val'], N>> =
BinaryTreeNodeProperty<N>[];
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BinaryTreeOptions = AbstractBinaryTreeOptions & { loopType?: LoopType }
export type BinaryTreeOptions = { loopType?: LoopType }

View file

@ -1,11 +1,11 @@
import {BSTNode} from '../../data-structures/binary-tree';
import type {BinaryTreeOptions} from './binary-tree';
import {BinaryTreeNodeKey} from './abstract-binary-tree';
import type {BinaryTreeNodeKey, BinaryTreeOptions} from './binary-tree';
export type BSTComparator = (a: BinaryTreeNodeKey, b: BinaryTreeNodeKey) => number;
// prettier-ignore
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
export type BSTOptions = BinaryTreeOptions & {
comparator?: BSTComparator,
}

View file

@ -1 +1,3 @@
export type CompareFunction<T> = (a: T, b: T) => number;
export type HeapComparator<T> = (a: T, b: T) => number;
export type HeapDFSOrderPattern = 'pre' | 'in' | 'post';

View file

@ -5,10 +5,8 @@ export * from './segment-tree';
export * from './tree-multiset';
export * from './abstract-graph';
export * from './map-graph';
export * from './abstract-binary-tree';
export * from './rb-tree';
export * from './directed-graph';
export * from './priority-queue';
export * from './heap';
export * from './singly-linked-list';
export * from './doubly-linked-list';

View file

@ -1,4 +1,5 @@
export type Direction = 'up' | 'right' | 'down' | 'left';
export type Turning = {[key in Direction]: Direction};
export type NavigatorParams<T = any> = {

View file

@ -1,9 +0,0 @@
export type PriorityQueueComparator<T> = (a: T, b: T) => number;
export type PriorityQueueOptions<T> = {
nodes?: T[];
isFix?: boolean;
comparator: PriorityQueueComparator<T>;
};
export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post';

View file

@ -1,7 +1,7 @@
import {CompareFunction, MaxHeap} from '../../../../src';
import {HeapComparator, MaxHeap} from '../../../../src';
describe('MaxHeap', () => {
const numberComparator: CompareFunction<number> = (a, b) => b - a;
const numberComparator: HeapComparator<number> = (a, b) => b - a;
let maxHeap: MaxHeap<number>;
beforeEach(() => {

View file

@ -1,7 +1,7 @@
import {CompareFunction, MinHeap} from '../../../../src';
import {HeapComparator, MinHeap} from '../../../../src';
describe('MinHeap', () => {
const numberComparator: CompareFunction<number> = (a, b) => a - b;
const numberComparator: HeapComparator<number> = (a, b) => a - b;
let minHeap: MinHeap<number>;
beforeEach(() => {