queue-typed 1.47.6 → 1.47.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
  10. package/dist/data-structures/binary-tree/segment-tree.js +7 -7
  11. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  12. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  13. package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
  14. package/dist/data-structures/graph/abstract-graph.js +30 -30
  15. package/dist/data-structures/graph/directed-graph.d.ts +24 -24
  16. package/dist/data-structures/graph/directed-graph.js +28 -28
  17. package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
  18. package/dist/data-structures/graph/undirected-graph.js +18 -18
  19. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  20. package/dist/data-structures/hash/hash-map.js +5 -8
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
  24. package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
  25. package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
  26. package/dist/data-structures/queue/queue.d.ts +13 -13
  27. package/dist/data-structures/queue/queue.js +13 -13
  28. package/dist/data-structures/stack/stack.d.ts +6 -6
  29. package/dist/data-structures/stack/stack.js +7 -7
  30. package/dist/data-structures/trie/trie.d.ts +3 -0
  31. package/dist/data-structures/trie/trie.js +19 -4
  32. package/dist/interfaces/binary-tree.d.ts +3 -3
  33. package/dist/types/common.d.ts +6 -1
  34. package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
  35. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  36. package/package.json +2 -2
  37. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  38. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  39. package/src/data-structures/binary-tree/bst.ts +157 -154
  40. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  41. package/src/data-structures/binary-tree/segment-tree.ts +10 -10
  42. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  43. package/src/data-structures/graph/abstract-graph.ts +46 -46
  44. package/src/data-structures/graph/directed-graph.ts +40 -40
  45. package/src/data-structures/graph/undirected-graph.ts +26 -26
  46. package/src/data-structures/hash/hash-map.ts +8 -8
  47. package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
  48. package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
  49. package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
  50. package/src/data-structures/queue/queue.ts +13 -13
  51. package/src/data-structures/stack/stack.ts +9 -9
  52. package/src/data-structures/trie/trie.ts +23 -4
  53. package/src/interfaces/binary-tree.ts +3 -3
  54. package/src/types/common.ts +11 -1
  55. package/src/types/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, Comparator } from '../../types';
9
- import { CP, IterableEntriesOrKeys, IterationType } from '../../types';
8
+ import type { BSTNested, BSTNodeKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, BTNKey, BTNodeExemplar, Comparator } from '../../types';
9
+ import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
@@ -33,17 +33,26 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
33
33
  */
34
34
  set right(v: N | undefined);
35
35
  }
36
+ /**
37
+ * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
38
+ * 2. Unique Keys: No duplicate keys in a standard BST.
39
+ * 3. Efficient Search: Enables quick search, minimum, and maximum operations.
40
+ * 4. Inorder Traversal: Yields nodes in ascending order.
41
+ * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
42
+ * 6. Balance Variability: Can become unbalanced; special types maintain balance.
43
+ * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
44
+ */
36
45
  export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
37
46
  /**
38
- * The constructor function initializes a binary search tree with an optional comparator function.
39
- * @param {BSTOptions} [options] - An optional object that contains additional configuration options
40
- * for the binary search tree.
47
+ * This is the constructor function for a binary search tree class in TypeScript, which initializes
48
+ * the tree with optional elements and options.
49
+ * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
50
+ * binary search tree.
51
+ * @param [options] - The `options` parameter is an optional object that can contain additional
52
+ * configuration options for the binary search tree. It can have the following properties:
41
53
  */
42
- constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<BSTOptions>);
54
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<BSTOptions>);
43
55
  protected _root?: N;
44
- /**
45
- * Get the root node of the binary tree.
46
- */
47
56
  get root(): N | undefined;
48
57
  comparator: Comparator<BTNKey>;
49
58
  /**
@@ -55,45 +64,50 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
55
64
  * @returns a new instance of the BSTNode class with the specified key and value.
56
65
  */
57
66
  createNode(key: BTNKey, value?: V): N;
67
+ /**
68
+ * The function creates a new binary search tree with the specified options.
69
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
70
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
71
+ * that defines various options for creating a binary search tree.
72
+ * @returns a new instance of the BST class with the specified options.
73
+ */
58
74
  createTree(options?: Partial<BSTOptions>): TREE;
59
75
  /**
60
76
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
61
77
  * Space Complexity: O(1) - Constant space is used.
62
- *
63
- * The `add` function adds a new node to a binary search tree based on the provided key and value.
64
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
65
- * following types:
66
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
67
- * key or node being added to the binary search tree.
68
- * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
69
- * no node was inserted, it returns `undefined`.
70
78
  */
71
- add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
72
79
  /**
73
80
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
74
81
  * Space Complexity: O(1) - Constant space is used.
82
+ *
83
+ * The `add` function adds a new node to a binary search tree, either by key or by providing a node
84
+ * object.
85
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
86
+ * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
87
+ * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
75
88
  */
89
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
76
90
  /**
77
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
78
- * Space Complexity: O(n) - Additional space is required for the sorted array.
91
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
92
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
93
+ */
94
+ /**
95
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
96
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
79
97
  *
80
- * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
81
- * to a binary search tree.
82
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
83
- * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
84
- * node), or `undefined`.
85
- * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
86
- * nodes being added. If provided, the length of the `data` array must be the same as the length of
87
- * the `keysOrNodes` array.
98
+ * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
99
+ * unbalanced manner, and returns an array of the inserted nodes.
100
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
101
+ * binary tree.
88
102
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
89
- * adding the nodes. The default value is `true`.
103
+ * adding the nodes. The default value is true.
90
104
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
91
- * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
92
- * default value of `this.iterationType`, which means it will use the iteration type specified in the
93
- * current instance of the binary search tree
94
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
105
+ * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
106
+ * value of `this.iterationType`, which means it will use the iteration type specified by the binary
107
+ * tree instance.
108
+ * @returns The `addMany` function returns an array of `N` or `undefined` values.
95
109
  */
96
- addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
110
+ addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
97
111
  /**
98
112
  * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
99
113
  * Space Complexity: O(n) - Additional space is required for the sorted array.
@@ -113,7 +127,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
113
127
  * the key of the leftmost node if the comparison result is greater than, and the key of the
114
128
  * rightmost node otherwise. If no node is found, it returns 0.
115
129
  */
116
- lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
130
+ lastKey(beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): BTNKey;
117
131
  /**
118
132
  * Time Complexity: O(log n) - Average case for a balanced tree.
119
133
  * Space Complexity: O(1) - Constant space is used.
@@ -138,7 +152,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
138
152
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
139
153
  */
140
154
  /**
141
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
155
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
142
156
  * otherwise it returns the key itself.
143
157
  * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
144
158
  * `undefined`.
@@ -146,7 +160,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
146
160
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
147
161
  * @returns either a node object (N) or undefined.
148
162
  */
149
- ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
163
+ ensureNode(key: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N | undefined;
150
164
  /**
151
165
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
152
166
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -170,7 +184,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
170
184
  * performed on the binary tree. It can have two possible values:
171
185
  * @returns The method returns an array of nodes (`N[]`).
172
186
  */
173
- getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
187
+ getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): N[];
174
188
  /**
175
189
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
176
190
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -196,7 +210,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
196
210
  * @returns The function `lesserOrGreaterTraverse` returns an array of values of type
197
211
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
198
212
  */
199
- lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
213
+ lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BSTNodeKeyOrNode<N>, iterationType?: IterationType): ReturnType<C>[];
200
214
  /**
201
215
  * Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
202
216
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
@@ -236,11 +250,6 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
236
250
  * @returns a boolean value.
237
251
  */
238
252
  isAVLBalanced(iterationType?: IterationType): boolean;
239
- /**
240
- * Time Complexity: O(n) - Visiting each node once.
241
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
242
- */
243
- init(elements: IterableEntriesOrKeys<V>): void;
244
253
  protected _setRoot(v: N | undefined): void;
245
254
  /**
246
255
  * The function compares two values using a comparator function and returns whether the first value
@@ -45,11 +45,23 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
45
45
  }
46
46
  }
47
47
  exports.BSTNode = BSTNode;
48
+ /**
49
+ * 1. Node Order: Each node's left child has a lesser value, and the right child has a greater value.
50
+ * 2. Unique Keys: No duplicate keys in a standard BST.
51
+ * 3. Efficient Search: Enables quick search, minimum, and maximum operations.
52
+ * 4. Inorder Traversal: Yields nodes in ascending order.
53
+ * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
54
+ * 6. Balance Variability: Can become unbalanced; special types maintain balance.
55
+ * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
56
+ */
48
57
  class BST extends binary_tree_1.BinaryTree {
49
58
  /**
50
- * The constructor function initializes a binary search tree with an optional comparator function.
51
- * @param {BSTOptions} [options] - An optional object that contains additional configuration options
52
- * for the binary search tree.
59
+ * This is the constructor function for a binary search tree class in TypeScript, which initializes
60
+ * the tree with optional elements and options.
61
+ * @param [elements] - An optional iterable of BTNodeExemplar objects that will be added to the
62
+ * binary search tree.
63
+ * @param [options] - The `options` parameter is an optional object that can contain additional
64
+ * configuration options for the binary search tree. It can have the following properties:
53
65
  */
54
66
  constructor(elements, options) {
55
67
  super([], options);
@@ -62,11 +74,8 @@ class BST extends binary_tree_1.BinaryTree {
62
74
  }
63
75
  this._root = undefined;
64
76
  if (elements)
65
- this.init(elements);
77
+ this.addMany(elements);
66
78
  }
67
- /**
68
- * Get the root node of the binary tree.
69
- */
70
79
  get root() {
71
80
  return this._root;
72
81
  }
@@ -81,165 +90,156 @@ class BST extends binary_tree_1.BinaryTree {
81
90
  createNode(key, value) {
82
91
  return new BSTNode(key, value);
83
92
  }
93
+ /**
94
+ * The function creates a new binary search tree with the specified options.
95
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
96
+ * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
97
+ * that defines various options for creating a binary search tree.
98
+ * @returns a new instance of the BST class with the specified options.
99
+ */
84
100
  createTree(options) {
85
101
  return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
86
102
  }
103
+ /**
104
+ * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
105
+ * Space Complexity: O(1) - Constant space is used.
106
+ */
87
107
  /**
88
108
  * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
89
109
  * Space Complexity: O(1) - Constant space is used.
90
110
  *
91
- * The `add` function adds a new node to a binary search tree based on the provided key and value.
92
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
93
- * following types:
94
- * @param {V} [value] - The `value` parameter is an optional value that can be associated with the
95
- * key or node being added to the binary search tree.
96
- * @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
97
- * no node was inserted, it returns `undefined`.
111
+ * The `add` function adds a new node to a binary search tree, either by key or by providing a node
112
+ * object.
113
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
114
+ * @returns The method returns either the newly added node (`newNode`) or `undefined` if the input
115
+ * (`keyOrNodeOrEntry`) is null, undefined, or does not match any of the expected types.
98
116
  */
99
- add(keyOrNode, value) {
100
- if (keyOrNode === null)
117
+ add(keyOrNodeOrEntry) {
118
+ if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
101
119
  return undefined;
102
- // TODO support node as a parameter
103
- let inserted;
120
+ }
104
121
  let newNode;
105
- if (keyOrNode instanceof BSTNode) {
106
- newNode = keyOrNode;
122
+ if (keyOrNodeOrEntry instanceof BSTNode) {
123
+ newNode = keyOrNodeOrEntry;
124
+ }
125
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
126
+ newNode = this.createNode(keyOrNodeOrEntry);
107
127
  }
108
- else if (this.isNodeKey(keyOrNode)) {
109
- newNode = this.createNode(keyOrNode, value);
128
+ else if (this.isEntry(keyOrNodeOrEntry)) {
129
+ const [key, value] = keyOrNodeOrEntry;
130
+ if (key === undefined || key === null) {
131
+ return;
132
+ }
133
+ else {
134
+ newNode = this.createNode(key, value);
135
+ }
110
136
  }
111
137
  else {
112
- newNode = undefined;
138
+ return;
113
139
  }
114
140
  if (this.root === undefined) {
115
141
  this._setRoot(newNode);
116
- this._size = this.size + 1;
117
- inserted = this.root;
142
+ this._size++;
143
+ return this.root;
118
144
  }
119
- else {
120
- let cur = this.root;
121
- let traversing = true;
122
- while (traversing) {
123
- if (cur !== undefined && newNode !== undefined) {
124
- if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
125
- if (newNode) {
126
- cur.value = newNode.value;
127
- }
128
- //Duplicates are not accepted.
129
- traversing = false;
130
- inserted = cur;
131
- }
132
- else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
133
- // Traverse left of the node
134
- if (cur.left === undefined) {
135
- if (newNode) {
136
- newNode.parent = cur;
137
- }
138
- //Add to the left of the current node
139
- cur.left = newNode;
140
- this._size = this.size + 1;
141
- traversing = false;
142
- inserted = cur.left;
143
- }
144
- else {
145
- //Traverse the left of the current node
146
- if (cur.left)
147
- cur = cur.left;
148
- }
149
- }
150
- else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
151
- // Traverse right of the node
152
- if (cur.right === undefined) {
153
- if (newNode) {
154
- newNode.parent = cur;
155
- }
156
- //Add to the right of the current node
157
- cur.right = newNode;
158
- this._size = this.size + 1;
159
- traversing = false;
160
- inserted = cur.right;
161
- }
162
- else {
163
- //Traverse the left of the current node
164
- if (cur.right)
165
- cur = cur.right;
166
- }
167
- }
145
+ let current = this.root;
146
+ while (current !== undefined) {
147
+ if (this._compare(current.key, newNode.key) === types_1.CP.eq) {
148
+ // if (current !== newNode) {
149
+ // The key value is the same but the reference is different, update the value of the existing node
150
+ this._replaceNode(current, newNode);
151
+ return newNode;
152
+ // } else {
153
+ // The key value is the same and the reference is the same, replace the entire node
154
+ // this._replaceNode(current, newNode);
155
+ // return;
156
+ // }
157
+ }
158
+ else if (this._compare(current.key, newNode.key) === types_1.CP.gt) {
159
+ if (current.left === undefined) {
160
+ current.left = newNode;
161
+ newNode.parent = current;
162
+ this._size++;
163
+ return newNode;
168
164
  }
169
- else {
170
- traversing = false;
165
+ current = current.left;
166
+ }
167
+ else {
168
+ if (current.right === undefined) {
169
+ current.right = newNode;
170
+ newNode.parent = current;
171
+ this._size++;
172
+ return newNode;
171
173
  }
174
+ current = current.right;
172
175
  }
173
176
  }
174
- return inserted;
177
+ return undefined;
175
178
  }
176
179
  /**
177
- * Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
178
- * Space Complexity: O(1) - Constant space is used.
180
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
181
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
179
182
  */
180
183
  /**
181
- * Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
182
- * Space Complexity: O(n) - Additional space is required for the sorted array.
184
+ * Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
185
+ * Space Complexity: O(k) - Additional space is required for the sorted array.
183
186
  *
184
- * The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
185
- * to a binary search tree.
186
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
187
- * binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
188
- * node), or `undefined`.
189
- * @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
190
- * nodes being added. If provided, the length of the `data` array must be the same as the length of
191
- * the `keysOrNodes` array.
187
+ * The `addMany` function in TypeScript adds multiple nodes to a binary tree, either in a balanced or
188
+ * unbalanced manner, and returns an array of the inserted nodes.
189
+ * @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
190
+ * binary tree.
192
191
  * @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
193
- * adding the nodes. The default value is `true`.
192
+ * adding the nodes. The default value is true.
194
193
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
195
- * type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
196
- * default value of `this.iterationType`, which means it will use the iteration type specified in the
197
- * current instance of the binary search tree
198
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
194
+ * type of iteration to use when adding multiple keys or nodes to the binary tree. It has a default
195
+ * value of `this.iterationType`, which means it will use the iteration type specified by the binary
196
+ * tree instance.
197
+ * @returns The `addMany` function returns an array of `N` or `undefined` values.
199
198
  */
200
- addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
201
- // TODO this addMany function is inefficient, it should be optimized
202
- function hasNoUndefined(arr) {
203
- return arr.indexOf(undefined) === -1;
204
- }
205
- if (!isBalanceAdd || !hasNoUndefined(keysOrNodes)) {
206
- return super.addMany(keysOrNodes, data).map(n => n !== null && n !== void 0 ? n : undefined);
207
- }
199
+ addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
208
200
  const inserted = [];
209
- const combinedArr = keysOrNodes.map((value, index) => [value, data === null || data === void 0 ? void 0 : data[index]]);
210
- let sorted = [];
211
- function _isNodeOrUndefinedTuple(arr) {
212
- for (const [keyOrNode] of arr)
213
- if (keyOrNode instanceof BSTNode)
214
- return true;
215
- return false;
201
+ if (!isBalanceAdd) {
202
+ for (const kve of keysOrNodesOrEntries) {
203
+ const nn = this.add(kve);
204
+ inserted.push(nn);
205
+ }
206
+ return inserted;
216
207
  }
217
- const _isBinaryTreeKeyOrNullTuple = (arr) => {
218
- for (const [keyOrNode] of arr)
219
- if (this.isNodeKey(keyOrNode))
220
- return true;
221
- return false;
208
+ const realBTNExemplars = [];
209
+ const isRealBTNExemplar = (kve) => {
210
+ if (kve === undefined || kve === null)
211
+ return false;
212
+ return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
222
213
  };
223
- let sortedKeysOrNodes = [], sortedData = [];
224
- if (_isNodeOrUndefinedTuple(combinedArr)) {
225
- sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
226
- }
227
- else if (_isBinaryTreeKeyOrNullTuple(combinedArr)) {
228
- sorted = combinedArr.sort((a, b) => a[0] - b[0]);
229
- }
230
- else {
231
- throw new Error('Invalid input keysOrNodes');
214
+ for (const kve of keysOrNodesOrEntries) {
215
+ isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
232
216
  }
233
- sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
234
- sortedData = sorted.map(([, value]) => value);
235
- const _dfs = (arr, data) => {
217
+ // TODO this addMany function is inefficient, it should be optimized
218
+ let sorted = [];
219
+ sorted = realBTNExemplars.sort((a, b) => {
220
+ let aR, bR;
221
+ if (this.isEntry(a))
222
+ aR = a[0];
223
+ else if (this.isRealNode(a))
224
+ aR = a.key;
225
+ else
226
+ aR = a;
227
+ if (this.isEntry(b))
228
+ bR = b[0];
229
+ else if (this.isRealNode(b))
230
+ bR = b.key;
231
+ else
232
+ bR = b;
233
+ return aR - bR;
234
+ });
235
+ const _dfs = (arr) => {
236
236
  if (arr.length === 0)
237
237
  return;
238
238
  const mid = Math.floor((arr.length - 1) / 2);
239
- const newNode = this.add(arr[mid], data === null || data === void 0 ? void 0 : data[mid]);
239
+ const newNode = this.add(arr[mid]);
240
240
  inserted.push(newNode);
241
- _dfs(arr.slice(0, mid), data === null || data === void 0 ? void 0 : data.slice(0, mid));
242
- _dfs(arr.slice(mid + 1), data === null || data === void 0 ? void 0 : data.slice(mid + 1));
241
+ _dfs(arr.slice(0, mid));
242
+ _dfs(arr.slice(mid + 1));
243
243
  };
244
244
  const _iterate = () => {
245
245
  const n = sorted.length;
@@ -250,7 +250,7 @@ class BST extends binary_tree_1.BinaryTree {
250
250
  const [l, r] = popped;
251
251
  if (l <= r) {
252
252
  const m = l + Math.floor((r - l) / 2);
253
- const newNode = this.add(sortedKeysOrNodes[m], sortedData === null || sortedData === void 0 ? void 0 : sortedData[m]);
253
+ const newNode = this.add(sorted[m]);
254
254
  inserted.push(newNode);
255
255
  stack.push([m + 1, r]);
256
256
  stack.push([l, m - 1]);
@@ -259,7 +259,7 @@ class BST extends binary_tree_1.BinaryTree {
259
259
  }
260
260
  };
261
261
  if (iterationType === types_1.IterationType.RECURSIVE) {
262
- _dfs(sortedKeysOrNodes, sortedData);
262
+ _dfs(sorted);
263
263
  }
264
264
  else {
265
265
  _iterate();
@@ -348,7 +348,7 @@ class BST extends binary_tree_1.BinaryTree {
348
348
  * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
349
349
  */
350
350
  /**
351
- * The function `ensureNotKey` returns the node corresponding to the given key if it is a node key,
351
+ * The function `ensureNode` returns the node corresponding to the given key if it is a node key,
352
352
  * otherwise it returns the key itself.
353
353
  * @param {BTNKey | N | undefined} key - The `key` parameter can be of type `BTNKey`, `N`, or
354
354
  * `undefined`.
@@ -356,7 +356,7 @@ class BST extends binary_tree_1.BinaryTree {
356
356
  * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
357
357
  * @returns either a node object (N) or undefined.
358
358
  */
359
- ensureNotKey(key, iterationType = types_1.IterationType.ITERATIVE) {
359
+ ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
360
360
  return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
361
361
  }
362
362
  /**
@@ -383,7 +383,7 @@ class BST extends binary_tree_1.BinaryTree {
383
383
  * @returns The method returns an array of nodes (`N[]`).
384
384
  */
385
385
  getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
386
- beginRoot = this.ensureNotKey(beginRoot);
386
+ beginRoot = this.ensureNode(beginRoot);
387
387
  if (!beginRoot)
388
388
  return [];
389
389
  const ans = [];
@@ -464,7 +464,7 @@ class BST extends binary_tree_1.BinaryTree {
464
464
  * `ReturnType<C>`, which is the return type of the callback function passed as an argument.
465
465
  */
466
466
  lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
467
- targetNode = this.ensureNotKey(targetNode);
467
+ targetNode = this.ensureNode(targetNode);
468
468
  const ans = [];
469
469
  if (!targetNode)
470
470
  return ans;
@@ -529,7 +529,7 @@ class BST extends binary_tree_1.BinaryTree {
529
529
  return;
530
530
  const m = l + Math.floor((r - l) / 2);
531
531
  const midNode = sorted[m];
532
- this.add(midNode.key, midNode.value);
532
+ this.add([midNode.key, midNode.value]);
533
533
  buildBalanceBST(l, m - 1);
534
534
  buildBalanceBST(m + 1, r);
535
535
  };
@@ -546,7 +546,7 @@ class BST extends binary_tree_1.BinaryTree {
546
546
  const m = l + Math.floor((r - l) / 2);
547
547
  const midNode = sorted[m];
548
548
  debugger;
549
- this.add(midNode.key, midNode.value);
549
+ this.add([midNode.key, midNode.value]);
550
550
  stack.push([m + 1, r]);
551
551
  stack.push([l, m - 1]);
552
552
  }
@@ -623,23 +623,6 @@ class BST extends binary_tree_1.BinaryTree {
623
623
  }
624
624
  return balanced;
625
625
  }
626
- /**
627
- * Time Complexity: O(n) - Visiting each node once.
628
- * Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
629
- */
630
- init(elements) {
631
- if (elements) {
632
- for (const entryOrKey of elements) {
633
- if (Array.isArray(entryOrKey)) {
634
- const [key, value] = entryOrKey;
635
- this.add(key, value);
636
- }
637
- else {
638
- this.add(entryOrKey);
639
- }
640
- }
641
- }
642
- }
643
626
  _setRoot(v) {
644
627
  if (v) {
645
628
  v.parent = undefined;