mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
refactor: Differentiate between type imports and regular object imports.
This commit is contained in:
parent
9d4228c9fe
commit
60a3154178
|
@ -10,11 +10,12 @@ import type {
|
|||
AVLTreeNested,
|
||||
AVLTreeNodeNested,
|
||||
AVLTreeOptions,
|
||||
BiTreeDeleteResult,
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BTNodeExemplar
|
||||
BTNCallback,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode
|
||||
} from '../../types';
|
||||
import { BTNCallback, BTNodeKeyOrNode } from '../../types';
|
||||
import { IBinaryTree } from '../../interfaces';
|
||||
|
||||
export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
||||
|
@ -34,7 +35,6 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
|
|||
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
||||
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
||||
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
||||
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
||||
*/
|
||||
export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
|
||||
extends BST<K, V, N, TREE>
|
||||
|
@ -42,14 +42,14 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
|
||||
/**
|
||||
* The constructor function initializes an AVLTree object with optional elements and options.
|
||||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
||||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
||||
* objects. It represents a collection of elements that will be added to the AVL tree during
|
||||
* initialization.
|
||||
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
||||
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
||||
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
||||
*/
|
||||
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
|
||||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
|
||||
super([], options);
|
||||
if (elements) super.addMany(elements);
|
||||
}
|
||||
|
@ -83,10 +83,10 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
|
||||
/**
|
||||
* The function checks if an exemplar is an instance of AVLTreeNode.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
||||
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
||||
*/
|
||||
override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
||||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
||||
return exemplar instanceof AVLTreeNode;
|
||||
}
|
||||
|
||||
|
@ -96,7 +96,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
* data type.
|
||||
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
||||
*/
|
||||
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
||||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
||||
return !(potentialKey instanceof AVLTreeNode)
|
||||
}
|
||||
|
||||
|
@ -118,7 +118,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
* being added to the binary tree.
|
||||
* @returns The method is returning either the inserted node or undefined.
|
||||
*/
|
||||
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
||||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
||||
if (keyOrNodeOrEntry === null) return undefined;
|
||||
const inserted = super.add(keyOrNodeOrEntry, value);
|
||||
if (inserted) this._balancePath(inserted);
|
||||
|
@ -143,12 +143,12 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
||||
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
||||
* parameter of type `N
|
||||
* @returns The method is returning an array of `BiTreeDeleteResult<N>`.
|
||||
* @returns The method is returning an array of `BinaryTreeDeleteResult<N>`.
|
||||
*/
|
||||
override delete<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C>,
|
||||
callback: C = this._defaultOneParamCallback as C
|
||||
): BiTreeDeleteResult<N>[] {
|
||||
): BinaryTreeDeleteResult<N>[] {
|
||||
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
||||
const deletedResults = super.delete(identifier, callback);
|
||||
for (const { needBalanced } of deletedResults) {
|
||||
|
|
|
@ -7,23 +7,20 @@
|
|||
*/
|
||||
|
||||
import type {
|
||||
BinaryTreeDeleteResult,
|
||||
BinaryTreeNested,
|
||||
BinaryTreeNodeNested,
|
||||
BinaryTreeOptions,
|
||||
BTNCallback,
|
||||
BTNodeEntry,
|
||||
BTNodeExemplar,
|
||||
BTNodeKeyOrNode,
|
||||
} from '../../types';
|
||||
import {
|
||||
BinaryTreeNested,
|
||||
BinaryTreePrintOptions,
|
||||
BiTreeDeleteResult,
|
||||
BTNCallback,
|
||||
BTNEntry,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
DFSOrderPattern,
|
||||
EntryCallback,
|
||||
FamilyPosition,
|
||||
IterationType,
|
||||
NodeDisplayLayout
|
||||
} from '../../types';
|
||||
import { FamilyPosition, IterationType } from '../../types';
|
||||
import { IBinaryTree } from '../../interfaces';
|
||||
import { trampoline } from '../../utils';
|
||||
import { Queue } from '../queue';
|
||||
|
@ -107,14 +104,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
/**
|
||||
* The constructor function initializes a binary tree object with optional elements and options.
|
||||
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
||||
* @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the
|
||||
* elements to be added to the binary tree.
|
||||
* @param [options] - The `options` parameter is an optional object that can contain additional
|
||||
* configuration options for the binary tree. In this case, it is of type
|
||||
* `Partial<BinaryTreeOptions>`, which means that not all properties of `BinaryTreeOptions` are
|
||||
* required.
|
||||
*/
|
||||
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
|
||||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BinaryTreeOptions<K>>) {
|
||||
super();
|
||||
if (options) {
|
||||
const { iterationType, extractor } = options;
|
||||
|
@ -172,22 +169,22 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
/**
|
||||
* The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
|
||||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V,N>`.
|
||||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V,N>`.
|
||||
* @returns a boolean value indicating whether the exemplar is an instance of the class N.
|
||||
*/
|
||||
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
||||
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
||||
return exemplar instanceof BinaryTreeNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `exemplarToNode` converts an exemplar object into a node object.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
||||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
||||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
||||
* is provided, it will be `undefined`.
|
||||
* @returns a value of type N (node), or null, or undefined.
|
||||
*/
|
||||
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
||||
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
|
||||
if (exemplar === undefined) return;
|
||||
|
||||
let node: N | null | undefined;
|
||||
|
@ -214,11 +211,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
/**
|
||||
* The function checks if a given value is an entry in a binary tree node.
|
||||
* @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
|
||||
* @param kne - BTNExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
|
||||
* two type parameters V and N, representing the value and node type respectively.
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isEntry(kne: BTNodeExemplar<K, V, N>): kne is BTNodeEntry<K, V> {
|
||||
isEntry(kne: BTNExemplar<K, V, N>): kne is BTNEntry<K, V> {
|
||||
return Array.isArray(kne) && kne.length === 2;
|
||||
}
|
||||
|
||||
|
@ -237,7 +234,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {V} [value] - The value to be inserted into the binary tree.
|
||||
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
||||
*/
|
||||
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined {
|
||||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | null | undefined {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
||||
if (newNode === undefined) return;
|
||||
|
||||
|
@ -303,11 +300,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
*
|
||||
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
||||
* adds each node with its corresponding value to the data structure.
|
||||
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
||||
* @param nodes - An iterable collection of BTNExemplar objects.
|
||||
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
||||
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
||||
*/
|
||||
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
||||
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[] {
|
||||
// TODO not sure addMany not be run multi times
|
||||
const inserted: (N | null | undefined)[] = [];
|
||||
|
||||
|
@ -338,7 +335,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
refill(nodesOrKeysOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
|
||||
refill(nodesOrKeysOrEntries: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): void {
|
||||
this.clear();
|
||||
this.addMany(nodesOrKeysOrEntries, values);
|
||||
}
|
||||
|
@ -348,11 +345,11 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* Space Complexity: O(1)
|
||||
*/
|
||||
|
||||
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BiTreeDeleteResult<N>[];
|
||||
delete<C extends BTNCallback<N, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<N>[];
|
||||
|
||||
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BiTreeDeleteResult<N>[];
|
||||
delete<C extends BTNCallback<N, N>>(identifier: N | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
|
||||
|
||||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BiTreeDeleteResult<N>[];
|
||||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C): BinaryTreeDeleteResult<N>[];
|
||||
|
||||
/**
|
||||
* Time Complexity: O(n)
|
||||
|
@ -367,13 +364,13 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
||||
* identifier of the node to be deleted. It is optional and has a default value of
|
||||
* `this._defaultOneParamCallback`. The `callback` function should return the identifier of the node.
|
||||
* @returns an array of `BiTreeDeleteResult<N>`.
|
||||
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
||||
*/
|
||||
delete<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C
|
||||
): BiTreeDeleteResult<N>[] {
|
||||
const deletedResult: BiTreeDeleteResult<N>[] = [];
|
||||
): BinaryTreeDeleteResult<N>[] {
|
||||
const deletedResult: BinaryTreeDeleteResult<N>[] = [];
|
||||
if (!this.root) return deletedResult;
|
||||
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
||||
callback = (node => node) as C;
|
||||
|
@ -437,7 +434,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
|
||||
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
||||
*/
|
||||
getDepth(distNode: BTNodeKeyOrNode<K, N>, beginRoot: BTNodeKeyOrNode<K, N> = this.root): number {
|
||||
getDepth(distNode: BTNKeyOrNode<K, N>, beginRoot: BTNKeyOrNode<K, N> = this.root): number {
|
||||
distNode = this.ensureNode(distNode);
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
let depth = 0;
|
||||
|
@ -470,7 +467,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* values:
|
||||
* @returns the height of the binary tree.
|
||||
*/
|
||||
getHeight(beginRoot: BTNodeKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
||||
getHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (!beginRoot) return -1;
|
||||
|
||||
|
@ -519,7 +516,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* to calculate the minimum height of a binary tree. It can have two possible values:
|
||||
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
||||
*/
|
||||
getMinHeight(beginRoot: BTNodeKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
||||
getMinHeight(beginRoot: BTNKeyOrNode<K, N> = this.root, iterationType = this.iterationType): number {
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (!beginRoot) return -1;
|
||||
|
||||
|
@ -579,7 +576,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isPerfectlyBalanced(beginRoot: BTNodeKeyOrNode<K, N> = this.root): boolean {
|
||||
isPerfectlyBalanced(beginRoot: BTNKeyOrNode<K, N> = this.root): boolean {
|
||||
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
|
||||
}
|
||||
|
||||
|
@ -592,7 +589,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
identifier: K,
|
||||
callback?: C,
|
||||
onlyOne?: boolean,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N[];
|
||||
|
||||
|
@ -600,7 +597,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
identifier: N | null | undefined,
|
||||
callback?: C,
|
||||
onlyOne?: boolean,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N[];
|
||||
|
||||
|
@ -608,7 +605,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
identifier: ReturnType<C>,
|
||||
callback: C,
|
||||
onlyOne?: boolean,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N[];
|
||||
|
||||
|
@ -641,7 +638,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
onlyOne = false,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N[] {
|
||||
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
||||
|
@ -689,21 +686,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
has<C extends BTNCallback<N, K>>(
|
||||
identifier: K,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): boolean;
|
||||
|
||||
has<C extends BTNCallback<N, N>>(
|
||||
identifier: N | null | undefined,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): boolean;
|
||||
|
||||
has<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): boolean;
|
||||
|
||||
|
@ -730,7 +727,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
has<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): boolean {
|
||||
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
||||
|
@ -747,21 +744,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
getNode<C extends BTNCallback<N, K>>(
|
||||
identifier: K,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N | null | undefined;
|
||||
|
||||
getNode<C extends BTNCallback<N, N>>(
|
||||
identifier: N | null | undefined,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N | null | undefined;
|
||||
|
||||
getNode<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C>,
|
||||
callback: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): N | null | undefined;
|
||||
|
||||
|
@ -789,7 +786,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
getNode<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N | null | undefined {
|
||||
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
||||
|
@ -858,28 +855,28 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
||||
* itself if it is not a valid node key.
|
||||
*/
|
||||
ensureNode(key: BTNodeKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
|
||||
ensureNode(key: BTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | null | undefined {
|
||||
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
||||
}
|
||||
|
||||
get<C extends BTNCallback<N, K>>(
|
||||
identifier: K,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): V | undefined;
|
||||
|
||||
get<C extends BTNCallback<N, N>>(
|
||||
identifier: N | null | undefined,
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): V | undefined;
|
||||
|
||||
get<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C>,
|
||||
callback: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType
|
||||
): V | undefined;
|
||||
|
||||
|
@ -908,7 +905,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
get<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): V | undefined {
|
||||
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
|
||||
|
@ -952,7 +949,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
||||
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
||||
*/
|
||||
getPathToRoot(beginRoot: BTNodeKeyOrNode<K, N>, isReverse = true): N[] {
|
||||
getPathToRoot(beginRoot: BTNKeyOrNode<K, N>, isReverse = true): N[] {
|
||||
// TODO to support get path through passing key
|
||||
const result: N[] = [];
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
|
@ -989,7 +986,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
||||
*/
|
||||
getLeftMost(
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N | null | undefined {
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
|
@ -1035,7 +1032,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
||||
*/
|
||||
getRightMost(
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N | null | undefined {
|
||||
// TODO support get right most by passing key in
|
||||
|
@ -1077,7 +1074,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* possible values:
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isSubtreeBST(beginRoot: BTNodeKeyOrNode<K, N>, iterationType = this.iterationType): boolean {
|
||||
isSubtreeBST(beginRoot: BTNKeyOrNode<K, N>, iterationType = this.iterationType): boolean {
|
||||
// TODO there is a bug
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (!beginRoot) return true;
|
||||
|
@ -1138,21 +1135,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
subTreeTraverse<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: false
|
||||
): ReturnType<C>[];
|
||||
|
||||
subTreeTraverse<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: undefined
|
||||
): ReturnType<C>[];
|
||||
|
||||
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: true
|
||||
): ReturnType<C>[];
|
||||
|
@ -1181,7 +1178,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
*/
|
||||
subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType,
|
||||
includeNull = false
|
||||
): ReturnType<C>[] {
|
||||
|
@ -1236,7 +1233,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isRealNode(node: BTNodeExemplar<K, V, N>): node is N {
|
||||
isRealNode(node: BTNExemplar<K, V, N>): node is N {
|
||||
return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
|
||||
}
|
||||
|
||||
|
@ -1245,7 +1242,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isNIL(node: BTNodeExemplar<K, V, N>) {
|
||||
isNIL(node: BTNExemplar<K, V, N>) {
|
||||
return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
|
||||
}
|
||||
|
||||
|
@ -1254,7 +1251,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
||||
* @returns a boolean value.
|
||||
*/
|
||||
isNodeOrNull(node: BTNodeExemplar<K, V, N>): node is N | null {
|
||||
isNodeOrNull(node: BTNExemplar<K, V, N>): node is N | null {
|
||||
return this.isRealNode(node) || node === null;
|
||||
}
|
||||
|
||||
|
@ -1264,14 +1261,14 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* data type.
|
||||
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
||||
*/
|
||||
isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
||||
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
||||
return !(potentialKey instanceof BinaryTreeNode)
|
||||
}
|
||||
|
||||
dfs<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
pattern?: DFSOrderPattern,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: false
|
||||
): ReturnType<C>[];
|
||||
|
@ -1279,7 +1276,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
dfs<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
pattern?: DFSOrderPattern,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: undefined
|
||||
): ReturnType<C>[];
|
||||
|
@ -1287,7 +1284,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
dfs<C extends BTNCallback<N | null | undefined>>(
|
||||
callback?: C,
|
||||
pattern?: DFSOrderPattern,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: true
|
||||
): ReturnType<C>[];
|
||||
|
@ -1318,7 +1315,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
dfs<C extends BTNCallback<N | null | undefined>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
pattern: DFSOrderPattern = 'in',
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType: IterationType = IterationType.ITERATIVE,
|
||||
includeNull = false
|
||||
): ReturnType<C>[] {
|
||||
|
@ -1417,21 +1414,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
bfs<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: false
|
||||
): ReturnType<C>[];
|
||||
|
||||
bfs<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: undefined
|
||||
): ReturnType<C>[];
|
||||
|
||||
bfs<C extends BTNCallback<N | null | undefined>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: true
|
||||
): ReturnType<C>[];
|
||||
|
@ -1459,7 +1456,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
*/
|
||||
bfs<C extends BTNCallback<N | null | undefined>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType,
|
||||
includeNull = false
|
||||
): ReturnType<C>[] {
|
||||
|
@ -1518,21 +1515,21 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
|
||||
listLevels<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: false
|
||||
): ReturnType<C>[][];
|
||||
|
||||
listLevels<C extends BTNCallback<N>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: undefined
|
||||
): ReturnType<C>[][];
|
||||
|
||||
listLevels<C extends BTNCallback<N | null | undefined>>(
|
||||
callback?: C,
|
||||
beginRoot?: BTNodeKeyOrNode<K, N>,
|
||||
beginRoot?: BTNKeyOrNode<K, N>,
|
||||
iterationType?: IterationType,
|
||||
includeNull?: true
|
||||
): ReturnType<C>[][];
|
||||
|
@ -1560,7 +1557,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
*/
|
||||
listLevels<C extends BTNCallback<N | null | undefined>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType,
|
||||
includeNull = false
|
||||
): ReturnType<C>[][] {
|
||||
|
@ -1677,7 +1674,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
morris<C extends BTNCallback<N>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
pattern: DFSOrderPattern = 'in',
|
||||
beginRoot: BTNodeKeyOrNode<K, N> = this.root
|
||||
beginRoot: BTNKeyOrNode<K, N> = this.root
|
||||
): ReturnType<C>[] {
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (beginRoot === null) return [];
|
||||
|
@ -1856,7 +1853,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* following types:
|
||||
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
|
||||
*/
|
||||
print(beginRoot: BTNodeKeyOrNode<K, N> = this.root, options?: BinaryTreePrintOptions): void {
|
||||
print(beginRoot: BTNKeyOrNode<K, N> = this.root, options?: BinaryTreePrintOptions): void {
|
||||
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
if (!beginRoot) return;
|
||||
|
@ -1968,7 +1965,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* @param {N} destNode - The destination node to swap.
|
||||
* @returns {N} - The destination node after the swap.
|
||||
*/
|
||||
protected _swapProperties(srcNode: BTNodeKeyOrNode<K, N>, destNode: BTNodeKeyOrNode<K, N>): N | undefined {
|
||||
protected _swapProperties(srcNode: BTNKeyOrNode<K, N>, destNode: BTNKeyOrNode<K, N>): N | undefined {
|
||||
srcNode = this.ensureNode(srcNode);
|
||||
destNode = this.ensureNode(destNode);
|
||||
|
||||
|
@ -2026,7 +2023,7 @@ export class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = Bi
|
|||
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
||||
* If the parent node is null, the function also returns undefined.
|
||||
*/
|
||||
protected _addTo(newNode: N | null | undefined, parent: BTNodeKeyOrNode<K, N>): N | null | undefined {
|
||||
protected _addTo(newNode: N | null | undefined, parent: BTNKeyOrNode<K, N>): N | null | undefined {
|
||||
if (this.isNotNodeInstance(parent)) parent = this.getNode(parent);
|
||||
|
||||
if (parent) {
|
||||
|
|
|
@ -11,10 +11,11 @@ import type {
|
|||
BSTNodeNested,
|
||||
BSTOptions,
|
||||
BTNCallback,
|
||||
BTNodeExemplar,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
BTNodePureExemplar
|
||||
} from '../../types';
|
||||
import { BSTVariant, BTNodeKeyOrNode, CP, IterationType } from '../../types';
|
||||
import { BSTVariant, CP, IterationType } from '../../types';
|
||||
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
||||
import { IBinaryTree } from '../../interfaces';
|
||||
import { Queue } from '../queue';
|
||||
|
@ -87,12 +88,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
/**
|
||||
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
||||
* the tree with optional elements and options.
|
||||
* @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
|
||||
* @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
|
||||
* binary search tree.
|
||||
* @param [options] - The `options` parameter is an optional object that can contain additional
|
||||
* configuration options for the binary search tree. It can have the following properties:
|
||||
*/
|
||||
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
||||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
||||
super([], options);
|
||||
|
||||
if (options) {
|
||||
|
@ -147,10 +148,10 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
|
||||
/**
|
||||
* The function checks if an exemplar is an instance of BSTNode.
|
||||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNodeExemplar<K, V, N>`.
|
||||
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
||||
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
||||
*/
|
||||
override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
||||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
||||
return exemplar instanceof BSTNode;
|
||||
}
|
||||
|
||||
|
@ -158,12 +159,12 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
/**
|
||||
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
||||
* otherwise it returns undefined.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
||||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
||||
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
||||
* @returns a node of type N or undefined.
|
||||
*/
|
||||
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
||||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
||||
let node: N | undefined;
|
||||
if (exemplar === null || exemplar === undefined) {
|
||||
return;
|
||||
|
@ -201,7 +202,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
||||
* node was not added.
|
||||
*/
|
||||
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
||||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
||||
if (newNode === undefined) return;
|
||||
|
||||
|
@ -273,7 +274,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
||||
*/
|
||||
override addMany(
|
||||
keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>,
|
||||
keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>,
|
||||
values?: Iterable<V | undefined>,
|
||||
isBalanceAdd = true,
|
||||
iterationType = this.iterationType
|
||||
|
@ -297,7 +298,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
|
||||
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
||||
|
||||
const isRealBTNExemplar = (kve: BTNodeExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
||||
const isRealBTNExemplar = (kve: BTNExemplar<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
||||
if (kve === undefined || kve === null) return false;
|
||||
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
||||
}
|
||||
|
@ -448,7 +449,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
* data type.
|
||||
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
||||
*/
|
||||
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
||||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
||||
return !(potentialKey instanceof BSTNode)
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,11 @@
|
|||
*/
|
||||
|
||||
import {
|
||||
BiTreeDeleteResult,
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BTNCallback,
|
||||
BTNodeExemplar,
|
||||
BTNodeKeyOrNode,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
IterationType,
|
||||
RBTNColor,
|
||||
RBTreeOptions,
|
||||
|
@ -20,7 +20,6 @@ import {
|
|||
} from '../../types';
|
||||
import { BST, BSTNode } from './bst';
|
||||
import { IBinaryTree } from '../../interfaces';
|
||||
import { BinaryTreeNode } from './binary-tree';
|
||||
|
||||
export class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<
|
||||
K, V,
|
||||
|
@ -49,7 +48,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
/**
|
||||
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
||||
* initializes the tree with optional elements and options.
|
||||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<K, V, N>`
|
||||
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
||||
* objects. It represents the initial elements that will be added to the RBTree during its
|
||||
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
||||
* elements to the
|
||||
|
@ -57,7 +56,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
||||
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
||||
*/
|
||||
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
|
||||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<RBTreeOptions<K>>) {
|
||||
super([], options);
|
||||
|
||||
this._root = this.Sentinel;
|
||||
|
@ -108,11 +107,11 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
|
||||
/**
|
||||
* The function checks if an exemplar is an instance of the RedBlackTreeNode class.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
||||
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
||||
* class.
|
||||
*/
|
||||
override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
||||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
||||
return exemplar instanceof RedBlackTreeNode;
|
||||
}
|
||||
|
||||
|
@ -122,19 +121,19 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
* data type.
|
||||
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
||||
*/
|
||||
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
||||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
||||
return !(potentialKey instanceof RedBlackTreeNode)
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
||||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
||||
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
||||
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
||||
* @returns a node of type N or undefined.
|
||||
*/
|
||||
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
||||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
||||
let node: N | undefined;
|
||||
|
||||
if (exemplar === null || exemplar === undefined) {
|
||||
|
@ -173,7 +172,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
* being added to the binary search tree.
|
||||
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
||||
*/
|
||||
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined {
|
||||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
||||
if (newNode === undefined) return;
|
||||
|
||||
|
@ -242,13 +241,13 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` and
|
||||
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
||||
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
||||
* @returns an array of `BiTreeDeleteResult<N>`.
|
||||
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
||||
*/
|
||||
delete<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | null | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C
|
||||
): BiTreeDeleteResult<N>[] {
|
||||
const ans: BiTreeDeleteResult<N>[] = [];
|
||||
): BinaryTreeDeleteResult<N>[] {
|
||||
const ans: BinaryTreeDeleteResult<N>[] = [];
|
||||
if (identifier === null) return ans;
|
||||
const helper = (node: N | undefined): void => {
|
||||
let z: N = this.Sentinel;
|
||||
|
@ -368,7 +367,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
beginRoot: BSTNodeKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N | null | undefined {
|
||||
if ((identifier as any) instanceof BinaryTreeNode) callback = (node => node) as C;
|
||||
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
||||
}
|
||||
|
|
|
@ -5,15 +5,17 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { BSTNodeKeyOrNode, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
||||
import {
|
||||
BiTreeDeleteResult,
|
||||
import type {
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BTNCallback,
|
||||
BTNodeKeyOrNode,
|
||||
FamilyPosition,
|
||||
IterationType,
|
||||
TreeMultimapNested
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
TreeMultimapNested,
|
||||
TreeMultimapNodeNested,
|
||||
TreeMultimapOptions
|
||||
} from '../../types';
|
||||
import { FamilyPosition, IterationType } from '../../types';
|
||||
import { IBinaryTree } from '../../interfaces';
|
||||
import { AVLTree, AVLTreeNode } from './avl-tree';
|
||||
|
||||
|
@ -48,7 +50,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
extends AVLTree<K, V, N, TREE>
|
||||
implements IBinaryTree<K, V, N, TREE> {
|
||||
|
||||
constructor(elements?: Iterable<BTNodeExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
||||
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<TreeMultimapOptions<K>>) {
|
||||
super([], options);
|
||||
if (elements) this.addMany(elements);
|
||||
}
|
||||
|
@ -84,11 +86,11 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
|
||||
/**
|
||||
* The function checks if an exemplar is an instance of the TreeMultimapNode class.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
||||
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
||||
* class.
|
||||
*/
|
||||
override isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N {
|
||||
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
||||
return exemplar instanceof TreeMultimapNode;
|
||||
}
|
||||
|
||||
|
@ -98,13 +100,13 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* data type.
|
||||
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
||||
*/
|
||||
override isNotNodeInstance(potentialKey: BTNodeKeyOrNode<K, N>): potentialKey is K {
|
||||
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
||||
return !(potentialKey instanceof TreeMultimapNode)
|
||||
}
|
||||
|
||||
/**
|
||||
* The function `exemplarToNode` converts an exemplar object into a node object.
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, which means it
|
||||
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
||||
* can be one of the following:
|
||||
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
||||
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
||||
|
@ -113,7 +115,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* times the value should be added to the node. If not provided, it defaults to 1.
|
||||
* @returns a node of type `N` or `undefined`.
|
||||
*/
|
||||
override exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
||||
override exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
||||
let node: N | undefined;
|
||||
if (exemplar === undefined || exemplar === null) {
|
||||
return;
|
||||
|
@ -155,7 +157,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
||||
* was not successful.
|
||||
*/
|
||||
override add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
||||
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count = 1): N | undefined {
|
||||
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
||||
if (newNode === undefined) return;
|
||||
|
||||
|
@ -182,7 +184,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* either keys, nodes, or entries.
|
||||
* @returns The method is returning an array of type `N | undefined`.
|
||||
*/
|
||||
override addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>): (N | undefined)[] {
|
||||
override addMany(keysOrNodesOrEntries: Iterable<BTNExemplar<K, V, N>>): (N | undefined)[] {
|
||||
return super.addMany(keysOrNodesOrEntries);
|
||||
}
|
||||
|
||||
|
@ -262,14 +264,14 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
||||
* deleted regardless of its count. If set to false (default), the count of the node will be
|
||||
* decremented by 1 and
|
||||
* @returns an array of `BiTreeDeleteResult<N>`.
|
||||
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
||||
*/
|
||||
override delete<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C>,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
ignoreCount = false
|
||||
): BiTreeDeleteResult<N>[] {
|
||||
const deletedResult: BiTreeDeleteResult<N>[] = [];
|
||||
): BinaryTreeDeleteResult<N>[] {
|
||||
const deletedResult: BinaryTreeDeleteResult<N>[] = [];
|
||||
if (!this.root) return deletedResult;
|
||||
|
||||
const curr: N | undefined = this.getNode(identifier, callback) ?? undefined;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import { isWeakKey, rangeCheck } from '../../utils';
|
||||
import { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
||||
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
||||
import { IterableEntryBase } from "../base";
|
||||
|
||||
export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
* @license MIT License
|
||||
*/
|
||||
|
||||
export class HashTableNode<K, V> {
|
||||
import { HashFunction } from '../../types';
|
||||
|
||||
export class HashTableNode<K = any, V = any> {
|
||||
key: K;
|
||||
value: V;
|
||||
next: HashTableNode<K, V> | undefined;
|
||||
|
@ -18,8 +20,6 @@ export class HashTableNode<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
import { HashFunction } from '../../types';
|
||||
|
||||
export class HashTable<K = any, V = any> {
|
||||
protected static readonly DEFAULT_CAPACITY = 16;
|
||||
protected static readonly LOAD_FACTOR = 0.75;
|
||||
|
|
|
@ -5,8 +5,7 @@
|
|||
* @license MIT License
|
||||
*/
|
||||
|
||||
import type { Comparator, DFSOrderPattern, ElementCallback } from '../../types';
|
||||
import { HeapOptions } from "../../types";
|
||||
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
||||
import { IterableElementBase } from "../base";
|
||||
|
||||
export class Heap<E = any> extends IterableElementBase<E> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* data-structure-typed
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* data-structure-typed
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import { Heap } from '../heap';
|
||||
import { PriorityQueueOptions } from '../../types';
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
|
||||
export class PriorityQueue<E = any> extends Heap<E> {
|
||||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
|
||||
import { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
||||
import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
||||
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
||||
import { IterableElementBase } from "../base";
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
import { SinglyLinkedList } from '../linked-list';
|
||||
import { IterableElementBase } from "../base";
|
||||
import { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
export class Queue<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* @license MIT
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import { IterableElementBase } from "../base";
|
||||
import { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
||||
import {
|
||||
BinaryTreeDeleteResult,
|
||||
BinaryTreeNested,
|
||||
BinaryTreeNodeNested,
|
||||
BinaryTreeOptions,
|
||||
BiTreeDeleteResult,
|
||||
BTNCallback,
|
||||
BTNodeExemplar,
|
||||
BTNExemplar,
|
||||
} from '../types';
|
||||
|
||||
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
||||
|
@ -13,9 +13,9 @@ export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V,
|
|||
|
||||
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
||||
|
||||
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
||||
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
||||
|
||||
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
||||
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
||||
|
||||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
||||
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
||||
}
|
||||
|
|
|
@ -27,11 +27,11 @@ export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLeng
|
|||
|
||||
export type BinaryTreePrintOptions = { isShowUndefined?: boolean, isShowNull?: boolean, isShowRedBlackNIL?: boolean }
|
||||
|
||||
export type BTNodeEntry<K, V> = [K | null | undefined, V | undefined];
|
||||
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
||||
|
||||
export type BTNodeKeyOrNode<K, N> = K | null | undefined | N;
|
||||
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
||||
|
||||
export type BTNodeExemplar<K, V, N> = BTNodeEntry<K, V> | BTNodeKeyOrNode<K, N>
|
||||
export type BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>
|
||||
|
||||
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ export enum FamilyPosition {
|
|||
MAL_NODE = 'MAL_NODE'
|
||||
}
|
||||
|
||||
export type BiTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
||||
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
||||
|
||||
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
|
|
Loading…
Reference in a new issue