linked-list-typed 1.51.7 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
+
import { BTNEntry } from '../../types';
|
|
9
10
|
import { IBinaryTree } from '../../interfaces';
|
|
10
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
-
export declare class AVLTreeMultiMapNode<K
|
|
12
|
+
export declare class AVLTreeMultiMapNode<K extends Comparable, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
12
13
|
/**
|
|
13
14
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
14
15
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -36,8 +37,16 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
36
37
|
/**
|
|
37
38
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
38
39
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K
|
|
40
|
-
|
|
40
|
+
export declare class AVLTreeMultiMap<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMapNested<K, V, R, NODE>>> extends AVLTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
41
|
+
/**
|
|
42
|
+
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
43
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
44
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
45
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
46
|
+
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
47
|
+
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
48
|
+
*/
|
|
49
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeMultiMapOptions<K, V, R>);
|
|
41
50
|
protected _count: number;
|
|
42
51
|
/**
|
|
43
52
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
@@ -59,44 +68,46 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
59
68
|
*/
|
|
60
69
|
getComputedCount(): number;
|
|
61
70
|
/**
|
|
62
|
-
* The function creates a new
|
|
63
|
-
* @param {K} key - The key parameter
|
|
64
|
-
*
|
|
65
|
-
* @param {
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
71
|
+
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
|
|
72
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
73
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
74
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
75
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
76
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
77
|
+
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
|
|
78
|
+
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
79
|
+
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
69
80
|
*/
|
|
70
81
|
createNode(key: K, value?: V, count?: number): NODE;
|
|
71
82
|
/**
|
|
72
83
|
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
73
84
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
74
|
-
* configuration options for creating the
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
84
|
-
*
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
|
|
86
|
+
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
87
|
+
* object.
|
|
88
|
+
*/
|
|
89
|
+
createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE;
|
|
90
|
+
/**
|
|
91
|
+
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
92
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
93
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
94
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
95
|
+
* an instance of the `AVLTreeMultiMapNode` class.
|
|
96
|
+
*/
|
|
97
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
98
|
+
/**
|
|
99
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
100
|
+
* a node object.
|
|
101
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
102
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
103
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
104
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
105
|
+
* value is provided, it will default to `undefined`.
|
|
88
106
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
89
|
-
* times the value should be added to the
|
|
90
|
-
* @returns a
|
|
91
|
-
*/
|
|
92
|
-
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
|
|
93
|
-
/**
|
|
94
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
95
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
96
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
97
|
-
* class.
|
|
107
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
108
|
+
* @returns either a NODE object or undefined.
|
|
98
109
|
*/
|
|
99
|
-
|
|
110
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
|
|
100
111
|
/**
|
|
101
112
|
* Time Complexity: O(log n)
|
|
102
113
|
* Space Complexity: O(1)
|
|
@@ -105,19 +116,20 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
105
116
|
* Time Complexity: O(log n)
|
|
106
117
|
* Space Complexity: O(1)
|
|
107
118
|
*
|
|
108
|
-
* The function overrides the add method of a
|
|
109
|
-
*
|
|
110
|
-
*
|
|
119
|
+
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
120
|
+
* and update the count.
|
|
121
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
122
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
|
|
123
|
+
* can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
124
|
+
* entry, or raw element
|
|
111
125
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
112
|
-
*
|
|
113
|
-
* method.
|
|
126
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
114
127
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
115
|
-
* be added to the
|
|
116
|
-
* added once. However, you can specify a different value for `count` if you want to add
|
|
117
|
-
* @returns
|
|
118
|
-
* was not successful.
|
|
128
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
129
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
130
|
+
* @returns a boolean value.
|
|
119
131
|
*/
|
|
120
|
-
add(
|
|
132
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
|
|
121
133
|
/**
|
|
122
134
|
* Time Complexity: O(log n)
|
|
123
135
|
* Space Complexity: O(1)
|
|
@@ -126,19 +138,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
126
138
|
* Time Complexity: O(log n)
|
|
127
139
|
* Space Complexity: O(1)
|
|
128
140
|
*
|
|
129
|
-
* The `delete` function in
|
|
130
|
-
*
|
|
131
|
-
* @param identifier - The identifier is the value
|
|
132
|
-
*
|
|
141
|
+
* The `delete` function in a binary tree data structure deletes a node based on its identifier and
|
|
142
|
+
* returns the deleted node along with the parent node that needs to be balanced.
|
|
143
|
+
* @param identifier - The identifier parameter is the value used to identify the node that needs to
|
|
144
|
+
* be deleted from the binary tree. It can be of any type and is the return type of the callback
|
|
133
145
|
* function.
|
|
134
|
-
* @param {C} callback - The `callback` parameter is a function that is used to determine
|
|
135
|
-
*
|
|
136
|
-
* function takes
|
|
137
|
-
*
|
|
146
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
147
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
148
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
|
|
149
|
+
* of a node, and returns a value that
|
|
138
150
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
139
151
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
140
|
-
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
141
|
-
*
|
|
152
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be taken
|
|
153
|
+
* into account and the node
|
|
142
154
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
143
155
|
*/
|
|
144
156
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
@@ -150,7 +162,8 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
150
162
|
* Time Complexity: O(1)
|
|
151
163
|
* Space Complexity: O(1)
|
|
152
164
|
*
|
|
153
|
-
* The clear
|
|
165
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
166
|
+
* zero.
|
|
154
167
|
*/
|
|
155
168
|
clear(): void;
|
|
156
169
|
/**
|
|
@@ -160,13 +173,14 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
160
173
|
/**
|
|
161
174
|
* Time Complexity: O(n log n)
|
|
162
175
|
* Space Complexity: O(log n)
|
|
163
|
-
*
|
|
164
176
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
165
177
|
* tree using either a recursive or iterative approach.
|
|
166
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
167
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
168
|
-
*
|
|
169
|
-
*
|
|
178
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
179
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
180
|
+
* default value of `this.iterationType`, which means it will use the iteration type currently set in
|
|
181
|
+
* the object.
|
|
182
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
183
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
170
184
|
*/
|
|
171
185
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
172
186
|
/**
|
|
@@ -177,27 +191,42 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
177
191
|
* Time complexity: O(n)
|
|
178
192
|
* Space complexity: O(n)
|
|
179
193
|
*
|
|
180
|
-
* The
|
|
194
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
181
195
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
182
196
|
*/
|
|
183
197
|
clone(): TREE;
|
|
184
198
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
|
|
188
|
-
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
189
|
-
* node where the values from the source node will be swapped to.
|
|
190
|
-
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
191
|
-
* if either `srcNode` or `destNode` is undefined.
|
|
199
|
+
* Time Complexity: O(1)
|
|
200
|
+
* Space Complexity: O(1)
|
|
192
201
|
*/
|
|
193
|
-
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
194
202
|
/**
|
|
203
|
+
* Time Complexity: O(1)
|
|
204
|
+
* Space Complexity: O(1)
|
|
205
|
+
*
|
|
206
|
+
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
207
|
+
* in a binary search tree.
|
|
208
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
209
|
+
* that will be swapped with the `destNode`.
|
|
210
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
211
|
+
* node where the properties will be swapped with the source node.
|
|
212
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
213
|
+
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
214
|
+
*/
|
|
215
|
+
protected _swapProperties(srcNode: R | BSTNKeyOrNode<K, NODE>, destNode: R | BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* Time Complexity: O(1)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
*/
|
|
220
|
+
/**
|
|
221
|
+
* Time Complexity: O(1)
|
|
222
|
+
* Space Complexity: O(1)
|
|
223
|
+
*
|
|
195
224
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
196
|
-
* @param {NODE} oldNode - The
|
|
197
|
-
*
|
|
198
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
225
|
+
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
226
|
+
* data structure. It is of type NODE.
|
|
227
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
199
228
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
200
|
-
* superclass,
|
|
229
|
+
* superclass, which is of type `NODE`.
|
|
201
230
|
*/
|
|
202
231
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
203
232
|
}
|
|
@@ -39,13 +39,20 @@ exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
|
|
|
39
39
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
40
40
|
*/
|
|
41
41
|
class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
42
|
-
|
|
42
|
+
/**
|
|
43
|
+
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
44
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
45
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
46
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
47
|
+
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
48
|
+
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
49
|
+
*/
|
|
50
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
43
51
|
super([], options);
|
|
44
52
|
this._count = 0;
|
|
45
|
-
if (
|
|
46
|
-
this.addMany(
|
|
53
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
54
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
47
55
|
}
|
|
48
|
-
// TODO the _count is not accurate after nodes count modified
|
|
49
56
|
/**
|
|
50
57
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
51
58
|
* search.
|
|
@@ -72,13 +79,15 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
72
79
|
return sum;
|
|
73
80
|
}
|
|
74
81
|
/**
|
|
75
|
-
* The function creates a new
|
|
76
|
-
* @param {K} key - The key parameter
|
|
77
|
-
*
|
|
78
|
-
* @param {
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
+
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
|
|
83
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
84
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
85
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
86
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
87
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
88
|
+
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
|
|
89
|
+
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
90
|
+
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
82
91
|
*/
|
|
83
92
|
createNode(key, value, count) {
|
|
84
93
|
return new AVLTreeMultiMapNode(key, value, count);
|
|
@@ -86,59 +95,55 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
86
95
|
/**
|
|
87
96
|
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
88
97
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
89
|
-
* configuration options for creating the
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
93
|
-
* default options. The returned value is casted as `TREE`.
|
|
98
|
+
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
|
|
99
|
+
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
100
|
+
* object.
|
|
94
101
|
*/
|
|
95
102
|
createTree(options) {
|
|
96
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType,
|
|
103
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
107
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
108
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
109
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
110
|
+
* an instance of the `AVLTreeMultiMapNode` class.
|
|
111
|
+
*/
|
|
112
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
113
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
|
|
97
114
|
}
|
|
98
115
|
/**
|
|
99
|
-
* The function `
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
116
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
117
|
+
* a node object.
|
|
118
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
119
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
120
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
121
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
122
|
+
* value is provided, it will default to `undefined`.
|
|
105
123
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
106
|
-
* times the value should be added to the
|
|
107
|
-
* @returns a
|
|
124
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
125
|
+
* @returns either a NODE object or undefined.
|
|
108
126
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
127
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
128
|
+
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null)
|
|
112
129
|
return;
|
|
130
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
131
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
132
|
+
if (this.toEntryFn) {
|
|
133
|
+
const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
134
|
+
if (key)
|
|
135
|
+
return this.createNode(key, entryValue !== null && entryValue !== void 0 ? entryValue : value, count);
|
|
113
136
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
118
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
119
|
-
if (key === undefined || key === null) {
|
|
137
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
138
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
139
|
+
if (key === undefined || key === null)
|
|
120
140
|
return;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
node = this.createNode(key, value, count);
|
|
124
|
-
}
|
|
141
|
+
else
|
|
142
|
+
return this.createNode(key, value, count);
|
|
125
143
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
else {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
return node;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
136
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
137
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
138
|
-
* class.
|
|
139
|
-
*/
|
|
140
|
-
isNode(keyOrNodeOrEntry) {
|
|
141
|
-
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
144
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
145
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
146
|
+
return;
|
|
142
147
|
}
|
|
143
148
|
/**
|
|
144
149
|
* Time Complexity: O(log n)
|
|
@@ -148,20 +153,21 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
148
153
|
* Time Complexity: O(log n)
|
|
149
154
|
* Space Complexity: O(1)
|
|
150
155
|
*
|
|
151
|
-
* The function overrides the add method of a
|
|
152
|
-
*
|
|
153
|
-
*
|
|
156
|
+
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
157
|
+
* and update the count.
|
|
158
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
159
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
|
|
160
|
+
* can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
161
|
+
* entry, or raw element
|
|
154
162
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
155
|
-
*
|
|
156
|
-
* method.
|
|
163
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
157
164
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
158
|
-
* be added to the
|
|
159
|
-
* added once. However, you can specify a different value for `count` if you want to add
|
|
160
|
-
* @returns
|
|
161
|
-
* was not successful.
|
|
165
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
166
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
167
|
+
* @returns a boolean value.
|
|
162
168
|
*/
|
|
163
|
-
add(
|
|
164
|
-
const newNode = this.
|
|
169
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
170
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
165
171
|
if (newNode === undefined)
|
|
166
172
|
return false;
|
|
167
173
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
@@ -179,19 +185,19 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
179
185
|
* Time Complexity: O(log n)
|
|
180
186
|
* Space Complexity: O(1)
|
|
181
187
|
*
|
|
182
|
-
* The `delete` function in
|
|
183
|
-
*
|
|
184
|
-
* @param identifier - The identifier is the value
|
|
185
|
-
*
|
|
188
|
+
* The `delete` function in a binary tree data structure deletes a node based on its identifier and
|
|
189
|
+
* returns the deleted node along with the parent node that needs to be balanced.
|
|
190
|
+
* @param identifier - The identifier parameter is the value used to identify the node that needs to
|
|
191
|
+
* be deleted from the binary tree. It can be of any type and is the return type of the callback
|
|
186
192
|
* function.
|
|
187
|
-
* @param {C} callback - The `callback` parameter is a function that is used to determine
|
|
188
|
-
*
|
|
189
|
-
* function takes
|
|
190
|
-
*
|
|
193
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
194
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
195
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
|
|
196
|
+
* of a node, and returns a value that
|
|
191
197
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
192
198
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
193
|
-
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
194
|
-
*
|
|
199
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be taken
|
|
200
|
+
* into account and the node
|
|
195
201
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
196
202
|
*/
|
|
197
203
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -261,7 +267,8 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
261
267
|
* Time Complexity: O(1)
|
|
262
268
|
* Space Complexity: O(1)
|
|
263
269
|
*
|
|
264
|
-
* The clear
|
|
270
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
271
|
+
* zero.
|
|
265
272
|
*/
|
|
266
273
|
clear() {
|
|
267
274
|
super.clear();
|
|
@@ -274,13 +281,14 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
274
281
|
/**
|
|
275
282
|
* Time Complexity: O(n log n)
|
|
276
283
|
* Space Complexity: O(log n)
|
|
277
|
-
*
|
|
278
284
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
279
285
|
* tree using either a recursive or iterative approach.
|
|
280
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
281
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
282
|
-
*
|
|
283
|
-
*
|
|
286
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
287
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
288
|
+
* default value of `this.iterationType`, which means it will use the iteration type currently set in
|
|
289
|
+
* the object.
|
|
290
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
291
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
284
292
|
*/
|
|
285
293
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
286
294
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
@@ -326,7 +334,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
326
334
|
* Time complexity: O(n)
|
|
327
335
|
* Space complexity: O(n)
|
|
328
336
|
*
|
|
329
|
-
* The
|
|
337
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
330
338
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
331
339
|
*/
|
|
332
340
|
clone() {
|
|
@@ -335,13 +343,21 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
335
343
|
return cloned;
|
|
336
344
|
}
|
|
337
345
|
/**
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
346
|
+
* Time Complexity: O(1)
|
|
347
|
+
* Space Complexity: O(1)
|
|
348
|
+
*/
|
|
349
|
+
/**
|
|
350
|
+
* Time Complexity: O(1)
|
|
351
|
+
* Space Complexity: O(1)
|
|
352
|
+
*
|
|
353
|
+
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
354
|
+
* in a binary search tree.
|
|
355
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
356
|
+
* that will be swapped with the `destNode`.
|
|
357
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
358
|
+
* node where the properties will be swapped with the source node.
|
|
359
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
360
|
+
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
345
361
|
*/
|
|
346
362
|
_swapProperties(srcNode, destNode) {
|
|
347
363
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -365,12 +381,19 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
365
381
|
return undefined;
|
|
366
382
|
}
|
|
367
383
|
/**
|
|
384
|
+
* Time Complexity: O(1)
|
|
385
|
+
* Space Complexity: O(1)
|
|
386
|
+
*/
|
|
387
|
+
/**
|
|
388
|
+
* Time Complexity: O(1)
|
|
389
|
+
* Space Complexity: O(1)
|
|
390
|
+
*
|
|
368
391
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
369
|
-
* @param {NODE} oldNode - The
|
|
370
|
-
*
|
|
371
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
392
|
+
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
393
|
+
* data structure. It is of type NODE.
|
|
394
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
372
395
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
373
|
-
* superclass,
|
|
396
|
+
* superclass, which is of type `NODE`.
|
|
374
397
|
*/
|
|
375
398
|
_replaceNode(oldNode, newNode) {
|
|
376
399
|
newNode.count = oldNode.count + newNode.count;
|