max-priority-queue-typed
Advanced tools
Comparing version 1.54.2 to 1.54.3
@@ -8,6 +8,7 @@ /** | ||
*/ | ||
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, OptNodeOrNull } from '../../types'; | ||
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
export declare class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> { | ||
parent?: AVLTreeCounterNode<K, V>; | ||
/** | ||
@@ -24,9 +25,8 @@ * The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
constructor(key: K, value?: V, count?: number); | ||
parent?: AVLTreeCounterNode<K, V>; | ||
_left?: OptNodeOrNull<AVLTreeCounterNode<K, V>>; | ||
get left(): OptNodeOrNull<AVLTreeCounterNode<K, V>>; | ||
set left(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>); | ||
_right?: OptNodeOrNull<AVLTreeCounterNode<K, V>>; | ||
get right(): OptNodeOrNull<AVLTreeCounterNode<K, V>>; | ||
set right(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>); | ||
_left?: AVLTreeCounterNode<K, V> | null | undefined; | ||
get left(): AVLTreeCounterNode<K, V> | null | undefined; | ||
set left(v: AVLTreeCounterNode<K, V> | null | undefined); | ||
_right?: AVLTreeCounterNode<K, V> | null | undefined; | ||
get right(): AVLTreeCounterNode<K, V> | null | undefined; | ||
set right(v: AVLTreeCounterNode<K, V> | null | undefined); | ||
} | ||
@@ -45,3 +45,3 @@ /** | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, AVLTreeCounterNode<K, V>> | R>, options?: AVLTreeCounterOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeCounterOptions<K, V, R>); | ||
protected _count: number; | ||
@@ -85,8 +85,8 @@ /** | ||
* The function checks if the input is an instance of AVLTreeCounterNode. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeCounterNode` class. | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>): keyNodeOrEntry is AVLTreeCounterNode<K, V>; | ||
isNode(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeCounterNode<K, V>; | ||
/** | ||
@@ -98,5 +98,5 @@ * Time Complexity: O(log n) | ||
* and update the count. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node, | ||
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node, | ||
* entry, or raw element | ||
@@ -110,3 +110,3 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, value?: V, count?: number): boolean; | ||
add(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean; | ||
/** | ||
@@ -118,3 +118,3 @@ * Time Complexity: O(log n) | ||
* nodes and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition for deleting a node from the | ||
@@ -132,3 +132,3 @@ * binary tree. It can be a key, node, or entry that determines which | ||
*/ | ||
delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, ignoreCount?: boolean): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[]; | ||
delete(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[]; | ||
/** | ||
@@ -145,2 +145,3 @@ * Time Complexity: O(1) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
@@ -186,4 +187,4 @@ * tree using either a recursive or iterative approach. | ||
* a node object. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
@@ -196,3 +197,3 @@ * `override` function. It represents the value associated with the key in the data structure. If no | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, value?: V, count?: number): [AVLTreeCounterNode<K, V> | undefined, V | undefined]; | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [AVLTreeCounterNode<K, V> | undefined, V | undefined]; | ||
/** | ||
@@ -199,0 +200,0 @@ * Time Complexity: O(1) |
@@ -108,4 +108,4 @@ "use strict"; | ||
* The function checks if the input is an instance of AVLTreeCounterNode. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
@@ -123,5 +123,5 @@ * an instance of the `AVLTreeCounterNode` class. | ||
* and update the count. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node, | ||
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node, | ||
* entry, or raw element | ||
@@ -152,3 +152,3 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
* nodes and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition for deleting a node from the | ||
@@ -238,2 +238,3 @@ * binary tree. It can be a key, node, or entry that determines which | ||
* Space Complexity: O(log n) | ||
* | ||
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
@@ -336,4 +337,4 @@ * tree using either a recursive or iterative approach. | ||
* a node object. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
@@ -340,0 +341,0 @@ * `override` function. It represents the value associated with the key in the data structure. If no |
@@ -8,6 +8,7 @@ /** | ||
*/ | ||
import { AVLTreeMultiMapOptions, BTNRep, OptNodeOrNull } from '../../types'; | ||
import { AVLTreeMultiMapOptions } from '../../types'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> { | ||
parent?: AVLTreeMultiMapNode<K, V>; | ||
/** | ||
@@ -23,9 +24,8 @@ * This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
constructor(key: K, value: V[]); | ||
parent?: AVLTreeMultiMapNode<K, V>; | ||
_left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>); | ||
_right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>; | ||
set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>); | ||
_left?: AVLTreeMultiMapNode<K, V> | null | undefined; | ||
get left(): AVLTreeMultiMapNode<K, V> | null | undefined; | ||
set left(v: AVLTreeMultiMapNode<K, V> | null | undefined); | ||
_right?: AVLTreeMultiMapNode<K, V> | null | undefined; | ||
get right(): AVLTreeMultiMapNode<K, V> | null | undefined; | ||
set right(v: AVLTreeMultiMapNode<K, V> | null | undefined); | ||
} | ||
@@ -47,3 +47,3 @@ /** | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R>, options?: AVLTreeMultiMapOptions<K, V[], R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>); | ||
/** | ||
@@ -74,3 +74,3 @@ * Time Complexity: O(1) | ||
createNode(key: K): AVLTreeMultiMapNode<K, V>; | ||
add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean; | ||
add(node: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean; | ||
add(key: K, value: V): boolean; | ||
@@ -83,3 +83,3 @@ /** | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
@@ -95,3 +95,3 @@ * pair in the AVLTreeMultiMapNode, or just the key itself. | ||
*/ | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean; | ||
deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K, value: V): boolean; | ||
/** | ||
@@ -98,0 +98,0 @@ * Time Complexity: O(n) |
@@ -97,3 +97,3 @@ "use strict"; | ||
* tree multi-map. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it | ||
@@ -151,3 +151,3 @@ * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
@@ -154,0 +154,0 @@ * pair in the AVLTreeMultiMapNode, or just the key itself. |
@@ -9,5 +9,6 @@ /** | ||
import { BST, BSTNode } from './bst'; | ||
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, OptNodeOrNull } from '../../types'; | ||
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
parent?: AVLTreeNode<K, V>; | ||
/** | ||
@@ -23,9 +24,8 @@ * This TypeScript constructor function initializes an instance with a key and an optional value. | ||
constructor(key: K, value?: V); | ||
parent?: AVLTreeNode<K, V>; | ||
_left?: OptNodeOrNull<AVLTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<AVLTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<AVLTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<AVLTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<AVLTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<AVLTreeNode<K, V>>); | ||
_left?: AVLTreeNode<K, V> | null | undefined; | ||
get left(): AVLTreeNode<K, V> | null | undefined; | ||
set left(v: AVLTreeNode<K, V> | null | undefined); | ||
_right?: AVLTreeNode<K, V> | null | undefined; | ||
get right(): AVLTreeNode<K, V> | null | undefined; | ||
set right(v: AVLTreeNode<K, V> | null | undefined); | ||
} | ||
@@ -46,3 +46,4 @@ /** | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* iterable that can contain either ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
@@ -54,3 +55,3 @@ * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R>, options?: AVLTreeOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>); | ||
/** | ||
@@ -85,8 +86,9 @@ * Time Complexity: O(1) | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeNode` class. | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V>; | ||
isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>; | ||
/** | ||
@@ -98,4 +100,5 @@ * Time Complexity: O(log n) | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -105,3 +108,3 @@ * the key or node being added to the data structure. | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean; | ||
add(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean; | ||
/** | ||
@@ -113,3 +116,3 @@ * Time Complexity: O(log n) | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -121,3 +124,3 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
*/ | ||
delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[]; | ||
delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[]; | ||
/** | ||
@@ -225,6 +228,7 @@ * Time Complexity: O(n) | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or | ||
* ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
*/ | ||
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void; | ||
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void; | ||
/** | ||
@@ -231,0 +235,0 @@ * Time Complexity: O(1) |
@@ -62,3 +62,4 @@ "use strict"; | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* iterable that can contain either ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
@@ -108,4 +109,5 @@ * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
@@ -123,4 +125,5 @@ * an instance of the `AVLTreeNode` class. | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -144,3 +147,3 @@ * the key or node being added to the data structure. | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -471,4 +474,5 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or | ||
* ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
*/ | ||
@@ -475,0 +479,0 @@ _balancePath(node) { |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, RBTNColor, ToEntryFn } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, RBTNColor, ToEntryFn } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
@@ -19,2 +19,5 @@ import { IterableEntryBase } from '../base'; | ||
export declare class BinaryTreeNode<K = any, V = any> { | ||
key: K; | ||
value?: V; | ||
parent?: BinaryTreeNode<K, V>; | ||
/** | ||
@@ -29,11 +32,8 @@ * The constructor function initializes an object with a key and an optional value in TypeScript. | ||
constructor(key: K, value?: V); | ||
key: K; | ||
value?: V; | ||
parent?: BinaryTreeNode<K, V>; | ||
_left?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<BinaryTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<BinaryTreeNode<K, V>>); | ||
_left?: BinaryTreeNode<K, V> | null | undefined; | ||
get left(): BinaryTreeNode<K, V> | null | undefined; | ||
set left(v: BinaryTreeNode<K, V> | null | undefined); | ||
_right?: BinaryTreeNode<K, V> | null | undefined; | ||
get right(): BinaryTreeNode<K, V> | null | undefined; | ||
set right(v: BinaryTreeNode<K, V> | null | undefined); | ||
_height: number; | ||
@@ -56,4 +56,67 @@ get height(): number; | ||
* 5. Leaf Nodes: Nodes without children are leaves. | ||
* @example | ||
* // determine loan approval using a decision tree | ||
* // Decision tree structure | ||
* const loanDecisionTree = new BinaryTree<string>( | ||
* ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'], | ||
* { isDuplicate: true } | ||
* ); | ||
* | ||
* function determineLoanApproval( | ||
* node?: BinaryTreeNode<string> | null, | ||
* conditions?: { [key: string]: boolean } | ||
* ): string { | ||
* if (!node) throw new Error('Invalid node'); | ||
* | ||
* // If it's a leaf node, return the decision result | ||
* if (!node.left && !node.right) return node.key; | ||
* | ||
* // Check if a valid condition exists for the current node's key | ||
* return conditions?.[node.key] | ||
* ? determineLoanApproval(node.left, conditions) | ||
* : determineLoanApproval(node.right, conditions); | ||
* } | ||
* | ||
* // Test case 1: Stable income and good credit score | ||
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved' | ||
* | ||
* // Test case 2: Stable income but poor credit score | ||
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected' | ||
* | ||
* // Test case 3: No stable income | ||
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected' | ||
* | ||
* // Test case 4: No stable income and poor credit score | ||
* console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected' | ||
* @example | ||
* // evaluate the arithmetic expression represented by the binary tree | ||
* const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]); | ||
* | ||
* function evaluate(node?: BinaryTreeNode<number | string> | null): number { | ||
* if (!node) return 0; | ||
* | ||
* if (typeof node.key === 'number') return node.key; | ||
* | ||
* const leftValue = evaluate(node.left); // Evaluate the left subtree | ||
* const rightValue = evaluate(node.right); // Evaluate the right subtree | ||
* | ||
* // Perform the operation based on the current node's operator | ||
* switch (node.key) { | ||
* case '+': | ||
* return leftValue + rightValue; | ||
* case '-': | ||
* return leftValue - rightValue; | ||
* case '*': | ||
* return leftValue * rightValue; | ||
* case '/': | ||
* return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero | ||
* default: | ||
* throw new Error(`Unsupported operator: ${node.key}`); | ||
* } | ||
* } | ||
* | ||
* console.log(evaluate(expressionTree.root)); // -27 | ||
*/ | ||
export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
iterationType: IterationType; | ||
/** | ||
@@ -63,3 +126,3 @@ * This TypeScript constructor function initializes a binary tree with optional options and adds | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either objects of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It | ||
* iterable that can contain either objects of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It | ||
* is used to initialize the binary tree with keys, nodes, entries, or raw data. | ||
@@ -69,10 +132,11 @@ * @param [options] - The `options` parameter in the constructor is an optional object that can | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, options?: BinaryTreeOptions<K, V, R>); | ||
iterationType: IterationType; | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>); | ||
protected _isMapMode: boolean; | ||
get isMapMode(): boolean; | ||
protected _isDuplicate: boolean; | ||
get isDuplicate(): boolean; | ||
protected _store: Map<K, V | undefined>; | ||
get store(): Map<K, V | undefined>; | ||
protected _root?: OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
get root(): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
protected _root?: BinaryTreeNode<K, V> | null | undefined; | ||
get root(): BinaryTreeNode<K, V> | null | undefined; | ||
protected _size: number; | ||
@@ -115,4 +179,4 @@ get size(): number; | ||
* value and returns the corresponding node or null. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `ensureNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `ensureNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It | ||
* is used to determine whether the input is a key, node, entry, or raw data. The | ||
@@ -125,3 +189,3 @@ * @param {IterationType} iterationType - The `iterationType` parameter in the `ensureNode` function | ||
*/ | ||
ensureNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
ensureNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined; | ||
/** | ||
@@ -132,3 +196,3 @@ * Time Complexity: O(1) | ||
* The function isNode checks if the input is an instance of BinaryTreeNode. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be either a key, a node, an entry, or raw data. The function is | ||
@@ -142,3 +206,3 @@ * checking if the input is an instance of a `BinaryTreeNode` and returning a boolean value | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
isNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
/** | ||
@@ -149,3 +213,3 @@ * Time Complexity: O(1) | ||
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | R} keyNodeEntryOrRaw - BTNRep<K, V, BinaryTreeNode<K, V>> | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R} keyNodeEntryOrRaw - K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by | ||
@@ -155,3 +219,3 @@ * checking if it is an object. If the parameter is an object, the function will return `true`, | ||
*/ | ||
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, BinaryTreeNode<K, V>> | R): keyNodeEntryOrRaw is R; | ||
isRaw(keyNodeEntryOrRaw: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R): keyNodeEntryOrRaw is R; | ||
/** | ||
@@ -162,4 +226,4 @@ * Time Complexity: O(1) | ||
* The function `isRealNode` checks if a given input is a valid node in a binary tree. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isRealNode` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isRealNode` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. | ||
* The function checks if the input parameter is a `BinaryTreeNode<K, V>` type by verifying if it is not equal | ||
@@ -171,3 +235,3 @@ * @returns The function `isRealNode` is checking if the input `keyNodeOrEntry` is a valid | ||
*/ | ||
isRealNode(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
isRealNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>; | ||
/** | ||
@@ -178,3 +242,3 @@ * Time Complexity: O(1) | ||
* The function checks if a given input is a valid node or null. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` in the `isRealNodeOrNull` function can be of type `BTNRep<K, | ||
@@ -186,3 +250,3 @@ * V, BinaryTreeNode<K, V>>` or `R`. It is a union type that can either be a key, a node, an entry, or | ||
*/ | ||
isRealNodeOrNull(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BinaryTreeNode<K, V> | null; | ||
isRealNodeOrNull(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V> | null; | ||
/** | ||
@@ -193,3 +257,3 @@ * Time Complexity: O(1) | ||
* The function isNIL checks if a given key, node, entry, or raw value is equal to the _NIL value. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - BTNRep<K, V, | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - BTNRep<K, V, | ||
* BinaryTreeNode<K, V>> | ||
@@ -199,3 +263,3 @@ * @returns The function is checking if the `keyNodeOrEntry` parameter is equal to the `_NIL` | ||
*/ | ||
isNIL(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
isNIL(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean; | ||
/** | ||
@@ -206,5 +270,5 @@ * Time Complexity: O(1) | ||
* The function `isRange` checks if the input parameter is an instance of the `Range` class. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} | ||
* keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be | ||
* of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, `NodePredicate<BinaryTreeNode<K, V>>`, or | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `isRange` function can be | ||
* of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `NodePredicate<BinaryTreeNode<K, V>>`, or | ||
* `Range<K>`. The function checks if the `keyNodeEntry | ||
@@ -216,3 +280,3 @@ * @returns The `isRange` function is checking if the `keyNodeEntryOrPredicate` parameter is an | ||
*/ | ||
isRange(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>> | Range<K>): keyNodeEntryOrPredicate is Range<K>; | ||
isRange(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>): keyNodeEntryOrPredicate is Range<K>; | ||
/** | ||
@@ -224,4 +288,4 @@ * Time Complexity: O(1) | ||
* tree. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. It represents a | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It represents a | ||
* key, node, entry, or raw data in a binary tree structure. The function `isLeaf` checks whether the | ||
@@ -232,3 +296,3 @@ * provided | ||
*/ | ||
isLeaf(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
isLeaf(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean; | ||
/** | ||
@@ -240,4 +304,4 @@ * Time Complexity: O(1) | ||
* with a length of 2. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isEntry` function can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or type `R`. | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `isEntry` function can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or type `R`. | ||
* The function checks if the provided `keyNodeOrEntry` is of type `BTN | ||
@@ -248,3 +312,3 @@ * @returns The `isEntry` function is checking if the `keyNodeOrEntry` parameter is an array | ||
*/ | ||
isEntry(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): keyNodeOrEntry is BTNEntry<K, V>; | ||
isEntry(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BTNEntry<K, V>; | ||
/** | ||
@@ -268,3 +332,3 @@ * Time Complexity O(1) | ||
* and finding the correct insertion position. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `add` method you provided | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `add` method you provided | ||
* seems to be for adding a new node to a binary tree structure. The `keyNodeOrEntry` | ||
@@ -280,3 +344,3 @@ * parameter in the method can accept different types of values: | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V): boolean; | ||
add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean; | ||
/** | ||
@@ -291,3 +355,3 @@ * Time Complexity: O(k * n) | ||
* mix of keys, nodes, entries, or raw values. Each element in this iterable can be of type | ||
* `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. | ||
* `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. | ||
* @param [values] - The `values` parameter in the `addMany` function is an optional parameter that | ||
@@ -301,3 +365,3 @@ * accepts an iterable of values. These values correspond to the keys or nodes being added in the | ||
*/ | ||
addMany(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, values?: Iterable<V | undefined>): boolean[]; | ||
addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[]; | ||
/** | ||
@@ -319,3 +383,3 @@ * Time Complexity: O(k * n) | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the `refill` | ||
* method can accept an iterable containing a mix of `BTNRep<K, V, BinaryTreeNode<K, V>>` objects or `R` | ||
* method can accept an iterable containing a mix of `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` | ||
* objects. | ||
@@ -325,3 +389,3 @@ * @param [values] - The `values` parameter in the `refill` method is an optional parameter that | ||
*/ | ||
refill(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>> | R>, values?: Iterable<V | undefined>): void; | ||
refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void; | ||
/** | ||
@@ -333,3 +397,3 @@ * Time Complexity: O(n) | ||
* the deleted node along with information for tree balancing. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry | ||
* - The `delete` method you provided is used to delete a node from a binary tree based on the key, | ||
@@ -343,3 +407,3 @@ * node, entry or raw data. The method returns an array of | ||
*/ | ||
delete(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[]; | ||
delete(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[]; | ||
/** | ||
@@ -351,3 +415,3 @@ * Time Complexity: O(n) | ||
* structure based on a given predicate or key, with options to return multiple results or just one. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `search` function can accept three types of values: | ||
@@ -359,4 +423,4 @@ * @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<BinaryTreeNode<K, V>>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `search` function is | ||
* extends `NodeCallback<BinaryTreeNode<K, V> | null>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `search` function is | ||
* used to specify the node from which the search operation should begin. It represents the starting | ||
@@ -371,27 +435,6 @@ * point in the binary tree where the search will be performed. If no specific `startNode` is | ||
*/ | ||
search<C extends NodeCallback<BinaryTreeNode<K, V>>>(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean, callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(k + log n) | ||
* | ||
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data, | ||
* or predicate, with options for recursive or iterative traversal. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `getNodes` function you provided takes several parameters: | ||
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that | ||
* determines whether to return only the first node that matches the criteria specified by the | ||
* `keyNodeEntryOrPredicate` parameter. If `onlyOne` is set to `true`, the function will | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `getNodes` function is used to specify the starting point for traversing the binary tree. It | ||
* represents the root node of the binary tree or the node from which the traversal should begin. If | ||
* not provided, the default value is set to `this._root | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function | ||
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It | ||
* can have two possible values: | ||
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition | ||
* based on the input parameters and the iteration type specified. | ||
*/ | ||
getNodes(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): BinaryTreeNode<K, V>[]; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
@@ -401,6 +444,6 @@ * | ||
* predicate. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `getNode` function can accept a key, | ||
* node, entry, raw data, or a predicate function. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `getNode` function is used to specify the starting point for searching for a node in a binary | ||
@@ -416,3 +459,3 @@ * tree. If no specific starting point is provided, the default value is set to `this._root`, which | ||
*/ | ||
getNode(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined; | ||
/** | ||
@@ -424,6 +467,6 @@ * Time Complexity: O(n) | ||
* node, entry, raw data, or predicate in a data structure. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `get` method can accept one of the | ||
* following types: | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `get` | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `get` | ||
* method is used to specify the starting point for searching for a key or node in the binary tree. | ||
@@ -441,26 +484,5 @@ * If no specific starting point is provided, the default starting point is the root of the binary | ||
*/ | ||
get(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): V | undefined; | ||
get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined; | ||
has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean; | ||
/** | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(log n) | ||
* | ||
* The `has` function in TypeScript checks if a specified key, node, entry, raw data, or predicate | ||
* exists in the data structure. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate | ||
* - The `keyNodeEntryOrPredicate` parameter in the `override has` method can accept one of | ||
* the following types: | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* `override` method is used to specify the starting point for the search operation within the data | ||
* structure. It defaults to `this._root` if not provided explicitly. | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `override has` method | ||
* is used to specify the type of iteration to be performed. It has a default value of | ||
* `this.iterationType`, which means it will use the iteration type defined in the current context if | ||
* no value is provided when calling the method. | ||
* @returns The `override has` method is returning a boolean value. It checks if there are any nodes | ||
* that match the provided key, node, entry, raw data, or predicate in the tree structure. If there | ||
* are matching nodes, it returns `true`, indicating that the tree contains the specified element. | ||
* Otherwise, it returns `false`. | ||
*/ | ||
has(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): boolean; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -488,3 +510,3 @@ * Space Complexity: O(1) | ||
* its height. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for checking if the binary tree is perfectly balanced. It represents the root node of the | ||
@@ -498,3 +520,3 @@ * binary tree or a specific node from which the balance check should begin. | ||
*/ | ||
isPerfectlyBalanced(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): boolean; | ||
isPerfectlyBalanced(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean; | ||
/** | ||
@@ -506,3 +528,3 @@ * Time Complexity: O(n) | ||
* or iterative methods. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `isBST` | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `isBST` | ||
* function represents the starting point for checking whether a binary search tree (BST) is valid. | ||
@@ -519,3 +541,3 @@ * It can be a node in the BST or a reference to the root of the BST. If no specific node is | ||
*/ | ||
isBST(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): boolean; | ||
isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean; | ||
/** | ||
@@ -526,6 +548,6 @@ * Time Complexity: O(n) | ||
* The `getDepth` function calculates the depth between two nodes in a binary tree. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} dist - The `dist` parameter in the `getDepth` | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } dist - The `dist` parameter in the `getDepth` | ||
* function represents the node or entry in a binary tree map, or a reference to a node in the tree. | ||
* It is the target node for which you want to calculate the depth from the `startNode` node. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `getDepth` function represents the starting point from which you want to calculate the depth of a | ||
@@ -538,3 +560,3 @@ * given node or entry in a binary tree. If no specific starting point is provided, the default value | ||
*/ | ||
getDepth(dist: BTNRep<K, V, BinaryTreeNode<K, V>>, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): number; | ||
getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number; | ||
/** | ||
@@ -546,3 +568,3 @@ * Time Complexity: O(n) | ||
* or iterative approach in TypeScript. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point from which the height of the binary tree will be calculated. It can be a node in the binary | ||
@@ -558,3 +580,3 @@ * tree or a reference to the root of the tree. If not provided, it defaults to the root of the | ||
*/ | ||
getHeight(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): number; | ||
getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number; | ||
/** | ||
@@ -566,3 +588,3 @@ * Time Complexity: O(n) | ||
* recursive or iterative approach in TypeScript. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `getMinHeight` function represents the starting node from which the minimum height of the binary | ||
@@ -579,3 +601,3 @@ * tree will be calculated. It is either a node in the binary tree or a reference to the root of the | ||
*/ | ||
getMinHeight(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): number; | ||
getMinHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number; | ||
/** | ||
@@ -591,3 +613,3 @@ * Time Complexity: O(log n) | ||
* type `C | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} beginNode - The `beginNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } beginNode - The `beginNode` parameter in the | ||
* `getPathToRoot` function can be either a key, a node, an entry, or any other value of type `R`. | ||
@@ -602,3 +624,3 @@ * @param [isReverse=true] - The `isReverse` parameter in the `getPathToRoot` function determines | ||
*/ | ||
getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(beginNode: BTNRep<K, V, BinaryTreeNode<K, V>>, callback?: C, isReverse?: boolean): ReturnType<C>[]; | ||
getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback?: C, isReverse?: boolean): ReturnType<C>[]; | ||
/** | ||
@@ -613,3 +635,3 @@ * Time Complexity: O(log n) | ||
* value of `_DEFAULT_NODE_CALLBACK` if not specified. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `getLeftMost` function represents the starting point for finding the leftmost node in a binary | ||
@@ -626,3 +648,3 @@ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific | ||
*/ | ||
getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>; | ||
getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>; | ||
/** | ||
@@ -638,3 +660,3 @@ * Time Complexity: O(log n) | ||
* as | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `getRightMost` function represents the starting point for finding the rightmost node in a binary | ||
@@ -651,3 +673,3 @@ * tree. It can be either a key, a node, or an entry in the binary tree structure. If no specific | ||
*/ | ||
getRightMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>; | ||
getRightMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>; | ||
/** | ||
@@ -680,7 +702,7 @@ * Time Complexity: O(log n) | ||
*/ | ||
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): OptNodeOrNull<BinaryTreeNode<K, V>>; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[]; | ||
/** | ||
@@ -694,3 +716,3 @@ * Time complexity: O(n) | ||
* in the binary tree. It is optional and defaults to a default callback function if not provided. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `leaves` | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `leaves` | ||
* method is used to specify the starting point for finding and processing the leaves of a binary | ||
@@ -705,5 +727,6 @@ * tree. It can be provided as either a key, a node, or an entry in the binary tree structure. If not | ||
*/ | ||
leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][]; | ||
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[]; | ||
/** | ||
@@ -713,23 +736,2 @@ * Time complexity: O(n) | ||
* | ||
* The `morris` function in TypeScript performs a Depth-First Search traversal on a binary tree using | ||
* Morris Traversal algorithm with different order patterns. | ||
* @param {C} callback - The `callback` parameter in the `morris` function is a function that will be | ||
* called on each node in the binary tree during the traversal. It is of type `C`, which extends the | ||
* `NodeCallback<BinaryTreeNode<K, V>>` type. The default value for `callback` is `this._DEFAULT | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `morris` function specifies | ||
* the type of Depth-First Search (DFS) order pattern to traverse the binary tree. The possible | ||
* values for the `pattern` parameter are: | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `morris` | ||
* function is the starting point for the Morris traversal algorithm. It represents the root node of | ||
* the binary tree or the node from which the traversal should begin. It can be provided as either a | ||
* key, a node, an entry, or a reference | ||
* @returns The `morris` function is returning an array of values that are the result of applying the | ||
* provided callback function to each node in the binary tree in the specified order pattern (IN, | ||
* PRE, or POST). | ||
*/ | ||
morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): ReturnType<C>[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `clone` function creates a deep copy of a tree structure by traversing it using breadth-first | ||
@@ -743,3 +745,2 @@ * search. | ||
clone(): BinaryTree<K, V, R, MK, MV, MR>; | ||
protected _clone(cloned: BinaryTree<K, V, R, MK, MV, MR>): void; | ||
/** | ||
@@ -788,3 +789,3 @@ * Time Complexity: O(n) | ||
* customizable options for displaying undefined, null, and sentinel nodes. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `toVisual` method is used to specify the starting point for visualizing the binary tree structure. | ||
@@ -801,3 +802,3 @@ * It can be a node, key, entry, or the root of the tree. If no specific starting point is provided, | ||
*/ | ||
toVisual(startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, options?: BinaryTreePrintOptions): string; | ||
toVisual(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, options?: BinaryTreePrintOptions): string; | ||
/** | ||
@@ -813,3 +814,3 @@ * Time Complexity: O(n) | ||
* options. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the | ||
* `override print` method is used to specify the starting point for printing the binary tree. It can | ||
@@ -819,3 +820,4 @@ * be either a key, a node, an entry, or the root of the tree. If no specific starting point is | ||
*/ | ||
print(options?: BinaryTreePrintOptions, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>): void; | ||
print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void; | ||
protected _clone(cloned: BinaryTree<K, V, R, MK, MV, MR>): void; | ||
/** | ||
@@ -827,5 +829,5 @@ * Time Complexity: O(1) | ||
* or returns null. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The | ||
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeOrEntry`, which | ||
* can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. This parameter represents either a key, a | ||
* can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. This parameter represents either a key, a | ||
* node, an entry | ||
@@ -836,51 +838,9 @@ * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is | ||
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node | ||
* (`OptNodeOrNull<BinaryTreeNode<K, V>>`) based on the input parameters provided. The function checks the type of the | ||
* (`BinaryTreeNode<K, V> | null | undefined`) based on the input parameters provided. The function checks the type of the | ||
* input parameter (`keyNodeOrEntry`) and processes it accordingly to return a node or null | ||
* value. | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V): [OptNodeOrNull<BinaryTreeNode<K, V>>, V | undefined]; | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined]; | ||
protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[]; | ||
/** | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
* | ||
* The `_dfs` function performs a depth-first search traversal on a binary tree structure based on | ||
* the specified order pattern and callback function. | ||
* @param {C} callback - The `callback` parameter in the `_dfs` method is a function that will be | ||
* called on each node visited during the depth-first search traversal. It is of type `C`, which | ||
* extends `NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>`. The default value for this parameter is `this._DEFAULT | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `_dfs` method specifies the | ||
* order in which the nodes are visited during the Depth-First Search traversal. It can have one of | ||
* the following values: | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} startNode - The `startNode` parameter in the `_dfs` | ||
* method is used to specify the starting point for the depth-first search traversal in a binary | ||
* tree. It can be provided as either a `BTNRep` object or a reference to the root node | ||
* of the tree. If no specific | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `_dfs` method | ||
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a | ||
* binary tree. It can have two possible values: | ||
* @param [includeNull=false] - The `includeNull` parameter in the `_dfs` method is a boolean flag | ||
* that determines whether null nodes should be included in the depth-first search traversal. If | ||
* `includeNull` is set to `true`, null nodes will be considered during the traversal process. If it | ||
* is set to `false`, | ||
* @param shouldVisitLeft - The `shouldVisitLeft` parameter is a function that takes a node as input | ||
* and returns a boolean value. It is used to determine whether the left child of a node should be | ||
* visited during the depth-first search traversal. By default, it checks if the node is truthy (not | ||
* null or undefined | ||
* @param shouldVisitRight - The `shouldVisitRight` parameter is a function that takes a node as an | ||
* argument and returns a boolean value. It is used to determine whether the right child of a node | ||
* should be visited during the depth-first search traversal. The default implementation checks if | ||
* the node is truthy before visiting the right child | ||
* @param shouldVisitRoot - The `shouldVisitRoot` parameter is a function that takes a node as an | ||
* argument and returns a boolean value. It is used to determine whether the root node should be | ||
* visited during the depth-first search traversal based on certain conditions. The default | ||
* implementation checks if the node is a real node or null based | ||
* @param shouldProcessRoot - The `shouldProcessRoot` parameter is a function that takes a node as an | ||
* argument and returns a boolean value indicating whether the node should be processed during the | ||
* depth-first search traversal. The default implementation checks if the node is a real node or null | ||
* based on the `includeNull` flag. If ` | ||
* @returns The function `_dfs` returns an array of the return type of the callback function provided | ||
* as input. | ||
*/ | ||
protected _dfs<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BinaryTreeNode<K, V>>, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldVisitRight?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldVisitRoot?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean, shouldProcessRoot?: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => boolean): ReturnType<C>[]; | ||
/** | ||
* Time Complexity: O(1) | ||
@@ -900,3 +860,3 @@ * Space Complexity: O(1) | ||
*/ | ||
protected _getIterator(node?: OptNodeOrNull<BinaryTreeNode<K, V>>): IterableIterator<[K, V | undefined]>; | ||
protected _getIterator(node?: BinaryTreeNode<K, V> | null | undefined): IterableIterator<[K, V | undefined]>; | ||
/** | ||
@@ -917,4 +877,4 @@ * Time Complexity: O(n) | ||
*/ | ||
protected _displayAux(node: OptNodeOrNull<BinaryTreeNode<K, V>>, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
protected _DEFAULT_NODE_CALLBACK: (node: OptNodeOrNull<BinaryTreeNode<K, V>>) => K | undefined; | ||
protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout; | ||
protected _DEFAULT_NODE_CALLBACK: (node: BinaryTreeNode<K, V> | null | undefined) => K | undefined; | ||
/** | ||
@@ -925,8 +885,8 @@ * Time Complexity: O(1) | ||
* The _swapProperties function swaps key and value properties between two nodes in a binary tree. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} srcNode - The `srcNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } srcNode - The `srcNode` parameter in the | ||
* `_swapProperties` method can be either a BTNRep object containing key and value | ||
* properties, or it can be of type R. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} destNode - The `destNode` parameter in the | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } destNode - The `destNode` parameter in the | ||
* `_swapProperties` method represents the node or entry where the properties will be swapped with | ||
* the `srcNode`. It can be of type `BTNRep<K, V, BinaryTreeNode<K, V>>` or `R`. The method ensures that | ||
* the `srcNode`. It can be of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. The method ensures that | ||
* both `srcNode | ||
@@ -936,3 +896,3 @@ * @returns The `_swapProperties` method returns either the `destNode` with its key and value swapped | ||
*/ | ||
protected _swapProperties(srcNode: BTNRep<K, V, BinaryTreeNode<K, V>>, destNode: BTNRep<K, V, BinaryTreeNode<K, V>>): BinaryTreeNode<K, V> | undefined; | ||
protected _swapProperties(srcNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, destNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeNode<K, V> | undefined; | ||
/** | ||
@@ -959,6 +919,7 @@ * Time Complexity: O(1) | ||
* of the previous root node. | ||
* @param v - The parameter `v` in the `_setRoot` method is of type `OptNodeOrNull<BinaryTreeNode<K, V>>`, which means | ||
* @param v - The parameter `v` in the `_setRoot` method is of type `BinaryTreeNode<K, V> | null | undefined`, which means | ||
* it can either be an optional `BinaryTreeNode<K, V>` type or `null`. | ||
*/ | ||
protected _setRoot(v: OptNodeOrNull<BinaryTreeNode<K, V>>): void; | ||
protected _setRoot(v: BinaryTreeNode<K, V> | null | undefined): void; | ||
protected _ensurePredicate(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>): NodePredicate<BinaryTreeNode<K, V>>; | ||
/** | ||
@@ -968,15 +929,2 @@ * Time Complexity: O(1) | ||
* | ||
* The function `_ensurePredicate` in TypeScript ensures that the input is converted into a valid | ||
* predicate function for a binary tree node. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `_ensurePredicate` method in the provided code snippet is responsible for ensuring that the input | ||
* parameter `keyNodeEntryOrPredicate` is transformed into a valid predicate function that can be | ||
* used for filtering nodes in a binary tree. | ||
* @returns A NodePredicate<BinaryTreeNode<K, V>> function is being returned. | ||
*/ | ||
protected _ensurePredicate(keyNodeEntryOrPredicate: BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>): NodePredicate<BinaryTreeNode<K, V>>; | ||
/** | ||
* Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* The function `_isPredicate` checks if a given parameter is a function. | ||
@@ -997,4 +945,4 @@ * @param {any} p - The parameter `p` is a variable of type `any`, which means it can hold any type | ||
* entry, raw data, or null/undefined. | ||
* @param {BTNRep<K, V, BinaryTreeNode<K, V>>} keyNodeOrEntry - The `_extractKey` method you provided is a | ||
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `BTNRep<K, V, BinaryTreeNode<K, V>>`, | ||
* @param {K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `_extractKey` method you provided is a | ||
* TypeScript method that takes in a parameter `keyNodeOrEntry` of type `K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, | ||
* where `BTNRep` is a generic type with keys `K`, `V`, and `BinaryTreeNode<K, V>`, and ` | ||
@@ -1005,3 +953,3 @@ * @returns The `_extractKey` method returns the key value extracted from the `keyNodeOrEntry` | ||
*/ | ||
protected _extractKey(keyNodeOrEntry: BTNRep<K, V, BinaryTreeNode<K, V>>): K | null | undefined; | ||
protected _extractKey(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): K | null | undefined; | ||
/** | ||
@@ -1008,0 +956,0 @@ * Time Complexity: O(1) |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode, OptNodeOrNull } from '../../types'; | ||
import type { BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types'; | ||
import { BinaryTree, BinaryTreeNode } from './binary-tree'; | ||
@@ -14,2 +14,3 @@ import { IBinaryTree } from '../../interfaces'; | ||
export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> { | ||
parent?: BSTNode<K, V>; | ||
/** | ||
@@ -25,9 +26,8 @@ * This TypeScript constructor function initializes an instance with a key and an optional value. | ||
constructor(key: K, value?: V); | ||
parent?: BSTNode<K, V>; | ||
_left?: OptNodeOrNull<BSTNode<K, V>>; | ||
get left(): OptNodeOrNull<BSTNode<K, V>>; | ||
set left(v: OptNodeOrNull<BSTNode<K, V>>); | ||
_right?: OptNodeOrNull<BSTNode<K, V>>; | ||
get right(): OptNodeOrNull<BSTNode<K, V>>; | ||
set right(v: OptNodeOrNull<BSTNode<K, V>>); | ||
_left?: BSTNode<K, V> | null | undefined; | ||
get left(): BSTNode<K, V> | null | undefined; | ||
set left(v: BSTNode<K, V> | null | undefined); | ||
_right?: BSTNode<K, V> | null | undefined; | ||
get right(): BSTNode<K, V> | null | undefined; | ||
set right(v: BSTNode<K, V> | null | undefined); | ||
} | ||
@@ -68,5 +68,5 @@ /** | ||
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
@@ -105,3 +105,3 @@ * console.log(bst.search(new Range(15, 20, false))); // [18] | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* iterable that can contain elements of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
@@ -111,3 +111,3 @@ * @param [options] - The `options` parameter is an optional object that can contain the following | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, BSTNode<K, V>> | R>, options?: BSTOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>); | ||
protected _root?: BSTNode<K, V>; | ||
@@ -150,3 +150,3 @@ get root(): OptNode<BSTNode<K, V>>; | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
@@ -160,3 +160,3 @@ * entry, or raw element that needs to be ensured in the tree. | ||
*/ | ||
ensureNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
ensureNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
/** | ||
@@ -167,8 +167,8 @@ * Time Complexity: O(1) | ||
* The function checks if the input is an instance of the BSTNode class. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `BSTNode` class. | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V>; | ||
isNode(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BSTNode<K, V>; | ||
/** | ||
@@ -191,4 +191,4 @@ * Time Complexity: O(1) | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -198,3 +198,3 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean; | ||
add(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean; | ||
/** | ||
@@ -228,3 +228,3 @@ * Time Complexity: O(k log n) | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
@@ -237,5 +237,5 @@ * following types: | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* extends `NodeCallback<BSTNode<K, V> | null>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -252,3 +252,3 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
*/ | ||
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
search<C extends NodeCallback<BSTNode<K, V>>>(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>> | Range<K>, onlyOne?: boolean, callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -263,5 +263,5 @@ * Time Complexity: O(log n) | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V> | null>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -276,3 +276,3 @@ * begin. It is the starting point for the range search operation. | ||
*/ | ||
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -283,4 +283,4 @@ * Time Complexity: O(log n) | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
@@ -299,3 +299,3 @@ * is used to specify the starting point for searching nodes in the binary search tree. If no | ||
*/ | ||
getNode(keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
getNode(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, startNode?: BSTNOptKeyOrNode<K, BSTNode<K, V>>, iterationType?: IterationType): OptNode<BSTNode<K, V>>; | ||
/** | ||
@@ -305,19 +305,25 @@ * Time complexity: O(n) | ||
* | ||
* The function overrides the depth-first search method and returns an array of the return types of | ||
* the callback function. | ||
* The function `dfs` in TypeScript overrides the base class method with default parameters and | ||
* returns the result of the super class `dfs` method. | ||
* @param {C} callback - The `callback` parameter is a function that will be called for each node | ||
* during the depth-first search traversal. It is an optional parameter and defaults to | ||
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function. | ||
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the | ||
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
* node entry. If not specified, the default value is the root of the tree. | ||
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the | ||
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the | ||
* following values: | ||
* @returns The method is returning an array of the return type of the callback function. | ||
* visited during the Depth-First Search traversal. It is a generic type `C` that extends the | ||
* `NodeCallback` interface for `BSTNode<K, V>`. The default value for `callback` is `this._ | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `override dfs` method | ||
* specifies the order in which the Depth-First Search (DFS) traversal should be performed on the | ||
* Binary Search Tree (BST). The possible values for the `pattern` parameter are: | ||
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `override dfs` method is a | ||
* boolean flag that indicates whether you want to stop the depth-first search traversal after | ||
* finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set | ||
* to `true`, the traversal will stop after finding | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} startNode - | ||
* The `startNode` parameter in the `override dfs` method can be one of the following types: | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `override dfs` method | ||
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a | ||
* Binary Search Tree (BST). It is used to determine the order in which nodes are visited during the | ||
* traversal. The possible values for ` | ||
* @returns The `override` function is returning the result of calling the `dfs` method from the | ||
* superclass, with the provided arguments `callback`, `pattern`, `onlyOne`, `startNode`, and | ||
* `iterationType`. The return type is an array of the return type of the callback function `C`. | ||
*/ | ||
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
dfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -332,3 +338,3 @@ * Time complexity: O(n) | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -341,3 +347,3 @@ * object. If no value is provided, the default value is the root of the tree. | ||
*/ | ||
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
bfs<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -350,5 +356,5 @@ * Time complexity: O(n) | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V> | null>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -362,3 +368,3 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
*/ | ||
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[][]; | ||
listLevels<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[][]; | ||
/** | ||
@@ -376,3 +382,3 @@ * Time complexity: O(n) | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -386,3 +392,3 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
*/ | ||
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNRep<K, V, BSTNode<K, V>>, iterationType?: IterationType): ReturnType<C>[]; | ||
lesserOrGreaterTraverse<C extends NodeCallback<BSTNode<K, V>>>(callback?: C, lesserOrGreater?: CP, targetNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[]; | ||
/** | ||
@@ -448,4 +454,4 @@ * Time complexity: O(n) | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - A variable that can be of | ||
* type R or K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined . It represents either a key, a node, an entry, or a raw | ||
* element. | ||
@@ -456,3 +462,3 @@ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): [OptNode<BSTNode<K, V>>, V | undefined]; | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [OptNode<BSTNode<K, V>>, V | undefined]; | ||
/** | ||
@@ -459,0 +465,0 @@ * Time Complexity: O(1) |
@@ -78,5 +78,5 @@ "use strict"; | ||
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
@@ -115,3 +115,3 @@ * console.log(bst.search(new Range(15, 20, false))); // [18] | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* iterable that can contain elements of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
@@ -200,3 +200,3 @@ * @param [options] - The `options` parameter is an optional object that can contain the following | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
@@ -219,4 +219,4 @@ * entry, or raw element that needs to be ensured in the tree. | ||
* The function checks if the input is an instance of the BSTNode class. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
@@ -247,4 +247,4 @@ * an instance of the `BSTNode` class. | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -420,3 +420,3 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
@@ -429,5 +429,5 @@ * following types: | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* extends `NodeCallback<BSTNode<K, V> | null>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -456,3 +456,7 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
if (isRange) { | ||
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
predicate = node => { | ||
if (!node) | ||
return false; | ||
return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
}; | ||
} | ||
@@ -462,3 +466,7 @@ else { | ||
} | ||
const isToLeftByRange = (cur) => { | ||
const shouldVisitLeft = (cur) => { | ||
if (!cur) | ||
return false; | ||
if (!this.isRealNode(cur.left)) | ||
return false; | ||
if (isRange) { | ||
@@ -470,5 +478,13 @@ const range = keyNodeEntryOrPredicate; | ||
} | ||
return false; | ||
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) > 0; | ||
} | ||
return true; | ||
}; | ||
const isToRightByRange = (cur) => { | ||
const shouldVisitRight = (cur) => { | ||
if (!cur) | ||
return false; | ||
if (!this.isRealNode(cur.right)) | ||
return false; | ||
if (isRange) { | ||
@@ -480,79 +496,13 @@ const range = keyNodeEntryOrPredicate; | ||
} | ||
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) < 0; | ||
} | ||
return true; | ||
}; | ||
return super._dfs(callback, 'IN', onlyOne, startNode, iterationType, false, shouldVisitLeft, shouldVisitRight, () => true, cur => { | ||
if (cur) | ||
return predicate(cur); | ||
return false; | ||
}; | ||
const ans = []; | ||
if (iterationType === 'RECURSIVE') { | ||
const dfs = (cur) => { | ||
if (predicate(cur)) { | ||
ans.push(callback(cur)); | ||
if (onlyOne) | ||
return; | ||
} | ||
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) | ||
return; | ||
if (isRange) { | ||
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) | ||
dfs(cur.left); | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) | ||
dfs(cur.right); | ||
} | ||
else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if (this.isRealNode(cur.left) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) > 0) | ||
dfs(cur.left); | ||
if (this.isRealNode(cur.right) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) < 0) | ||
dfs(cur.right); | ||
} | ||
else { | ||
if (this.isRealNode(cur.left)) | ||
dfs(cur.left); | ||
if (this.isRealNode(cur.right)) | ||
dfs(cur.right); | ||
} | ||
}; | ||
dfs(startNode); | ||
} | ||
else { | ||
const stack = [startNode]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop(); | ||
if (predicate(cur)) { | ||
ans.push(callback(cur)); | ||
if (onlyOne) | ||
return ans; | ||
} | ||
if (isRange) { | ||
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) | ||
stack.push(cur.left); | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) | ||
stack.push(cur.right); | ||
} | ||
else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if (this.isRealNode(cur.right) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) < 0) | ||
stack.push(cur.right); | ||
if (this.isRealNode(cur.left) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) > 0) | ||
stack.push(cur.left); | ||
} | ||
else { | ||
if (this.isRealNode(cur.right)) | ||
stack.push(cur.right); | ||
if (this.isRealNode(cur.left)) | ||
stack.push(cur.left); | ||
} | ||
} | ||
} | ||
return ans; | ||
}); | ||
} | ||
@@ -568,5 +518,5 @@ /** | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V> | null>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -590,4 +540,4 @@ * begin. It is the starting point for the range search operation. | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
@@ -614,20 +564,26 @@ * is used to specify the starting point for searching nodes in the binary search tree. If no | ||
* | ||
* The function overrides the depth-first search method and returns an array of the return types of | ||
* the callback function. | ||
* The function `dfs` in TypeScript overrides the base class method with default parameters and | ||
* returns the result of the super class `dfs` method. | ||
* @param {C} callback - The `callback` parameter is a function that will be called for each node | ||
* during the depth-first search traversal. It is an optional parameter and defaults to | ||
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function. | ||
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the | ||
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
* node entry. If not specified, the default value is the root of the tree. | ||
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the | ||
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the | ||
* following values: | ||
* @returns The method is returning an array of the return type of the callback function. | ||
* visited during the Depth-First Search traversal. It is a generic type `C` that extends the | ||
* `NodeCallback` interface for `BSTNode<K, V>`. The default value for `callback` is `this._ | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `override dfs` method | ||
* specifies the order in which the Depth-First Search (DFS) traversal should be performed on the | ||
* Binary Search Tree (BST). The possible values for the `pattern` parameter are: | ||
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `override dfs` method is a | ||
* boolean flag that indicates whether you want to stop the depth-first search traversal after | ||
* finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set | ||
* to `true`, the traversal will stop after finding | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} startNode - | ||
* The `startNode` parameter in the `override dfs` method can be one of the following types: | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `override dfs` method | ||
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a | ||
* Binary Search Tree (BST). It is used to determine the order in which nodes are visited during the | ||
* traversal. The possible values for ` | ||
* @returns The `override` function is returning the result of calling the `dfs` method from the | ||
* superclass, with the provided arguments `callback`, `pattern`, `onlyOne`, `startNode`, and | ||
* `iterationType`. The return type is an array of the return type of the callback function `C`. | ||
*/ | ||
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', startNode = this._root, iterationType = this.iterationType) { | ||
return super.dfs(callback, pattern, startNode, iterationType); | ||
dfs(callback = this._DEFAULT_NODE_CALLBACK, pattern = 'IN', onlyOne = false, startNode = this._root, iterationType = this.iterationType) { | ||
return super.dfs(callback, pattern, onlyOne, startNode, iterationType); | ||
} | ||
@@ -643,3 +599,3 @@ /** | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -662,5 +618,5 @@ * object. If no value is provided, the default value is the root of the tree. | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V> | null>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -689,3 +645,3 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -761,5 +717,5 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
if (this._isMapMode && midNode !== null) | ||
this.add(midNode.key); | ||
else | ||
else if (midNode !== null) | ||
this.add([midNode.key, midNode.value]); | ||
@@ -781,5 +737,5 @@ buildBalanceBST(l, m - 1); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
if (this._isMapMode && midNode !== null) | ||
this.add(midNode.key); | ||
else | ||
else if (midNode !== null) | ||
this.add([midNode.key, midNode.value]); | ||
@@ -897,4 +853,4 @@ stack.push([m + 1, r]); | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - A variable that can be of | ||
* type R or K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined . It represents either a key, a node, an entry, or a raw | ||
* element. | ||
@@ -901,0 +857,0 @@ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the |
@@ -1,5 +0,6 @@ | ||
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, OptNodeOrNull, RBTNColor, RedBlackTreeOptions } from '../../types'; | ||
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types'; | ||
import { BST, BSTNode } from './bst'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
parent?: RedBlackTreeNode<K, V>; | ||
/** | ||
@@ -16,9 +17,8 @@ * The constructor initializes a node with a key, value, and color for a Red-Black Tree. | ||
constructor(key: K, value?: V, color?: RBTNColor); | ||
parent?: RedBlackTreeNode<K, V>; | ||
_left?: OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
get left(): OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>); | ||
_right?: OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
get right(): OptNodeOrNull<RedBlackTreeNode<K, V>>; | ||
set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>); | ||
_left?: RedBlackTreeNode<K, V> | null | undefined; | ||
get left(): RedBlackTreeNode<K, V> | null | undefined; | ||
set left(v: RedBlackTreeNode<K, V> | null | undefined); | ||
_right?: RedBlackTreeNode<K, V> | null | undefined; | ||
get right(): RedBlackTreeNode<K, V> | null | undefined; | ||
set right(v: RedBlackTreeNode<K, V> | null | undefined); | ||
} | ||
@@ -29,8 +29,2 @@ /** | ||
* @example | ||
* // Find elements in a range | ||
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* @example | ||
* // using Red-Black Tree as a price-based index for stock data | ||
@@ -77,3 +71,3 @@ * // Define the structure of individual stock records | ||
* ); | ||
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META'] | ||
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT'] | ||
*/ | ||
@@ -85,3 +79,3 @@ export declare class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> { | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
@@ -93,3 +87,3 @@ * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R>, options?: RedBlackTreeOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>); | ||
protected _root: RedBlackTreeNode<K, V> | undefined; | ||
@@ -130,8 +124,8 @@ get root(): RedBlackTreeNode<K, V> | undefined; | ||
* The function checks if the input is an instance of the RedBlackTreeNode class. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `RedBlackTreeNode` class. | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V>; | ||
isNode(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is RedBlackTreeNode<K, V>; | ||
/** | ||
@@ -151,4 +145,4 @@ * Time Complexity: O(1) | ||
* added. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -161,3 +155,3 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean; | ||
add(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean; | ||
/** | ||
@@ -169,3 +163,3 @@ * Time Complexity: O(log n) | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method is used to specify the condition or key based on which a | ||
@@ -178,3 +172,3 @@ * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate | ||
*/ | ||
delete(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[]; | ||
delete(keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[]; | ||
/** | ||
@@ -181,0 +175,0 @@ * Time Complexity: O(n) |
@@ -47,8 +47,2 @@ "use strict"; | ||
* @example | ||
* // Find elements in a range | ||
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* @example | ||
* // using Red-Black Tree as a price-based index for stock data | ||
@@ -95,3 +89,3 @@ * // Define the structure of individual stock records | ||
* ); | ||
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META'] | ||
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT'] | ||
*/ | ||
@@ -103,3 +97,3 @@ class RedBlackTree extends bst_1.BST { | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
@@ -158,4 +152,4 @@ * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
* The function checks if the input is an instance of the RedBlackTreeNode class. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
@@ -184,4 +178,4 @@ * an instance of the `RedBlackTreeNode` class. | ||
* added. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -225,3 +219,3 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method is used to specify the condition or key based on which a | ||
@@ -228,0 +222,0 @@ * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate |
@@ -8,6 +8,7 @@ /** | ||
*/ | ||
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType, OptNodeOrNull, RBTNColor, TreeCounterOptions } from '../../types'; | ||
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, IterationType, RBTNColor, TreeCounterOptions } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
export declare class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> { | ||
parent?: TreeCounterNode<K, V>; | ||
/** | ||
@@ -26,9 +27,8 @@ * The constructor function initializes a Red-Black Tree node with a key, value, count, and color. | ||
constructor(key: K, value?: V, count?: number, color?: RBTNColor); | ||
parent?: TreeCounterNode<K, V>; | ||
_left?: OptNodeOrNull<TreeCounterNode<K, V>>; | ||
get left(): OptNodeOrNull<TreeCounterNode<K, V>>; | ||
set left(v: OptNodeOrNull<TreeCounterNode<K, V>>); | ||
_right?: OptNodeOrNull<TreeCounterNode<K, V>>; | ||
get right(): OptNodeOrNull<TreeCounterNode<K, V>>; | ||
set right(v: OptNodeOrNull<TreeCounterNode<K, V>>); | ||
_left?: TreeCounterNode<K, V> | null | undefined; | ||
get left(): TreeCounterNode<K, V> | null | undefined; | ||
set left(v: TreeCounterNode<K, V> | null | undefined); | ||
_right?: TreeCounterNode<K, V> | null | undefined; | ||
get right(): TreeCounterNode<K, V> | null | undefined; | ||
set right(v: TreeCounterNode<K, V> | null | undefined); | ||
} | ||
@@ -48,3 +48,3 @@ /** | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, TreeCounterNode<K, V>> | R>, options?: TreeCounterOptions<K, V, R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: TreeCounterOptions<K, V, R>); | ||
protected _count: number; | ||
@@ -90,8 +90,8 @@ /** | ||
* The function checks if the input is an instance of the TreeCounterNode class. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `TreeCounterNode` class. | ||
*/ | ||
isNode(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>): keyNodeOrEntry is TreeCounterNode<K, V>; | ||
isNode(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is TreeCounterNode<K, V>; | ||
/** | ||
@@ -103,3 +103,3 @@ * Time Complexity: O(log n) | ||
* the count and returning a boolean indicating success. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept one of the following types: | ||
@@ -114,3 +114,3 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
*/ | ||
add(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, value?: V, count?: number): boolean; | ||
add(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean; | ||
/** | ||
@@ -122,3 +122,3 @@ * Time Complexity: O(log n) | ||
* structure, handling cases where nodes have children and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition or key based on which a node | ||
@@ -132,3 +132,3 @@ * should be deleted from the binary tree. It can be a key, a node, or an entry. | ||
*/ | ||
delete(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[]; | ||
delete(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[]; | ||
/** | ||
@@ -183,4 +183,4 @@ * Time Complexity: O(1) | ||
* node based on the input. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that represents the value | ||
@@ -193,3 +193,3 @@ * associated with the key in the node. It is used when creating a new node or updating the value of | ||
*/ | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, value?: V, count?: number): [TreeCounterNode<K, V> | undefined, V | undefined]; | ||
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [TreeCounterNode<K, V> | undefined, V | undefined]; | ||
/** | ||
@@ -196,0 +196,0 @@ * Time Complexity: O(1) |
@@ -82,3 +82,3 @@ "use strict"; | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
this.dfs(node => (sum += node ? node.count : 0)); | ||
return sum; | ||
@@ -115,4 +115,4 @@ } | ||
* The function checks if the input is an instance of the TreeCounterNode class. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
@@ -130,3 +130,3 @@ * an instance of the `TreeCounterNode` class. | ||
* the count and returning a boolean indicating success. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept one of the following types: | ||
@@ -159,3 +159,3 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
* structure, handling cases where nodes have children and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition or key based on which a node | ||
@@ -301,5 +301,5 @@ * should be deleted from the binary tree. It can be a key, a node, or an entry. | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
if (this._isMapMode && midNode !== null) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
else if (midNode !== null) | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
@@ -321,5 +321,5 @@ buildBalanceBST(l, m - 1); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) | ||
if (this._isMapMode && midNode !== null) | ||
this.add(midNode.key, undefined, midNode.count); | ||
else | ||
else if (midNode !== null) | ||
this.add(midNode.key, midNode.value, midNode.count); | ||
@@ -343,3 +343,3 @@ stack.push([m + 1, r]); | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
this.bfs(node => cloned.add(node === null ? null : node.key, undefined, node === null ? 0 : node.count)); | ||
if (this._isMapMode) | ||
@@ -375,4 +375,4 @@ cloned._store = this._store; | ||
* node based on the input. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that represents the value | ||
@@ -379,0 +379,0 @@ * associated with the key in the node. It is used when creating a new node or updating the value of |
@@ -8,6 +8,7 @@ /** | ||
*/ | ||
import type { BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types'; | ||
import type { TreeMultiMapOptions } from '../../types'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> { | ||
parent?: TreeMultiMapNode<K, V>; | ||
/** | ||
@@ -23,9 +24,8 @@ * This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
constructor(key: K, value: V[]); | ||
parent?: TreeMultiMapNode<K, V>; | ||
_left?: OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
get left(): OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>); | ||
_right?: OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
get right(): OptNodeOrNull<TreeMultiMapNode<K, V>>; | ||
set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>); | ||
_left?: TreeMultiMapNode<K, V> | null | undefined; | ||
get left(): TreeMultiMapNode<K, V> | null | undefined; | ||
set left(v: TreeMultiMapNode<K, V> | null | undefined); | ||
_right?: TreeMultiMapNode<K, V> | null | undefined; | ||
get right(): TreeMultiMapNode<K, V> | null | undefined; | ||
set right(v: TreeMultiMapNode<K, V> | null | undefined); | ||
} | ||
@@ -37,4 +37,4 @@ /** | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 7, 10, 12] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
@@ -54,3 +54,3 @@ */ | ||
*/ | ||
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R>, options?: TreeMultiMapOptions<K, V[], R>); | ||
constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>); | ||
/** | ||
@@ -82,3 +82,3 @@ * Time Complexity: O(1) | ||
createNode(key: K): TreeMultiMapNode<K, V>; | ||
add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean; | ||
add(node: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean; | ||
add(key: K, value: V): boolean; | ||
@@ -91,3 +91,3 @@ /** | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
@@ -102,3 +102,3 @@ * array of values, or just a key itself. | ||
*/ | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean; | ||
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean; | ||
/** | ||
@@ -105,0 +105,0 @@ * Time Complexity: O(n) |
@@ -46,4 +46,4 @@ "use strict"; | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 7, 10, 12] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
@@ -105,3 +105,3 @@ */ | ||
* TreeMultiMapNode, handling different input types and scenarios. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array | ||
@@ -158,3 +158,3 @@ * of values, and a `TreeMultiMapNode`, or just a key. | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
@@ -161,0 +161,0 @@ * array of values, or just a key itself. |
@@ -8,2 +8,3 @@ import { IterationType, OptValue } from '../../common'; | ||
isMapMode?: boolean; | ||
isDuplicate?: boolean; | ||
}; | ||
@@ -10,0 +11,0 @@ export type BinaryTreePrintOptions = { |
import type { BinaryTreeOptions } from './binary-tree'; | ||
import { Comparable } from '../../utils'; | ||
import { OptValue } from '../../common'; | ||
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & { | ||
export type BSTOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'> & { | ||
specifyComparable?: (key: K) => Comparable; | ||
@@ -6,0 +6,0 @@ isReverse?: boolean; |
/** | ||
* data-structure-typed | ||
* | ||
* @author Tyler Zeng | ||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com> | ||
* @author Pablo Zeng | ||
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com> | ||
* @license MIT License | ||
@@ -7,0 +7,0 @@ */ |
{ | ||
"name": "max-priority-queue-typed", | ||
"version": "1.54.2", | ||
"version": "1.54.3", | ||
"description": "Max Priority Queue. Javascript & Typescript Data Structure.", | ||
@@ -130,4 +130,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"data-structure-typed": "^1.54.2" | ||
"data-structure-typed": "^1.54.3" | ||
} | ||
} |
@@ -12,6 +12,4 @@ /** | ||
BSTNOptKeyOrNode, | ||
BTNRep, | ||
EntryCallback, | ||
IterationType, | ||
OptNodeOrNull | ||
IterationType | ||
} from '../../types'; | ||
@@ -22,2 +20,4 @@ import { IBinaryTree } from '../../interfaces'; | ||
export class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> { | ||
override parent?: AVLTreeCounterNode<K, V> = undefined; | ||
/** | ||
@@ -38,11 +38,9 @@ * The constructor function initializes a BinaryTreeNode object with a key, value, and count. | ||
override parent?: AVLTreeCounterNode<K, V> = undefined; | ||
override _left?: AVLTreeCounterNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<AVLTreeCounterNode<K, V>> { | ||
override get left(): AVLTreeCounterNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) { | ||
override set left(v: AVLTreeCounterNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -54,9 +52,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<AVLTreeCounterNode<K, V>> = undefined; | ||
override _right?: AVLTreeCounterNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<AVLTreeCounterNode<K, V>> { | ||
override get right(): AVLTreeCounterNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<AVLTreeCounterNode<K, V>>) { | ||
override set right(v: AVLTreeCounterNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -85,3 +83,5 @@ v.parent = this; | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeCounterNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R | ||
> = [], | ||
options?: AVLTreeCounterOptions<K, V, R> | ||
@@ -153,8 +153,10 @@ ) { | ||
* The function checks if the input is an instance of AVLTreeCounterNode. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeCounterNode` class. | ||
*/ | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>): keyNodeOrEntry is AVLTreeCounterNode<K, V> { | ||
override isNode( | ||
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): keyNodeOrEntry is AVLTreeCounterNode<K, V> { | ||
return keyNodeOrEntry instanceof AVLTreeCounterNode; | ||
@@ -169,5 +171,5 @@ } | ||
* and update the count. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It | ||
* can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node, | ||
* can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node, | ||
* entry, or raw element | ||
@@ -181,3 +183,7 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, value?: V, count = 1): boolean { | ||
override add( | ||
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V, | ||
count = 1 | ||
): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count); | ||
@@ -200,3 +206,3 @@ if (newNode === undefined) return false; | ||
* nodes and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition for deleting a node from the | ||
@@ -215,3 +221,3 @@ * binary tree. It can be a key, node, or entry that determines which | ||
override delete( | ||
keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, | ||
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
ignoreCount = false | ||
@@ -289,2 +295,3 @@ ): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[] { | ||
* Space Complexity: O(log n) | ||
* | ||
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search | ||
@@ -388,4 +395,4 @@ * tree using either a recursive or iterative approach. | ||
* a node object. | ||
* @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`. | ||
* @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the | ||
@@ -399,3 +406,3 @@ * `override` function. It represents the value associated with the key in the data structure. If no | ||
protected override _keyValueNodeOrEntryToNodeAndValue( | ||
keyNodeOrEntry: BTNRep<K, V, AVLTreeCounterNode<K, V>>, | ||
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V, | ||
@@ -402,0 +409,0 @@ count = 1 |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types'; | ||
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull } from '../../types'; | ||
import { AVLTree, AVLTreeNode } from './avl-tree'; | ||
@@ -14,2 +14,4 @@ import { IBinaryTree } from '../../interfaces'; | ||
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> { | ||
override parent?: AVLTreeMultiMapNode<K, V> = undefined; | ||
/** | ||
@@ -28,11 +30,9 @@ * This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
override parent?: AVLTreeMultiMapNode<K, V> = undefined; | ||
override _left?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> { | ||
override get left(): AVLTreeMultiMapNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) { | ||
override set left(v: AVLTreeMultiMapNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -44,9 +44,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined; | ||
override _right?: AVLTreeMultiMapNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> { | ||
override get right(): AVLTreeMultiMapNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) { | ||
override set right(v: AVLTreeMultiMapNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -78,3 +78,5 @@ v.parent = this; | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R | ||
> = [], | ||
options?: AVLTreeMultiMapOptions<K, V[], R> | ||
@@ -125,3 +127,5 @@ ) { | ||
override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean; | ||
override add( | ||
node: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | ||
): boolean; | ||
@@ -136,3 +140,3 @@ override add(key: K, value: V): boolean; | ||
* tree multi-map. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it | ||
@@ -147,3 +151,6 @@ * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean { | ||
override add( | ||
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K, | ||
value?: V | ||
): boolean { | ||
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry); | ||
@@ -191,3 +198,3 @@ | ||
* structure and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value | ||
@@ -203,3 +210,6 @@ * pair in the AVLTreeMultiMapNode, or just the key itself. | ||
*/ | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean { | ||
deleteValue( | ||
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K, | ||
value: V | ||
): boolean { | ||
const values = this.get(keyNodeOrEntry); | ||
@@ -206,0 +216,0 @@ if (Array.isArray(values)) { |
@@ -9,13 +9,8 @@ /** | ||
import { BST, BSTNode } from './bst'; | ||
import type { | ||
AVLTreeOptions, | ||
BinaryTreeDeleteResult, | ||
BSTNOptKeyOrNode, | ||
BTNRep, | ||
EntryCallback, | ||
OptNodeOrNull | ||
} from '../../types'; | ||
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types'; | ||
import { IBinaryTree } from '../../interfaces'; | ||
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
override parent?: AVLTreeNode<K, V> = undefined; | ||
/** | ||
@@ -34,11 +29,9 @@ * This TypeScript constructor function initializes an instance with a key and an optional value. | ||
override parent?: AVLTreeNode<K, V> = undefined; | ||
override _left?: AVLTreeNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<AVLTreeNode<K, V>> { | ||
override get left(): AVLTreeNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<AVLTreeNode<K, V>>) { | ||
override set left(v: AVLTreeNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -50,9 +43,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined; | ||
override _right?: AVLTreeNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<AVLTreeNode<K, V>> { | ||
override get right(): AVLTreeNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<AVLTreeNode<K, V>>) { | ||
override set right(v: AVLTreeNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -82,3 +75,4 @@ v.parent = this; | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is | ||
* iterable that can contain either ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is | ||
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided | ||
@@ -91,3 +85,5 @@ * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V, | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R | ||
> = [], | ||
options?: AVLTreeOptions<K, V, R> | ||
@@ -141,8 +137,11 @@ ) { | ||
* The function checks if the input is an instance of AVLTreeNode. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `AVLTreeNode` class. | ||
*/ | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V> { | ||
override isNode( | ||
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): keyNodeOrEntry is AVLTreeNode<K, V> { | ||
return keyNodeOrEntry instanceof AVLTreeNode; | ||
@@ -157,4 +156,5 @@ } | ||
* structure, then balances the path. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept values of type `R`, ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -164,3 +164,6 @@ * the key or node being added to the data structure. | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean { | ||
override add( | ||
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V | ||
): boolean { | ||
if (keyNodeOrEntry === null) return false; | ||
@@ -178,3 +181,3 @@ const inserted = super.add(keyNodeOrEntry, value); | ||
* balances the tree if necessary. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method can be one of the following types: | ||
@@ -186,3 +189,5 @@ * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete` | ||
*/ | ||
override delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] { | ||
override delete( | ||
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] { | ||
const deletedResults = super.delete(keyNodeOrEntry); | ||
@@ -500,6 +505,7 @@ for (const { needBalanced } of deletedResults) { | ||
* to restore balance in an AVL tree after inserting a node. | ||
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or | ||
* `BTNRep<K, V, AVLTreeNode<K, V>>`. | ||
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or | ||
* ` | ||
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
*/ | ||
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void { | ||
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void { | ||
node = this.ensureNode(node); | ||
@@ -506,0 +512,0 @@ const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n) |
@@ -20,4 +20,3 @@ /** | ||
NodePredicate, | ||
OptNode, | ||
OptNodeOrNull | ||
OptNode | ||
} from '../../types'; | ||
@@ -31,2 +30,4 @@ import { BinaryTree, BinaryTreeNode } from './binary-tree'; | ||
export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> { | ||
override parent?: BSTNode<K, V> = undefined; | ||
/** | ||
@@ -45,11 +46,9 @@ * This TypeScript constructor function initializes an instance with a key and an optional value. | ||
override parent?: BSTNode<K, V> = undefined; | ||
override _left?: BSTNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<BSTNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<BSTNode<K, V>> { | ||
override get left(): BSTNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<BSTNode<K, V>>) { | ||
override set left(v: BSTNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -61,9 +60,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<BSTNode<K, V>> = undefined; | ||
override _right?: BSTNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<BSTNode<K, V>> { | ||
override get right(): BSTNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<BSTNode<K, V>>) { | ||
override set right(v: BSTNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -110,5 +109,5 @@ v.parent = this; | ||
* const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [10, 5, 7] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['10', '12', '5', '7'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [10, 5, 7] | ||
* console.log(bst.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12'] | ||
* console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10] | ||
* console.log(bst.rangeSearch([15, 20])); // [15, 18] | ||
@@ -150,3 +149,3 @@ * console.log(bst.search(new Range(15, 20, false))); // [18] | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain elements of type `BTNRep<K, V, BSTNode<K, V>>` or `R`. It is used to | ||
* iterable that can contain elements of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` or `R`. It is used to | ||
* initialize the binary search tree with keys, nodes, entries, or raw data. | ||
@@ -156,3 +155,8 @@ * @param [options] - The `options` parameter is an optional object that can contain the following | ||
*/ | ||
constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, BSTNode<K, V>> | R> = [], options?: BSTOptions<K, V, R>) { | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R | ||
> = [], | ||
options?: BSTOptions<K, V, R> | ||
) { | ||
super([], options); | ||
@@ -253,3 +257,3 @@ | ||
* it doesn't exist. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R`, which represents the key, node, | ||
@@ -264,3 +268,3 @@ * entry, or raw element that needs to be ensured in the tree. | ||
override ensureNode( | ||
keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, | ||
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
iterationType: IterationType = this.iterationType | ||
@@ -276,8 +280,10 @@ ): OptNode<BSTNode<K, V>> { | ||
* The function checks if the input is an instance of the BSTNode class. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `BSTNode` class. | ||
*/ | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>): keyNodeOrEntry is BSTNode<K, V> { | ||
override isNode( | ||
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): keyNodeOrEntry is BSTNode<K, V> { | ||
return keyNodeOrEntry instanceof BSTNode; | ||
@@ -306,4 +312,4 @@ } | ||
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `. | ||
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the | ||
@@ -313,3 +319,6 @@ * key in the binary search tree. If provided, it will be stored in the node along with the key. | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, value?: V): boolean { | ||
override add( | ||
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V | ||
): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
@@ -398,3 +407,3 @@ if (newNode === undefined) return false; | ||
const realBTNExemplars: { | ||
key: R | BTNRep<K, V, BSTNode<K, V>>; | ||
key: R | K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined; | ||
value: V | undefined; | ||
@@ -410,3 +419,7 @@ orgIndex: number; | ||
let sorted: { key: R | BTNRep<K, V, BSTNode<K, V>>; value: V | undefined; orgIndex: number }[] = []; | ||
let sorted: { | ||
key: R | K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined; | ||
value: V | undefined; | ||
orgIndex: number; | ||
}[] = []; | ||
@@ -435,3 +448,9 @@ sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => { | ||
const _dfs = (arr: { key: R | BTNRep<K, V, BSTNode<K, V>>; value: V | undefined; orgIndex: number }[]) => { | ||
const _dfs = ( | ||
arr: { | ||
key: R | K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined; | ||
value: V | undefined; | ||
orgIndex: number; | ||
}[] | ||
) => { | ||
if (arr.length === 0) return; | ||
@@ -491,3 +510,3 @@ | ||
* on specified criteria. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The | ||
* `keyNodeEntryOrPredicate` parameter in the `override search` method can accept one of the | ||
@@ -500,5 +519,5 @@ * following types: | ||
* that will be called on each node that matches the search criteria. It is of type `C`, which | ||
* extends `NodeCallback<BSTNode<K, V>>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* extends `NodeCallback<BSTNode<K, V> | null>`. The callback function should accept a node of type `BSTNode<K, V>` as its | ||
* argument and | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `override search` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `override search` | ||
* method represents the node from which the search operation will begin. It is the starting point | ||
@@ -516,6 +535,13 @@ * for searching within the tree data structure. The method ensures that the `startNode` is a valid | ||
override search<C extends NodeCallback<BSTNode<K, V>>>( | ||
keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>> | Range<K>, | ||
keyNodeEntryOrPredicate: | ||
| K | ||
| BSTNode<K, V> | ||
| [K | null | undefined, V | undefined] | ||
| null | ||
| undefined | ||
| NodePredicate<BSTNode<K, V>> | ||
| Range<K>, | ||
onlyOne = false, | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -532,7 +558,12 @@ ): ReturnType<C>[] { | ||
if (isRange) { | ||
predicate = node => keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
predicate = node => { | ||
if (!node) return false; | ||
return keyNodeEntryOrPredicate.isInRange(node.key, this._comparator); | ||
}; | ||
} else { | ||
predicate = this._ensurePredicate(keyNodeEntryOrPredicate); | ||
} | ||
const isToLeftByRange = (cur: BSTNode<K, V>) => { | ||
const shouldVisitLeft = (cur: BSTNode<K, V> | null | undefined) => { | ||
if (!cur) return false; | ||
if (!this.isRealNode(cur.left)) return false; | ||
if (isRange) { | ||
@@ -544,6 +575,12 @@ const range = keyNodeEntryOrPredicate; | ||
} | ||
return false; | ||
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) > 0; | ||
} | ||
return true; | ||
}; | ||
const isToRightByRange = (cur: BSTNode<K, V>) => { | ||
const shouldVisitRight = (cur: BSTNode<K, V> | null | undefined) => { | ||
if (!cur) return false; | ||
if (!this.isRealNode(cur.right)) return false; | ||
if (isRange) { | ||
@@ -556,75 +593,23 @@ const range = keyNodeEntryOrPredicate; | ||
} | ||
return false; | ||
if (!isRange && !this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
return benchmarkKey !== null && benchmarkKey !== undefined && this._compare(cur.key, benchmarkKey) < 0; | ||
} | ||
return true; | ||
}; | ||
const ans: ReturnType<C>[] = []; | ||
if (iterationType === 'RECURSIVE') { | ||
const dfs = (cur: BSTNode<K, V>) => { | ||
if (predicate(cur)) { | ||
ans.push(callback(cur)); | ||
if (onlyOne) return; | ||
} | ||
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return; | ||
if (isRange) { | ||
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) dfs(cur.left); | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) dfs(cur.right); | ||
} else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if ( | ||
this.isRealNode(cur.left) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) > 0 | ||
) | ||
dfs(cur.left); | ||
if ( | ||
this.isRealNode(cur.right) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) < 0 | ||
) | ||
dfs(cur.right); | ||
} else { | ||
if (this.isRealNode(cur.left)) dfs(cur.left); | ||
if (this.isRealNode(cur.right)) dfs(cur.right); | ||
} | ||
}; | ||
dfs(startNode); | ||
} else { | ||
const stack = [startNode]; | ||
while (stack.length > 0) { | ||
const cur = stack.pop()!; | ||
if (predicate(cur)) { | ||
ans.push(callback(cur)); | ||
if (onlyOne) return ans; | ||
} | ||
if (isRange) { | ||
if (this.isRealNode(cur.left) && isToLeftByRange(cur)) stack.push(cur.left); | ||
if (this.isRealNode(cur.right) && isToRightByRange(cur)) stack.push(cur.right); | ||
} else if (!this._isPredicate(keyNodeEntryOrPredicate)) { | ||
const benchmarkKey = this._extractKey(keyNodeEntryOrPredicate); | ||
if ( | ||
this.isRealNode(cur.right) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) < 0 | ||
) | ||
stack.push(cur.right); | ||
if ( | ||
this.isRealNode(cur.left) && | ||
benchmarkKey !== null && | ||
benchmarkKey !== undefined && | ||
this._compare(cur.key, benchmarkKey) > 0 | ||
) | ||
stack.push(cur.left); | ||
} else { | ||
if (this.isRealNode(cur.right)) stack.push(cur.right); | ||
if (this.isRealNode(cur.left)) stack.push(cur.left); | ||
} | ||
return super._dfs( | ||
callback, | ||
'IN', | ||
onlyOne, | ||
startNode, | ||
iterationType, | ||
false, | ||
shouldVisitLeft, | ||
shouldVisitRight, | ||
() => true, | ||
cur => { | ||
if (cur) return predicate(cur); | ||
return false; | ||
} | ||
} | ||
return ans; | ||
); | ||
} | ||
@@ -641,5 +626,5 @@ | ||
* function that is used to process each node that is found within the specified range during the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V>>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* search operation. It is of type `NodeCallback<BSTNode<K, V> | null>`, where `BSTNode<K, V>` is the type of nodes in the | ||
* data structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter in the `rangeSearch` | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter in the `rangeSearch` | ||
* function represents the node from which the search for nodes within the specified range will | ||
@@ -657,3 +642,3 @@ * begin. It is the starting point for the range search operation. | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -670,4 +655,4 @@ ) { | ||
* This function retrieves a node based on a given keyNodeEntryOrPredicate within a binary search tree structure. | ||
* @param {BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `BTNRep<K, V, BSTNode<K, V>>`, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>} keyNodeEntryOrPredicate - The `keyNodeEntryOrPredicate` | ||
* parameter can be of type `K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `, `R`, or `NodePredicate<BSTNode<K, V>>`. | ||
* @param {BSTNOptKeyOrNode<K, BSTNode<K, V>>} startNode - The `startNode` parameter in the `getNode` method | ||
@@ -687,3 +672,9 @@ * is used to specify the starting point for searching nodes in the binary search tree. If no | ||
override getNode( | ||
keyNodeEntryOrPredicate: BTNRep<K, V, BSTNode<K, V>> | NodePredicate<BSTNode<K, V>>, | ||
keyNodeEntryOrPredicate: | ||
| K | ||
| BSTNode<K, V> | ||
| [K | null | undefined, V | undefined] | ||
| null | ||
| undefined | ||
| NodePredicate<BSTNode<K, V>>, | ||
startNode: BSTNOptKeyOrNode<K, BSTNode<K, V>> = this._root, | ||
@@ -699,17 +690,23 @@ iterationType: IterationType = this.iterationType | ||
* | ||
* The function overrides the depth-first search method and returns an array of the return types of | ||
* the callback function. | ||
* The function `dfs` in TypeScript overrides the base class method with default parameters and | ||
* returns the result of the super class `dfs` method. | ||
* @param {C} callback - The `callback` parameter is a function that will be called for each node | ||
* during the depth-first search traversal. It is an optional parameter and defaults to | ||
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function. | ||
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the | ||
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can | ||
* take one of the following values: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a | ||
* node entry. If not specified, the default value is the root of the tree. | ||
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the | ||
* type of iteration to be used during the Depth-First Search (DFS) traversal. It can have one of the | ||
* following values: | ||
* @returns The method is returning an array of the return type of the callback function. | ||
* visited during the Depth-First Search traversal. It is a generic type `C` that extends the | ||
* `NodeCallback` interface for `BSTNode<K, V>`. The default value for `callback` is `this._ | ||
* @param {DFSOrderPattern} [pattern=IN] - The `pattern` parameter in the `override dfs` method | ||
* specifies the order in which the Depth-First Search (DFS) traversal should be performed on the | ||
* Binary Search Tree (BST). The possible values for the `pattern` parameter are: | ||
* @param {boolean} [onlyOne=false] - The `onlyOne` parameter in the `override dfs` method is a | ||
* boolean flag that indicates whether you want to stop the depth-first search traversal after | ||
* finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set | ||
* to `true`, the traversal will stop after finding | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} startNode - | ||
* The `startNode` parameter in the `override dfs` method can be one of the following types: | ||
* @param {IterationType} iterationType - The `iterationType` parameter in the `override dfs` method | ||
* specifies the type of iteration to be performed during the Depth-First Search (DFS) traversal of a | ||
* Binary Search Tree (BST). It is used to determine the order in which nodes are visited during the | ||
* traversal. The possible values for ` | ||
* @returns The `override` function is returning the result of calling the `dfs` method from the | ||
* superclass, with the provided arguments `callback`, `pattern`, `onlyOne`, `startNode`, and | ||
* `iterationType`. The return type is an array of the return type of the callback function `C`. | ||
*/ | ||
@@ -719,6 +716,7 @@ override dfs<C extends NodeCallback<BSTNode<K, V>>>( | ||
pattern: DFSOrderPattern = 'IN', | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
onlyOne: boolean = false, | ||
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
): ReturnType<C>[] { | ||
return super.dfs(callback, pattern, startNode, iterationType); | ||
return super.dfs(callback, pattern, onlyOne, startNode, iterationType); | ||
} | ||
@@ -735,3 +733,3 @@ | ||
* node being visited, and it can return a value of any type. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry | ||
@@ -746,3 +744,3 @@ * object. If no value is provided, the default value is the root of the tree. | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -760,5 +758,5 @@ ): ReturnType<C>[] { | ||
* @param {C} callback - The `callback` parameter is a generic type `C` that extends | ||
* `NodeCallback<BSTNode<K, V>>`. It represents a callback function that will be called for each node in the | ||
* `NodeCallback<BSTNode<K, V> | null>`. It represents a callback function that will be called for each node in the | ||
* tree during the iteration process. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} startNode - The `startNode` parameter is the starting | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } startNode - The `startNode` parameter is the starting | ||
* point for listing the levels of the binary tree. It can be either a root node of the tree, a | ||
@@ -774,3 +772,3 @@ * key-value pair representing a node in the tree, or a key representing a node in the tree. If no | ||
callback: C = this._DEFAULT_NODE_CALLBACK as C, | ||
startNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
startNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -793,3 +791,3 @@ ): ReturnType<C>[][] { | ||
* 0, or 1, where: | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} targetNode - The `targetNode` parameter is the node in | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } targetNode - The `targetNode` parameter is the node in | ||
* the binary tree that you want to start traversing from. It can be specified either by providing | ||
@@ -806,3 +804,3 @@ * the key of the node, the node itself, or an entry containing the key and value of the node. If no | ||
lesserOrGreater: CP = -1, | ||
targetNode: BTNRep<K, V, BSTNode<K, V>> = this._root, | ||
targetNode: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined = this._root, | ||
iterationType: IterationType = this.iterationType | ||
@@ -867,4 +865,4 @@ ): ReturnType<C>[] { | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key); | ||
else this.add([midNode.key, midNode.value]); | ||
if (this._isMapMode && midNode !== null) this.add(midNode.key); | ||
else if (midNode !== null) this.add([midNode.key, midNode.value]); | ||
buildBalanceBST(l, m - 1); | ||
@@ -885,4 +883,4 @@ buildBalanceBST(m + 1, r); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key); | ||
else this.add([midNode.key, midNode.value]); | ||
if (this._isMapMode && midNode !== null) this.add(midNode.key); | ||
else if (midNode !== null) this.add([midNode.key, midNode.value]); | ||
stack.push([m + 1, r]); | ||
@@ -915,3 +913,3 @@ stack.push([l, m - 1]); | ||
if (iterationType === 'RECURSIVE') { | ||
const _height = (cur: OptNodeOrNull<BSTNode<K, V>>): number => { | ||
const _height = (cur: BSTNode<K, V> | null | undefined): number => { | ||
if (!cur) return 0; | ||
@@ -1005,4 +1003,4 @@ const leftHeight = _height(cur.left), | ||
* The function overrides a method and converts a key, value pair or entry or raw element to a node. | ||
* @param {BTNRep<K, V, BSTNode<K, V>>} keyNodeOrEntry - A variable that can be of | ||
* type R or BTNRep<K, V, BSTNode<K, V>>. It represents either a key, a node, an entry, or a raw | ||
* @param {K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - A variable that can be of | ||
* type R or K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined . It represents either a key, a node, an entry, or a raw | ||
* element. | ||
@@ -1014,3 +1012,3 @@ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the | ||
protected override _keyValueNodeOrEntryToNodeAndValue( | ||
keyNodeOrEntry: BTNRep<K, V, BSTNode<K, V>>, | ||
keyNodeOrEntry: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V | ||
@@ -1017,0 +1015,0 @@ ): [OptNode<BSTNode<K, V>>, V | undefined] { |
@@ -1,11 +0,2 @@ | ||
import type { | ||
BinaryTreeDeleteResult, | ||
BTNRep, | ||
CRUD, | ||
EntryCallback, | ||
OptNode, | ||
OptNodeOrNull, | ||
RBTNColor, | ||
RedBlackTreeOptions | ||
} from '../../types'; | ||
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, OptNode, RBTNColor, RedBlackTreeOptions } from '../../types'; | ||
import { BST, BSTNode } from './bst'; | ||
@@ -15,2 +6,4 @@ import { IBinaryTree } from '../../interfaces'; | ||
export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> { | ||
override parent?: RedBlackTreeNode<K, V> = undefined; | ||
/** | ||
@@ -31,11 +24,9 @@ * The constructor initializes a node with a key, value, and color for a Red-Black Tree. | ||
override parent?: RedBlackTreeNode<K, V> = undefined; | ||
override _left?: RedBlackTreeNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<RedBlackTreeNode<K, V>> { | ||
override get left(): RedBlackTreeNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) { | ||
override set left(v: RedBlackTreeNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -47,9 +38,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined; | ||
override _right?: RedBlackTreeNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<RedBlackTreeNode<K, V>> { | ||
override get right(): RedBlackTreeNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) { | ||
override set right(v: RedBlackTreeNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -66,8 +57,2 @@ v.parent = this; | ||
* @example | ||
* // Find elements in a range | ||
* const bst = new RedBlackTree<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(bst.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(bst.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(bst.search(new Range(15, 20))); // [15, 18] | ||
* @example | ||
* // using Red-Black Tree as a price-based index for stock data | ||
@@ -114,3 +99,3 @@ * // Define the structure of individual stock records | ||
* ); | ||
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META'] | ||
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT'] | ||
*/ | ||
@@ -125,3 +110,3 @@ export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> | ||
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an | ||
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It | ||
* iterable that can contain either `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined` objects or `R` objects. It | ||
* is used to initialize the Red-Black Tree with keys, nodes, entries, or | ||
@@ -134,3 +119,5 @@ * @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K, | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R | ||
> = [], | ||
options?: RedBlackTreeOptions<K, V, R> | ||
@@ -198,8 +185,10 @@ ) { | ||
* The function checks if the input is an instance of the RedBlackTreeNode class. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `RedBlackTreeNode` class. | ||
*/ | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V> { | ||
override isNode( | ||
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): keyNodeOrEntry is RedBlackTreeNode<K, V> { | ||
return keyNodeOrEntry instanceof RedBlackTreeNode; | ||
@@ -226,4 +215,4 @@ } | ||
* added. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`. | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can accept a value of type `R` or `K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with | ||
@@ -236,3 +225,6 @@ * the key in the data structure. It represents the value that you want to add or update in the data | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean { | ||
override add( | ||
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V | ||
): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value); | ||
@@ -267,3 +259,3 @@ if (!this.isRealNode(newNode)) return false; | ||
* a given predicate and maintain the binary search tree properties. | ||
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override delete` method is used to specify the condition or key based on which a | ||
@@ -277,3 +269,3 @@ * node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate | ||
override delete( | ||
keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>> | ||
keyNodeOrEntry: K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] { | ||
@@ -280,0 +272,0 @@ if (keyNodeOrEntry === null) return []; |
@@ -11,7 +11,5 @@ /** | ||
BSTNOptKeyOrNode, | ||
BTNRep, | ||
EntryCallback, | ||
IterationType, | ||
OptNode, | ||
OptNodeOrNull, | ||
RBTNColor, | ||
@@ -24,2 +22,4 @@ TreeCounterOptions | ||
export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> { | ||
override parent?: TreeCounterNode<K, V> = undefined; | ||
/** | ||
@@ -42,11 +42,9 @@ * The constructor function initializes a Red-Black Tree node with a key, value, count, and color. | ||
override parent?: TreeCounterNode<K, V> = undefined; | ||
override _left?: TreeCounterNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<TreeCounterNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<TreeCounterNode<K, V>> { | ||
override get left(): TreeCounterNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<TreeCounterNode<K, V>>) { | ||
override set left(v: TreeCounterNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -58,9 +56,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<TreeCounterNode<K, V>> = undefined; | ||
override _right?: TreeCounterNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<TreeCounterNode<K, V>> { | ||
override get right(): TreeCounterNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<TreeCounterNode<K, V>>) { | ||
override set right(v: TreeCounterNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -90,3 +88,5 @@ v.parent = this; | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, TreeCounterNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R | ||
> = [], | ||
options?: TreeCounterOptions<K, V, R> | ||
@@ -119,3 +119,3 @@ ) { | ||
let sum = 0; | ||
this.dfs(node => (sum += node.count)); | ||
this.dfs(node => (sum += node ? node.count : 0)); | ||
return sum; | ||
@@ -161,8 +161,10 @@ } | ||
* The function checks if the input is an instance of the TreeCounterNode class. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is | ||
* an instance of the `TreeCounterNode` class. | ||
*/ | ||
override isNode(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>): keyNodeOrEntry is TreeCounterNode<K, V> { | ||
override isNode( | ||
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | ||
): keyNodeOrEntry is TreeCounterNode<K, V> { | ||
return keyNodeOrEntry instanceof TreeCounterNode; | ||
@@ -177,3 +179,3 @@ } | ||
* the count and returning a boolean indicating success. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The | ||
* `keyNodeOrEntry` parameter can accept one of the following types: | ||
@@ -188,3 +190,7 @@ * @param {V} [value] - The `value` parameter represents the value associated with the key in the | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, value?: V, count = 1): boolean { | ||
override add( | ||
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V, | ||
count = 1 | ||
): boolean { | ||
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value, count); | ||
@@ -208,3 +214,3 @@ const orgCount = newNode?.count || 0; | ||
* structure, handling cases where nodes have children and maintaining balance in the tree. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate` | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate` | ||
* parameter in the `delete` method is used to specify the condition or key based on which a node | ||
@@ -219,3 +225,3 @@ * should be deleted from the binary tree. It can be a key, a node, or an entry. | ||
override delete( | ||
keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, | ||
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
ignoreCount = false | ||
@@ -354,4 +360,4 @@ ): BinaryTreeDeleteResult<TreeCounterNode<K, V>>[] { | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
if (this._isMapMode && midNode !== null) this.add(midNode.key, undefined, midNode.count); | ||
else if (midNode !== null) this.add(midNode.key, midNode.value, midNode.count); | ||
buildBalanceBST(l, m - 1); | ||
@@ -372,4 +378,4 @@ buildBalanceBST(m + 1, r); | ||
const midNode = sorted[m]; | ||
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count); | ||
else this.add(midNode.key, midNode.value, midNode.count); | ||
if (this._isMapMode && midNode !== null) this.add(midNode.key, undefined, midNode.count); | ||
else if (midNode !== null) this.add(midNode.key, midNode.value, midNode.count); | ||
stack.push([m + 1, r]); | ||
@@ -393,3 +399,3 @@ stack.push([l, m - 1]); | ||
const cloned = this.createTree(); | ||
this.bfs(node => cloned.add(node.key, undefined, node.count)); | ||
this.bfs(node => cloned.add(node === null ? null : node.key, undefined, node === null ? 0 : node.count)); | ||
if (this._isMapMode) cloned._store = this._store; | ||
@@ -430,4 +436,4 @@ return cloned; | ||
* node based on the input. | ||
* @param {BTNRep<K, V, TreeCounterNode<K, V>>} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, TreeCounterNode<K, V>>`. | ||
* @param {K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter | ||
* `keyNodeOrEntry` can be of type `R` or `K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`. | ||
* @param {V} [value] - The `value` parameter is an optional value that represents the value | ||
@@ -441,3 +447,3 @@ * associated with the key in the node. It is used when creating a new node or updating the value of | ||
protected override _keyValueNodeOrEntryToNodeAndValue( | ||
keyNodeOrEntry: BTNRep<K, V, TreeCounterNode<K, V>>, | ||
keyNodeOrEntry: K | TreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, | ||
value?: V, | ||
@@ -444,0 +450,0 @@ count = 1 |
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
import type { BTNOptKeyOrNull, BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types'; | ||
import type { BTNOptKeyOrNull, TreeMultiMapOptions } from '../../types'; | ||
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree'; | ||
@@ -14,2 +14,4 @@ import { IBinaryTree } from '../../interfaces'; | ||
export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> { | ||
override parent?: TreeMultiMapNode<K, V> = undefined; | ||
/** | ||
@@ -28,11 +30,9 @@ * This TypeScript constructor initializes an object with a key of type K and an array of values of | ||
override parent?: TreeMultiMapNode<K, V> = undefined; | ||
override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined; | ||
override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined; | ||
override get left(): OptNodeOrNull<TreeMultiMapNode<K, V>> { | ||
override get left(): TreeMultiMapNode<K, V> | null | undefined { | ||
return this._left; | ||
} | ||
override set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) { | ||
override set left(v: TreeMultiMapNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -44,9 +44,9 @@ v.parent = this; | ||
override _right?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined; | ||
override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined; | ||
override get right(): OptNodeOrNull<TreeMultiMapNode<K, V>> { | ||
override get right(): TreeMultiMapNode<K, V> | null | undefined { | ||
return this._right; | ||
} | ||
override set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) { | ||
override set right(v: TreeMultiMapNode<K, V> | null | undefined) { | ||
if (v) { | ||
@@ -64,4 +64,4 @@ v.parent = this; | ||
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]); | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7] | ||
* console.log(tmm.search(new Range(5, 10))); // [5, 7, 10] | ||
* console.log(tmm.search(new Range(4, 12))); // [5, 7, 10, 12] | ||
* console.log(tmm.search(new Range(15, 20))); // [15, 18] | ||
@@ -85,3 +85,5 @@ */ | ||
constructor( | ||
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R> = [], | ||
keysNodesEntriesOrRaws: Iterable< | ||
K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R | ||
> = [], | ||
options?: TreeMultiMapOptions<K, V[], R> | ||
@@ -133,3 +135,3 @@ ) { | ||
override add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean; | ||
override add(node: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean; | ||
@@ -144,3 +146,3 @@ override add(key: K, value: V): boolean; | ||
* TreeMultiMapNode, handling different input types and scenarios. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array | ||
@@ -154,3 +156,6 @@ * of values, and a `TreeMultiMapNode`, or just a key. | ||
*/ | ||
override add(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value?: V): boolean { | ||
override add( | ||
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, | ||
value?: V | ||
): boolean { | ||
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry); | ||
@@ -198,3 +203,3 @@ | ||
* and deletes the entire node if no values are left for that key. | ||
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* @param {K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined} keyNodeOrEntry - The `keyNodeOrEntry` | ||
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an | ||
@@ -209,3 +214,6 @@ * array of values, or just a key itself. | ||
*/ | ||
deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean { | ||
deleteValue( | ||
keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, | ||
value: V | ||
): boolean { | ||
const values = this.get(keyNodeOrEntry); | ||
@@ -212,0 +220,0 @@ if (Array.isArray(values)) { |
@@ -10,2 +10,3 @@ import { IterationType, OptValue } from '../../common'; | ||
isMapMode?: boolean; | ||
isDuplicate?: boolean; | ||
} | ||
@@ -12,0 +13,0 @@ |
@@ -5,3 +5,3 @@ import type { BinaryTreeOptions } from './binary-tree'; | ||
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & { | ||
export type BSTOptions<K, V, R> = Omit<BinaryTreeOptions<K, V, R>, 'isDuplicate'> & { | ||
specifyComparable?: (key: K) => Comparable | ||
@@ -8,0 +8,0 @@ isReverse?: boolean; |
/** | ||
* data-structure-typed | ||
* | ||
* @author Tyler Zeng | ||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com> | ||
* @author Pablo Zeng | ||
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com> | ||
* @license MIT License | ||
@@ -7,0 +7,0 @@ */ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
2241539
45264
Updateddata-structure-typed@^1.54.3