refactor: Renamed some type aliases

This commit is contained in:
Revone 2024-03-16 09:47:22 +08:00
parent 94d34e61e7
commit ec8242044a
10 changed files with 271 additions and 259 deletions

View file

@ -12,8 +12,8 @@ import type {
BinaryTreeDeleteResult,
BSTNKeyOrNode,
BTNCallback,
IterationType,
KeyOrNodeOrEntry
BTNKeyOrNodeOrEntry,
IterationType
} from '../../types';
import { BTNEntry } from '../../types';
import { IBinaryTree } from '../../interfaces';
@ -86,7 +86,7 @@ export class AVLTreeMultiMap<
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: AVLTreeMultiMapOptions<K, V, R>
) {
super([], options);
@ -155,13 +155,13 @@ export class AVLTreeMultiMap<
/**
* The function checks if the input is an instance of AVLTreeMultiMapNode.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `AVLTreeMultiMapNode` class.
*/
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
}
@ -169,8 +169,8 @@ export class AVLTreeMultiMap<
/**
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
* a node object.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `override` function. It represents the value associated with the key in the data structure. If no
* value is provided, it will default to `undefined`.
@ -179,7 +179,7 @@ export class AVLTreeMultiMap<
* @returns either a NODE object or undefined.
*/
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
value?: V,
count = 1
): NODE | undefined {
@ -213,9 +213,9 @@ export class AVLTreeMultiMap<
*
* The function overrides the add method of a TypeScript class to add a new node to a data structure
* and update the count.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
* can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
* can also accept a value of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
* entry, or raw element
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* data structure. It is an optional parameter, so it can be omitted if not needed.
@ -224,7 +224,7 @@ export class AVLTreeMultiMap<
* be added once. However, you can specify a different value for `count` if you want to add
* @returns a boolean value.
*/
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
if (newNode === undefined) return false;

View file

@ -13,7 +13,7 @@ import type {
BinaryTreeDeleteResult,
BSTNKeyOrNode,
BTNCallback,
KeyOrNodeOrEntry
BTNKeyOrNodeOrEntry
} from '../../types';
import { BTNEntry } from '../../types';
import { IBinaryTree } from '../../interfaces';
@ -86,7 +86,7 @@ export class AVLTree<
* `nodeBuilder` (
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: AVLTreeOptions<K, V, R>
) {
super([], options);
@ -123,13 +123,13 @@ export class AVLTree<
/**
* The function checks if the input is an instance of AVLTreeNode.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `AVLTreeNode` class.
*/
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
}
@ -146,14 +146,14 @@ export class AVLTree<
*
* The function overrides the add method of a class and inserts a key-value pair into a data
* structure, then balances the path.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
* `RawElement`.
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
* the key or node being added to the data structure.
* @returns The method is returning a boolean value.
*/
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
if (keyOrNodeOrEntryOrRawElement === null) return false;
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
@ -488,10 +488,10 @@ export class AVLTree<
*
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
* to restore balance in an AVL tree after inserting a node.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
* `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
*/
protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void {
protected _balancePath(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): void {
node = this.ensureNode(node);
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
for (let i = 0; i < path.length; i++) {

View file

@ -14,12 +14,13 @@ import type {
BinaryTreePrintOptions,
BTNCallback,
BTNEntry,
BTNKeyOrNodeOrEntry,
DFSOrderPattern,
EntryCallback,
FamilyPosition,
IterationType,
KeyOrNodeOrEntry,
NodeDisplayLayout
NodeDisplayLayout,
OptBTNOrNull
} from '../../types';
import { IBinaryTree } from '../../interfaces';
import { trampoline } from '../../utils';
@ -62,16 +63,16 @@ export class BinaryTreeNode<
* @returns The left node of the current node is being returned. It can be either a NODE object,
* null, or undefined.
*/
get left(): NODE | null | undefined {
get left(): OptBTNOrNull<NODE> {
return this._left;
}
/**
* The function sets the left child of a node and updates its parent reference.
* @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
* `undefined`.
*/
set left(v: NODE | null | undefined) {
set left(v: OptBTNOrNull<NODE>) {
if (v) {
v.parent = this as unknown as NODE;
}
@ -85,16 +86,16 @@ export class BinaryTreeNode<
* @returns The method is returning the value of the `_right` property, which can be a `NODE` object,
* `null`, or `undefined`.
*/
get right(): NODE | null | undefined {
get right(): OptBTNOrNull<NODE> {
return this._right;
}
/**
* The function sets the right child of a node and updates its parent.
* @param {NODE | null | undefined} v - The parameter `v` can be of type `NODE`, `null`, or
* @param {OptBTNOrNull<NODE>} v - The parameter `v` can be of type `NODE`, `null`, or
* `undefined`.
*/
set right(v: NODE | null | undefined) {
set right(v: OptBTNOrNull<NODE>) {
if (v) {
v.parent = this as unknown as NODE;
}
@ -142,7 +143,7 @@ export class BinaryTree<
/**
* The constructor function initializes a binary tree object with optional keysOrNodesOrEntriesOrRawElements and options.
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of KeyOrNodeOrEntry objects. These objects represent the
* @param [keysOrNodesOrEntriesOrRawElements] - Optional iterable of BTNKeyOrNodeOrEntry objects. These objects represent the
* nodes 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
@ -150,7 +151,7 @@ export class BinaryTree<
* required.
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: BinaryTreeOptions<K, V, R>
) {
super();
@ -164,14 +165,14 @@ export class BinaryTree<
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
}
protected _root?: NODE | null;
protected _root?: OptBTNOrNull<NODE>;
/**
* The function returns the root node, which can be of type NODE, null, or undefined.
* @returns The method is returning the value of the `_root` property, which can be of type `NODE`,
* `null`, or `undefined`.
*/
get root(): NODE | null | undefined {
get root(): OptBTNOrNull<NODE> {
return this._root;
}
@ -229,8 +230,8 @@ export class BinaryTree<
/**
* The function `keyValueOrEntryOrRawElementToNode` converts a key-value pair, entry, or raw element
* into a node object.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
* `keyValueOrEntryOrRawElementToNode` function. It represents the value associated with a key in a
* key-value pair. If provided, it will be used to create a node with the specified key and value.
@ -238,9 +239,9 @@ export class BinaryTree<
* or `undefined`.
*/
keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
value?: V
): NODE | null | undefined {
): OptBTNOrNull<NODE> {
if (keyOrNodeOrEntryOrRawElement === undefined) return;
if (keyOrNodeOrEntryOrRawElement === null) return null;
@ -275,8 +276,8 @@ export class BinaryTree<
*
* The `ensureNode` function checks if the input is a valid node and returns it, or converts it to a
* node if it is a key or entry.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
* a raw element.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
* parameter that specifies the type of iteration to be used when searching for a node. It has a
@ -284,9 +285,9 @@ export class BinaryTree<
* @returns The function `ensureNode` returns either a `NODE` object, `null`, or `undefined`.
*/
ensureNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = 'ITERATIVE'
): NODE | null | undefined {
): OptBTNOrNull<NODE> {
if (keyOrNodeOrEntryOrRawElement === null) return null;
if (keyOrNodeOrEntryOrRawElement === undefined) return;
if (keyOrNodeOrEntryOrRawElement === this.NIL) return;
@ -310,55 +311,55 @@ export class BinaryTree<
/**
* The function checks if the input is an instance of the BinaryTreeNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `BinaryTreeNode` class.
*/
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof BinaryTreeNode;
}
/**
* The function checks if a given node is a valid node in a binary search tree.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isRealNode(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
isRealNode(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE {
if (node === this.NIL || node === null || node === undefined) return false;
return this.isNode(node);
}
/**
* The function checks if a given node is a real node or null.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isNodeOrNull(node: R | KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
isNodeOrNull(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
return this.isRealNode(node) || node === null;
}
/**
* The function checks if a given node is equal to the NIL value.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The parameter `node` can be of type `R` or
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isNIL(node: R | KeyOrNodeOrEntry<K, V, NODE>) {
isNIL(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>) {
return node === this.NIL;
}
/**
* The function checks if the input is an array with two elements, indicating it is a binary tree
* node entry.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value.
*/
isEntry(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is BTNEntry<K, V> {
return Array.isArray(keyOrNodeOrEntryOrRawElement) && keyOrNodeOrEntryOrRawElement.length === 2;
}
@ -402,17 +403,17 @@ export class BinaryTree<
*
* The `add` function is used to insert a new node into a binary tree, checking for duplicate keys
* and finding the appropriate insertion position.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which represents the key,
* node, entry, or raw element to be added to the tree. It can also accept a value of type
* `KeyOrNodeOrEntry<K, V, NODE>
* `BTNKeyOrNodeOrEntry<K, V, NODE>
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
* key being added to the tree. It represents the value that will be stored in the tree for the given
* key.
* @returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
* insertion position cannot be found or if there are duplicate keys.
*/
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
if (newNode === undefined) return false;
@ -486,7 +487,7 @@ export class BinaryTree<
* successfully added to the data structure.
*/
addMany(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
values?: Iterable<V | undefined>
): boolean[] {
// TODO not sure addMany not be run multi times
@ -525,13 +526,13 @@ export class BinaryTree<
*
* The `refill` function clears the current data and adds new data to the collection.
* @param keysOrNodesOrEntriesOrRawElements - An iterable collection of keys, nodes, entries, or raw
* elements. These can be of any type (R) or a specific type (KeyOrNodeOrEntry<K, V, NODE>).
* elements. These can be of any type (R) or a specific type (BTNKeyOrNodeOrEntry<K, V, NODE>).
* @param [values] - The `values` parameter is an optional iterable of values that will be associated
* with the keys or nodes being added. If provided, the values will be assigned to the corresponding
* keys or nodes. If not provided, the values will be set to `undefined`.
*/
refill(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
values?: Iterable<V | undefined>
): void {
this.clear();
@ -541,7 +542,7 @@ export class BinaryTree<
delete<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C): BinaryTreeDeleteResult<NODE>[];
delete<C extends BTNCallback<NODE, NODE>>(
identifier: NODE | null | undefined,
identifier: OptBTNOrNull<NODE>,
callback?: C
): BinaryTreeDeleteResult<NODE>[];
@ -619,15 +620,15 @@ export class BinaryTree<
identifier: K,
callback?: C,
onlyOne?: boolean,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE[];
getNodes<C extends BTNCallback<NODE, NODE>>(
identifier: NODE | null | undefined,
identifier: OptBTNOrNull<NODE>,
callback?: C,
onlyOne?: boolean,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE[];
@ -635,7 +636,7 @@ export class BinaryTree<
identifier: ReturnType<C>,
callback: C,
onlyOne?: boolean,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE[];
@ -661,7 +662,7 @@ export class BinaryTree<
* the identifier or all nodes that match the identifier. If set to true, only the first matching
* node will be returned. If set to false, all matching nodes will be returned. The default value is
* false.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the search. It can be either a node object, a key-value pair, or a key. If it is not
* provided, the `root` of the data structure is used as the starting point.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@ -672,7 +673,7 @@ export class BinaryTree<
identifier: ReturnType<C> | null | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
onlyOne = false,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE[] {
beginRoot = this.ensureNode(beginRoot);
@ -714,23 +715,23 @@ export class BinaryTree<
getNode<C extends BTNCallback<NODE, K>>(
identifier: K,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE | null | undefined;
): OptBTNOrNull<NODE>;
getNode<C extends BTNCallback<NODE, NODE>>(
identifier: NODE | null | undefined,
identifier: OptBTNOrNull<NODE>,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE | null | undefined;
): OptBTNOrNull<NODE>;
getNode<C extends BTNCallback<NODE>>(
identifier: ReturnType<C>,
callback: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): NODE | null | undefined;
): OptBTNOrNull<NODE>;
/**
* Time Complexity: O(n)
@ -748,7 +749,7 @@ export class BinaryTree<
* the `C` callback function, or it can be `null` or `undefined`.
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
* node matches the desired criteria. It should return a value that can be used to identify the node.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for searching nodes in a tree structure. It can be either a root node, a key-value pair, or
* a node entry. If not provided, the search will start from the root of the tree.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@ -758,9 +759,9 @@ export class BinaryTree<
getNode<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE | null | undefined {
): OptBTNOrNull<NODE> {
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
}
@ -781,28 +782,28 @@ export class BinaryTree<
* It has a default value of `'ITERATIVE'`.
* @returns a value of type NODE, null, or undefined.
*/
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBTNOrNull<NODE> {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
override get<C extends BTNCallback<NODE, K>>(
identifier: K,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): V | undefined;
override get<C extends BTNCallback<NODE, NODE>>(
identifier: NODE | null | undefined,
identifier: OptBTNOrNull<NODE>,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): V | undefined;
override get<C extends BTNCallback<NODE>>(
identifier: ReturnType<C>,
callback: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): V | undefined;
@ -822,7 +823,7 @@ export class BinaryTree<
* callback function `C`. It can also be `null` or `undefined` if no identifier is provided.
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
* node matches the given identifier. It is optional and defaults to `this._DEFAULT_CALLBACK`.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the search in the binary tree. It can be either a root node of the tree or a key, node,
* or entry object that exists in the tree. If no specific starting point is provided, the search
* will begin from the root of the
@ -835,7 +836,7 @@ export class BinaryTree<
override get<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): V | undefined {
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
@ -844,21 +845,21 @@ export class BinaryTree<
override has<C extends BTNCallback<NODE, K>>(
identifier: K,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): boolean;
override has<C extends BTNCallback<NODE, NODE>>(
identifier: NODE | null | undefined,
identifier: OptBTNOrNull<NODE>,
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): boolean;
override has<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | null | undefined,
callback: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType
): boolean;
@ -880,7 +881,7 @@ export class BinaryTree<
* @param {C} callback - The `callback` parameter is a function that will be used to determine
* whether a node should be included in the result or not. It is of type `C`, which extends the
* `BTNCallback<NODE>` type.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the iteration in the data structure. It can be either a root node, a key-value pair, or
* a node entry. If not specified, it defaults to the root of the data structure.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@ -890,7 +891,7 @@ export class BinaryTree<
override has<C extends BTNCallback<NODE>>(
identifier: ReturnType<C> | null | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): boolean {
callback = this._ensureCallback(identifier, callback);
@ -941,13 +942,13 @@ export class BinaryTree<
*
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
* height of the tree.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The parameter `beginRoot` is optional and
* has a default value of `this.root`. It represents the starting point for checking if the tree is
* perfectly balanced. It can be either a root node (`R`), a key or node or entry
* (`KeyOrNodeOrEntry<K, V, NODE
* (`BTNKeyOrNodeOrEntry<K, V, NODE
* @returns a boolean value.
*/
isPerfectlyBalanced(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
isPerfectlyBalanced(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root): boolean {
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
}
@ -961,7 +962,7 @@ export class BinaryTree<
* Space Complexity: O(1)
*
* The function `isBST` checks if a binary search tree is valid, either recursively or iteratively.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for checking if a binary search tree (BST) is valid. It can be either a root node
* of the BST, a key value of a node in the BST, or an entry object containing both the key and value
* of a node in the BST
@ -971,7 +972,7 @@ export class BinaryTree<
* @returns a boolean value.
*/
isBST(
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): boolean {
// TODO there is a bug
@ -979,7 +980,7 @@ export class BinaryTree<
if (!beginRoot) return true;
if (iterationType === 'RECURSIVE') {
const dfs = (cur: NODE | null | undefined, min: number, max: number): boolean => {
const dfs = (cur: OptBTNOrNull<NODE>, min: number, max: number): boolean => {
if (!this.isRealNode(cur)) return true;
const numKey = Number(cur.key);
if (numKey <= min || numKey >= max) return false;
@ -994,7 +995,7 @@ export class BinaryTree<
const stack = [];
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
// @ts-ignore
let curr: NODE | null | undefined = beginRoot;
let curr: OptBTNOrNull<NODE> = beginRoot;
while (this.isRealNode(curr) || stack.length > 0) {
while (this.isRealNode(curr)) {
stack.push(curr);
@ -1024,16 +1025,19 @@ export class BinaryTree<
* Space Complexity: O(1)
*
* The function calculates the depth of a given node or key in a tree-like data structure.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
* (representing a root node), or a `KeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} dist - The `dist` parameter can be either a `R`
* (representing a root node), or a `BTNKeyOrNodeOrEntry<K, V, NODE>` (representing a key, node, or
* entry).
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is optional and
* represents the starting point from which to calculate the depth. It can be either a reference to a
* node in the tree or a key-value pair or an entry object. If not provided, the default value is
* `this.root`, which refers to the root node
* @returns the depth of a node in a tree structure.
*/
getDepth(dist: R | KeyOrNodeOrEntry<K, V, NODE>, beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root): number {
getDepth(
dist: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
): number {
let distEnsured = this.ensureNode(dist);
const beginRootEnsured = this.ensureNode(beginRoot);
let depth = 0;
@ -1058,22 +1062,22 @@ export class BinaryTree<
*
* The `getHeight` function calculates the maximum height of a binary tree using either a recursive
* or iterative approach.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for calculating the height of a tree. It can be either a root node (`R`), a key or
* node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
* node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current tree.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* iteration used to calculate the height of the tree. It can have two possible values:
* @returns the maximum height of the binary tree.
*/
getHeight(
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): number {
beginRoot = this.ensureNode(beginRoot);
if (!this.isRealNode(beginRoot)) return -1;
if (iterationType === 'RECURSIVE') {
const _getMaxHeight = (cur: NODE | null | undefined): number => {
const _getMaxHeight = (cur: OptBTNOrNull<NODE>): number => {
if (!this.isRealNode(cur)) return -1;
const leftHeight = _getMaxHeight(cur.left);
const rightHeight = _getMaxHeight(cur.right);
@ -1109,9 +1113,9 @@ export class BinaryTree<
*
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
* recursive or iterative approach.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for calculating the minimum height of a tree. It can be either a root node (`R`), a
* key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
* key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or it defaults to the root of the current
* tree.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
* iteration to be used when calculating the minimum height of the tree. It can have two possible
@ -1120,14 +1124,14 @@ export class BinaryTree<
* binary tree.
*/
getMinHeight(
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): number {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return -1;
if (iterationType === 'RECURSIVE') {
const _getMinHeight = (cur: NODE | null | undefined): number => {
const _getMinHeight = (cur: OptBTNOrNull<NODE>): number => {
if (!this.isRealNode(cur)) return 0;
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return 0;
const leftMinHeight = _getMinHeight(cur.left);
@ -1138,8 +1142,8 @@ export class BinaryTree<
return _getMinHeight(beginRoot);
} else {
const stack: NODE[] = [];
let node: NODE | null | undefined = beginRoot,
last: NODE | null | undefined = null;
let node: OptBTNOrNull<NODE> = beginRoot,
last: OptBTNOrNull<NODE> = null;
const depths: Map<NODE, number> = new Map();
while (stack.length > 0 || node) {
@ -1176,14 +1180,14 @@ export class BinaryTree<
*
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
* up to the root node, with an option to reverse the order of the nodes.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
* type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginNode - The `beginNode` parameter can be either of
* type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
* @returns The function `getPathToRoot` returns an array of `NODE` objects.
*/
getPathToRoot(beginNode: R | KeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
getPathToRoot(beginNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>, isReverse = true): NODE[] {
const result: NODE[] = [];
let beginNodeEnsured = this.ensureNode(beginNode);
@ -1209,17 +1213,17 @@ export class BinaryTree<
*
* The `getLeftMost` function returns the leftmost node in a binary tree, either using recursive or
* iterative traversal.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for finding the leftmost node in a binary tree. It can be either a root node (`R`),
* a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
* a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
* of iteration to be performed. It can have two possible values:
* @returns The function `getLeftMost` returns the leftmost node in a binary tree.
*/
getLeftMost(
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE | null | undefined {
): OptBTNOrNull<NODE> {
if (this.isNIL(beginRoot)) return beginRoot as NODE;
beginRoot = this.ensureNode(beginRoot);
@ -1254,18 +1258,18 @@ export class BinaryTree<
*
* The `getRightMost` function returns the rightmost node in a binary tree, either recursively or
* iteratively.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for finding the rightmost node in a binary tree. It can be either a root node
* (`R`), a key or node or entry (`KeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
* (`R`), a key or node or entry (`BTNKeyOrNodeOrEntry<K, V, NODE>`), or `null` or `undefined`.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
* of iteration to be performed when finding the rightmost node in a binary tree. It can have two
* possible values:
* @returns The function `getRightMost` returns a NODE object, `null`, or `undefined`.
*/
getRightMost(
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE | null | undefined {
): OptBTNOrNull<NODE> {
if (this.isNIL(beginRoot)) return beginRoot as NODE;
// TODO support get right most by passing key in
beginRoot = this.ensureNode(beginRoot);
@ -1305,7 +1309,7 @@ export class BinaryTree<
*/
getPredecessor(node: NODE): NODE {
if (this.isRealNode(node.left)) {
let predecessor: NODE | null | undefined = node.left;
let predecessor: OptBTNOrNull<NODE> = node.left;
while (!this.isRealNode(predecessor) || (this.isRealNode(predecessor.right) && predecessor.right !== node)) {
if (this.isRealNode(predecessor)) {
predecessor = predecessor.right;
@ -1331,7 +1335,7 @@ export class BinaryTree<
* @returns The function `getSuccessor` returns a `NODE` object if a successor exists, `null` if
* there is no successor, and `undefined` if the input `x` is not a valid node.
*/
getSuccessor(x?: K | NODE | null): NODE | null | undefined {
getSuccessor(x?: K | NODE | null): OptBTNOrNull<NODE> {
x = this.ensureNode(x);
if (!this.isRealNode(x)) return undefined;
@ -1339,7 +1343,7 @@ export class BinaryTree<
return this.getLeftMost(x.right);
}
let y: NODE | null | undefined = x.parent;
let y: OptBTNOrNull<NODE> = x.parent;
while (this.isRealNode(y) && x === y.right) {
x = y;
y = y.parent;
@ -1350,7 +1354,7 @@ export class BinaryTree<
dfs<C extends BTNCallback<NODE>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[];
@ -1358,7 +1362,7 @@ export class BinaryTree<
dfs<C extends BTNCallback<NODE | null>>(
callback?: C,
pattern?: DFSOrderPattern,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[];
@ -1379,7 +1383,7 @@ export class BinaryTree<
* return type of the callback function is determined by the generic type `C`.
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter determines the order in which the
* nodes are visited during the depth-first search. It can have one of the following values:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point of the depth-first search. It can be either a node object, a key-value pair, or a key. If it
* is a key or key-value pair, the method will find the corresponding node in the tree and start the
* search from there.
@ -1391,10 +1395,10 @@ export class BinaryTree<
* values will
* @returns an array of the return types of the callback function.
*/
dfs<C extends BTNCallback<NODE | null | undefined>>(
dfs<C extends BTNCallback<OptBTNOrNull<NODE>>>(
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = 'ITERATIVE',
includeNull = false
): ReturnType<C>[] {
@ -1402,7 +1406,7 @@ export class BinaryTree<
if (!beginRoot) return [];
const ans: ReturnType<C>[] = [];
if (iterationType === 'RECURSIVE') {
const dfs = (node: NODE | null | undefined) => {
const dfs = (node: OptBTNOrNull<NODE>) => {
switch (pattern) {
case 'IN':
if (includeNull) {
@ -1444,7 +1448,7 @@ export class BinaryTree<
dfs(beginRoot);
} else {
// 0: visit, 1: print
const stack: { opt: 0 | 1; node: NODE | null | undefined }[] = [{ opt: 0, node: beginRoot }];
const stack: { opt: 0 | 1; node: OptBTNOrNull<NODE> }[] = [{ opt: 0, node: beginRoot }];
while (stack.length > 0) {
const cur = stack.pop();
@ -1488,14 +1492,14 @@ export class BinaryTree<
bfs<C extends BTNCallback<NODE>>(
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[];
bfs<C extends BTNCallback<NODE | null>>(
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[];
@ -1514,7 +1518,7 @@ export class BinaryTree<
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
* the breadth-first search traversal. It takes a single argument, which is the current node being
* visited, and returns a value of any type.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point of the breadth-first search. It can be either a root node of a tree or a key, node,
* or entry object. If no value is provided, the `root` property of the class is used as the default
* starting point.
@ -1529,7 +1533,7 @@ export class BinaryTree<
*/
bfs<C extends BTNCallback<NODE | null>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType,
includeNull = false
): ReturnType<C>[] {
@ -1539,7 +1543,7 @@ export class BinaryTree<
const ans: ReturnType<BTNCallback<NODE>>[] = [];
if (iterationType === 'RECURSIVE') {
const queue: Queue<NODE | null | undefined> = new Queue<NODE | null | undefined>([beginRoot]);
const queue: Queue<OptBTNOrNull<NODE>> = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
const dfs = (level: number) => {
if (queue.size === 0) return;
@ -1560,7 +1564,7 @@ export class BinaryTree<
dfs(0);
} else {
const queue = new Queue<NODE | null | undefined>([beginRoot]);
const queue = new Queue<OptBTNOrNull<NODE>>([beginRoot]);
while (queue.size > 0) {
const levelSize = queue.size;
@ -1583,14 +1587,14 @@ export class BinaryTree<
listLevels<C extends BTNCallback<NODE>>(
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: false
): ReturnType<C>[][];
listLevels<C extends BTNCallback<NODE | null>>(
callback?: C,
beginRoot?: R | KeyOrNodeOrEntry<K, V, NODE>,
beginRoot?: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType?: IterationType,
includeNull?: true
): ReturnType<C>[][];
@ -1609,7 +1613,7 @@ export class BinaryTree<
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
* the tree. It takes a node as an argument and returns a value. The return type of the callback
* function is determined by the generic type `C` which extends `BTNCallback<NODE | null>`.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter represents the
* starting point for traversing the tree. It can be either a root node, a key-value pair, or a node
* entry. If no value is provided, the `root` property of the class is used as the default starting
* point.
@ -1623,7 +1627,7 @@ export class BinaryTree<
*/
listLevels<C extends BTNCallback<NODE | null>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType,
includeNull = false
): ReturnType<C>[][] {
@ -1686,7 +1690,7 @@ export class BinaryTree<
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function is used
* to specify the order in which the nodes of a binary tree are traversed. It can take one of the
* following values:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the traversal. It can be either a node object, a key, or an entry object. If no value is
* provided, the `root` of the tree is used as the starting point.
* @returns The function `morris` returns an array of values that are the return values of the
@ -1695,16 +1699,16 @@ export class BinaryTree<
morris<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root
): ReturnType<C>[] {
beginRoot = this.ensureNode(beginRoot);
if (beginRoot === null) return [];
const ans: ReturnType<BTNCallback<NODE>>[] = [];
let cur: NODE | null | undefined = beginRoot;
const _reverseEdge = (node: NODE | null | undefined) => {
let pre: NODE | null | undefined = null;
let next: NODE | null | undefined = null;
let cur: OptBTNOrNull<NODE> = beginRoot;
const _reverseEdge = (node: OptBTNOrNull<NODE>) => {
let pre: OptBTNOrNull<NODE> = null;
let next: OptBTNOrNull<NODE> = null;
while (node) {
next = node.right;
node.right = pre;
@ -1713,9 +1717,9 @@ export class BinaryTree<
}
return pre;
};
const _printEdge = (node: NODE | null | undefined) => {
const tail: NODE | null | undefined = _reverseEdge(node);
let cur: NODE | null | undefined = tail;
const _printEdge = (node: OptBTNOrNull<NODE>) => {
const tail: OptBTNOrNull<NODE> = _reverseEdge(node);
let cur: OptBTNOrNull<NODE> = tail;
while (cur) {
ans.push(callback(cur));
cur = cur.right;
@ -1884,7 +1888,7 @@ export class BinaryTree<
* Space Complexity: O(n)
*
* The `print` function in TypeScript prints the binary tree structure with customizable options.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for printing the binary tree. It can be either a node of the binary tree or a key or entry
* that exists in the binary tree. If no value is provided, the root of the binary tree will be used
* as the starting point.
@ -1893,7 +1897,7 @@ export class BinaryTree<
* @returns Nothing is being returned. The function has a return type of `void`, which means it does
* not return any value.
*/
override print(beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
override print(beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root, options?: BinaryTreePrintOptions): void {
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return;
@ -1908,7 +1912,7 @@ export class BinaryTree<
console.log(`S for Sentinel Node(NIL)
`);
const display = (root: NODE | null | undefined): void => {
const display = (root: OptBTNOrNull<NODE>): void => {
const [lines, , ,] = this._displayAux(root, opts);
for (const line of lines) {
console.log(line);
@ -1937,8 +1941,8 @@ export class BinaryTree<
if (!node) return;
if (this.iterationType === 'ITERATIVE') {
const stack: (NODE | null | undefined)[] = [];
let current: NODE | null | undefined = node;
const stack: OptBTNOrNull<NODE>[] = [];
let current: OptBTNOrNull<NODE> = node;
while (current || stack.length > 0) {
while (this.isRealNode(current)) {
@ -1975,7 +1979,7 @@ export class BinaryTree<
*
* The `_displayAux` function is responsible for generating the display layout of a binary tree node,
* taking into account various options such as whether to show null, undefined, or NaN nodes.
* @param {NODE | null | undefined} node - The `node` parameter represents a node in a binary tree.
* @param {OptBTNOrNull<NODE>} node - The `node` parameter represents a node in a binary tree.
* It can be of type `NODE`, `null`, or `undefined`.
* @param {BinaryTreePrintOptions} options - The `options` parameter is an object that contains the
* following properties:
@ -1986,7 +1990,7 @@ export class BinaryTree<
* 3. `totalHeight`: The total height of the node display.
* 4. `middleIndex`: The index of the middle character
*/
protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout {
protected _displayAux(node: OptBTNOrNull<NODE>, options: BinaryTreePrintOptions): NodeDisplayLayout {
const { isShowNull, isShowUndefined, isShowRedBlackNIL } = options;
const emptyDisplayLayout = <NodeDisplayLayout>[['─'], 1, 0, 0];
@ -2054,7 +2058,7 @@ export class BinaryTree<
}
}
protected _DEFAULT_CALLBACK = (node: NODE | null | undefined) => (node ? node.key : undefined);
protected _DEFAULT_CALLBACK = (node: OptBTNOrNull<NODE>) => (node ? node.key : undefined);
/**
* Time Complexity: O(1)
@ -2066,17 +2070,17 @@ export class BinaryTree<
* Space Complexity: O(1)
*
* The function `_swapProperties` swaps the key-value properties between two nodes.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} srcNode - The source node that will be swapped with the
* destination node. It can be either an instance of the class `R`, or an object of type
* `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} destNode - The `destNode` parameter is the node where
* the properties will be swapped with the `srcNode`.
* @returns either the `destNode` object with its properties swapped with the `srcNode` object's
* properties, or `undefined` if either `srcNode` or `destNode` is falsy.
*/
protected _swapProperties(
srcNode: R | KeyOrNodeOrEntry<K, V, NODE>,
destNode: R | KeyOrNodeOrEntry<K, V, NODE>
srcNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
destNode: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): NODE | undefined {
srcNode = this.ensureNode(srcNode);
destNode = this.ensureNode(destNode);
@ -2144,10 +2148,10 @@ export class BinaryTree<
*
* The function sets the root property of an object to the provided value, and also updates the
* parent property of the new root.
* @param {NODE | null | undefined} v - The parameter `v` is of type `NODE | null | undefined`. This
* @param {OptBTNOrNull<NODE>} v - The parameter `v` is of type `OptBTNOrNull<NODE>`. This
* means that it can accept a value of type `NODE`, `null`, or `undefined`.
*/
protected _setRoot(v: NODE | null | undefined) {
protected _setRoot(v: OptBTNOrNull<NODE>) {
if (v) {
v.parent = undefined;
}

View file

@ -11,12 +11,13 @@ import type {
BSTNodeNested,
BSTOptions,
BTNCallback,
BTNKeyOrNodeOrEntry,
BTNPureKeyOrNodeOrEntry,
Comparator,
CP,
DFSOrderPattern,
IterationType,
KeyOrNodeOrEntry
OptBSTN
} from '../../types';
import { BTNEntry } from '../../types';
import { BinaryTree, BinaryTreeNode } from './binary-tree';
@ -43,16 +44,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
* The function returns the value of the `_left` property.
* @returns The `_left` property of the current object is being returned.
*/
override get left(): NODE | undefined {
override get left(): OptBSTN<NODE> {
return this._left;
}
/**
* The function sets the left child of a node and updates the parent reference of the child.
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be an
* instance of the `NODE` class or `undefined`.
*/
override set left(v: NODE | undefined) {
override set left(v: OptBSTN<NODE>) {
if (v) {
v.parent = this as unknown as NODE;
}
@ -66,16 +67,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
* `undefined`.
*/
override get right(): NODE | undefined {
override get right(): OptBSTN<NODE> {
return this._right;
}
/**
* The function sets the right child of a node and updates the parent reference of the child.
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
* @param {OptBSTN<NODE>} v - The parameter `v` is of type `OptBSTN<NODE>`. It can either be a
* `NODE` object or `undefined`.
*/
override set right(v: NODE | undefined) {
override set right(v: OptBSTN<NODE>) {
if (v) {
v.parent = this as unknown as NODE;
}
@ -110,7 +111,7 @@ export class BST<
* It can include a comparator function that defines the order of the elements in the tree.
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: BSTOptions<K, V, R>
) {
super([], options);
@ -129,7 +130,7 @@ export class BST<
* The function returns the root node of a tree structure.
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
*/
override get root(): NODE | undefined {
override get root(): OptBSTN<NODE> {
return this._root;
}
@ -162,17 +163,17 @@ export class BST<
/**
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
* type R or KeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - A variable that can be of
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
* element.
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
* value associated with a key in a key-value pair.
* @returns either a NODE object or undefined.
*/
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
value?: V
): NODE | undefined {
): OptBSTN<NODE> {
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value) ?? undefined;
}
@ -182,7 +183,7 @@ export class BST<
*
* The function ensures the existence of a node in a data structure and returns it, or undefined if
* it doesn't exist.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R`, which represents the key, node,
* entry, or raw element that needs to be ensured in the tree.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
@ -192,21 +193,21 @@ export class BST<
* not be ensured.
*/
override ensureNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
iterationType: IterationType = 'ITERATIVE'
): NODE | undefined {
): OptBSTN<NODE> {
return super.ensureNode(keyOrNodeOrEntryOrRawElement, iterationType) ?? undefined;
}
/**
* The function checks if the input is an instance of the BSTNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `BSTNode` class.
*/
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
}
@ -216,13 +217,13 @@ export class BST<
* Space Complexity: O(1)
*
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
* key in the binary search tree. If provided, it will be stored in the node along with the key.
* @returns a boolean value.
*/
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
if (newNode === undefined) return false;
@ -284,7 +285,7 @@ export class BST<
* successfully inserted into the data structure.
*/
override addMany(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>,
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
values?: Iterable<V | undefined>,
isBalanceAdd = true,
iterationType: IterationType = this.iterationType
@ -308,7 +309,9 @@ export class BST<
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
const isRealBTNExemplar = (kve: R | KeyOrNodeOrEntry<K, V, NODE>): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
const isRealBTNExemplar = (
kve: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
if (kve === undefined || kve === null) return false;
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
};
@ -395,7 +398,7 @@ export class BST<
* @param [onlyOne=false] - A boolean value indicating whether to return only the first matching node
* or all matching nodes. If set to true, only the first matching node will be returned. If set to
* false, all matching nodes will be returned. The default value is false.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the search in the binary tree. It can be either a node object, a key-value pair, or an
* entry object. If it is not provided, the `root` of the binary tree is used as the starting point.
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
@ -406,7 +409,7 @@ export class BST<
identifier: ReturnType<C> | undefined,
callback: C = this._DEFAULT_CALLBACK as C,
onlyOne = false,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE[] {
beginRoot = this.ensureNode(beginRoot);
@ -496,7 +499,7 @@ export class BST<
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
iterationType: IterationType = this.iterationType
): NODE | undefined {
): OptBSTN<NODE> {
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
}
@ -518,7 +521,7 @@ export class BST<
* It has a default value of `'ITERATIVE'`.
* @returns The method is returning a NODE object or undefined.
*/
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): OptBSTN<NODE> {
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
}
@ -539,7 +542,7 @@ export class BST<
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
* take one of the following values:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
* node entry. If not specified, the default value is the root of the tree.
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
@ -550,7 +553,7 @@ export class BST<
override dfs<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
pattern: DFSOrderPattern = 'IN',
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = 'ITERATIVE'
): ReturnType<C>[] {
return super.dfs(callback, pattern, beginRoot, iterationType, false);
@ -570,7 +573,7 @@ export class BST<
* @param {C} callback - The `callback` parameter is a function that will be called for each node
* visited during the breadth-first search. It should take a single argument, which is the current
* node being visited, and it can return a value of any type.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
* object. If no value is provided, the default value is the root of the tree.
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
@ -580,7 +583,7 @@ export class BST<
*/
override bfs<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[] {
return super.bfs(callback, beginRoot, iterationType, false);
@ -600,7 +603,7 @@ export class BST<
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
* tree during the iteration process.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} beginRoot - The `beginRoot` parameter is the starting
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
* value is provided, the root of
@ -611,7 +614,7 @@ export class BST<
*/
override listLevels<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
beginRoot: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
beginRoot: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[][] {
return super.listLevels(callback, beginRoot, iterationType, false);
@ -634,7 +637,7 @@ export class BST<
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
* 0, or 1, where:
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} targetNode - The `targetNode` parameter is the node in
* the binary tree that you want to start traversing from. It can be specified either by providing
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
* `targetNode` is provided,
@ -646,7 +649,7 @@ export class BST<
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
callback: C = this._DEFAULT_CALLBACK as C,
lesserOrGreater: CP = -1,
targetNode: R | KeyOrNodeOrEntry<K, V, NODE> = this.root,
targetNode: R | BTNKeyOrNodeOrEntry<K, V, NODE> = this.root,
iterationType: IterationType = this.iterationType
): ReturnType<C>[] {
const targetNodeEnsured = this.ensureNode(targetNode);
@ -760,7 +763,7 @@ export class BST<
let balanced = true;
if (iterationType === 'RECURSIVE') {
const _height = (cur: NODE | undefined): number => {
const _height = (cur: OptBSTN<NODE>): number => {
if (!cur) return 0;
const leftHeight = _height(cur.left),
rightHeight = _height(cur.right);
@ -770,8 +773,8 @@ export class BST<
_height(this.root);
} else {
const stack: NODE[] = [];
let node: NODE | undefined = this.root,
last: NODE | undefined = undefined;
let node: OptBSTN<NODE> = this.root,
last: OptBSTN<NODE> = undefined;
const depths: Map<NODE, number> = new Map();
while (stack.length > 0 || node) {
@ -827,9 +830,9 @@ export class BST<
/**
* The function sets the root of a tree-like structure and updates the parent property of the new
* root.
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
* @param {OptBSTN<NODE>} v - v is a parameter of type NODE or undefined.
*/
protected override _setRoot(v: NODE | undefined) {
protected override _setRoot(v: OptBSTN<NODE>) {
if (v) {
v.parent = undefined;
}

View file

@ -1,8 +1,8 @@
import type {
BinaryTreeDeleteResult,
BTNCallback,
BTNKeyOrNodeOrEntry,
CRUD,
KeyOrNodeOrEntry,
RBTNColor,
RBTreeOptions,
RedBlackTreeNested,
@ -72,7 +72,7 @@ export class RedBlackTree<
* depend on the implementation
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: RBTreeOptions<K, V, R>
) {
super([], options);
@ -135,13 +135,13 @@ export class RedBlackTree<
* Space Complexity: O(1)
*
* The function checks if the input is an instance of the RedBlackTreeNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `RedBlackTreeNode` class.
*/
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
}
@ -157,11 +157,11 @@ export class RedBlackTree<
// *
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
// * valid, otherwise it returns undefined.
// * @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
// */
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
//
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
@ -210,8 +210,8 @@ export class RedBlackTree<
*
* The function adds a new node to a binary search tree and returns true if the node was successfully
* added.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
* the key in the data structure. It represents the value that you want to add or update in the data
* structure.
@ -219,7 +219,7 @@ export class RedBlackTree<
* the method returns true. If the node already exists and its value is updated, the method also
* returns true. If the node cannot be added or updated, the method returns false.
*/
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
if (!this.isRealNode(newNode)) return false;

View file

@ -9,8 +9,8 @@ import type {
BinaryTreeDeleteResult,
BSTNKeyOrNode,
BTNCallback,
BTNKeyOrNodeOrEntry,
IterationType,
KeyOrNodeOrEntry,
RBTNColor,
TreeMultiMapNested,
TreeMultiMapNodeNested,
@ -81,7 +81,7 @@ export class TreeMultiMap<
* `compareValues`, which are functions used to compare keys and values respectively.
*/
constructor(
keysOrNodesOrEntriesOrRawElements: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [],
keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
options?: TreeMultiMapOptions<K, V, R>
) {
super([], options);
@ -153,8 +153,8 @@ export class TreeMultiMap<
/**
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
* node based on the input.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @param {V} [value] - The `value` parameter is an optional value that represents the value
* associated with the key in the node. It is used when creating a new node or updating the value of
* an existing node.
@ -163,7 +163,7 @@ export class TreeMultiMap<
* @returns either a NODE object or undefined.
*/
override keyValueOrEntryOrRawElementToNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
value?: V,
count = 1
): NODE | undefined {
@ -190,13 +190,13 @@ export class TreeMultiMap<
/**
* The function checks if the input is an instance of the TreeMultiMapNode class.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
* an instance of the `TreeMultiMapNode` class.
*/
override isNode(
keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
): keyOrNodeOrEntryOrRawElement is NODE {
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
}
@ -212,7 +212,7 @@ export class TreeMultiMap<
*
* The function overrides the add method of a class and adds a new node to a data structure, updating
* the count and returning a boolean indicating success.
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
* data structure. It is an optional parameter, so it can be omitted if not needed.
@ -222,7 +222,7 @@ export class TreeMultiMap<
* @returns The method is returning a boolean value. It returns true if the addition of the new node
* was successful, and false otherwise.
*/
override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
const orgCount = newNode?.count || 0;
const isSuccessAdded = super.add(newNode);

View file

@ -5,7 +5,7 @@ import type {
BinaryTreeNodeNested,
BinaryTreeOptions,
BTNCallback,
KeyOrNodeOrEntry
BTNKeyOrNodeOrEntry
} from '../types';
export interface IBinaryTree<
@ -19,9 +19,9 @@ export interface IBinaryTree<
createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
addMany(nodes: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<NODE>[];
}

View file

@ -1,11 +1,5 @@
export type CP = 1 | -1 | 0;
/**
* 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 type IterationType = 'ITERATIVE' | 'RECURSIVE';
export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
@ -16,8 +10,6 @@ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
export type NodeDisplayLayout = [string[], number, number, number];
export type BTNCallback<N, D = any> = (node: N) => D;
export interface IterableWithSize<T> extends Iterable<T> {
size: number | ((...args: any[]) => number);
}
@ -26,22 +18,8 @@ export interface IterableWithLength<T> extends Iterable<T> {
length: number | ((...args: any[]) => number);
}
export type OptValue<V> = V | undefined;
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
export type BTNodePureKeyOrNode<K, N> = K | N;
export type BTNPureKeyOrNodeOrEntry<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
export type BSTNKeyOrNode<K, N> = K | undefined | N;
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';

View file

@ -1,5 +1,5 @@
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
import { BTNEntry, IterationType } from '../../common';
import { IterationType, OptValue } from '../../common';
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@ -9,3 +9,23 @@ export type BinaryTreeOptions<K, V, R> = {
iterationType?: IterationType;
toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
}
export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
export type OptBTNOrNull<NODE> = NODE | null | undefined;
export type OptBTNKeyOrNull<K> = K | null | undefined;
export type BTNEntry<K, V> = [OptBTNKeyOrNull<K>, OptValue<V>];
export type BTNKeyOrNode<K, NODE> = OptBTNKeyOrNull<K> | NODE;
export type BTNKeyOrNodeOrEntry<K, V, NODE> = BTNEntry<K, V> | BTNKeyOrNode<K, NODE>;
export type BTNPureKeyOrNode<K, NODE> = K | NODE;
export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyOrNode<K, NODE>;
export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
export type BTNCallback<NODE, D = any> = (node: NODE) => D;

View file

@ -9,3 +9,10 @@ export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
comparator?: Comparator<K>
}
export type OptBSTNKey<K> = K | undefined;
export type OptBSTN<NODE> = NODE | undefined;
export type BSTNKeyOrNode<K, NODE> = OptBSTNKey<K> | NODE;