undirected-graph-typed
Advanced tools
Comparing version 1.38.5 to 1.38.6
@@ -11,2 +11,3 @@ /** | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { MapCallback } from '../../types'; | ||
export declare class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<V, FAMILY> { | ||
@@ -47,7 +48,12 @@ height: number; | ||
* node if necessary. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects. | ||
*/ | ||
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[]; | ||
/** | ||
@@ -54,0 +60,0 @@ * The function swaps the key, value, and height properties between two nodes in a binary tree. |
@@ -60,8 +60,13 @@ "use strict"; | ||
* node if necessary. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects. | ||
*/ | ||
delete(nodeOrKey) { | ||
const deletedResults = super.delete(nodeOrKey); | ||
delete(identifier, callback = this._defaultCallbackByKey) { | ||
const deletedResults = super.delete(identifier, callback); | ||
for (const { needBalanced } of deletedResults) { | ||
@@ -68,0 +73,0 @@ if (needBalanced) { |
@@ -9,3 +9,3 @@ /** | ||
import type { BFSCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback } from '../../types'; | ||
import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types'; | ||
import { BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -136,18 +136,11 @@ /** | ||
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[]; | ||
/** | ||
* The `delete` function removes a node from a binary search tree and returns the deleted node along | ||
* with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or | ||
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the | ||
* binary tree. | ||
* @returns an array of `BinaryTreeDeletedResult<N>` objects. | ||
*/ | ||
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[]; | ||
/** | ||
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a | ||
* specified root node. | ||
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node | ||
* @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node | ||
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value | ||
* of the node (`BinaryTreeNodeKey`), or `null`. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which we want to calculate the depth. It can be either a node object or the key | ||
@@ -158,7 +151,7 @@ * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root | ||
*/ | ||
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot?: N | BinaryTreeNodeKey | null): number; | ||
getDepth(distNode: BinaryTreeNodeKey | N | null, beginRoot?: BinaryTreeNodeKey | N | null): number; | ||
/** | ||
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or | ||
* iterative approach. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which the height of the binary tree is calculated. It can be either a node | ||
@@ -172,3 +165,3 @@ * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting | ||
*/ | ||
getHeight(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): number; | ||
getHeight(beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): number; | ||
/** | ||
@@ -193,59 +186,18 @@ * The `getMinHeight` function calculates the minimum height of a binary tree using either a | ||
isPerfectlyBalanced(beginRoot?: N | null): boolean; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null, iterationType: IterationType): N[]; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null, iterationType: IterationType): N | null; | ||
/** | ||
* The function `getNodes` returns an array of nodes that match a given node property, using either | ||
* recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey`, which | ||
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the | ||
* first node that matches the nodeProperty. If set to true, the function will return an array with | ||
* only one element (or an empty array if no matching node is found). If set to false (default), the | ||
* function will continue searching for all | ||
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the | ||
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary | ||
* tree. | ||
* @param iterationType - The `iterationType` parameter determines the type of iteration used to | ||
* traverse the binary tree. It can have two possible values: | ||
* @returns The function `getNodes` returns an array of nodes (`N[]`). | ||
*/ | ||
getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[]; | ||
/** | ||
* The function checks if a binary tree has a node with a given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a | ||
* generic type `N`. | ||
* @param callback - The `callback` parameter is a function that is used to determine whether a node | ||
* matches the desired criteria. It takes a node as input and returns a boolean value indicating | ||
* whether the node matches the criteria or not. The default callback function | ||
* `this._defaultCallbackByKey` is used if no callback function is | ||
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies | ||
* the node from which the search should begin. By default, it is set to `this.root`, which means the | ||
* search will start from the root node of the binary tree. However, you can provide a different node | ||
* as | ||
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be | ||
* performed when searching for nodes in the binary tree. It can have one of the following values: | ||
* @returns a boolean value. | ||
*/ | ||
has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean; | ||
/** | ||
* The function `get` returns the first node in a binary tree that matches the given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N` | ||
* type. | ||
* @param callback - The `callback` parameter is a function that is used to determine whether a node | ||
* matches the desired criteria. It takes a node as input and returns a boolean value indicating | ||
* whether the node matches the criteria or not. The default callback function | ||
* (`this._defaultCallbackByKey`) is used if no callback function is | ||
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies | ||
* the root node from which the search should begin. | ||
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be | ||
* performed when searching for a node in the binary tree. It can have one of the following values: | ||
* @returns either the found node (of type N) or null if no node is found. | ||
*/ | ||
get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null; | ||
/** | ||
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing | ||
@@ -264,3 +216,3 @@ * up to the root node, with the option to reverse the order of the nodes. | ||
* iterative traversal. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value | ||
@@ -273,3 +225,3 @@ * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty. | ||
*/ | ||
getLeftMost(beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): N | null; | ||
getLeftMost(beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): N | null; | ||
/** | ||
@@ -313,3 +265,3 @@ * The function `getRightMost` returns the rightmost node in a binary tree, either recursively or | ||
* an array. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to | ||
@@ -321,3 +273,3 @@ * start from the root of the tree. | ||
*/ | ||
subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[]; | ||
subTreeTraverse<C extends MapCallback<N>>(callback?: C, beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -338,3 +290,3 @@ * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback | ||
*/ | ||
dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -347,3 +299,3 @@ * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback | ||
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines | ||
* whether or not to include the level of each node in the callback function. If `withLevel` is set | ||
* whether to include the level of each node in the callback function. If `withLevel` is set | ||
* to `true`, the level of each node will be passed as an argument to the callback function. If | ||
@@ -379,3 +331,3 @@ * `withLevel` is | ||
*/ | ||
morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[]; | ||
morris<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[]; | ||
/** | ||
@@ -395,3 +347,3 @@ * Swap the data of two nodes in the binary tree. | ||
*/ | ||
protected _defaultCallbackByKey: (node: N) => number; | ||
protected _defaultCallbackByKey: DefaultMapCallback<N>; | ||
/** | ||
@@ -398,0 +350,0 @@ * The function `_addTo` adds a new node to a binary tree if there is an available position. |
@@ -67,31 +67,12 @@ "use strict"; | ||
const that = this; | ||
if (that.parent) { | ||
if (that.parent.left === that) { | ||
if (that.left || that.right) { | ||
return types_1.FamilyPosition.ROOT_LEFT; | ||
} | ||
else { | ||
return types_1.FamilyPosition.LEFT; | ||
} | ||
} | ||
else if (that.parent.right === that) { | ||
if (that.left || that.right) { | ||
return types_1.FamilyPosition.ROOT_RIGHT; | ||
} | ||
else { | ||
return types_1.FamilyPosition.RIGHT; | ||
} | ||
} | ||
else { | ||
return types_1.FamilyPosition.MAL_NODE; | ||
} | ||
if (!this.parent) { | ||
return this.left || this.right ? types_1.FamilyPosition.ROOT : types_1.FamilyPosition.ISOLATED; | ||
} | ||
else { | ||
if (that.left || that.right) { | ||
return types_1.FamilyPosition.ROOT; | ||
} | ||
else { | ||
return types_1.FamilyPosition.ISOLATED; | ||
} | ||
if (this.parent.left === that) { | ||
return this.left || this.right ? types_1.FamilyPosition.ROOT_LEFT : types_1.FamilyPosition.LEFT; | ||
} | ||
else if (this.parent.right === that) { | ||
return this.left || this.right ? types_1.FamilyPosition.ROOT_RIGHT : types_1.FamilyPosition.RIGHT; | ||
} | ||
return types_1.FamilyPosition.MAL_NODE; | ||
} | ||
@@ -214,3 +195,4 @@ } | ||
} | ||
const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined; | ||
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined; | ||
const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined; | ||
if (this.root) { | ||
@@ -249,17 +231,12 @@ if (existNode) { | ||
// TODO not sure addMany not be run multi times | ||
const inserted = []; | ||
for (let i = 0; i < keysOrNodes.length; i++) { | ||
const keyOrNode = keysOrNodes[i]; | ||
return keysOrNodes.map((keyOrNode, i) => { | ||
if (keyOrNode instanceof BinaryTreeNode) { | ||
inserted.push(this.add(keyOrNode.key, keyOrNode.val)); | ||
continue; | ||
return this.add(keyOrNode.key, keyOrNode.val); | ||
} | ||
if (keyOrNode === null) { | ||
inserted.push(this.add(null)); | ||
continue; | ||
return this.add(null); | ||
} | ||
const val = values === null || values === void 0 ? void 0 : values[i]; | ||
inserted.push(this.add(keyOrNode, val)); | ||
} | ||
return inserted; | ||
return this.add(keyOrNode, val); | ||
}); | ||
} | ||
@@ -282,12 +259,20 @@ /** | ||
* with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or | ||
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the | ||
* binary tree. | ||
* @returns an array of `BinaryTreeDeletedResult<N>` objects. | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey`, which | ||
*/ | ||
delete(nodeOrKey) { | ||
delete(identifier, callback = this._defaultCallbackByKey) { | ||
const bstDeletedResult = []; | ||
if (!this.root) | ||
return bstDeletedResult; | ||
const curr = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey; | ||
if (identifier instanceof BinaryTreeNode) | ||
callback = (node => node); | ||
const curr = this.get(identifier, callback); | ||
if (!curr) | ||
@@ -334,6 +319,6 @@ return bstDeletedResult; | ||
* specified root node. | ||
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node | ||
* @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node | ||
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value | ||
* of the node (`BinaryTreeNodeKey`), or `null`. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which we want to calculate the depth. It can be either a node object or the key | ||
@@ -362,3 +347,3 @@ * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root | ||
* iterative approach. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which the height of the binary tree is calculated. It can be either a node | ||
@@ -473,11 +458,11 @@ * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting | ||
* recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey`, which | ||
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the | ||
* first node that matches the nodeProperty. If set to true, the function will return an array with | ||
* first node that matches the identifier. If set to true, the function will return an array with | ||
* only one element (or an empty array if no matching node is found). If set to false (default), the | ||
@@ -492,9 +477,11 @@ * function will continue searching for all | ||
*/ | ||
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
if (!beginRoot) | ||
return []; | ||
if (identifier instanceof BinaryTreeNode) | ||
callback = (node => node); | ||
const ans = []; | ||
if (iterationType === types_1.IterationType.RECURSIVE) { | ||
const _traverse = (cur) => { | ||
if (callback(cur) === nodeProperty) { | ||
if (callback(cur) === identifier) { | ||
ans.push(cur); | ||
@@ -516,3 +503,3 @@ if (onlyOne) | ||
if (cur) { | ||
if (callback(cur) === nodeProperty) { | ||
if (callback(cur) === identifier) { | ||
ans.push(cur); | ||
@@ -531,3 +518,3 @@ if (onlyOne) | ||
* The function checks if a binary tree has a node with a given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a | ||
@@ -547,9 +534,11 @@ * generic type `N`. | ||
*/ | ||
has(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
has(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
if (identifier instanceof BinaryTreeNode) | ||
callback = (node => node); | ||
// TODO may support finding node by value equal | ||
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0; | ||
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0; | ||
} | ||
/** | ||
* The function `get` returns the first node in a binary tree that matches the given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N` | ||
@@ -567,6 +556,8 @@ * type. | ||
*/ | ||
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
var _a; | ||
if (identifier instanceof BinaryTreeNode) | ||
callback = (node => node); | ||
// TODO may support finding node by value equal | ||
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null; | ||
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null; | ||
} | ||
@@ -598,3 +589,3 @@ /** | ||
* iterative traversal. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value | ||
@@ -723,3 +714,3 @@ * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty. | ||
* an array. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to | ||
@@ -848,3 +839,3 @@ * start from the root of the tree. | ||
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines | ||
* whether or not to include the level of each node in the callback function. If `withLevel` is set | ||
* whether to include the level of each node in the callback function. If `withLevel` is set | ||
* to `true`, the level of each node will be passed as an argument to the callback function. If | ||
@@ -851,0 +842,0 @@ * `withLevel` is |
@@ -46,3 +46,3 @@ /** | ||
* maintaining balance. | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function | ||
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an | ||
@@ -61,3 +61,3 @@ * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or | ||
* callback. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the | ||
* property of the binary tree node that you want to search for. It can be either a specific key | ||
@@ -77,3 +77,3 @@ * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines | ||
*/ | ||
get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null; | ||
/** | ||
@@ -98,3 +98,3 @@ * The function `lastKey` returns the key of the rightmost node if the comparison result is less | ||
* using either recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property | ||
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a | ||
@@ -117,3 +117,3 @@ * generic type `N`. | ||
*/ | ||
getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(nodeProperty: BinaryTreeNodeKey | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[]; | ||
/** | ||
@@ -128,3 +128,3 @@ * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to | ||
* of the following values: | ||
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the | ||
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the | ||
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should | ||
@@ -137,3 +137,3 @@ * start. It can be either a reference to a specific node (`N`), the key of a node | ||
*/ | ||
lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(callback?: C, lesserOrGreater?: CP, targetNode?: N | BinaryTreeNodeKey | null, iterationType?: IterationType): ReturnType<C>[]; | ||
lesserOrGreaterTraverse<C extends MapCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -140,0 +140,0 @@ * Balancing Adjustment: |
@@ -129,3 +129,3 @@ "use strict"; | ||
* maintaining balance. | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function | ||
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an | ||
@@ -212,3 +212,3 @@ * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or | ||
* callback. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the | ||
* property of the binary tree node that you want to search for. It can be either a specific key | ||
@@ -228,5 +228,5 @@ * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines | ||
*/ | ||
get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) { | ||
var _a; | ||
return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null; | ||
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null; | ||
} | ||
@@ -260,3 +260,3 @@ /** | ||
* using either recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property | ||
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a | ||
@@ -279,3 +279,3 @@ * generic type `N`. | ||
*/ | ||
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) { | ||
if (!beginRoot) | ||
@@ -287,3 +287,3 @@ return []; | ||
const callbackResult = callback(cur); | ||
if (callbackResult === nodeProperty) { | ||
if (callbackResult === identifier) { | ||
ans.push(cur); | ||
@@ -297,5 +297,5 @@ if (onlyOne) | ||
if (callback === this._defaultCallbackByKey) { | ||
if (this._compare(cur.key, nodeProperty) === types_1.CP.gt) | ||
if (this._compare(cur.key, identifier) === types_1.CP.gt) | ||
cur.left && _traverse(cur.left); | ||
if (this._compare(cur.key, nodeProperty) === types_1.CP.lt) | ||
if (this._compare(cur.key, identifier) === types_1.CP.lt) | ||
cur.right && _traverse(cur.right); | ||
@@ -316,3 +316,3 @@ } | ||
const callbackResult = callback(cur); | ||
if (callbackResult === nodeProperty) { | ||
if (callbackResult === identifier) { | ||
ans.push(cur); | ||
@@ -324,5 +324,5 @@ if (onlyOne) | ||
if (callback === this._defaultCallbackByKey) { | ||
if (this._compare(cur.key, nodeProperty) === types_1.CP.gt) | ||
if (this._compare(cur.key, identifier) === types_1.CP.gt) | ||
cur.left && queue.push(cur.left); | ||
if (this._compare(cur.key, nodeProperty) === types_1.CP.lt) | ||
if (this._compare(cur.key, identifier) === types_1.CP.lt) | ||
cur.right && queue.push(cur.right); | ||
@@ -349,3 +349,3 @@ } | ||
* of the following values: | ||
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the | ||
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the | ||
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should | ||
@@ -352,0 +352,0 @@ * start. It can be either a reference to a specific node (`N`), the key of a node |
@@ -9,3 +9,3 @@ /** | ||
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types'; | ||
import { BinaryTreeDeletedResult, IterationType } from '../../types'; | ||
import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -96,5 +96,9 @@ import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
* node along with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted | ||
* from the binary tree. | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node | ||
@@ -106,3 +110,3 @@ * being deleted. If set to true, the count of the node will not be considered and the node will be | ||
*/ | ||
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[]; | ||
/** | ||
@@ -109,0 +113,0 @@ * The clear() function clears the contents of a data structure and sets the count to zero. |
@@ -248,5 +248,9 @@ "use strict"; | ||
* node along with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted | ||
* from the binary tree. | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node | ||
@@ -258,7 +262,7 @@ * being deleted. If set to true, the count of the node will not be considered and the node will be | ||
*/ | ||
delete(nodeOrKey, ignoreCount = false) { | ||
delete(identifier, callback = this._defaultCallbackByKey, ignoreCount = false) { | ||
const bstDeletedResult = []; | ||
if (!this.root) | ||
return bstDeletedResult; | ||
const curr = this.get(nodeOrKey); | ||
const curr = this.get(identifier, callback); | ||
if (!curr) | ||
@@ -265,0 +269,0 @@ return bstDeletedResult; |
import { BinaryTreeNode } from '../data-structures'; | ||
import { BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../types'; | ||
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } from '../types'; | ||
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> { | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; | ||
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[]; | ||
} |
@@ -0,4 +1,6 @@ | ||
import { BinaryTreeNodeKey } from './data-structures'; | ||
export type Comparator<T> = (a: T, b: T) => number; | ||
export type DFSOrderPattern = 'pre' | 'in' | 'post'; | ||
export type MapCallback<N, D = any> = (node: N) => D; | ||
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D; | ||
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>; | ||
@@ -5,0 +7,0 @@ export declare enum CP { |
{ | ||
"name": "undirected-graph-typed", | ||
"version": "1.38.5", | ||
"version": "1.38.6", | ||
"description": "Undirected Graph. Javascript & Typescript Data Structure.", | ||
@@ -147,4 +147,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.38.5" | ||
"data-structure-typed": "^1.38.6" | ||
} | ||
} |
@@ -11,2 +11,3 @@ /** | ||
import {IBinaryTree} from '../../interfaces'; | ||
import {MapCallback} from '../../types'; | ||
@@ -68,8 +69,16 @@ export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode< | ||
* node if necessary. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects. | ||
*/ | ||
override delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] { | ||
const deletedResults = super.delete(nodeOrKey); | ||
override delete<C extends MapCallback<N>>( | ||
identifier: ReturnType<C>, | ||
callback: C = this._defaultCallbackByKey as C | ||
): BinaryTreeDeletedResult<N>[] { | ||
const deletedResults = super.delete(identifier, callback); | ||
for (const {needBalanced} of deletedResults) { | ||
@@ -76,0 +85,0 @@ if (needBalanced) { |
@@ -18,3 +18,3 @@ /** | ||
} from '../../types'; | ||
import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types'; | ||
import {BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType} from '../../types'; | ||
import {IBinaryTree} from '../../interfaces'; | ||
@@ -101,25 +101,13 @@ import {trampoline} from '../../utils'; | ||
const that = this as unknown as FAMILY; | ||
if (that.parent) { | ||
if (that.parent.left === that) { | ||
if (that.left || that.right) { | ||
return FamilyPosition.ROOT_LEFT; | ||
} else { | ||
return FamilyPosition.LEFT; | ||
} | ||
} else if (that.parent.right === that) { | ||
if (that.left || that.right) { | ||
return FamilyPosition.ROOT_RIGHT; | ||
} else { | ||
return FamilyPosition.RIGHT; | ||
} | ||
} else { | ||
return FamilyPosition.MAL_NODE; | ||
} | ||
} else { | ||
if (that.left || that.right) { | ||
return FamilyPosition.ROOT; | ||
} else { | ||
return FamilyPosition.ISOLATED; | ||
} | ||
if (!this.parent) { | ||
return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED; | ||
} | ||
if (this.parent.left === that) { | ||
return this.left || this.right ? FamilyPosition.ROOT_LEFT : FamilyPosition.LEFT; | ||
} else if (this.parent.right === that) { | ||
return this.left || this.right ? FamilyPosition.ROOT_RIGHT : FamilyPosition.RIGHT; | ||
} | ||
return FamilyPosition.MAL_NODE; | ||
} | ||
@@ -239,3 +227,4 @@ } | ||
const existNode = keyOrNode ? this.get(keyOrNode, this._defaultCallbackByKey) : undefined; | ||
const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined; | ||
const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined; | ||
@@ -273,20 +262,14 @@ if (this.root) { | ||
// TODO not sure addMany not be run multi times | ||
const inserted: (N | null | undefined)[] = []; | ||
for (let i = 0; i < keysOrNodes.length; i++) { | ||
const keyOrNode = keysOrNodes[i]; | ||
return keysOrNodes.map((keyOrNode, i) => { | ||
if (keyOrNode instanceof BinaryTreeNode) { | ||
inserted.push(this.add(keyOrNode.key, keyOrNode.val)); | ||
continue; | ||
return this.add(keyOrNode.key, keyOrNode.val); | ||
} | ||
if (keyOrNode === null) { | ||
inserted.push(this.add(null)); | ||
continue; | ||
return this.add(null); | ||
} | ||
const val = values?.[i]; | ||
inserted.push(this.add(keyOrNode, val)); | ||
} | ||
return inserted; | ||
return this.add(keyOrNode, val); | ||
}); | ||
} | ||
@@ -308,15 +291,29 @@ | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[]; | ||
/** | ||
* The `delete` function removes a node from a binary search tree and returns the deleted node along | ||
* with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or | ||
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the | ||
* binary tree. | ||
* @returns an array of `BinaryTreeDeletedResult<N>` objects. | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey`, which | ||
*/ | ||
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] { | ||
delete<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C | ||
): BinaryTreeDeletedResult<N>[] { | ||
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = []; | ||
if (!this.root) return bstDeletedResult; | ||
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C; | ||
const curr: N | null = typeof nodeOrKey === 'number' ? this.get(nodeOrKey) : nodeOrKey; | ||
const curr = this.get(identifier, callback); | ||
if (!curr) return bstDeletedResult; | ||
@@ -362,6 +359,6 @@ | ||
* specified root node. | ||
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node | ||
* @param {BinaryTreeNodeKey | N | null} distNode - The `distNode` parameter represents the node | ||
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value | ||
* of the node (`BinaryTreeNodeKey`), or `null`. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which we want to calculate the depth. It can be either a node object or the key | ||
@@ -372,3 +369,3 @@ * of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root | ||
*/ | ||
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number { | ||
getDepth(distNode: BinaryTreeNodeKey | N | null, beginRoot: BinaryTreeNodeKey | N | null = this.root): number { | ||
if (typeof distNode === 'number') distNode = this.get(distNode); | ||
@@ -390,3 +387,3 @@ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); | ||
* iterative approach. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter represents the | ||
* starting node from which the height of the binary tree is calculated. It can be either a node | ||
@@ -400,3 +397,3 @@ * object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting | ||
*/ | ||
getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): number { | ||
getHeight(beginRoot: BinaryTreeNodeKey | N | null = this.root, iterationType = this.iterationType): number { | ||
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); | ||
@@ -503,14 +500,37 @@ if (!beginRoot) return -1; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[]; | ||
getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[]; | ||
getNodes<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C, | ||
onlyOne: boolean, | ||
beginRoot: N | null | ||
): N[]; | ||
getNodes<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C, | ||
onlyOne: boolean, | ||
beginRoot: N | null, | ||
iterationType: IterationType | ||
): N[]; | ||
/** | ||
* The function `getNodes` returns an array of nodes that match a given node property, using either | ||
* recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey`, which | ||
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the | ||
* first node that matches the nodeProperty. If set to true, the function will return an array with | ||
* first node that matches the identifier. If set to true, the function will return an array with | ||
* only one element (or an empty array if no matching node is found). If set to false (default), the | ||
@@ -525,4 +545,4 @@ * function will continue searching for all | ||
*/ | ||
getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
nodeProperty: BinaryTreeNodeKey | N, | ||
getNodes<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -534,3 +554,3 @@ onlyOne = false, | ||
if (!beginRoot) return []; | ||
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C; | ||
const ans: N[] = []; | ||
@@ -540,3 +560,3 @@ | ||
const _traverse = (cur: N) => { | ||
if (callback(cur) === nodeProperty) { | ||
if (callback(cur) === identifier) { | ||
ans.push(cur); | ||
@@ -556,3 +576,3 @@ if (onlyOne) return; | ||
if (cur) { | ||
if (callback(cur) === nodeProperty) { | ||
if (callback(cur) === identifier) { | ||
ans.push(cur); | ||
@@ -570,5 +590,13 @@ if (onlyOne) return ans; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean; | ||
has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean; | ||
/** | ||
* The function checks if a binary tree has a node with a given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a | ||
@@ -588,4 +616,4 @@ * generic type `N`. | ||
*/ | ||
has<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
nodeProperty: BinaryTreeNodeKey | N, | ||
has<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -595,9 +623,25 @@ beginRoot = this.root, | ||
): boolean { | ||
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C; | ||
// TODO may support finding node by value equal | ||
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType).length > 0; | ||
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0; | ||
} | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null; | ||
get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null; | ||
get<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C, | ||
beginRoot: N | null, | ||
iterationType: IterationType | ||
): N | null; | ||
/** | ||
* The function `get` returns the first node in a binary tree that matches the given property or key. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of | ||
* @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of | ||
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N` | ||
@@ -615,4 +659,4 @@ * type. | ||
*/ | ||
get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
nodeProperty: BinaryTreeNodeKey | N, | ||
get<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -622,4 +666,5 @@ beginRoot = this.root, | ||
): N | null { | ||
if (identifier instanceof BinaryTreeNode) callback = (node => node) as C; | ||
// TODO may support finding node by value equal | ||
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0] ?? null; | ||
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null; | ||
} | ||
@@ -653,3 +698,3 @@ | ||
* iterative traversal. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value | ||
@@ -662,3 +707,3 @@ * of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty. | ||
*/ | ||
getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): N | null { | ||
getLeftMost(beginRoot: BinaryTreeNodeKey | N | null = this.root, iterationType = this.iterationType): N | null { | ||
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot); | ||
@@ -778,3 +823,3 @@ | ||
* an array. | ||
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* @param {BinaryTreeNodeKey | N | null} beginRoot - The `beginRoot` parameter is the starting point | ||
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to | ||
@@ -786,5 +831,5 @@ * start from the root of the tree. | ||
*/ | ||
subTreeTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
subTreeTraverse<C extends MapCallback<N>>( | ||
callback: C = this._defaultCallbackByKey as C, | ||
beginRoot: N | BinaryTreeNodeKey | null = this.root, | ||
beginRoot: BinaryTreeNodeKey | N | null = this.root, | ||
iterationType = this.iterationType | ||
@@ -834,3 +879,3 @@ ): ReturnType<C>[] { | ||
*/ | ||
dfs<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
dfs<C extends MapCallback<N>>( | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -913,3 +958,3 @@ pattern: DFSOrderPattern = 'in', | ||
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines | ||
* whether or not to include the level of each node in the callback function. If `withLevel` is set | ||
* whether to include the level of each node in the callback function. If `withLevel` is set | ||
* to `true`, the level of each node will be passed as an argument to the callback function. If | ||
@@ -992,3 +1037,3 @@ * `withLevel` is | ||
*/ | ||
morris<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
morris<C extends MapCallback<N>>( | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -1106,5 +1151,4 @@ pattern: DFSOrderPattern = 'in', | ||
*/ | ||
protected _defaultCallbackByKey: DefaultMapCallback<N> = node => node.key; | ||
protected _defaultCallbackByKey: (node: N) => number = node => node.key; | ||
/** | ||
@@ -1111,0 +1155,0 @@ * The function `_addTo` adds a new node to a binary tree if there is an available position. |
@@ -135,3 +135,3 @@ /** | ||
* maintaining balance. | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function | ||
* @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function | ||
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an | ||
@@ -227,3 +227,3 @@ * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or | ||
* callback. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the | ||
* property of the binary tree node that you want to search for. It can be either a specific key | ||
@@ -243,4 +243,4 @@ * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines | ||
*/ | ||
override get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
nodeProperty: BinaryTreeNodeKey | N, | ||
override get<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -250,3 +250,3 @@ beginRoot = this.root, | ||
): N | null { | ||
return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0] ?? null; | ||
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null; | ||
} | ||
@@ -278,3 +278,3 @@ | ||
* using either recursive or iterative traversal. | ||
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property | ||
* @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property | ||
* of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a | ||
@@ -297,4 +297,4 @@ * generic type `N`. | ||
*/ | ||
override getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
nodeProperty: BinaryTreeNodeKey | N, | ||
override getNodes<C extends MapCallback<N>>( | ||
identifier: ReturnType<C> | N, | ||
callback: C = this._defaultCallbackByKey as C, | ||
@@ -311,3 +311,3 @@ onlyOne = false, | ||
const callbackResult = callback(cur); | ||
if (callbackResult === nodeProperty) { | ||
if (callbackResult === identifier) { | ||
ans.push(cur); | ||
@@ -320,4 +320,4 @@ if (onlyOne) return; | ||
if (callback === this._defaultCallbackByKey) { | ||
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left); | ||
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right); | ||
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left); | ||
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right); | ||
} else { | ||
@@ -336,3 +336,3 @@ cur.left && _traverse(cur.left); | ||
const callbackResult = callback(cur); | ||
if (callbackResult === nodeProperty) { | ||
if (callbackResult === identifier) { | ||
ans.push(cur); | ||
@@ -343,4 +343,4 @@ if (onlyOne) return ans; | ||
if (callback === this._defaultCallbackByKey) { | ||
if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left); | ||
if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right); | ||
if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left); | ||
if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right); | ||
} else { | ||
@@ -368,3 +368,3 @@ cur.left && queue.push(cur.left); | ||
* of the following values: | ||
* @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the | ||
* @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the | ||
* `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should | ||
@@ -377,6 +377,6 @@ * start. It can be either a reference to a specific node (`N`), the key of a node | ||
*/ | ||
lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>( | ||
lesserOrGreaterTraverse<C extends MapCallback<N>>( | ||
callback: C = this._defaultCallbackByKey as C, | ||
lesserOrGreater: CP = CP.lt, | ||
targetNode: N | BinaryTreeNodeKey | null = this.root, | ||
targetNode: BinaryTreeNodeKey | N | null = this.root, | ||
iterationType = this.iterationType | ||
@@ -383,0 +383,0 @@ ): ReturnType<C>[] { |
@@ -208,4 +208,4 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types'; | ||
// | ||
// override delete(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] { | ||
// const node = this.get(nodeOrKey); | ||
// override delete(keyOrNode: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] { | ||
// const node = this.get(keyOrNode); | ||
// const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}]; | ||
@@ -212,0 +212,0 @@ // if (!node) return result; // Node does not exist |
@@ -9,3 +9,3 @@ /** | ||
import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types'; | ||
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType} from '../../types'; | ||
import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, MapCallback} from '../../types'; | ||
import {IBinaryTree} from '../../interfaces'; | ||
@@ -269,5 +269,9 @@ import {AVLTree, AVLTreeNode} from './avl-tree'; | ||
* node along with the parent node that needs to be balanced. | ||
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object | ||
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted | ||
* from the binary tree. | ||
* @param {ReturnType<C>} identifier - The `identifier` parameter is either a | ||
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are | ||
* searching for. It can be a specific key value or any other property of the node. | ||
* @param callback - The `callback` parameter is a function that takes a node as input and returns a | ||
* value. This value is compared with the `identifier` parameter to determine if the node should be | ||
* included in the result. The `callback` parameter has a default value of | ||
* `this._defaultCallbackByKey` | ||
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node | ||
@@ -279,7 +283,11 @@ * being deleted. If set to true, the count of the node will not be considered and the node will be | ||
*/ | ||
override delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] { | ||
override delete<C extends MapCallback<N>>( | ||
identifier: ReturnType<C>, | ||
callback: C = this._defaultCallbackByKey as C, | ||
ignoreCount = false | ||
): BinaryTreeDeletedResult<N>[] { | ||
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = []; | ||
if (!this.root) return bstDeletedResult; | ||
const curr: N | null = this.get(nodeOrKey); | ||
const curr: N | null = this.get(identifier, callback); | ||
if (!curr) return bstDeletedResult; | ||
@@ -286,0 +294,0 @@ |
@@ -597,3 +597,2 @@ /** | ||
/** | ||
@@ -600,0 +599,0 @@ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list. |
import {BinaryTreeNode} from '../data-structures'; | ||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types'; | ||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback} from '../types'; | ||
@@ -9,3 +9,3 @@ export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> { | ||
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[]; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[]; | ||
} |
@@ -0,1 +1,3 @@ | ||
import {BinaryTreeNodeKey} from './data-structures'; | ||
export type Comparator<T> = (a: T, b: T) => number; | ||
@@ -7,2 +9,4 @@ | ||
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D; | ||
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>; | ||
@@ -9,0 +13,0 @@ |
1395037
24763
Updateddata-structure-typed@^1.38.6