deque-typed 1.49.5 → 1.49.7
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 +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- 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 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- 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/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -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 keysOrNodesOrEntries and options.
|
|
49
|
+
* @param [keysOrNodesOrEntries] - 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(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: 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,24 +57,23 @@ 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 keysOrNodesOrEntries and options.
|
|
61
|
+
* @param [keysOrNodesOrEntries] - 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(keysOrNodesOrEntries = [], 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
|
-
if (variant)
|
|
71
|
+
if (variant)
|
|
72
72
|
this._variant = variant;
|
|
73
|
-
}
|
|
74
73
|
}
|
|
75
74
|
this._root = undefined;
|
|
76
|
-
if (
|
|
77
|
-
this.addMany(
|
|
75
|
+
if (keysOrNodesOrEntries)
|
|
76
|
+
this.addMany(keysOrNodesOrEntries);
|
|
78
77
|
}
|
|
79
78
|
get root() {
|
|
80
79
|
return this._root;
|
|
@@ -104,31 +103,23 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
104
103
|
return new BST([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
105
104
|
}
|
|
106
105
|
/**
|
|
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,
|
|
106
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
116
107
|
* otherwise it returns undefined.
|
|
117
|
-
* @param
|
|
108
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
118
109
|
* @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
|
|
110
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
120
111
|
* @returns a node of type N or undefined.
|
|
121
112
|
*/
|
|
122
|
-
exemplarToNode(
|
|
113
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
123
114
|
let node;
|
|
124
|
-
if (
|
|
115
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
125
116
|
return;
|
|
126
117
|
}
|
|
127
|
-
else if (this.isNode(
|
|
128
|
-
node =
|
|
118
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
119
|
+
node = keyOrNodeOrEntry;
|
|
129
120
|
}
|
|
130
|
-
else if (this.isEntry(
|
|
131
|
-
const [key, value] =
|
|
121
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
122
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
132
123
|
if (key === undefined || key === null) {
|
|
133
124
|
return;
|
|
134
125
|
}
|
|
@@ -136,8 +127,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
136
127
|
node = this.createNode(key, value);
|
|
137
128
|
}
|
|
138
129
|
}
|
|
139
|
-
else if (this.isNotNodeInstance(
|
|
140
|
-
node = this.createNode(
|
|
130
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
131
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
141
132
|
}
|
|
142
133
|
else {
|
|
143
134
|
return;
|
|
@@ -145,12 +136,62 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
145
136
|
return node;
|
|
146
137
|
}
|
|
147
138
|
/**
|
|
148
|
-
* Time Complexity: O(log n)
|
|
149
|
-
* Space Complexity: O(
|
|
139
|
+
* Time Complexity: O(log n)
|
|
140
|
+
* Space Complexity: O(log n)
|
|
141
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(log n)
|
|
145
|
+
* Space Complexity: O(log n)
|
|
146
|
+
*
|
|
147
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
148
|
+
* otherwise it returns the key itself.
|
|
149
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
150
|
+
* `undefined`.
|
|
151
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
152
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
153
|
+
* @returns either a node object (N) or undefined.
|
|
154
|
+
*/
|
|
155
|
+
ensureNode(keyOrNodeOrEntry, iterationType = types_1.IterationType.ITERATIVE) {
|
|
156
|
+
let res;
|
|
157
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
158
|
+
res = keyOrNodeOrEntry;
|
|
159
|
+
}
|
|
160
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
161
|
+
if (keyOrNodeOrEntry[0])
|
|
162
|
+
res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
if (keyOrNodeOrEntry)
|
|
166
|
+
res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
167
|
+
}
|
|
168
|
+
return res;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
172
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
173
|
+
* data type.
|
|
174
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
175
|
+
*/
|
|
176
|
+
isNotNodeInstance(potentialKey) {
|
|
177
|
+
return !(potentialKey instanceof BSTNode);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
181
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
182
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
183
|
+
*/
|
|
184
|
+
isNode(keyOrNodeOrEntry) {
|
|
185
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Time Complexity: O(log n)
|
|
189
|
+
* Space Complexity: O(1)
|
|
190
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
150
191
|
*/
|
|
151
192
|
/**
|
|
152
|
-
* Time Complexity: O(log n)
|
|
153
|
-
* Space Complexity: O(1)
|
|
193
|
+
* Time Complexity: O(log n)
|
|
194
|
+
* Space Complexity: O(1)
|
|
154
195
|
*
|
|
155
196
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
156
197
|
* or inserting a new node if the key is unique.
|
|
@@ -163,11 +204,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
163
204
|
add(keyOrNodeOrEntry, value) {
|
|
164
205
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
165
206
|
if (newNode === undefined)
|
|
166
|
-
return;
|
|
207
|
+
return false;
|
|
167
208
|
if (this.root === undefined) {
|
|
168
209
|
this._setRoot(newNode);
|
|
169
210
|
this._size++;
|
|
170
|
-
return
|
|
211
|
+
return true;
|
|
171
212
|
}
|
|
172
213
|
let current = this.root;
|
|
173
214
|
while (current !== undefined) {
|
|
@@ -175,7 +216,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
175
216
|
// if (current !== newNode) {
|
|
176
217
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
177
218
|
this._replaceNode(current, newNode);
|
|
178
|
-
return
|
|
219
|
+
return true;
|
|
179
220
|
// } else {
|
|
180
221
|
// The key value is the same and the reference is the same, replace the entire node
|
|
181
222
|
// this._replaceNode(current, newNode);
|
|
@@ -187,7 +228,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
187
228
|
current.left = newNode;
|
|
188
229
|
newNode.parent = current;
|
|
189
230
|
this._size++;
|
|
190
|
-
return
|
|
231
|
+
return true;
|
|
191
232
|
}
|
|
192
233
|
current = current.left;
|
|
193
234
|
}
|
|
@@ -196,20 +237,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
196
237
|
current.right = newNode;
|
|
197
238
|
newNode.parent = current;
|
|
198
239
|
this._size++;
|
|
199
|
-
return
|
|
240
|
+
return true;
|
|
200
241
|
}
|
|
201
242
|
current = current.right;
|
|
202
243
|
}
|
|
203
244
|
}
|
|
204
|
-
return
|
|
245
|
+
return false;
|
|
205
246
|
}
|
|
206
247
|
/**
|
|
207
|
-
* Time Complexity: O(k log n)
|
|
208
|
-
* Space Complexity: O(k)
|
|
248
|
+
* Time Complexity: O(k log n)
|
|
249
|
+
* Space Complexity: O(k)
|
|
250
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
209
251
|
*/
|
|
210
252
|
/**
|
|
211
|
-
* Time Complexity: O(k log n)
|
|
212
|
-
* Space Complexity: O(k)
|
|
253
|
+
* Time Complexity: O(k log n)
|
|
254
|
+
* Space Complexity: O(k)
|
|
213
255
|
*
|
|
214
256
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
215
257
|
* balancing the tree after each addition.
|
|
@@ -220,7 +262,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
220
262
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
221
263
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
222
264
|
* 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
|
|
265
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
224
266
|
* in the order they appear in the input.
|
|
225
267
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
226
268
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -302,20 +344,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
302
344
|
return inserted;
|
|
303
345
|
}
|
|
304
346
|
/**
|
|
305
|
-
* Time Complexity: O(n log n)
|
|
306
|
-
* Space Complexity: O(n)
|
|
347
|
+
* Time Complexity: O(n log n)
|
|
348
|
+
* Space Complexity: O(n)
|
|
349
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
307
350
|
*/
|
|
308
351
|
/**
|
|
309
|
-
* Time Complexity: O(log n)
|
|
310
|
-
* Space Complexity: O(
|
|
352
|
+
* Time Complexity: O(n log n)
|
|
353
|
+
* Space Complexity: O(n)
|
|
311
354
|
*
|
|
312
355
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
313
356
|
* leftmost node if the comparison result is greater than.
|
|
314
357
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
315
358
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
316
359
|
* 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
360
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
320
361
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
321
362
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -324,7 +365,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
324
365
|
let current = this.ensureNode(beginRoot);
|
|
325
366
|
if (!current)
|
|
326
367
|
return undefined;
|
|
327
|
-
if (this._variant === types_1.BSTVariant.
|
|
368
|
+
if (this._variant === types_1.BSTVariant.STANDARD) {
|
|
328
369
|
// For BSTVariant.MIN, find the rightmost node
|
|
329
370
|
while (current.right !== undefined) {
|
|
330
371
|
current = current.right;
|
|
@@ -339,12 +380,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
339
380
|
return current.key;
|
|
340
381
|
}
|
|
341
382
|
/**
|
|
342
|
-
* Time Complexity: O(log n)
|
|
343
|
-
* Space Complexity: O(1)
|
|
383
|
+
* Time Complexity: O(log n)
|
|
384
|
+
* Space Complexity: O(1)
|
|
344
385
|
*/
|
|
345
386
|
/**
|
|
346
|
-
* Time Complexity: O(log n)
|
|
347
|
-
* Space Complexity: O(
|
|
387
|
+
* Time Complexity: O(log n)
|
|
388
|
+
* Space Complexity: O(1)
|
|
348
389
|
*
|
|
349
390
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
350
391
|
* either recursive or iterative methods.
|
|
@@ -388,33 +429,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
388
429
|
}
|
|
389
430
|
}
|
|
390
431
|
/**
|
|
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.
|
|
432
|
+
* Time Complexity: O(log n)
|
|
433
|
+
* Space Complexity: O(log n)
|
|
434
|
+
* 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.
|
|
435
|
+
* /
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Time Complexity: O(log n)
|
|
439
|
+
* Space Complexity: O(log n)
|
|
418
440
|
*
|
|
419
441
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
420
442
|
* recursive or iterative approach.
|
|
@@ -492,12 +514,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
492
514
|
return ans;
|
|
493
515
|
}
|
|
494
516
|
/**
|
|
495
|
-
* Time Complexity: O(log n)
|
|
496
|
-
* Space Complexity: O(log n)
|
|
517
|
+
* Time Complexity: O(log n)
|
|
518
|
+
* Space Complexity: O(log n)
|
|
519
|
+
* 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
520
|
*/
|
|
498
521
|
/**
|
|
499
|
-
* Time Complexity: O(log n)
|
|
500
|
-
* Space Complexity: O(log n)
|
|
522
|
+
* Time Complexity: O(log n)
|
|
523
|
+
* Space Complexity: O(log n)
|
|
501
524
|
*
|
|
502
525
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
503
526
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -557,12 +580,12 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
557
580
|
}
|
|
558
581
|
}
|
|
559
582
|
/**
|
|
560
|
-
* Time Complexity: O(log n)
|
|
561
|
-
* Space Complexity: O(log n)
|
|
583
|
+
* Time Complexity: O(log n)
|
|
584
|
+
* Space Complexity: O(log n)
|
|
562
585
|
*/
|
|
563
586
|
/**
|
|
564
|
-
* Time Complexity: O(n)
|
|
565
|
-
* Space Complexity: O(n)
|
|
587
|
+
* Time Complexity: O(log n)
|
|
588
|
+
* Space Complexity: O(log n)
|
|
566
589
|
*
|
|
567
590
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
568
591
|
* ensures the tree is perfectly balanced.
|
|
@@ -621,8 +644,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
621
644
|
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
622
645
|
*/
|
|
623
646
|
/**
|
|
624
|
-
* Time Complexity: O(n)
|
|
625
|
-
* Space Complexity: O(log n)
|
|
647
|
+
* Time Complexity: O(n)
|
|
648
|
+
* Space Complexity: O(log n)
|
|
626
649
|
*
|
|
627
650
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
628
651
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -692,7 +715,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
692
715
|
_compare(a, b) {
|
|
693
716
|
const extractedA = this.extractor(a);
|
|
694
717
|
const extractedB = this.extractor(b);
|
|
695
|
-
const compared = this.variant === types_1.BSTVariant.
|
|
718
|
+
const compared = this.variant === types_1.BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
696
719
|
return compared > 0 ? types_1.CP.gt : compared < 0 ? types_1.CP.lt : types_1.CP.eq;
|
|
697
720
|
}
|
|
698
721
|
}
|