directed-graph-typed
Advanced tools
Comparing version 1.38.6 to 1.38.7
@@ -10,9 +10,9 @@ /** | ||
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types'; | ||
import { MapCallback } from '../../types'; | ||
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> { | ||
export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> { | ||
height: number; | ||
constructor(key: BinaryTreeNodeKey, val?: V); | ||
} | ||
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IBinaryTree<N> { | ||
export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode> extends BST<V, N> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -30,7 +30,7 @@ * This is a constructor function for an AVL tree data structure in TypeScript. | ||
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of | ||
* type `N['val']`, which means it can be any value that is assignable to the `val` property of the | ||
* type `V`, which means it can be any value that is assignable to the `val` property of the | ||
* node type `N`. | ||
* @returns a new AVLTreeNode object with the specified key and value. | ||
*/ | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
createNode(key: BinaryTreeNodeKey, val?: V): N; | ||
/** | ||
@@ -45,3 +45,3 @@ * The function overrides the add method of a binary tree node and balances the tree after inserting | ||
*/ | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined; | ||
/** | ||
@@ -48,0 +48,0 @@ * The function overrides the delete method of a binary tree and balances the tree after deleting a |
@@ -34,3 +34,3 @@ "use strict"; | ||
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of | ||
* type `N['val']`, which means it can be any value that is assignable to the `val` property of the | ||
* type `V`, which means it can be any value that is assignable to the `val` property of the | ||
* node type `N`. | ||
@@ -37,0 +37,0 @@ * @returns a new AVLTreeNode object with the specified key and value. |
@@ -14,5 +14,5 @@ /** | ||
* @template V - The type of data stored in the node. | ||
* @template FAMILY - The type of the family relationship in the binary tree. | ||
* @template N - The type of the family relationship in the binary tree. | ||
*/ | ||
export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> { | ||
export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> { | ||
/** | ||
@@ -29,3 +29,3 @@ * The key associated with the node. | ||
*/ | ||
parent: FAMILY | null | undefined; | ||
parent: N | null | undefined; | ||
/** | ||
@@ -41,8 +41,8 @@ * Creates a new instance of BinaryTreeNode. | ||
*/ | ||
get left(): FAMILY | null | undefined; | ||
get left(): N | null | undefined; | ||
/** | ||
* Set the left child node. | ||
* @param {FAMILY | null | undefined} v - The left child node. | ||
* @param {N | null | undefined} v - The left child node. | ||
*/ | ||
set left(v: FAMILY | null | undefined); | ||
set left(v: N | null | undefined); | ||
private _right; | ||
@@ -52,8 +52,8 @@ /** | ||
*/ | ||
get right(): FAMILY | null | undefined; | ||
get right(): N | null | undefined; | ||
/** | ||
* Set the right child node. | ||
* @param {FAMILY | null | undefined} v - The right child node. | ||
* @param {N | null | undefined} v - The right child node. | ||
*/ | ||
set right(v: FAMILY | null | undefined); | ||
set right(v: N | null | undefined); | ||
/** | ||
@@ -69,4 +69,3 @@ * Get the position of the node within its family. | ||
*/ | ||
export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> { | ||
private _loopType; | ||
export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -77,2 +76,12 @@ * Creates a new instance of BinaryTree. | ||
constructor(options?: BinaryTreeOptions); | ||
private _iterationType; | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType(): IterationType; | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v: IterationType); | ||
private _root; | ||
@@ -89,17 +98,8 @@ /** | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType(): IterationType; | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v: IterationType); | ||
/** | ||
* Creates a new instance of BinaryTreeNode with the given key and value. | ||
* @param {BinaryTreeNodeKey} key - The key for the new node. | ||
* @param {N['val']} val - The value for the new node. | ||
* @param {V} val - The value for the new node. | ||
* @returns {N} - The newly created BinaryTreeNode. | ||
*/ | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
createNode(key: BinaryTreeNodeKey, val?: V): N; | ||
/** | ||
@@ -117,6 +117,6 @@ * Clear the binary tree, removing all nodes. | ||
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree. | ||
* @param {N['val']} val - The value for the new node (optional). | ||
* @param {V} val - The value for the new node (optional). | ||
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed. | ||
*/ | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined; | ||
/** | ||
@@ -127,3 +127,3 @@ * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data | ||
* objects, or null values. | ||
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to | ||
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to | ||
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided, | ||
@@ -133,3 +133,3 @@ * the value of the nodes will be `undefined`. | ||
*/ | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[]; | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[]; | ||
/** | ||
@@ -139,3 +139,3 @@ * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data. | ||
* `BinaryTreeNodeKey` or `N` values. | ||
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes` | ||
@@ -145,3 +145,3 @@ * array. Each value in the `data` array will be assigned to the | ||
*/ | ||
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean; | ||
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean; | ||
delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[]; | ||
@@ -148,0 +148,0 @@ delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[]; |
@@ -17,3 +17,3 @@ "use strict"; | ||
* @template V - The type of data stored in the node. | ||
* @template FAMILY - The type of the family relationship in the binary tree. | ||
* @template N - The type of the family relationship in the binary tree. | ||
*/ | ||
@@ -38,3 +38,3 @@ class BinaryTreeNode { | ||
* Set the left child node. | ||
* @param {FAMILY | null | undefined} v - The left child node. | ||
* @param {N | null | undefined} v - The left child node. | ||
*/ | ||
@@ -55,3 +55,3 @@ set left(v) { | ||
* Set the right child node. | ||
* @param {FAMILY | null | undefined} v - The right child node. | ||
* @param {N | null | undefined} v - The right child node. | ||
*/ | ||
@@ -93,3 +93,3 @@ set right(v) { | ||
constructor(options) { | ||
this._loopType = types_1.IterationType.ITERATIVE; | ||
this._iterationType = types_1.IterationType.ITERATIVE; | ||
this._root = null; | ||
@@ -107,6 +107,19 @@ this._size = 0; | ||
const { iterationType = types_1.IterationType.ITERATIVE } = options; | ||
this._loopType = iterationType; | ||
this._iterationType = iterationType; | ||
} | ||
} | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType() { | ||
return this._iterationType; | ||
} | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v) { | ||
this._iterationType = v; | ||
} | ||
/** | ||
* Get the root node of the binary tree. | ||
@@ -124,18 +137,5 @@ */ | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType() { | ||
return this._loopType; | ||
} | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v) { | ||
this._loopType = v; | ||
} | ||
/** | ||
* Creates a new instance of BinaryTreeNode with the given key and value. | ||
* @param {BinaryTreeNodeKey} key - The key for the new node. | ||
* @param {N['val']} val - The value for the new node. | ||
* @param {V} val - The value for the new node. | ||
* @returns {N} - The newly created BinaryTreeNode. | ||
@@ -163,3 +163,3 @@ */ | ||
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree. | ||
* @param {N['val']} val - The value for the new node (optional). | ||
* @param {V} val - The value for the new node (optional). | ||
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed. | ||
@@ -229,3 +229,3 @@ */ | ||
* objects, or null values. | ||
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to | ||
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to | ||
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided, | ||
@@ -252,3 +252,3 @@ * the value of the nodes will be `undefined`. | ||
* `BinaryTreeNodeKey` or `N` values. | ||
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes` | ||
@@ -255,0 +255,0 @@ * array. Each value in the `data` array will be assigned to the |
@@ -12,6 +12,6 @@ /** | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> { | ||
export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> { | ||
constructor(key: BinaryTreeNodeKey, val?: V); | ||
} | ||
export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> { | ||
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -32,3 +32,3 @@ * The constructor function initializes a binary search tree object with an optional comparator | ||
*/ | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
createNode(key: BinaryTreeNodeKey, val?: V): N; | ||
/** | ||
@@ -44,3 +44,3 @@ * The `add` function in a binary search tree class inserts a new node with a given key and value | ||
*/ | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined; | ||
/** | ||
@@ -53,3 +53,3 @@ * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while | ||
* `null | ||
* @param {N['val'][]} data - The values of tree nodes | ||
* @param {V[]} data - The values of tree nodes | ||
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method. | ||
@@ -60,3 +60,3 @@ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used. | ||
*/ | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[]; | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[]; | ||
/** | ||
@@ -63,0 +63,0 @@ * The function returns the first node in the binary tree that matches the given node property and |
@@ -133,3 +133,3 @@ "use strict"; | ||
* `null | ||
* @param {N['val'][]} data - The values of tree nodes | ||
* @param {V[]} data - The values of tree nodes | ||
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method. | ||
@@ -136,0 +136,0 @@ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used. |
import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { BST, BSTNode } from './bst'; | ||
export declare class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<V, FAMILY> { | ||
export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> { | ||
constructor(key: BinaryTreeNodeKey, val?: V); | ||
@@ -10,5 +10,5 @@ private _color; | ||
} | ||
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> { | ||
export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> { | ||
constructor(options?: RBTreeOptions); | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
createNode(key: BinaryTreeNodeKey, val?: V): N; | ||
} |
@@ -12,3 +12,3 @@ /** | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> { | ||
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> { | ||
count: number; | ||
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements IBinaryTree<N> { | ||
export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode> extends AVLTree<V, N> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -50,3 +50,3 @@ * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to | ||
*/ | ||
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N; | ||
createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N; | ||
/** | ||
@@ -65,3 +65,3 @@ * The `add` function adds a new node to a binary search tree, updating the count if the key already | ||
*/ | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined; | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count?: number): N | null | undefined; | ||
/** | ||
@@ -81,3 +81,3 @@ * The function adds a new node to a binary tree if there is an available slot in the parent node. | ||
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode. | ||
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond | ||
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond | ||
* to the keys or nodes being added to the multiset. It is used to associate additional data with | ||
@@ -87,3 +87,3 @@ * each key or node. | ||
*/ | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[]; | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[]; | ||
/** | ||
@@ -90,0 +90,0 @@ * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced |
@@ -180,3 +180,3 @@ "use strict"; | ||
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode. | ||
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond | ||
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond | ||
* to the keys or nodes being added to the multiset. It is used to associate additional data with | ||
@@ -195,3 +195,3 @@ * each key or node. | ||
if (keyOrNode === null) { | ||
inserted.push(this.add(NaN, null, 0)); | ||
inserted.push(this.add(NaN, undefined, 0)); | ||
continue; | ||
@@ -198,0 +198,0 @@ } |
import { BinaryTreeNode } from '../data-structures'; | ||
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } from '../types'; | ||
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> { | ||
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback } from '../types'; | ||
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> { | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
@@ -5,0 +5,0 @@ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; |
{ | ||
"name": "directed-graph-typed", | ||
"version": "1.38.6", | ||
"version": "1.38.7", | ||
"description": "Directed Graph. Javascript & Typescript Data Structure.", | ||
@@ -149,4 +149,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.38.6" | ||
"data-structure-typed": "^1.38.7" | ||
} | ||
} |
@@ -10,9 +10,6 @@ /** | ||
import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types'; | ||
import {MapCallback} from '../../types'; | ||
import {IBinaryTree} from '../../interfaces'; | ||
import {MapCallback} from '../../types'; | ||
export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode< | ||
V, | ||
FAMILY | ||
> { | ||
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> { | ||
height: number; | ||
@@ -26,3 +23,6 @@ | ||
export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IBinaryTree<N> { | ||
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode> | ||
extends BST<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
/** | ||
@@ -43,8 +43,8 @@ * This is a constructor function for an AVL tree data structure in TypeScript. | ||
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of | ||
* type `N['val']`, which means it can be any value that is assignable to the `val` property of the | ||
* type `V`, which means it can be any value that is assignable to the `val` property of the | ||
* node type `N`. | ||
* @returns a new AVLTreeNode object with the specified key and value. | ||
*/ | ||
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N { | ||
return new AVLTreeNode<N['val'], N>(key, val) as N; | ||
override createNode(key: BinaryTreeNodeKey, val?: V): N { | ||
return new AVLTreeNode<V, N>(key, val) as N; | ||
} | ||
@@ -61,3 +61,3 @@ | ||
*/ | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined { | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined { | ||
// TODO support node as a param | ||
@@ -64,0 +64,0 @@ const inserted = super.add(keyOrNode, val); |
@@ -26,5 +26,5 @@ /** | ||
* @template V - The type of data stored in the node. | ||
* @template FAMILY - The type of the family relationship in the binary tree. | ||
* @template N - The type of the family relationship in the binary tree. | ||
*/ | ||
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> { | ||
export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> { | ||
/** | ||
@@ -43,3 +43,3 @@ * The key associated with the node. | ||
*/ | ||
parent: FAMILY | null | undefined; | ||
parent: N | null | undefined; | ||
@@ -56,3 +56,3 @@ /** | ||
private _left: FAMILY | null | undefined; | ||
private _left: N | null | undefined; | ||
@@ -62,3 +62,3 @@ /** | ||
*/ | ||
get left(): FAMILY | null | undefined { | ||
get left(): N | null | undefined { | ||
return this._left; | ||
@@ -69,7 +69,7 @@ } | ||
* Set the left child node. | ||
* @param {FAMILY | null | undefined} v - The left child node. | ||
* @param {N | null | undefined} v - The left child node. | ||
*/ | ||
set left(v: FAMILY | null | undefined) { | ||
set left(v: N | null | undefined) { | ||
if (v) { | ||
v.parent = this as unknown as FAMILY; | ||
v.parent = this as unknown as N; | ||
} | ||
@@ -79,3 +79,3 @@ this._left = v; | ||
private _right: FAMILY | null | undefined; | ||
private _right: N | null | undefined; | ||
@@ -85,3 +85,3 @@ /** | ||
*/ | ||
get right(): FAMILY | null | undefined { | ||
get right(): N | null | undefined { | ||
return this._right; | ||
@@ -92,7 +92,7 @@ } | ||
* Set the right child node. | ||
* @param {FAMILY | null | undefined} v - The right child node. | ||
* @param {N | null | undefined} v - The right child node. | ||
*/ | ||
set right(v: FAMILY | null | undefined) { | ||
set right(v: N | null | undefined) { | ||
if (v) { | ||
v.parent = this as unknown as FAMILY; | ||
v.parent = this as unknown as N; | ||
} | ||
@@ -107,3 +107,3 @@ this._right = v; | ||
get familyPosition(): FamilyPosition { | ||
const that = this as unknown as FAMILY; | ||
const that = this as unknown as N; | ||
if (!this.parent) { | ||
@@ -127,5 +127,3 @@ return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED; | ||
*/ | ||
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> { | ||
private _loopType: IterationType = IterationType.ITERATIVE; | ||
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -138,6 +136,23 @@ * Creates a new instance of BinaryTree. | ||
const {iterationType = IterationType.ITERATIVE} = options; | ||
this._loopType = iterationType; | ||
this._iterationType = iterationType; | ||
} | ||
} | ||
private _iterationType: IterationType = IterationType.ITERATIVE; | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType(): IterationType { | ||
return this._iterationType; | ||
} | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v: IterationType) { | ||
this._iterationType = v; | ||
} | ||
private _root: N | null = null; | ||
@@ -162,24 +177,9 @@ | ||
/** | ||
* Get the iteration type used in the binary tree. | ||
*/ | ||
get iterationType(): IterationType { | ||
return this._loopType; | ||
} | ||
/** | ||
* Set the iteration type for the binary tree. | ||
* @param {IterationType} v - The new iteration type to set. | ||
*/ | ||
set iterationType(v: IterationType) { | ||
this._loopType = v; | ||
} | ||
/** | ||
* Creates a new instance of BinaryTreeNode with the given key and value. | ||
* @param {BinaryTreeNodeKey} key - The key for the new node. | ||
* @param {N['val']} val - The value for the new node. | ||
* @param {V} val - The value for the new node. | ||
* @returns {N} - The newly created BinaryTreeNode. | ||
*/ | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N { | ||
return new BinaryTreeNode<N['val'], N>(key, val) as N; | ||
createNode(key: BinaryTreeNodeKey, val?: V): N { | ||
return new BinaryTreeNode<V, N>(key, val) as N; | ||
} | ||
@@ -206,6 +206,6 @@ | ||
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree. | ||
* @param {N['val']} val - The value for the new node (optional). | ||
* @param {V} val - The value for the new node (optional). | ||
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed. | ||
*/ | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined { | ||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined { | ||
const _bfs = (root: N, newNode: N | null): N | undefined | null => { | ||
@@ -265,3 +265,3 @@ const queue = new Queue<N | null>([root]); | ||
* objects, or null values. | ||
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to | ||
* @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to | ||
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided, | ||
@@ -271,3 +271,3 @@ * the value of the nodes will be `undefined`. | ||
*/ | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[] { | ||
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[] { | ||
// TODO not sure addMany not be run multi times | ||
@@ -292,3 +292,3 @@ return keysOrNodes.map((keyOrNode, i) => { | ||
* `BinaryTreeNodeKey` or `N` values. | ||
* @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to | ||
* the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes` | ||
@@ -298,3 +298,3 @@ * array. Each value in the `data` array will be assigned to the | ||
*/ | ||
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean { | ||
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean { | ||
this.clear(); | ||
@@ -301,0 +301,0 @@ return keysOrNodes.length === this.addMany(keysOrNodes, data).length; |
@@ -21,3 +21,3 @@ /** | ||
export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> { | ||
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> { | ||
constructor(key: BinaryTreeNodeKey, val?: V) { | ||
@@ -28,3 +28,3 @@ super(key, val); | ||
export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> { | ||
export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> { | ||
/** | ||
@@ -54,4 +54,4 @@ * The constructor function initializes a binary search tree object with an optional comparator | ||
*/ | ||
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N { | ||
return new BSTNode<N['val'], N>(key, val) as N; | ||
override createNode(key: BinaryTreeNodeKey, val?: V): N { | ||
return new BSTNode<V, N>(key, val) as N; | ||
} | ||
@@ -69,3 +69,3 @@ | ||
*/ | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined { | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined { | ||
// TODO support node as a parameter | ||
@@ -143,3 +143,3 @@ let inserted: N | null = null; | ||
* `null | ||
* @param {N['val'][]} data - The values of tree nodes | ||
* @param {V[]} data - The values of tree nodes | ||
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method. | ||
@@ -153,3 +153,3 @@ * @param iterationType - The `iterationType` parameter determines the type of iteration to be used. | ||
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], | ||
data?: N['val'][], | ||
data?: V[], | ||
isBalanceAdd = true, | ||
@@ -183,3 +183,3 @@ iterationType = this.iterationType | ||
let sortedKeysOrNodes: (number | N | null)[] = [], | ||
sortedData: (N['val'] | undefined)[] | undefined = []; | ||
sortedData: (V | undefined)[] | undefined = []; | ||
@@ -195,3 +195,3 @@ if (isNodeOrNullTuple(combinedArr)) { | ||
sortedData = sorted.map(([, val]) => val); | ||
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => { | ||
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: (V | undefined)[]) => { | ||
if (arr.length === 0) return; | ||
@@ -198,0 +198,0 @@ |
@@ -5,6 +5,3 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types'; | ||
export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode< | ||
V, | ||
FAMILY | ||
> { | ||
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> { | ||
constructor(key: BinaryTreeNodeKey, val?: V) { | ||
@@ -26,3 +23,3 @@ super(key, val); | ||
export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> { | ||
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> { | ||
constructor(options?: RBTreeOptions) { | ||
@@ -32,7 +29,7 @@ super(options); | ||
override createNode(key: BinaryTreeNodeKey, val?: N['val']): N { | ||
override createNode(key: BinaryTreeNodeKey, val?: V): N { | ||
return new RBTreeNode(key, val) as N; | ||
} | ||
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined { | ||
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined { | ||
// const inserted = super.add(keyOrNode, val); | ||
@@ -39,0 +36,0 @@ // if (inserted) this._fixInsertViolation(inserted); |
@@ -15,4 +15,4 @@ /** | ||
V = any, | ||
FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V> | ||
> extends AVLTreeNode<V, FAMILY> { | ||
N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V> | ||
> extends AVLTreeNode<V, N> { | ||
count: number; | ||
@@ -39,5 +39,5 @@ | ||
*/ | ||
export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> | ||
extends AVLTree<N> | ||
implements IBinaryTree<N> | ||
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode> | ||
extends AVLTree<V, N> | ||
implements IBinaryTree<V, N> | ||
{ | ||
@@ -69,3 +69,3 @@ /** | ||
*/ | ||
override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N { | ||
override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N { | ||
return new TreeMultisetNode(key, val, count) as N; | ||
@@ -87,3 +87,3 @@ } | ||
*/ | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined { | ||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined { | ||
let inserted: N | null | undefined = undefined, | ||
@@ -194,3 +194,3 @@ newNode: N | null; | ||
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode. | ||
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond | ||
* @param {V[]} [data] - The `data` parameter is an optional array of values that correspond | ||
* to the keys or nodes being added to the multiset. It is used to associate additional data with | ||
@@ -200,6 +200,3 @@ * each key or node. | ||
*/ | ||
override addMany( | ||
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], | ||
data?: N['val'][] | ||
): (N | null | undefined)[] { | ||
override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] { | ||
const inserted: (N | null | undefined)[] = []; | ||
@@ -216,3 +213,3 @@ | ||
if (keyOrNode === null) { | ||
inserted.push(this.add(NaN, null, 0)); | ||
inserted.push(this.add(NaN, undefined, 0)); | ||
continue; | ||
@@ -219,0 +216,0 @@ } |
import {BinaryTreeNode} from '../data-structures'; | ||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback} from '../types'; | ||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types'; | ||
export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> { | ||
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> { | ||
createNode(key: BinaryTreeNodeKey, val?: N['val']): N; | ||
@@ -6,0 +6,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1444396
25140
Updateddata-structure-typed@^1.38.7