data-structure-typed 1.42.8 → 1.43.0
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/CHANGELOG.md +1 -1
- package/README.md +18 -18
- package/benchmark/report.html +12 -12
- package/benchmark/report.json +106 -106
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/cjs/src/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js +415 -236
- package/dist/cjs/src/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/cjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/cjs/src/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/cjs/src/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/cjs/src/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/cjs/src/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/cjs/src/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/cjs/src/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js +175 -12
- package/dist/cjs/src/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/cjs/src/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/cjs/src/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/cjs/src/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js +113 -3
- package/dist/cjs/src/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js +87 -0
- package/dist/cjs/src/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js +42 -0
- package/dist/cjs/src/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/cjs/src/data-structures/trie/trie.js +76 -1
- package/dist/cjs/src/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.d.ts +88 -23
- package/dist/mjs/src/data-structures/binary-tree/avl-tree.js +88 -23
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.d.ts +180 -74
- package/dist/mjs/src/data-structures/binary-tree/binary-tree.js +415 -236
- package/dist/mjs/src/data-structures/binary-tree/bst.d.ts +121 -66
- package/dist/mjs/src/data-structures/binary-tree/bst.js +121 -67
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.d.ts +72 -5
- package/dist/mjs/src/data-structures/binary-tree/rb-tree.js +95 -18
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.d.ts +82 -43
- package/dist/mjs/src/data-structures/binary-tree/tree-multimap.js +82 -43
- package/dist/mjs/src/data-structures/graph/abstract-graph.d.ts +139 -36
- package/dist/mjs/src/data-structures/graph/abstract-graph.js +147 -36
- package/dist/mjs/src/data-structures/graph/directed-graph.d.ts +126 -0
- package/dist/mjs/src/data-structures/graph/directed-graph.js +126 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.d.ts +63 -0
- package/dist/mjs/src/data-structures/graph/undirected-graph.js +63 -0
- package/dist/mjs/src/data-structures/heap/heap.d.ts +175 -12
- package/dist/mjs/src/data-structures/heap/heap.js +175 -12
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.d.ts +203 -0
- package/dist/mjs/src/data-structures/linked-list/doubly-linked-list.js +203 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.d.ts +182 -0
- package/dist/mjs/src/data-structures/linked-list/singly-linked-list.js +182 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.d.ts +64 -0
- package/dist/mjs/src/data-structures/linked-list/skip-linked-list.js +64 -0
- package/dist/mjs/src/data-structures/queue/deque.d.ts +113 -3
- package/dist/mjs/src/data-structures/queue/deque.js +113 -3
- package/dist/mjs/src/data-structures/queue/queue.d.ts +87 -0
- package/dist/mjs/src/data-structures/queue/queue.js +87 -0
- package/dist/mjs/src/data-structures/stack/stack.d.ts +42 -0
- package/dist/mjs/src/data-structures/stack/stack.js +42 -0
- package/dist/mjs/src/data-structures/trie/trie.d.ts +76 -0
- package/dist/mjs/src/data-structures/trie/trie.js +76 -1
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +97 -23
- package/src/data-structures/binary-tree/binary-tree.ts +465 -256
- 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
- package/test/integration/index.html +24 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +73 -5
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -1
|
@@ -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:
|
|
@@ -50,10 +50,9 @@ class BSTNode extends binary_tree_1.BinaryTreeNode {
|
|
|
50
50
|
exports.BSTNode = BSTNode;
|
|
51
51
|
class BST extends binary_tree_1.BinaryTree {
|
|
52
52
|
/**
|
|
53
|
-
* The constructor function initializes a binary search tree
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* binary search tree.
|
|
53
|
+
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
54
|
+
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
55
|
+
* for the binary search tree.
|
|
57
56
|
*/
|
|
58
57
|
constructor(options) {
|
|
59
58
|
super(options);
|
|
@@ -84,14 +83,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
84
83
|
return new BSTNode(key, value);
|
|
85
84
|
}
|
|
86
85
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
86
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
87
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
88
|
+
*/
|
|
89
|
+
/**
|
|
90
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
91
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
92
|
+
*
|
|
93
|
+
* The `add` function adds a new node to a binary search tree based on the provided key and value.
|
|
94
|
+
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
95
|
+
* following types:
|
|
96
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
97
|
+
* key or node being added to the binary search tree.
|
|
98
|
+
* @returns The method `add` returns a node (`N`) that was inserted into the binary search tree. If
|
|
99
|
+
* no node was inserted, it returns `undefined`.
|
|
95
100
|
*/
|
|
96
101
|
add(keyOrNode, value) {
|
|
97
102
|
if (keyOrNode === null)
|
|
@@ -171,17 +176,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
171
176
|
return inserted;
|
|
172
177
|
}
|
|
173
178
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
179
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
180
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
181
|
+
*/
|
|
182
|
+
/**
|
|
183
|
+
* Time Complexity: O(n log n) - Adding each element individually in a balanced tree.
|
|
184
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
185
|
+
*
|
|
186
|
+
* The `addMany` function is used to efficiently add multiple keys or nodes with corresponding data
|
|
187
|
+
* to a binary search tree.
|
|
188
|
+
* @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes to be added to the
|
|
189
|
+
* binary search tree. Each element can be of type `BTNKey` (binary tree node key), `N` (binary tree
|
|
190
|
+
* node), or `undefined`.
|
|
191
|
+
* @param {(V | undefined)[]} [data] - An optional array of values to associate with the keys or
|
|
192
|
+
* nodes being added. If provided, the length of the `data` array must be the same as the length of
|
|
193
|
+
* the `keysOrNodes` array.
|
|
194
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
195
|
+
* adding the nodes. The default value is `true`.
|
|
196
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
197
|
+
* type of iteration to use when adding multiple keys or nodes to the binary search tree. It has a
|
|
198
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified in the
|
|
199
|
+
* current instance of the binary search tree
|
|
200
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
185
201
|
*/
|
|
186
202
|
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
187
203
|
// TODO this addMany function is inefficient, it should be optimized
|
|
@@ -253,16 +269,20 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
253
269
|
return inserted;
|
|
254
270
|
}
|
|
255
271
|
/**
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
*
|
|
272
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
273
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
274
|
+
*/
|
|
275
|
+
/**
|
|
276
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
277
|
+
* Space Complexity: O(1) - Constant space is used.
|
|
278
|
+
*
|
|
279
|
+
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
280
|
+
* leftmost node if the comparison result is greater than.
|
|
281
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
282
|
+
* type `BTNKey`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
283
|
+
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
263
284
|
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
264
|
-
* be performed
|
|
265
|
-
* pre-order, in-order, or post-order.
|
|
285
|
+
* be performed. It can have one of the following values:
|
|
266
286
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
267
287
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
268
288
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
@@ -276,10 +296,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
276
296
|
return this.getRightMost(beginRoot, iterationType)?.key ?? 0;
|
|
277
297
|
}
|
|
278
298
|
/**
|
|
299
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
300
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
301
|
+
*/
|
|
302
|
+
/**
|
|
303
|
+
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
304
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
305
|
+
*
|
|
279
306
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
280
307
|
* either recursive or iterative methods.
|
|
281
308
|
* @param {BTNKey} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
282
|
-
* It is used to
|
|
309
|
+
* It is used to identify the node that we want to retrieve.
|
|
283
310
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
284
311
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
285
312
|
* values:
|
|
@@ -330,25 +357,31 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
330
357
|
return this.isNodeKey(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
331
358
|
}
|
|
332
359
|
/**
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
* @param
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
360
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
361
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
362
|
+
*/
|
|
363
|
+
/**
|
|
364
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
365
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
366
|
+
*
|
|
367
|
+
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
368
|
+
* recursive or iterative approach.
|
|
369
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
370
|
+
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
371
|
+
* callback function `C`.
|
|
372
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `N` as its
|
|
373
|
+
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
374
|
+
* function type that extends the `BTNCallback<N>` type. The `BTNCallback<N>` type is
|
|
375
|
+
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
376
|
+
* first node that matches the identifier. If set to true, the function will return an array
|
|
377
|
+
* containing only the first matching node. If set to false (default), the function will continue
|
|
378
|
+
* searching for all nodes that match the identifier and return an array containing
|
|
379
|
+
* @param {BTNKey | N | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
380
|
+
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
381
|
+
* traversal will start from the root of the tree.
|
|
382
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
383
|
+
* performed on the binary tree. It can have two possible values:
|
|
384
|
+
* @returns The method returns an array of nodes (`N[]`).
|
|
352
385
|
*/
|
|
353
386
|
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
354
387
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
@@ -406,23 +439,30 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
406
439
|
}
|
|
407
440
|
return ans;
|
|
408
441
|
}
|
|
409
|
-
// --- start additional functions
|
|
410
442
|
/**
|
|
411
|
-
*
|
|
412
|
-
*
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
*
|
|
443
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
444
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
445
|
+
*/
|
|
446
|
+
/**
|
|
447
|
+
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
448
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
449
|
+
*
|
|
450
|
+
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
451
|
+
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
452
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
453
|
+
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
454
|
+
* parameter of type `N` (the node type) and returns a value of any type.
|
|
416
455
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
417
|
-
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* @param iterationType - The `iterationType` parameter determines
|
|
424
|
-
*
|
|
425
|
-
* @returns The function `lesserOrGreaterTraverse` returns an array of
|
|
456
|
+
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
457
|
+
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
458
|
+
* `lesserOrGreater` are
|
|
459
|
+
* @param {BTNKey | N | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
460
|
+
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
461
|
+
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
462
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
463
|
+
* performed on the binary tree. It can have two possible values:
|
|
464
|
+
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
465
|
+
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
426
466
|
*/
|
|
427
467
|
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
428
468
|
targetNode = this.ensureNotKey(targetNode);
|
|
@@ -474,6 +514,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
474
514
|
* 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).
|
|
475
515
|
*/
|
|
476
516
|
/**
|
|
517
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
518
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
519
|
+
*/
|
|
520
|
+
/**
|
|
521
|
+
* Time Complexity: O(n) - Building a balanced tree from a sorted array.
|
|
522
|
+
* Space Complexity: O(n) - Additional space is required for the sorted array.
|
|
523
|
+
*
|
|
477
524
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
478
525
|
* ensures the tree is perfectly balanced.
|
|
479
526
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
@@ -519,6 +566,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
519
566
|
}
|
|
520
567
|
}
|
|
521
568
|
/**
|
|
569
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
570
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
571
|
+
*/
|
|
572
|
+
/**
|
|
573
|
+
* Time Complexity: O(n) - Visiting each node once.
|
|
574
|
+
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
575
|
+
*
|
|
522
576
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
523
577
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
524
578
|
* to check if the AVL tree is balanced. It can have two possible values:
|