diff --git a/README.md b/README.md
index b3d976ff..f79e615d 100644
--- a/README.md
+++ b/README.md
@@ -984,52 +984,40 @@ avl2.print();
[//]: # (No deletion!!! Start of Replace Section)
avl-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 48.42 | 20.65 | 0.00 |
10,000 add & delete randomly | 107.72 | 9.28 | 0.02 |
10,000 addMany | 55.40 | 18.05 | 6.22e-4 |
10,000 get | 53.89 | 18.56 | 0.02 |
-
-
binary-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 add randomly | 17.75 | 56.35 | 2.23e-4 |
1,000 add & delete randomly | 25.58 | 39.10 | 0.01 |
1,000 addMany | 19.65 | 50.89 | 0.00 |
1,000 get | 21.03 | 47.55 | 0.01 |
1,000 has | 19.81 | 50.48 | 0.01 |
1,000 dfs | 183.37 | 5.45 | 0.03 |
1,000 bfs | 65.61 | 15.24 | 0.02 |
1,000 morris | 231.00 | 4.33 | 0.06 |
-
-
bst
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 49.96 | 20.02 | 7.65e-4 |
10,000 add & delete randomly | 116.77 | 8.56 | 0.02 |
10,000 addMany | 49.06 | 20.38 | 0.01 |
10,000 get | 49.06 | 20.38 | 7.13e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 add randomly | 118.62 | 8.43 | 0.00 |
10,000 add & delete randomly | 179.74 | 5.56 | 0.00 |
10,000 addMany | 154.34 | 6.48 | 0.04 |
10,000 get | 50.53 | 19.79 | 0.00 |
rb-tree
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add | 83.42 | 11.99 | 0.01 |
100,000 add & delete randomly | 243.05 | 4.11 | 0.07 |
100,000 getNode | 218.87 | 4.57 | 0.05 |
100,000 add & iterator | 124.22 | 8.05 | 0.01 |
-
-
comparison
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
SRC PQ 10,000 add | 0.15 | 6710.40 | 1.90e-5 |
CJS PQ 10,000 add | 0.16 | 6407.42 | 4.25e-5 |
MJS PQ 10,000 add | 0.62 | 1602.37 | 1.51e-4 |
SRC PQ 10,000 add & poll | 3.67 | 272.42 | 0.00 |
CJS PQ 10,000 add & poll | 3.95 | 252.90 | 0.00 |
MJS PQ 10,000 add & poll | 3.33 | 300.28 | 8.65e-5 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add | 90.63 | 11.03 | 0.01 |
100,000 add & delete randomly | 225.64 | 4.43 | 0.01 |
100,000 getNode | 174.39 | 5.73 | 0.00 |
100,000 add & iterator | 120.94 | 8.27 | 0.02 |
directed-graph
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.11 | 8958.10 | 3.30e-5 |
1,000 addEdge | 6.37 | 156.98 | 1.96e-4 |
1,000 getVertex | 0.05 | 2.04e+4 | 8.22e-6 |
1,000 getEdge | 24.49 | 40.83 | 0.00 |
tarjan | 235.54 | 4.25 | 0.04 |
tarjan all | 6766.74 | 0.15 | 0.32 |
topologicalSort | 197.52 | 5.06 | 0.04 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000 addVertex | 0.10 | 9850.83 | 1.41e-6 |
1,000 addEdge | 6.21 | 160.97 | 2.65e-4 |
1,000 getVertex | 0.05 | 1.91e+4 | 2.18e-5 |
1,000 getEdge | 26.38 | 37.90 | 0.00 |
tarjan | 233.90 | 4.28 | 0.02 |
tarjan all | 7008.22 | 0.14 | 0.34 |
topologicalSort | 206.12 | 4.85 | 0.02 |
hash-map
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 set | 117.82 | 8.49 | 0.05 |
Native Map 1,000,000 set | 220.92 | 4.53 | 0.03 |
Native Set 1,000,000 add | 187.44 | 5.34 | 0.02 |
1,000,000 set & get | 125.44 | 7.97 | 0.04 |
Native Map 1,000,000 set & get | 300.10 | 3.33 | 0.06 |
Native Set 1,000,000 add & has | 200.88 | 4.98 | 0.02 |
1,000,000 ObjKey set & get | 361.00 | 2.77 | 0.06 |
Native Map 1,000,000 ObjKey set & get | 335.34 | 2.98 | 0.09 |
Native Set 1,000,000 ObjKey add & has | 261.28 | 3.83 | 0.07 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 set | 129.16 | 7.74 | 0.05 |
Native Map 1,000,000 set | 230.98 | 4.33 | 0.05 |
Native Set 1,000,000 add | 201.94 | 4.95 | 0.05 |
1,000,000 set & get | 132.13 | 7.57 | 0.03 |
Native Map 1,000,000 set & get | 317.16 | 3.15 | 0.06 |
Native Set 1,000,000 add & has | 229.56 | 4.36 | 0.06 |
1,000,000 ObjKey set & get | 323.02 | 3.10 | 0.04 |
Native Map 1,000,000 ObjKey set & get | 301.07 | 3.32 | 0.04 |
Native Set 1,000,000 ObjKey add & has | 282.01 | 3.55 | 0.04 |
heap
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & poll | 80.43 | 12.43 | 0.00 |
100,000 add & dfs | 36.95 | 27.07 | 0.00 |
10,000 fib add & pop | 386.63 | 2.59 | 0.05 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & poll | 27.32 | 36.61 | 9.27e-4 |
100,000 add & dfs | 33.83 | 29.56 | 3.94e-4 |
10,000 fib add & pop | 353.37 | 2.83 | 0.00 |
doubly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 235.15 | 4.25 | 0.07 |
1,000,000 unshift | 245.36 | 4.08 | 0.08 |
1,000,000 unshift & shift | 175.53 | 5.70 | 0.03 |
1,000,000 addBefore | 319.21 | 3.13 | 0.06 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 220.80 | 4.53 | 0.02 |
1,000,000 unshift | 235.94 | 4.24 | 0.06 |
1,000,000 unshift & shift | 180.11 | 5.55 | 0.04 |
1,000,000 addBefore | 335.72 | 2.98 | 0.11 |
singly-linked-list
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 202.02 | 4.95 | 0.04 |
10,000 push & pop | 228.77 | 4.37 | 0.04 |
10,000 addBefore | 274.25 | 3.65 | 0.05 |
-
-
max-priority-queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
10,000 refill & poll | 9.39 | 106.51 | 0.00 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push & shift | 217.29 | 4.60 | 0.06 |
10,000 push & pop | 223.41 | 4.48 | 0.02 |
10,000 addBefore | 253.79 | 3.94 | 0.01 |
priority-queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & poll | 114.36 | 8.74 | 0.02 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 add & poll | 81.55 | 12.26 | 0.01 |
deque
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 14.81 | 67.51 | 0.00 |
1,000,000 push & pop | 25.34 | 39.47 | 0.01 |
100,000 push & shift | 2.49 | 400.86 | 7.97e-4 |
Native Array 100,000 push & shift | 2390.92 | 0.42 | 0.17 |
100,000 unshift & shift | 2.48 | 403.14 | 6.46e-4 |
Native Array 100,000 unshift & shift | 4462.41 | 0.22 | 0.34 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 14.14 | 70.71 | 4.71e-4 |
1,000,000 push & pop | 29.75 | 33.61 | 0.01 |
100,000 push & shift | 3.06 | 326.93 | 0.00 |
Native Array 100,000 push & shift | 2211.02 | 0.45 | 0.05 |
100,000 unshift & shift | 2.23 | 447.63 | 3.63e-4 |
Native Array 100,000 unshift & shift | 4586.53 | 0.22 | 0.86 |
queue
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 51.99 | 19.24 | 0.03 |
100,000 push & shift | 5.23 | 191.34 | 7.00e-4 |
Native Array 100,000 push & shift | 2400.96 | 0.42 | 0.28 |
Native Array 100,000 push & pop | 4.36 | 229.52 | 1.14e-4 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 48.44 | 20.65 | 0.01 |
100,000 push & shift | 5.28 | 189.27 | 6.59e-4 |
Native Array 100,000 push & shift | 2335.96 | 0.43 | 0.14 |
Native Array 100,000 push & pop | 4.48 | 223.08 | 5.56e-4 |
stack
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 43.55 | 22.96 | 0.01 |
1,000,000 push & pop | 55.29 | 18.09 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
1,000,000 push | 45.61 | 21.93 | 0.01 |
1,000,000 push & pop | 50.96 | 19.62 | 0.01 |
trie
-
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 48.66 | 20.55 | 0.00 |
100,000 getWords | 95.09 | 10.52 | 0.01 |
+
test name | time taken (ms) | executions per sec | sample deviation |
---|
100,000 push | 58.40 | 17.12 | 0.02 |
100,000 getWords | 99.70 | 10.03 | 0.01 |
[//]: # (No deletion!!! End of Replace Section)
diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts
index fee02e61..487c5e2e 100644
--- a/src/data-structures/binary-tree/avl-tree.ts
+++ b/src/data-structures/binary-tree/avl-tree.ts
@@ -13,8 +13,7 @@ import type {
BinaryTreeDeleteResult,
BSTNKeyOrNode,
BTNCallback,
- BTNExemplar,
- BTNKeyOrNode
+ KeyOrNodeOrEntry
} from '../../types';
import { IBinaryTree } from '../../interfaces';
@@ -49,17 +48,17 @@ export class AVLTree<
extends BST
implements IBinaryTree {
/**
- * The constructor function initializes an AVLTree object with optional elements and options.
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar`
- * objects. It represents a collection of elements that will be added to the AVL tree during
+ * The constructor function initializes an AVLTree object with optional nodes and options.
+ * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry`
+ * objects. It represents a collection of nodes that will be added to the AVL tree during
* initialization.
* @param [options] - The `options` parameter is an optional object that allows you to customize the
* behavior of the AVL tree. It is of type `Partial`, which means that you can
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
*/
- constructor(elements?: Iterable>, options?: Partial>) {
+ constructor(nodes?: Iterable>, options?: Partial>) {
super([], options);
- if (elements) super.addMany(elements);
+ if (nodes) super.addMany(nodes);
}
/**
@@ -91,12 +90,12 @@ export class AVLTree<
}
/**
- * The function checks if an exemplar is an instance of AVLTreeNode.
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`.
- * @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
+ * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`.
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
*/
- override isNode(exemplar: BTNExemplar): exemplar is N {
- return exemplar instanceof AVLTreeNode;
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry): keyOrNodeOrEntry is N {
+ return keyOrNodeOrEntry instanceof AVLTreeNode;
}
/**
@@ -105,18 +104,19 @@ export class AVLTree<
* data type.
* @returns a boolean value indicating whether the potentialKey is of type number or not.
*/
- override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K {
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry): potentialKey is K {
return !(potentialKey instanceof AVLTreeNode);
}
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ * logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
*/
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*
* The function overrides the add method of a binary tree node and balances the tree after inserting
* a new node.
@@ -126,21 +126,21 @@ export class AVLTree<
* being added to the binary tree.
* @returns The method is returning either the inserted node or undefined.
*/
- override add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | undefined {
- if (keyOrNodeOrEntry === null) return undefined;
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean {
+ if (keyOrNodeOrEntry === null) return false;
const inserted = super.add(keyOrNodeOrEntry, value);
- if (inserted) this._balancePath(inserted);
+ if (inserted) this._balancePath(keyOrNodeOrEntry);
return inserted;
}
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*/
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*
* The function overrides the delete method of a binary tree, performs the deletion, and then
* balances the tree if necessary.
@@ -203,13 +203,14 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
*/
/**
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The function calculates the balance factor of a node in a binary tree.
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
@@ -227,13 +228,14 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ * constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
*/
/**
- * Time Complexity: O(1) - constant time, as it performs a fixed number of operations.
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The function updates the height of a node in a binary tree based on the heights of its left and
* right children.
@@ -249,20 +251,22 @@ export class AVLTree<
}
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ * logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
*/
/**
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root.
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
* to restore balance in an AVL tree after inserting a node.
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
* AVL tree that needs to be balanced.
*/
- protected _balancePath(node: N): void {
+ protected _balancePath(node: KeyOrNodeOrEntry): void {
+ node = this.ensureNode(node);
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
for (let i = 0; i < path.length; i++) {
// second O(log n)
@@ -302,13 +306,14 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ * constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
*/
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
* @param {N} A - A is a node in a binary tree.
@@ -340,13 +345,13 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
* @param {N} A - A is a node in a binary tree.
@@ -396,13 +401,13 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
* @param {N} A - A is a node in a binary tree.
@@ -439,13 +444,13 @@ export class AVLTree<
}
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
- * Time Complexity: O(1) - constant time, as these methods perform a fixed number of operations.
- * Space Complexity: O(1) - constant space, as they only use a constant amount of memory.
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
* @param {N} A - A is a node in a binary tree.
diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts
index b26f4a4e..d1d720aa 100644
--- a/src/data-structures/binary-tree/binary-tree.ts
+++ b/src/data-structures/binary-tree/binary-tree.ts
@@ -14,10 +14,9 @@ import type {
BinaryTreePrintOptions,
BTNCallback,
BTNEntry,
- BTNExemplar,
- BTNKeyOrNode,
DFSOrderPattern,
EntryCallback,
+ KeyOrNodeOrEntry,
NodeDisplayLayout
} from '../../types';
import { FamilyPosition, IterationType } from '../../types';
@@ -112,15 +111,15 @@ export class BinaryTree<
iterationType = IterationType.ITERATIVE;
/**
- * The constructor function initializes a binary tree object with optional elements and options.
- * @param [elements] - An optional iterable of BTNExemplar objects. These objects represent the
- * elements to be added to the binary tree.
+ * The constructor function initializes a binary tree object with optional nodes and options.
+ * @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
+ * nodes to be added to the binary tree.
* @param [options] - The `options` parameter is an optional object that can contain additional
* configuration options for the binary tree. In this case, it is of type
* `Partial`, which means that not all properties of `BinaryTreeOptions` are
* required.
*/
- constructor(elements?: Iterable>, options?: Partial>) {
+ constructor(nodes?: Iterable>, options?: Partial>) {
super();
if (options) {
const { iterationType, extractor } = options;
@@ -134,7 +133,7 @@ export class BinaryTree<
this._size = 0;
- if (elements) this.addMany(elements);
+ if (nodes) this.addMany(nodes);
}
protected _extractor = (key: K) => Number(key);
@@ -177,30 +176,21 @@ export class BinaryTree<
}
/**
- * The function "isNode" checks if an exemplar is an instance of the BinaryTreeNode class.
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar`.
- * @returns a boolean value indicating whether the exemplar is an instance of the class N.
- */
- isNode(exemplar: BTNExemplar): exemplar is N {
- return exemplar instanceof BinaryTreeNode;
- }
-
- /**
- * The function `exemplarToNode` converts an exemplar object into a node object.
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`.
+ * The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`.
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
- * `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If no value
* is provided, it will be `undefined`.
* @returns a value of type N (node), or null, or undefined.
*/
- exemplarToNode(exemplar: BTNExemplar, value?: V): N | null | undefined {
- if (exemplar === undefined) return;
+ exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | null | undefined {
+ if (keyOrNodeOrEntry === undefined) return;
let node: N | null | undefined;
- if (exemplar === null) {
+ if (keyOrNodeOrEntry === null) {
node = null;
- } else if (this.isEntry(exemplar)) {
- const [key, value] = exemplar;
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
+ const [key, value] = keyOrNodeOrEntry;
if (key === undefined) {
return;
} else if (key === null) {
@@ -208,24 +198,112 @@ export class BinaryTree<
} else {
node = this.createNode(key, value);
}
- } else if (this.isNode(exemplar)) {
- node = exemplar;
- } else if (this.isNotNodeInstance(exemplar)) {
- node = this.createNode(exemplar, value);
+ } else if (this.isNode(keyOrNodeOrEntry)) {
+ node = keyOrNodeOrEntry;
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
+ node = this.createNode(keyOrNodeOrEntry, value);
} else {
return;
}
return node;
}
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(log n)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(log n)
+ *
+ * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
+ * key, otherwise it returns the key itself.
+ * @param {K | N | null | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`,
+ * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
+ * type of iteration to be used when searching for a node by key. It has a default value of
+ * `IterationType.ITERATIVE`.
+ * @returns either the node corresponding to the given key if it is a valid node key, or the key
+ * itself if it is not a valid node key.
+ */
+ ensureNode(
+ keyOrNodeOrEntry: KeyOrNodeOrEntry,
+ iterationType = IterationType.ITERATIVE
+ ): N | null | undefined {
+ let res: N | null | undefined;
+ if (this.isRealNode(keyOrNodeOrEntry)) {
+ res = keyOrNodeOrEntry;
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
+ if (keyOrNodeOrEntry[0] === null) res = null;
+ else if (keyOrNodeOrEntry[0] !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
+ } else {
+ if (keyOrNodeOrEntry === null) res = null;
+ else if (keyOrNodeOrEntry !== undefined) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
+ }
+ return res;
+ }
+
+ /**
+ * The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry`.
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class N.
+ */
+ isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry): keyOrNodeOrEntry is N {
+ return keyOrNodeOrEntry instanceof BinaryTreeNode;
+ }
+
/**
* The function checks if a given value is an entry in a binary tree node.
- * @param kne - BTNExemplar - A generic type representing a node in a binary tree. It has
+ * @param keyOrNodeOrEntry - KeyOrNodeOrEntry - A generic type representing a node in a binary tree. It has
* two type parameters V and N, representing the value and node type respectively.
* @returns a boolean value.
*/
- isEntry(kne: BTNExemplar): kne is BTNEntry {
- return Array.isArray(kne) && kne.length === 2;
+ isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry): keyOrNodeOrEntry is BTNEntry {
+ return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
+ }
+
+ /**
+ * Time complexity: O(n)
+ * Space complexity: O(log n)
+ */
+
+ /**
+ * The function checks if a given node is a real node by verifying if it is an instance of
+ * BinaryTreeNode and its key is not NaN.
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
+ * @returns a boolean value.
+ */
+ isRealNode(node: KeyOrNodeOrEntry): node is N {
+ return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
+ }
+
+ /**
+ * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
+ * @returns a boolean value.
+ */
+ isNIL(node: KeyOrNodeOrEntry) {
+ return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
+ }
+
+ /**
+ * The function checks if a given node is a real node or null.
+ * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
+ * @returns a boolean value.
+ */
+ isNodeOrNull(node: KeyOrNodeOrEntry): node is N | null {
+ return this.isRealNode(node) || node === null;
+ }
+
+ /**
+ * The function "isNotNodeInstance" checks if a potential key is a K.
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
+ * data type.
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
+ */
+ isNotNodeInstance(potentialKey: KeyOrNodeOrEntry): potentialKey is K {
+ return !(potentialKey instanceof BinaryTreeNode);
}
/**
@@ -243,15 +321,15 @@ export class BinaryTree<
* @param {V} [value] - The value to be inserted into the binary tree.
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
*/
- add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | null | undefined {
+ add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
- if (newNode === undefined) return;
+ if (newNode === undefined) return false;
// If the tree is empty, directly set the new node as the root node
if (!this.root) {
this._root = newNode;
this._size = 1;
- return newNode;
+ return true;
}
const queue = new Queue([this.root]);
@@ -265,7 +343,7 @@ export class BinaryTree<
// Check for duplicate keys when newNode is not null
if (newNode !== null && cur.key === newNode.key) {
this._replaceNode(cur, newNode);
- return newNode; // If duplicate keys are found, no insertion is performed
+ return true; // If duplicate keys are found, no insertion is performed
}
// Record the first possible insertion location found
@@ -290,10 +368,10 @@ export class BinaryTree<
potentialParent.right = newNode;
}
this._size++;
- return newNode;
+ return true;
}
- return undefined; // If the insertion position cannot be found, return undefined
+ return false; // If the insertion position cannot be found, return undefined
}
/**
@@ -306,22 +384,22 @@ export class BinaryTree<
* Time Complexity: O(k log n) - O(k * n)
* Space Complexity: O(1)
*
- * The `addMany` function takes in a collection of nodes and an optional collection of values, and
+ * The `addMany` function takes in a collection of keysOrNodesOrEntries and an optional collection of values, and
* adds each node with its corresponding value to the data structure.
- * @param nodes - An iterable collection of BTNExemplar objects.
+ * @param keysOrNodesOrEntries - An iterable collection of KeyOrNodeOrEntry objects.
* @param [values] - An optional iterable of values that will be assigned to each node being added.
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
*/
- addMany(nodes: Iterable>, values?: Iterable): (N | null | undefined)[] {
+ addMany(keysOrNodesOrEntries: Iterable>, values?: Iterable): boolean[] {
// TODO not sure addMany not be run multi times
- const inserted: (N | null | undefined)[] = [];
+ const inserted: boolean[] = [];
let valuesIterator: Iterator | undefined;
if (values) {
valuesIterator = values[Symbol.iterator]();
}
- for (const kne of nodes) {
+ for (const keyOrNodeOrEntry of keysOrNodesOrEntries) {
let value: V | undefined | null = undefined;
if (valuesIterator) {
@@ -331,25 +409,39 @@ export class BinaryTree<
}
}
- inserted.push(this.add(kne, value));
+ inserted.push(this.add(keyOrNodeOrEntry, value));
}
return inserted;
}
/**
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
+ * Time Complexity: O(k * n)
* Space Complexity: O(1)
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
*/
- refill(nodesOrKeysOrEntries: Iterable>, values?: Iterable): void {
+ /**
+ * Time Complexity: O(k * n)
+ * Space Complexity: O(1)
+ *
+ * The `refill` function clears the current data and adds new key-value pairs to the data structure.
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries. These can be of type
+ * KeyOrNodeOrEntry.
+ * @param [values] - The `values` parameter is an optional iterable that contains the values to be
+ * associated with the keys or nodes or entries in the `keysOrNodesOrEntries` parameter. If provided,
+ * the values will be associated with the corresponding keys or nodes or entries in the
+ * `keysOrNodesOrEntries` iterable
+ */
+ refill(keysOrNodesOrEntries: Iterable>, values?: Iterable): void {
this.clear();
- this.addMany(nodesOrKeysOrEntries, values);
+ this.addMany(keysOrNodesOrEntries, values);
}
/**
- * Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
+ * Time Complexity: O(k * n)
* Space Complexity: O(1)
+ * "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
*/
delete>(identifier: K, callback?: C): BinaryTreeDeleteResult[];
@@ -433,24 +525,24 @@ export class BinaryTree<
* Space Complexity: O(1)
*
* The function calculates the depth of a given node in a binary tree.
- * @param {K | N | null | undefined} distNode - The `distNode` parameter represents the node in
+ * @param {K | N | null | undefined} dist - The `dist` parameter represents the node in
* the binary tree whose depth we want to find. It can be of type `K`, `N`, `null`, or
* `undefined`.
* @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter is the starting node
* from which we want to calculate the depth. It can be either a `K` (binary tree node key) or
* `N` (binary tree node) or `null` or `undefined`. If no value is provided for `beginRoot
- * @returns the depth of the `distNode` relative to the `beginRoot`.
+ * @returns the depth of the `dist` relative to the `beginRoot`.
*/
- getDepth(distNode: BTNKeyOrNode, beginRoot: BTNKeyOrNode = this.root): number {
- distNode = this.ensureNode(distNode);
+ getDepth(dist: KeyOrNodeOrEntry, beginRoot: KeyOrNodeOrEntry = this.root): number {
+ dist = this.ensureNode(dist);
beginRoot = this.ensureNode(beginRoot);
let depth = 0;
- while (distNode?.parent) {
- if (distNode === beginRoot) {
+ while (dist?.parent) {
+ if (dist === beginRoot) {
return depth;
}
depth++;
- distNode = distNode.parent;
+ dist = dist.parent;
}
return depth;
}
@@ -474,7 +566,7 @@ export class BinaryTree<
* values:
* @returns the height of the binary tree.
*/
- getHeight(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): number {
+ getHeight(beginRoot: KeyOrNodeOrEntry = this.root, iterationType = this.iterationType): number {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return -1;
@@ -523,7 +615,7 @@ export class BinaryTree<
* to calculate the minimum height of a binary tree. It can have two possible values:
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
*/
- getMinHeight(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): number {
+ getMinHeight(beginRoot: KeyOrNodeOrEntry = this.root, iterationType = this.iterationType): number {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return -1;
@@ -583,7 +675,7 @@ export class BinaryTree<
* value of a binary tree node), `N` (a node of a binary tree), `null`, or `undefined`. If
* @returns a boolean value.
*/
- isPerfectlyBalanced(beginRoot: BTNKeyOrNode = this.root): boolean {
+ isPerfectlyBalanced(beginRoot: KeyOrNodeOrEntry = this.root): boolean {
return this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot);
}
@@ -596,7 +688,7 @@ export class BinaryTree<
identifier: K,
callback?: C,
onlyOne?: boolean,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N[];
@@ -604,7 +696,7 @@ export class BinaryTree<
identifier: N | null | undefined,
callback?: C,
onlyOne?: boolean,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N[];
@@ -612,7 +704,7 @@ export class BinaryTree<
identifier: ReturnType,
callback: C,
onlyOne?: boolean,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N[];
@@ -645,7 +737,7 @@ export class BinaryTree<
identifier: ReturnType | null | undefined,
callback: C = this._defaultOneParamCallback as C,
onlyOne = false,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): N[] {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -693,26 +785,27 @@ export class BinaryTree<
has>(
identifier: K,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): boolean;
has>(
identifier: N | null | undefined,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): boolean;
has>(
identifier: ReturnType | null | undefined,
callback: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): boolean;
/**
* Time Complexity: O(n)
+ * Space Complexity: O(log n).
*
* The function checks if a Binary Tree Node with a specific identifier exists in the tree.
* @param {ReturnType | null | undefined} identifier - The `identifier` parameter is the value
@@ -734,7 +827,7 @@ export class BinaryTree<
has>(
identifier: ReturnType | null | undefined,
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): boolean {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -751,21 +844,21 @@ export class BinaryTree<
getNode>(
identifier: K,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N | null | undefined;
getNode>(
identifier: N | null | undefined,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N | null | undefined;
getNode>(
identifier: ReturnType,
callback: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): N | null | undefined;
@@ -793,7 +886,7 @@ export class BinaryTree<
getNode>(
identifier: ReturnType | null | undefined,
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): N | null | undefined {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -846,44 +939,24 @@ export class BinaryTree<
}
}
- /**
- * Time Complexity: O(n)
- * Space Complexity: O(log n)
- */
-
- /**
- * The function `ensureNode` returns the node corresponding to the given key if it is a valid node
- * key, otherwise it returns the key itself.
- * @param {K | N | null | undefined} key - The `key` parameter can be of type `K`, `N`,
- * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
- * type of iteration to be used when searching for a node by key. It has a default value of
- * `IterationType.ITERATIVE`.
- * @returns either the node corresponding to the given key if it is a valid node key, or the key
- * itself if it is not a valid node key.
- */
- ensureNode(key: BTNKeyOrNode, iterationType = IterationType.ITERATIVE): N | null | undefined {
- return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
- }
-
get>(
identifier: K,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): V | undefined;
get>(
identifier: N | null | undefined,
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): V | undefined;
get>(
identifier: ReturnType,
callback: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType
): V | undefined;
@@ -912,7 +985,7 @@ export class BinaryTree<
get>(
identifier: ReturnType | null | undefined,
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): V | undefined {
if ((!callback || callback === this._defaultOneParamCallback) && (identifier as any) instanceof BinaryTreeNode)
@@ -922,11 +995,14 @@ export class BinaryTree<
}
/**
- * Time Complexity: O(n)
- * Space Complexity: O(log n)
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
*/
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Clear the binary tree, removing all nodes.
*/
clear() {
@@ -935,6 +1011,14 @@ export class BinaryTree<
}
/**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ */
+
+ /**
+ * Time Complexity: O(1)
+ * Space Complexity: O(1)
+ *
* Check if the binary tree is empty.
* @returns {boolean} - True if the binary tree is empty, false otherwise.
*/
@@ -956,7 +1040,7 @@ export class BinaryTree<
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
*/
- getPathToRoot(beginRoot: BTNKeyOrNode, isReverse = true): N[] {
+ getPathToRoot(beginRoot: KeyOrNodeOrEntry, isReverse = true): N[] {
// TODO to support get path through passing key
const result: N[] = [];
beginRoot = this.ensureNode(beginRoot);
@@ -975,7 +1059,7 @@ export class BinaryTree<
/**
* Time Complexity: O(log n)
- * Space Complexity: O(log n)
+ * Space Complexity: O(1)
*/
/**
@@ -992,7 +1076,10 @@ export class BinaryTree<
* @returns The function `getLeftMost` returns the leftmost node (`N`) in the binary tree. If there
* is no leftmost node, it returns `null` or `undefined` depending on the input.
*/
- getLeftMost(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): N | null | undefined {
+ getLeftMost(
+ beginRoot: KeyOrNodeOrEntry = this.root,
+ iterationType = this.iterationType
+ ): N | null | undefined {
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return beginRoot;
@@ -1035,7 +1122,10 @@ export class BinaryTree<
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If there
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
*/
- getRightMost(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): N | null | undefined {
+ getRightMost(
+ beginRoot: KeyOrNodeOrEntry = this.root,
+ iterationType = this.iterationType
+ ): N | null | undefined {
// TODO support get right most by passing key in
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return beginRoot;
@@ -1075,7 +1165,7 @@ export class BinaryTree<
* possible values:
* @returns a boolean value.
*/
- isBST(beginRoot: BTNKeyOrNode = this.root, iterationType = this.iterationType): boolean {
+ isBST(beginRoot: KeyOrNodeOrEntry = this.root, iterationType = this.iterationType): boolean {
// TODO there is a bug
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return true;
@@ -1117,27 +1207,27 @@ export class BinaryTree<
}
/**
- * Time Complexity: O(n)
- * Space Complexity: O(1)
+ * Time complexity: O(n)
+ * Space complexity: O(log n)
*/
subTreeTraverse>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: false
): ReturnType[];
subTreeTraverse>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType[];
subTreeTraverse>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: true
): ReturnType[];
@@ -1161,12 +1251,12 @@ export class BinaryTree<
* whether to include null values in the traversal. If `includeNull` is set to `true`, the
* traversal will include null values, otherwise it will skip them.
* @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
- * the `callback` function on each node in the subtree. The type of the array elements is determined
+ * the `callback` function on each node in the subtree. The type of the array nodes is determined
* by the return type of the `callback` function.
*/
subTreeTraverse>(
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType,
includeNull = false
): ReturnType[] {
@@ -1210,53 +1300,10 @@ export class BinaryTree<
return ans;
}
- /**
- * Time complexity: O(n)
- * Space complexity: O(log n)
- */
-
- /**
- * The function checks if a given node is a real node by verifying if it is an instance of
- * BinaryTreeNode and its key is not NaN.
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
- * @returns a boolean value.
- */
- isRealNode(node: BTNExemplar): node is N {
- return node instanceof BinaryTreeNode && String(node.key) !== 'NaN';
- }
-
- /**
- * The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
- * @returns a boolean value.
- */
- isNIL(node: BTNExemplar) {
- return node instanceof BinaryTreeNode && String(node.key) === 'NaN';
- }
-
- /**
- * The function checks if a given node is a real node or null.
- * @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
- * @returns a boolean value.
- */
- isNodeOrNull(node: BTNExemplar): node is N | null {
- return this.isRealNode(node) || node === null;
- }
-
- /**
- * The function "isNotNodeInstance" checks if a potential key is a K.
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
- * data type.
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
- */
- isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K {
- return !(potentialKey instanceof BinaryTreeNode);
- }
-
dfs>(
callback?: C,
pattern?: DFSOrderPattern,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: false
): ReturnType[];
@@ -1264,7 +1311,7 @@ export class BinaryTree<
dfs>(
callback?: C,
pattern?: DFSOrderPattern,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType[];
@@ -1272,7 +1319,7 @@ export class BinaryTree<
dfs>(
callback?: C,
pattern?: DFSOrderPattern,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: true
): ReturnType[];
@@ -1303,7 +1350,7 @@ export class BinaryTree<
dfs>(
callback: C = this._defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType: IterationType = IterationType.ITERATIVE,
includeNull = false
): ReturnType[] {
@@ -1402,21 +1449,21 @@ export class BinaryTree<
bfs>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: false
): ReturnType[];
bfs>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType[];
bfs>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: true
): ReturnType[];
@@ -1444,7 +1491,7 @@ export class BinaryTree<
*/
bfs>(
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType,
includeNull = false
): ReturnType[] {
@@ -1503,21 +1550,21 @@ export class BinaryTree<
listLevels>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: false
): ReturnType[][];
listLevels>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: undefined
): ReturnType[][];
listLevels>(
callback?: C,
- beginRoot?: BTNKeyOrNode,
+ beginRoot?: KeyOrNodeOrEntry,
iterationType?: IterationType,
includeNull?: true
): ReturnType[][];
@@ -1545,7 +1592,7 @@ export class BinaryTree<
*/
listLevels>(
callback: C = this._defaultOneParamCallback as C,
- beginRoot: BTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType,
includeNull = false
): ReturnType[][] {
@@ -1642,7 +1689,13 @@ export class BinaryTree<
/**
* Time complexity: O(n)
- * Space complexity: O(1)
+ * Space complexity: O(n)
+ */
+
+ /**
+ * Time complexity: O(n)
+ * Space complexity: O(n)
+ *
* The `morris` function performs a depth-first traversal on a binary tree using the Morris traversal
* algorithm.
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
@@ -1655,13 +1708,13 @@ export class BinaryTree<
* for the traversal. It can be specified as a key, a node object, or `null`/`undefined` to indicate
* the root of the tree. If no value is provided, the default value is the root of the tree.
* @returns The function `morris` returns an array of values that are the result of invoking the
- * `callback` function on each node in the binary tree. The type of the array elements is determined
+ * `callback` function on each node in the binary tree. The type of the array nodes is determined
* by the return type of the `callback` function.
*/
morris>(
callback: C = this._defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
- beginRoot: BTNKeyOrNode = this.root
+ beginRoot: KeyOrNodeOrEntry = this.root
): ReturnType[] {
beginRoot = this.ensureNode(beginRoot);
if (beginRoot === null) return [];
@@ -1772,8 +1825,8 @@ export class BinaryTree<
* Time Complexity: O(n)
* Space Complexity: O(n)
*
- * The `filter` function creates a new tree by iterating over the elements of the current tree and
- * adding only the elements that satisfy the given predicate function.
+ * The `filter` function creates a new tree by iterating over the nodes of the current tree and
+ * adding only the nodes that satisfy the given predicate function.
* @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
* `key`, and `index`. It should return a boolean value indicating whether the pair should be
* included in the filtered tree or not.
@@ -1834,13 +1887,21 @@ export class BinaryTree<
//
/**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ */
+
+ /**
+ * Time Complexity: O(n)
+ * Space Complexity: O(n)
+ *
* The `print` function is used to display a binary tree structure in a visually appealing way.
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
* undefined`. It represents the root node of a binary tree. The root node can have one of the
* following types:
* @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
*/
- print(beginRoot: BTNKeyOrNode = this.root, options?: BinaryTreePrintOptions): void {
+ print(beginRoot: KeyOrNodeOrEntry = this.root, options?: BinaryTreePrintOptions): void {
const opts = { isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false, ...options };
beginRoot = this.ensureNode(beginRoot);
if (!beginRoot) return;
@@ -1972,7 +2033,7 @@ export class BinaryTree<
* @param {N} destNode - The destination node to swap.
* @returns {N} - The destination node after the swap.
*/
- protected _swapProperties(srcNode: BTNKeyOrNode, destNode: BTNKeyOrNode): N | undefined {
+ protected _swapProperties(srcNode: KeyOrNodeOrEntry, destNode: KeyOrNodeOrEntry): N | undefined {
srcNode = this.ensureNode(srcNode);
destNode = this.ensureNode(destNode);
diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts
index b1656a3a..77a058f9 100644
--- a/src/data-structures/binary-tree/bst.ts
+++ b/src/data-structures/binary-tree/bst.ts
@@ -7,13 +7,11 @@
*/
import type {
BSTNested,
- BSTNKeyOrNode,
BSTNodeNested,
BSTOptions,
BTNCallback,
- BTNExemplar,
- BTNKeyOrNode,
- BTNodePureExemplar
+ BTNodePureExemplar,
+ KeyOrNodeOrEntry
} from '../../types';
import { BSTVariant, CP, IterationType } from '../../types';
import { BinaryTree, BinaryTreeNode } from './binary-tree';
@@ -94,13 +92,13 @@ export class BST<
implements IBinaryTree {
/**
* This is the constructor function for a binary search tree class in TypeScript, which initializes
- * the tree with optional elements and options.
- * @param [elements] - An optional iterable of BTNExemplar objects that will be added to the
+ * the tree with optional nodes and options.
+ * @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
* binary search tree.
* @param [options] - The `options` parameter is an optional object that can contain additional
* configuration options for the binary search tree. It can have the following properties:
*/
- constructor(elements?: Iterable>, options?: Partial>) {
+ constructor(nodes?: Iterable>, options?: Partial>) {
super([], options);
if (options) {
@@ -112,7 +110,7 @@ export class BST<
this._root = undefined;
- if (elements) this.addMany(elements);
+ if (nodes) this.addMany(nodes);
}
protected override _root?: N;
@@ -155,37 +153,28 @@ export class BST<
}
/**
- * The function checks if an exemplar is an instance of BSTNode.
- * @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar`.
- * @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
- */
- override isNode(exemplar: BTNExemplar): exemplar is N {
- return exemplar instanceof BSTNode;
- }
-
- /**
- * The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
+ * The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
* otherwise it returns undefined.
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`, where:
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry`, where:
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
- * `exemplarToNode` function. It represents the value associated with the exemplar node.
+ * `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
* @returns a node of type N or undefined.
*/
- override exemplarToNode(exemplar: BTNExemplar, value?: V): N | undefined {
+ override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): N | undefined {
let node: N | undefined;
- if (exemplar === null || exemplar === undefined) {
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
return;
- } else if (this.isNode(exemplar)) {
- node = exemplar;
- } else if (this.isEntry(exemplar)) {
- const [key, value] = exemplar;
+ } else if (this.isNode(keyOrNodeOrEntry)) {
+ node = keyOrNodeOrEntry;
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
+ const [key, value] = keyOrNodeOrEntry;
if (key === undefined || key === null) {
return;
} else {
node = this.createNode(key, value);
}
- } else if (this.isNotNodeInstance(exemplar)) {
- node = this.createNode(exemplar, value);
+ } else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
+ node = this.createNode(keyOrNodeOrEntry, value);
} else {
return;
}
@@ -193,13 +182,66 @@ export class BST<
}
/**
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
- * Space Complexity: O(1) - Constant space is used.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
+ * Average case for a balanced tree. Space for the recursive call stack in the worst case.
*/
/**
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
- * Space Complexity: O(1) - Constant space is used.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
+ *
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
+ * otherwise it returns the key itself.
+ * @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
+ * `undefined`.
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
+ * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
+ * @returns either a node object (N) or undefined.
+ */
+ override ensureNode(
+ keyOrNodeOrEntry: KeyOrNodeOrEntry,
+ iterationType = IterationType.ITERATIVE
+ ): N | undefined {
+ let res: N | undefined;
+ if (this.isRealNode(keyOrNodeOrEntry)) {
+ res = keyOrNodeOrEntry;
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
+ if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
+ } else {
+ if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
+ }
+ return res;
+ }
+
+ /**
+ * The function "isNotNodeInstance" checks if a potential key is a K.
+ * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
+ * data type.
+ * @returns a boolean value indicating whether the potentialKey is of type number or not.
+ */
+ override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry): potentialKey is K {
+ return !(potentialKey instanceof BSTNode);
+ }
+
+ /**
+ * The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry`.
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
+ */
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry): keyOrNodeOrEntry is N {
+ return keyOrNodeOrEntry instanceof BSTNode;
+ }
+
+ /**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
+ * - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
+ */
+
+ /**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
* or inserting a new node if the key is unique.
@@ -209,14 +251,14 @@ export class BST<
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
* node was not added.
*/
- override add(keyOrNodeOrEntry: BTNExemplar, value?: V): N | undefined {
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry, value?: V): boolean {
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
- if (newNode === undefined) return;
+ if (newNode === undefined) return false;
if (this.root === undefined) {
this._setRoot(newNode);
this._size++;
- return this.root;
+ return true;
}
let current = this.root;
@@ -225,7 +267,7 @@ export class BST<
// if (current !== newNode) {
// The key value is the same but the reference is different, update the value of the existing node
this._replaceNode(current, newNode);
- return newNode;
+ return true;
// } else {
// The key value is the same and the reference is the same, replace the entire node
@@ -238,7 +280,7 @@ export class BST<
current.left = newNode;
newNode.parent = current;
this._size++;
- return newNode;
+ return true;
}
current = current.left;
} else {
@@ -246,23 +288,24 @@ export class BST<
current.right = newNode;
newNode.parent = current;
this._size++;
- return newNode;
+ return true;
}
current = current.right;
}
}
- return undefined;
+ return false;
}
/**
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
- * Space Complexity: O(k) - Additional space is required for the sorted array.
+ * Time Complexity: O(k log n)
+ * Space Complexity: O(k)
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
*/
/**
- * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
- * Space Complexity: O(k) - Additional space is required for the sorted array.
+ * Time Complexity: O(k log n)
+ * Space Complexity: O(k)
*
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
* balancing the tree after each addition.
@@ -273,7 +316,7 @@ export class BST<
* order. If not provided, undefined will be assigned as the value for each key or node.
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
- * algorithm. If set to false, the add operation will not be balanced and the elements will be added
+ * algorithm. If set to false, the add operation will not be balanced and the nodes will be added
* in the order they appear in the input.
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
* type of iteration to use when adding multiple keys or nodes. It has a default value of
@@ -281,12 +324,12 @@ export class BST<
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
*/
override addMany(
- keysOrNodesOrEntries: Iterable>,
+ keysOrNodesOrEntries: Iterable>,
values?: Iterable,
isBalanceAdd = true,
iterationType = this.iterationType
- ): (N | undefined)[] {
- const inserted: (N | undefined)[] = [];
+ ): boolean[] {
+ const inserted: boolean[] = [];
let valuesIterator: Iterator | undefined;
@@ -305,7 +348,7 @@ export class BST<
const realBTNExemplars: BTNodePureExemplar[] = [];
- const isRealBTNExemplar = (kve: BTNExemplar): kve is BTNodePureExemplar => {
+ const isRealBTNExemplar = (kve: KeyOrNodeOrEntry): kve is BTNodePureExemplar => {
if (kve === undefined || kve === null) return false;
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
};
@@ -367,26 +410,25 @@ export class BST<
}
/**
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
- * Space Complexity: O(n) - Additional space is required for the sorted array.
+ * Time Complexity: O(n log n)
+ * Space Complexity: O(n)
+ * Adding each element individually in a balanced tree. Additional space is required for the sorted array.
*/
/**
- * Time Complexity: O(log n) - Average case for a balanced tree.
- * Space Complexity: O(1) - Constant space is used.
+ * Time Complexity: O(n log n)
+ * Space Complexity: O(n)
*
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
* leftmost node if the comparison result is greater than.
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
- * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
- * be performed. It can have one of the following values:
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
* the key of the leftmost node if the comparison result is greater than, and the key of the
* rightmost node otherwise. If no node is found, it returns 0.
*/
- lastKey(beginRoot: BSTNKeyOrNode = this.root): K | undefined {
+ lastKey(beginRoot: KeyOrNodeOrEntry = this.root): K | undefined {
let current = this.ensureNode(beginRoot);
if (!current) return undefined;
@@ -405,13 +447,13 @@ export class BST<
}
/**
- * Time Complexity: O(log n) - Average case for a balanced tree.
- * Space Complexity: O(1) - Constant space is used.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*/
/**
- * Time Complexity: O(log n) - Average case for a balanced tree.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(1)
*
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
* either recursive or iterative methods.
@@ -449,36 +491,14 @@ export class BST<
}
/**
- * The function "isNotNodeInstance" checks if a potential key is a K.
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
- * data type.
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
- */
- override isNotNodeInstance(potentialKey: BTNKeyOrNode): potentialKey is K {
- return !(potentialKey instanceof BSTNode);
- }
-
- /**
- * Time Complexity: O(log n) - Average case for a balanced tree.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
- */
-
- /**
- * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
- * otherwise it returns the key itself.
- * @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
- * `undefined`.
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
- * @returns either a node object (N) or undefined.
- */
- override ensureNode(key: BSTNKeyOrNode, iterationType = IterationType.ITERATIVE): N | undefined {
- return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
- }
-
- /**
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
+ * /
+
+ /**
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
*
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
* recursive or iterative approach.
@@ -503,7 +523,7 @@ export class BST<
identifier: ReturnType | undefined,
callback: C = this._defaultOneParamCallback as C,
onlyOne = false,
- beginRoot: BSTNKeyOrNode = this.root,
+ beginRoot: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): N[] {
beginRoot = this.ensureNode(beginRoot);
@@ -556,13 +576,14 @@ export class BST<
}
/**
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
+ * Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
*/
/**
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
*
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
* are either lesser or greater than a target node, depending on the specified comparison type.
@@ -584,7 +605,7 @@ export class BST<
lesserOrGreaterTraverse>(
callback: C = this._defaultOneParamCallback as C,
lesserOrGreater: CP = CP.lt,
- targetNode: BSTNKeyOrNode = this.root,
+ targetNode: KeyOrNodeOrEntry = this.root,
iterationType = this.iterationType
): ReturnType[] {
targetNode = this.ensureNode(targetNode);
@@ -623,13 +644,13 @@ export class BST<
}
/**
- * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
*/
/**
- * Time Complexity: O(n) - Building a balanced tree from a sorted array.
- * Space Complexity: O(n) - Additional space is required for the sorted array.
+ * Time Complexity: O(log n)
+ * Space Complexity: O(log n)
*
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
* ensures the tree is perfectly balanced.
@@ -691,8 +712,8 @@ export class BST<
*/
/**
- * Time Complexity: O(n) - Visiting each node once.
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
+ * Time Complexity: O(n)
+ * Space Complexity: O(log n)
*
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts
index 1089ea5f..7b06226c 100644
--- a/src/data-structures/binary-tree/rb-tree.ts
+++ b/src/data-structures/binary-tree/rb-tree.ts
@@ -10,9 +10,8 @@ import {
BinaryTreeDeleteResult,
BSTNKeyOrNode,
BTNCallback,
- BTNExemplar,
- BTNKeyOrNode,
IterationType,
+ KeyOrNodeOrEntry,
RBTNColor,
RBTreeOptions,
RedBlackTreeNested,
@@ -53,20 +52,20 @@ export class RedBlackTree<
/**
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
- * initializes the tree with optional elements and options.
- * @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar`
- * objects. It represents the initial elements that will be added to the RBTree during its
+ * initializes the tree with optional nodes and options.
+ * @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry`
+ * objects. It represents the initial nodes that will be added to the RBTree during its
* construction. If this parameter is provided, the `addMany` method is called to add all the
- * elements to the
+ * nodes to the
* @param [options] - The `options` parameter is an optional object that allows you to customize the
* behavior of the RBTree. It is of type `Partial`, which means that you can provide
* only a subset of the properties defined in the `RBTreeOptions` interface.
*/
- constructor(elements?: Iterable>, options?: Partial>) {
+ constructor(nodes?: Iterable>, options?: Partial>) {
super([], options);
this._root = this.Sentinel;
- if (elements) super.addMany(elements);
+ if (nodes) super.addMany(nodes);
}
protected _root: N;
@@ -113,62 +112,73 @@ export class RedBlackTree<
}
/**
- * The function checks if an exemplar is an instance of the RedBlackTreeNode class.
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar`.
- * @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
- * class.
- */
- override isNode(exemplar: BTNExemplar): exemplar is N {
- return exemplar instanceof RedBlackTreeNode;
- }
-
- /**
- * The function "isNotNodeInstance" checks if a potential key is a K.
- * @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
- * data type.
- * @returns a boolean value indicating whether the potentialKey is of type number or not.
- */
- override isNotNodeInstance(potentialKey: BTNKeyOrNode