linked-list-typed 1.49.4 → 1.49.6
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/base/iterable-base.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +154 -143
- package/dist/data-structures/binary-tree/binary-tree.js +211 -198
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -55
- package/dist/data-structures/binary-tree/tree-multimap.js +59 -94
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/data-structures/graph/abstract-graph.js +3 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/data-structures/heap/heap.js +2 -3
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/index.d.ts +0 -2
- package/dist/data-structures/matrix/index.js +0 -2
- package/dist/data-structures/matrix/matrix.d.ts +128 -10
- package/dist/data-structures/matrix/matrix.js +400 -15
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +5 -7
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +6 -1
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +1 -1
- package/src/data-structures/base/iterable-base.ts +7 -10
- package/src/data-structures/binary-tree/avl-tree.ts +73 -61
- package/src/data-structures/binary-tree/binary-tree.ts +301 -270
- package/src/data-structures/binary-tree/bst.ts +139 -115
- package/src/data-structures/binary-tree/rb-tree.ts +81 -73
- package/src/data-structures/binary-tree/tree-multimap.ts +72 -103
- package/src/data-structures/graph/abstract-graph.ts +13 -11
- package/src/data-structures/graph/directed-graph.ts +1 -3
- package/src/data-structures/graph/map-graph.ts +6 -1
- package/src/data-structures/graph/undirected-graph.ts +3 -6
- package/src/data-structures/hash/hash-map.ts +18 -16
- package/src/data-structures/heap/heap.ts +7 -10
- package/src/data-structures/heap/max-heap.ts +2 -1
- package/src/data-structures/heap/min-heap.ts +2 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -3
- package/src/data-structures/matrix/index.ts +0 -2
- package/src/data-structures/matrix/matrix.ts +442 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -10
- package/src/data-structures/queue/deque.ts +18 -39
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/interfaces/binary-tree.ts +9 -4
- package/src/types/common.ts +5 -5
- package/src/types/data-structures/base/base.ts +14 -3
- package/src/types/data-structures/base/index.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +4 -2
- package/src/types/data-structures/hash/hash-map.ts +3 -3
- package/src/types/data-structures/heap/heap.ts +2 -2
- package/src/types/data-structures/priority-queue/priority-queue.ts +2 -2
- package/src/utils/utils.ts +7 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +0 -107
- package/dist/data-structures/matrix/matrix2d.js +0 -199
- package/dist/data-structures/matrix/vector2d.d.ts +0 -200
- package/dist/data-structures/matrix/vector2d.js +0 -290
- package/src/data-structures/matrix/matrix2d.ts +0 -211
- package/src/data-structures/matrix/vector2d.ts +0 -315
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BSTNested,
|
|
8
|
+
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -45,13 +45,13 @@ export declare class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTN
|
|
|
45
45
|
export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BSTNodeNested<K, V>>, TREE extends BST<K, V, N, TREE> = BST<K, V, N, BSTNested<K, V, N>>> extends BinaryTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
46
46
|
/**
|
|
47
47
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
48
|
-
* the tree with optional
|
|
49
|
-
* @param [
|
|
48
|
+
* the tree with optional nodes and options.
|
|
49
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
50
50
|
* binary search tree.
|
|
51
51
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
52
52
|
* configuration options for the binary search tree. It can have the following properties:
|
|
53
53
|
*/
|
|
54
|
-
constructor(
|
|
54
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>);
|
|
55
55
|
protected _root?: N;
|
|
56
56
|
get root(): N | undefined;
|
|
57
57
|
protected _variant: BSTVariant;
|
|
@@ -74,27 +74,53 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
74
74
|
*/
|
|
75
75
|
createTree(options?: Partial<BSTOptions<K>>): TREE;
|
|
76
76
|
/**
|
|
77
|
-
* The function
|
|
78
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
79
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
80
|
-
*/
|
|
81
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
82
|
-
/**
|
|
83
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
77
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
84
78
|
* otherwise it returns undefined.
|
|
85
|
-
* @param
|
|
79
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
86
80
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
87
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
81
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
88
82
|
* @returns a node of type N or undefined.
|
|
89
83
|
*/
|
|
90
|
-
exemplarToNode(
|
|
84
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Time Complexity: O(log n)
|
|
87
|
+
* Space Complexity: O(log n)
|
|
88
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
89
|
+
*/
|
|
90
|
+
/**
|
|
91
|
+
* Time Complexity: O(log n)
|
|
92
|
+
* Space Complexity: O(log n)
|
|
93
|
+
*
|
|
94
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
95
|
+
* otherwise it returns the key itself.
|
|
96
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
97
|
+
* `undefined`.
|
|
98
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
99
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
100
|
+
* @returns either a node object (N) or undefined.
|
|
101
|
+
*/
|
|
102
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
105
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
106
|
+
* data type.
|
|
107
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
108
|
+
*/
|
|
109
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
110
|
+
/**
|
|
111
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
112
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
113
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
114
|
+
*/
|
|
115
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
91
116
|
/**
|
|
92
|
-
* Time Complexity: O(log n)
|
|
93
|
-
* Space Complexity: O(1)
|
|
117
|
+
* Time Complexity: O(log n)
|
|
118
|
+
* Space Complexity: O(1)
|
|
119
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
94
120
|
*/
|
|
95
121
|
/**
|
|
96
|
-
* Time Complexity: O(log n)
|
|
97
|
-
* Space Complexity: O(1)
|
|
122
|
+
* Time Complexity: O(log n)
|
|
123
|
+
* Space Complexity: O(1)
|
|
98
124
|
*
|
|
99
125
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
100
126
|
* or inserting a new node if the key is unique.
|
|
@@ -104,14 +130,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
104
130
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
105
131
|
* node was not added.
|
|
106
132
|
*/
|
|
107
|
-
add(keyOrNodeOrEntry:
|
|
133
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
|
|
108
134
|
/**
|
|
109
|
-
* Time Complexity: O(k log n)
|
|
110
|
-
* Space Complexity: O(k)
|
|
135
|
+
* Time Complexity: O(k log n)
|
|
136
|
+
* Space Complexity: O(k)
|
|
137
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
111
138
|
*/
|
|
112
139
|
/**
|
|
113
|
-
* Time Complexity: O(k log n)
|
|
114
|
-
* Space Complexity: O(k)
|
|
140
|
+
* Time Complexity: O(k log n)
|
|
141
|
+
* Space Complexity: O(k)
|
|
115
142
|
*
|
|
116
143
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
144
|
* balancing the tree after each addition.
|
|
@@ -122,41 +149,40 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
122
149
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
150
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
151
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
152
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
126
153
|
* in the order they appear in the input.
|
|
127
154
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
128
155
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
156
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
157
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
131
158
|
*/
|
|
132
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
159
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
|
|
133
160
|
/**
|
|
134
|
-
* Time Complexity: O(n log n)
|
|
135
|
-
* Space Complexity: O(n)
|
|
161
|
+
* Time Complexity: O(n log n)
|
|
162
|
+
* Space Complexity: O(n)
|
|
163
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
136
164
|
*/
|
|
137
165
|
/**
|
|
138
|
-
* Time Complexity: O(log n)
|
|
139
|
-
* Space Complexity: O(
|
|
166
|
+
* Time Complexity: O(n log n)
|
|
167
|
+
* Space Complexity: O(n)
|
|
140
168
|
*
|
|
141
169
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
142
170
|
* leftmost node if the comparison result is greater than.
|
|
143
171
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
144
172
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
145
173
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
146
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
147
|
-
* be performed. It can have one of the following values:
|
|
148
174
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
149
175
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
150
176
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
151
177
|
*/
|
|
152
|
-
lastKey(beginRoot?:
|
|
178
|
+
lastKey(beginRoot?: KeyOrNodeOrEntry<K, V, N>): K | undefined;
|
|
153
179
|
/**
|
|
154
|
-
* Time Complexity: O(log n)
|
|
155
|
-
* Space Complexity: O(1)
|
|
180
|
+
* Time Complexity: O(log n)
|
|
181
|
+
* Space Complexity: O(1)
|
|
156
182
|
*/
|
|
157
183
|
/**
|
|
158
|
-
* Time Complexity: O(log n)
|
|
159
|
-
* Space Complexity: O(
|
|
184
|
+
* Time Complexity: O(log n)
|
|
185
|
+
* Space Complexity: O(1)
|
|
160
186
|
*
|
|
161
187
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
162
188
|
* either recursive or iterative methods.
|
|
@@ -170,29 +196,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
170
196
|
*/
|
|
171
197
|
getNodeByKey(key: K, iterationType?: IterationType): N | undefined;
|
|
172
198
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
*
|
|
181
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
182
|
-
*/
|
|
183
|
-
/**
|
|
184
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
185
|
-
* otherwise it returns the key itself.
|
|
186
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
187
|
-
* `undefined`.
|
|
188
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
189
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
190
|
-
* @returns either a node object (N) or undefined.
|
|
191
|
-
*/
|
|
192
|
-
ensureNode(key: BSTNKeyOrNode<K, N>, iterationType?: IterationType): N | undefined;
|
|
193
|
-
/**
|
|
194
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
195
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
199
|
+
* Time Complexity: O(log n)
|
|
200
|
+
* Space Complexity: O(log n)
|
|
201
|
+
* 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.
|
|
202
|
+
* /
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Time Complexity: O(log n)
|
|
206
|
+
* Space Complexity: O(log n)
|
|
196
207
|
*
|
|
197
208
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
198
209
|
* recursive or iterative approach.
|
|
@@ -213,14 +224,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
213
224
|
* performed on the binary tree. It can have two possible values:
|
|
214
225
|
* @returns The method returns an array of nodes (`N[]`).
|
|
215
226
|
*/
|
|
216
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?:
|
|
227
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): N[];
|
|
217
228
|
/**
|
|
218
|
-
* Time Complexity: O(log n)
|
|
219
|
-
* Space Complexity: O(log n)
|
|
229
|
+
* Time Complexity: O(log n)
|
|
230
|
+
* Space Complexity: O(log n)
|
|
231
|
+
* 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.
|
|
220
232
|
*/
|
|
221
233
|
/**
|
|
222
|
-
* Time Complexity: O(log n)
|
|
223
|
-
* Space Complexity: O(log n)
|
|
234
|
+
* Time Complexity: O(log n)
|
|
235
|
+
* Space Complexity: O(log n)
|
|
224
236
|
*
|
|
225
237
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
226
238
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -239,14 +251,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
239
251
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
240
252
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
241
253
|
*/
|
|
242
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?:
|
|
254
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: KeyOrNodeOrEntry<K, V, N>, iterationType?: IterationType): ReturnType<C>[];
|
|
243
255
|
/**
|
|
244
|
-
* Time Complexity: O(log n)
|
|
245
|
-
* Space Complexity: O(log n)
|
|
256
|
+
* Time Complexity: O(log n)
|
|
257
|
+
* Space Complexity: O(log n)
|
|
246
258
|
*/
|
|
247
259
|
/**
|
|
248
|
-
* Time Complexity: O(n)
|
|
249
|
-
* Space Complexity: O(n)
|
|
260
|
+
* Time Complexity: O(log n)
|
|
261
|
+
* Space Complexity: O(log n)
|
|
250
262
|
*
|
|
251
263
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
252
264
|
* ensures the tree is perfectly balanced.
|
|
@@ -270,8 +282,8 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
270
282
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
271
283
|
*/
|
|
272
284
|
/**
|
|
273
|
-
* Time Complexity: O(n)
|
|
274
|
-
* Space Complexity: O(log n)
|
|
285
|
+
* Time Complexity: O(n)
|
|
286
|
+
* Space Complexity: O(log n)
|
|
275
287
|
*
|
|
276
288
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
277
289
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -57,15 +57,15 @@ exports.BSTNode = BSTNode;
|
|
|
57
57
|
class BST extends binary_tree_1.BinaryTree {
|
|
58
58
|
/**
|
|
59
59
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
60
|
-
* the tree with optional
|
|
61
|
-
* @param [
|
|
60
|
+
* the tree with optional nodes and options.
|
|
61
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
62
62
|
* binary search tree.
|
|
63
63
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
64
64
|
* configuration options for the binary search tree. It can have the following properties:
|
|
65
65
|
*/
|
|
66
|
-
constructor(
|
|
66
|
+
constructor(nodes, options) {
|
|
67
67
|
super([], options);
|
|
68
|
-
this._variant = types_1.BSTVariant.
|
|
68
|
+
this._variant = types_1.BSTVariant.STANDARD;
|
|
69
69
|
if (options) {
|
|
70
70
|
const { variant } = options;
|
|
71
71
|
if (variant) {
|
|
@@ -73,8 +73,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
this._root = undefined;
|
|
76
|
-
if (
|
|
77
|
-
this.addMany(
|
|
76
|
+
if (nodes)
|
|
77
|
+
this.addMany(nodes);
|
|
78
78
|
}
|
|
79
79
|
get root() {
|
|
80
80
|
return this._root;
|
|
@@ -104,31 +104,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
104
104
|
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
105
105
|
}
|
|
106
106
|
/**
|
|
107
|
-
* The function
|
|
108
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
109
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
110
|
-
*/
|
|
111
|
-
isNode(exemplar) {
|
|
112
|
-
return exemplar instanceof BSTNode;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
107
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
116
108
|
* otherwise it returns undefined.
|
|
117
|
-
* @param
|
|
109
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
118
110
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
119
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
111
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
120
112
|
* @returns a node of type N or undefined.
|
|
121
113
|
*/
|
|
122
|
-
exemplarToNode(
|
|
114
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
123
115
|
let node;
|
|
124
|
-
if (
|
|
116
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
125
117
|
return;
|
|
126
118
|
}
|
|
127
|
-
else if (this.isNode(
|
|
128
|
-
node =
|
|
119
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
120
|
+
node = keyOrNodeOrEntry;
|
|
129
121
|
}
|
|
130
|
-
else if (this.isEntry(
|
|
131
|
-
const [key, value] =
|
|
122
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
123
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
132
124
|
if (key === undefined || key === null) {
|
|
133
125
|
return;
|
|
134
126
|
}
|
|
@@ -136,8 +128,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
136
128
|
node = this.createNode(key, value);
|
|
137
129
|
}
|
|
138
130
|
}
|
|
139
|
-
else if (this.isNotNodeInstance(
|
|
140
|
-
node = this.createNode(
|
|
131
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
132
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
141
133
|
}
|
|
142
134
|
else {
|
|
143
135
|
return;
|
|
@@ -145,12 +137,62 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
145
137
|
return node;
|
|
146
138
|
}
|
|
147
139
|
/**
|
|
148
|
-
* Time Complexity: O(log n)
|
|
149
|
-
* Space Complexity: O(
|
|
140
|
+
* Time Complexity: O(log n)
|
|
141
|
+
* Space Complexity: O(log n)
|
|
142
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
150
143
|
*/
|
|
151
144
|
/**
|
|
152
|
-
* Time Complexity: O(log n)
|
|
153
|
-
* Space Complexity: O(
|
|
145
|
+
* Time Complexity: O(log n)
|
|
146
|
+
* Space Complexity: O(log n)
|
|
147
|
+
*
|
|
148
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
149
|
+
* otherwise it returns the key itself.
|
|
150
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
151
|
+
* `undefined`.
|
|
152
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
153
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
154
|
+
* @returns either a node object (N) or undefined.
|
|
155
|
+
*/
|
|
156
|
+
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) {
|
|
157
|
+
let res;
|
|
158
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
159
|
+
res = keyOrNodeOrEntry;
|
|
160
|
+
}
|
|
161
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
162
|
+
if (keyOrNodeOrEntry[0])
|
|
163
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if (keyOrNodeOrEntry)
|
|
167
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
168
|
+
}
|
|
169
|
+
return res;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
173
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
174
|
+
* data type.
|
|
175
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
176
|
+
*/
|
|
177
|
+
isNotNodeInstance(potentialKey) {
|
|
178
|
+
return !(potentialKey instanceof BSTNode);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
182
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
183
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
184
|
+
*/
|
|
185
|
+
isNode(keyOrNodeOrEntry) {
|
|
186
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Time Complexity: O(log n)
|
|
190
|
+
* Space Complexity: O(1)
|
|
191
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
192
|
+
*/
|
|
193
|
+
/**
|
|
194
|
+
* Time Complexity: O(log n)
|
|
195
|
+
* Space Complexity: O(1)
|
|
154
196
|
*
|
|
155
197
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
156
198
|
* or inserting a new node if the key is unique.
|
|
@@ -163,11 +205,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
163
205
|
add(keyOrNodeOrEntry, value) {
|
|
164
206
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
165
207
|
if (newNode === undefined)
|
|
166
|
-
return;
|
|
208
|
+
return false;
|
|
167
209
|
if (this.root === undefined) {
|
|
168
210
|
this._setRoot(newNode);
|
|
169
211
|
this._size++;
|
|
170
|
-
return
|
|
212
|
+
return true;
|
|
171
213
|
}
|
|
172
214
|
let current = this.root;
|
|
173
215
|
while (current !== undefined) {
|
|
@@ -175,7 +217,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
175
217
|
// if (current !== newNode) {
|
|
176
218
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
177
219
|
this._replaceNode(current, newNode);
|
|
178
|
-
return
|
|
220
|
+
return true;
|
|
179
221
|
// } else {
|
|
180
222
|
// The key value is the same and the reference is the same, replace the entire node
|
|
181
223
|
// this._replaceNode(current, newNode);
|
|
@@ -187,7 +229,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
187
229
|
current.left = newNode;
|
|
188
230
|
newNode.parent = current;
|
|
189
231
|
this._size++;
|
|
190
|
-
return
|
|
232
|
+
return true;
|
|
191
233
|
}
|
|
192
234
|
current = current.left;
|
|
193
235
|
}
|
|
@@ -196,20 +238,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
196
238
|
current.right = newNode;
|
|
197
239
|
newNode.parent = current;
|
|
198
240
|
this._size++;
|
|
199
|
-
return
|
|
241
|
+
return true;
|
|
200
242
|
}
|
|
201
243
|
current = current.right;
|
|
202
244
|
}
|
|
203
245
|
}
|
|
204
|
-
return
|
|
246
|
+
return false;
|
|
205
247
|
}
|
|
206
248
|
/**
|
|
207
|
-
* Time Complexity: O(k log n)
|
|
208
|
-
* Space Complexity: O(k)
|
|
249
|
+
* Time Complexity: O(k log n)
|
|
250
|
+
* Space Complexity: O(k)
|
|
251
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
209
252
|
*/
|
|
210
253
|
/**
|
|
211
|
-
* Time Complexity: O(k log n)
|
|
212
|
-
* Space Complexity: O(k)
|
|
254
|
+
* Time Complexity: O(k log n)
|
|
255
|
+
* Space Complexity: O(k)
|
|
213
256
|
*
|
|
214
257
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
215
258
|
* balancing the tree after each addition.
|
|
@@ -220,7 +263,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
220
263
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
221
264
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
222
265
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
223
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
266
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
224
267
|
* in the order they appear in the input.
|
|
225
268
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
226
269
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -302,20 +345,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
302
345
|
return inserted;
|
|
303
346
|
}
|
|
304
347
|
/**
|
|
305
|
-
* Time Complexity: O(n log n)
|
|
306
|
-
* Space Complexity: O(n)
|
|
348
|
+
* Time Complexity: O(n log n)
|
|
349
|
+
* Space Complexity: O(n)
|
|
350
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
307
351
|
*/
|
|
308
352
|
/**
|
|
309
|
-
* Time Complexity: O(log n)
|
|
310
|
-
* Space Complexity: O(
|
|
353
|
+
* Time Complexity: O(n log n)
|
|
354
|
+
* Space Complexity: O(n)
|
|
311
355
|
*
|
|
312
356
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
313
357
|
* leftmost node if the comparison result is greater than.
|
|
314
358
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
315
359
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
316
360
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
317
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
318
|
-
* be performed. It can have one of the following values:
|
|
319
361
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
320
362
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
321
363
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -324,7 +366,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
324
366
|
let current = this.ensureNode(beginRoot);
|
|
325
367
|
if (!current)
|
|
326
368
|
return undefined;
|
|
327
|
-
if (this._variant === types_1.BSTVariant.
|
|
369
|
+
if (this._variant === types_1.BSTVariant.STANDARD) {
|
|
328
370
|
// For BSTVariant.MIN, find the rightmost node
|
|
329
371
|
while (current.right !== undefined) {
|
|
330
372
|
current = current.right;
|
|
@@ -339,12 +381,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
339
381
|
return current.key;
|
|
340
382
|
}
|
|
341
383
|
/**
|
|
342
|
-
* Time Complexity: O(log n)
|
|
343
|
-
* Space Complexity: O(1)
|
|
384
|
+
* Time Complexity: O(log n)
|
|
385
|
+
* Space Complexity: O(1)
|
|
344
386
|
*/
|
|
345
387
|
/**
|
|
346
|
-
* Time Complexity: O(log n)
|
|
347
|
-
* Space Complexity: O(
|
|
388
|
+
* Time Complexity: O(log n)
|
|
389
|
+
* Space Complexity: O(1)
|
|
348
390
|
*
|
|
349
391
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
350
392
|
* either recursive or iterative methods.
|
|
@@ -388,33 +430,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
388
430
|
}
|
|
389
431
|
}
|
|
390
432
|
/**
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
401
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
402
|
-
*/
|
|
403
|
-
/**
|
|
404
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
405
|
-
* otherwise it returns the key itself.
|
|
406
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
407
|
-
* `undefined`.
|
|
408
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
409
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
410
|
-
* @returns either a node object (N) or undefined.
|
|
411
|
-
*/
|
|
412
|
-
ensureNode(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
413
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
417
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
433
|
+
* Time Complexity: O(log n)
|
|
434
|
+
* Space Complexity: O(log n)
|
|
435
|
+
* 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.
|
|
436
|
+
* /
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Time Complexity: O(log n)
|
|
440
|
+
* Space Complexity: O(log n)
|
|
418
441
|
*
|
|
419
442
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
420
443
|
* recursive or iterative approach.
|
|
@@ -492,12 +515,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
492
515
|
return ans;
|
|
493
516
|
}
|
|
494
517
|
/**
|
|
495
|
-
* Time Complexity: O(log n)
|
|
496
|
-
* Space Complexity: O(log n)
|
|
518
|
+
* Time Complexity: O(log n)
|
|
519
|
+
* Space Complexity: O(log n)
|
|
520
|
+
* 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.
|
|
497
521
|
*/
|
|
498
522
|
/**
|
|
499
|
-
* Time Complexity: O(log n)
|
|
500
|
-
* Space Complexity: O(log n)
|
|
523
|
+
* Time Complexity: O(log n)
|
|
524
|
+
* Space Complexity: O(log n)
|
|
501
525
|
*
|
|
502
526
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
503
527
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -557,12 +581,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
557
581
|
}
|
|
558
582
|
}
|
|
559
583
|
/**
|
|
560
|
-
* Time Complexity: O(log n)
|
|
561
|
-
* Space Complexity: O(log n)
|
|
584
|
+
* Time Complexity: O(log n)
|
|
585
|
+
* Space Complexity: O(log n)
|
|
562
586
|
*/
|
|
563
587
|
/**
|
|
564
|
-
* Time Complexity: O(n)
|
|
565
|
-
* Space Complexity: O(n)
|
|
588
|
+
* Time Complexity: O(log n)
|
|
589
|
+
* Space Complexity: O(log n)
|
|
566
590
|
*
|
|
567
591
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
568
592
|
* ensures the tree is perfectly balanced.
|
|
@@ -621,8 +645,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
621
645
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
622
646
|
*/
|
|
623
647
|
/**
|
|
624
|
-
* Time Complexity: O(n)
|
|
625
|
-
* Space Complexity: O(log n)
|
|
648
|
+
* Time Complexity: O(n)
|
|
649
|
+
* Space Complexity: O(log n)
|
|
626
650
|
*
|
|
627
651
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
628
652
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -692,7 +716,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
692
716
|
_compare(a, b) {
|
|
693
717
|
const extractedA = this.extractor(a);
|
|
694
718
|
const extractedB = this.extractor(b);
|
|
695
|
-
const compared = this.variant === types_1.BSTVariant.
|
|
719
|
+
const compared = this.variant === types_1.BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
696
720
|
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq;
|
|
697
721
|
}
|
|
698
722
|
}
|