min-heap-typed 1.42.7 → 1.42.9
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.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/data-structures/binary-tree/binary-tree.js +388 -201
- package/dist/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/data-structures/binary-tree/bst.js +121 -67
- package/dist/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/data-structures/graph/abstract-graph.js +147 -36
- package/dist/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/data-structures/graph/directed-graph.js +126 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/data-structures/graph/undirected-graph.js +63 -0
- package/dist/data-structures/heap/heap.d.ts +175 -12
- package/dist/data-structures/heap/heap.js +175 -12
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/data-structures/queue/deque.d.ts +113 -3
- package/dist/data-structures/queue/deque.js +113 -3
- package/dist/data-structures/queue/queue.d.ts +87 -0
- package/dist/data-structures/queue/queue.js +87 -0
- package/dist/data-structures/stack/stack.d.ts +42 -0
- package/dist/data-structures/stack/stack.js +42 -0
- package/dist/data-structures/trie/trie.d.ts +76 -0
- package/dist/data-structures/trie/trie.js +76 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +419 -204
- package/src/data-structures/binary-tree/bst.ts +130 -68
- package/src/data-structures/binary-tree/rb-tree.ts +106 -19
- package/src/data-structures/binary-tree/tree-multimap.ts +88 -44
- package/src/data-structures/graph/abstract-graph.ts +133 -7
- package/src/data-structures/graph/directed-graph.ts +145 -1
- package/src/data-structures/graph/undirected-graph.ts +72 -0
- package/src/data-structures/heap/heap.ts +201 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +232 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +208 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +74 -0
- package/src/data-structures/queue/deque.ts +127 -3
- package/src/data-structures/queue/queue.ts +99 -0
- package/src/data-structures/stack/stack.ts +48 -0
- package/src/data-structures/trie/trie.ts +87 -4
|
@@ -35,10 +35,9 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
35
35
|
}
|
|
36
36
|
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
37
37
|
/**
|
|
38
|
-
* The constructor function initializes a binary search tree
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* binary search tree.
|
|
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.
|
|
42
41
|
*/
|
|
43
42
|
constructor(options?: BSTOptions);
|
|
44
43
|
protected _root?: N;
|
|
@@ -56,51 +55,79 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
56
55
|
*/
|
|
57
56
|
createNode(key: BTNKey, value?: V): N;
|
|
58
57
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
58
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
59
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
63
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
64
|
+
*
|
|
65
|
+
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
66
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
67
|
+
* following types:
|
|
68
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
69
|
+
* key or node being added to the binary search tree.
|
|
70
|
+
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
71
|
+
* no node was inserted, it returns `undefined`.
|
|
67
72
|
*/
|
|
68
73
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
69
74
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
75
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
76
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
80
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
81
|
+
*
|
|
82
|
+
* The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
|
|
83
|
+
* to a binary search tree.
|
|
84
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
|
|
85
|
+
* binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
|
|
86
|
+
* node), or `undefined`.
|
|
87
|
+
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
88
|
+
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
89
|
+
* the `keysOrNodes` array.
|
|
90
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
91
|
+
* adding the nodes. The default value is `true`.
|
|
92
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
93
|
+
* type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
|
|
94
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
95
|
+
* current instance of the binary search tree
|
|
96
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
81
97
|
*/
|
|
82
98
|
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
83
99
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
100
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
101
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
102
|
+
*/
|
|
103
|
+
/**
|
|
104
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
105
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
106
|
+
*
|
|
107
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
108
|
+
* leftmost node if the comparison result is greater than.
|
|
109
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
110
|
+
* type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
111
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
91
112
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
92
|
-
* be performed
|
|
93
|
-
* pre-order, in-order, or post-order.
|
|
113
|
+
* be performed. It can have one of the following values:
|
|
94
114
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
95
115
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
96
116
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
97
117
|
*/
|
|
98
118
|
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
|
|
99
119
|
/**
|
|
120
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
121
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
122
|
+
*/
|
|
123
|
+
/**
|
|
124
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
125
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
126
|
+
*
|
|
100
127
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
101
128
|
* either recursive or iterative methods.
|
|
102
129
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
103
|
-
* It is used to
|
|
130
|
+
* It is used to identify the node that we want to retrieve.
|
|
104
131
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
105
132
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
106
133
|
* values:
|
|
@@ -119,43 +146,57 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
119
146
|
*/
|
|
120
147
|
ensureNotKey(key: BTNKey | N | undefined, iterationType?: IterationType): N | undefined;
|
|
121
148
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* @param
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
*
|
|
149
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
150
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
151
|
+
*/
|
|
152
|
+
/**
|
|
153
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
154
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
155
|
+
*
|
|
156
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
157
|
+
* recursive or iterative approach.
|
|
158
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
159
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
160
|
+
* callback function `C`.
|
|
161
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
162
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
163
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
164
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
165
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
166
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
167
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
168
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
169
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
170
|
+
* traversal will start from the root of the tree.
|
|
171
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
172
|
+
* performed on the binary tree. It can have two possible values:
|
|
173
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
141
174
|
*/
|
|
142
175
|
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
|
|
143
176
|
/**
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
*
|
|
177
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
178
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
179
|
+
*/
|
|
180
|
+
/**
|
|
181
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
182
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
183
|
+
*
|
|
184
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
185
|
+
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
186
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
187
|
+
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
188
|
+
* parameter of type `N` (the node type) and returns a value of any type.
|
|
149
189
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
150
|
-
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* @param iterationType - The `iterationType` parameter determines
|
|
157
|
-
*
|
|
158
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of
|
|
190
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
191
|
+
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
192
|
+
* `lesserOrGreater` are
|
|
193
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
194
|
+
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
195
|
+
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
196
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
197
|
+
* performed on the binary tree. It can have two possible values:
|
|
198
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
199
|
+
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
159
200
|
*/
|
|
160
201
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
161
202
|
/**
|
|
@@ -168,6 +209,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
168
209
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
169
210
|
*/
|
|
170
211
|
/**
|
|
212
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
213
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
214
|
+
*/
|
|
215
|
+
/**
|
|
216
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
217
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
218
|
+
*
|
|
171
219
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
172
220
|
* ensures the tree is perfectly balanced.
|
|
173
221
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
@@ -177,6 +225,13 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
177
225
|
*/
|
|
178
226
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
179
227
|
/**
|
|
228
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
229
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
230
|
+
*/
|
|
231
|
+
/**
|
|
232
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
233
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
234
|
+
*
|
|
180
235
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
181
236
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
182
237
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
@@ -47,10 +47,9 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
47
47
|
exports.BSTNode = BSTNode;
|
|
48
48
|
class BST extends binary_tree_1.BinaryTree {
|
|
49
49
|
/**
|
|
50
|
-
* The constructor function initializes a binary search tree
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* binary search tree.
|
|
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.
|
|
54
53
|
*/
|
|
55
54
|
constructor(options) {
|
|
56
55
|
super(options);
|
|
@@ -81,14 +80,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
81
80
|
return new BSTNode(key, value);
|
|
82
81
|
}
|
|
83
82
|
/**
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
83
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
84
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
85
|
+
*/
|
|
86
|
+
/**
|
|
87
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
88
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
89
|
+
*
|
|
90
|
+
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
91
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
92
|
+
* following types:
|
|
93
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
94
|
+
* key or node being added to the binary search tree.
|
|
95
|
+
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
96
|
+
* no node was inserted, it returns `undefined`.
|
|
92
97
|
*/
|
|
93
98
|
add(keyOrNode, value) {
|
|
94
99
|
if (keyOrNode === null)
|
|
@@ -168,17 +173,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
168
173
|
return inserted;
|
|
169
174
|
}
|
|
170
175
|
/**
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
176
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
177
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
178
|
+
*/
|
|
179
|
+
/**
|
|
180
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
181
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
182
|
+
*
|
|
183
|
+
* The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
|
|
184
|
+
* to a binary search tree.
|
|
185
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
|
|
186
|
+
* binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
|
|
187
|
+
* node), or `undefined`.
|
|
188
|
+
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
189
|
+
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
190
|
+
* the `keysOrNodes` array.
|
|
191
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
192
|
+
* adding the nodes. The default value is `true`.
|
|
193
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
194
|
+
* type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
|
|
195
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
196
|
+
* current instance of the binary search tree
|
|
197
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
182
198
|
*/
|
|
183
199
|
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
184
200
|
// TODO this addMany function is inefficient, it should be optimized
|
|
@@ -250,16 +266,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
250
266
|
return inserted;
|
|
251
267
|
}
|
|
252
268
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
*
|
|
258
|
-
*
|
|
259
|
-
*
|
|
269
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
270
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
271
|
+
*/
|
|
272
|
+
/**
|
|
273
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
274
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
275
|
+
*
|
|
276
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
277
|
+
* leftmost node if the comparison result is greater than.
|
|
278
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
279
|
+
* type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
280
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
260
281
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
261
|
-
* be performed
|
|
262
|
-
* pre-order, in-order, or post-order.
|
|
282
|
+
* be performed. It can have one of the following values:
|
|
263
283
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
264
284
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
265
285
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -274,10 +294,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
274
294
|
return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
275
295
|
}
|
|
276
296
|
/**
|
|
297
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
298
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
299
|
+
*/
|
|
300
|
+
/**
|
|
301
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
302
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
303
|
+
*
|
|
277
304
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
278
305
|
* either recursive or iterative methods.
|
|
279
306
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
280
|
-
* It is used to
|
|
307
|
+
* It is used to identify the node that we want to retrieve.
|
|
281
308
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
282
309
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
283
310
|
* values:
|
|
@@ -328,25 +355,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
328
355
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
329
356
|
}
|
|
330
357
|
/**
|
|
331
|
-
*
|
|
332
|
-
*
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
* @param
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
358
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
359
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
360
|
+
*/
|
|
361
|
+
/**
|
|
362
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
363
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
364
|
+
*
|
|
365
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
366
|
+
* recursive or iterative approach.
|
|
367
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
368
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
369
|
+
* callback function `C`.
|
|
370
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
371
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
372
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
373
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
374
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
375
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
376
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
377
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
378
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
379
|
+
* traversal will start from the root of the tree.
|
|
380
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
381
|
+
* performed on the binary tree. It can have two possible values:
|
|
382
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
350
383
|
*/
|
|
351
384
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
352
385
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -404,23 +437,30 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
404
437
|
}
|
|
405
438
|
return ans;
|
|
406
439
|
}
|
|
407
|
-
// --- start additional functions
|
|
408
440
|
/**
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
*
|
|
441
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
442
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
443
|
+
*/
|
|
444
|
+
/**
|
|
445
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
446
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
447
|
+
*
|
|
448
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
449
|
+
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
450
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
451
|
+
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
452
|
+
* parameter of type `N` (the node type) and returns a value of any type.
|
|
414
453
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
415
|
-
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
* @param iterationType - The `iterationType` parameter determines
|
|
422
|
-
*
|
|
423
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of
|
|
454
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
455
|
+
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
456
|
+
* `lesserOrGreater` are
|
|
457
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
458
|
+
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
459
|
+
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
460
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
461
|
+
* performed on the binary tree. It can have two possible values:
|
|
462
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
463
|
+
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
424
464
|
*/
|
|
425
465
|
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
426
466
|
targetNode = this.ensureNotKey(targetNode);
|
|
@@ -472,6 +512,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
472
512
|
* AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
|
|
473
513
|
*/
|
|
474
514
|
/**
|
|
515
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
516
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
517
|
+
*/
|
|
518
|
+
/**
|
|
519
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
520
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
521
|
+
*
|
|
475
522
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
476
523
|
* ensures the tree is perfectly balanced.
|
|
477
524
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
@@ -517,6 +564,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
517
564
|
}
|
|
518
565
|
}
|
|
519
566
|
/**
|
|
567
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
568
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
569
|
+
*/
|
|
570
|
+
/**
|
|
571
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
572
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
573
|
+
*
|
|
520
574
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
521
575
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
522
576
|
* to check if the AVL tree is balanced. It can have two possible values:
|