undirected-graph-typed 1.51.8 → 1.52.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/base/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
- package/dist/data-structures/binary-tree/binary-tree.js +478 -366
- package/dist/data-structures/binary-tree/bst.d.ts +202 -212
- package/dist/data-structures/binary-tree/bst.js +208 -250
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- 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/graph/directed-graph.js +2 -1
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +28 -20
- package/dist/data-structures/queue/deque.js +45 -24
- package/dist/data-structures/queue/queue.d.ts +8 -29
- package/dist/data-structures/queue/queue.js +15 -32
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -23
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
- package/src/data-structures/binary-tree/avl-tree.ts +97 -70
- package/src/data-structures/binary-tree/binary-tree.ts +591 -455
- package/src/data-structures/binary-tree/bst.ts +266 -293
- package/src/data-structures/binary-tree/rb-tree.ts +124 -104
- package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +52 -27
- package/src/data-structures/queue/queue.ts +23 -37
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +10 -10
- package/src/types/common.ts +2 -25
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
- package/src/types/data-structures/binary-tree/bst.ts +11 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -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,64 +79,71 @@ 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);
|
|
85
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
97
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
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.
|
|
101
|
+
*/
|
|
86
102
|
createTree(options) {
|
|
87
103
|
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
88
104
|
}
|
|
89
105
|
/**
|
|
90
|
-
* The function
|
|
91
|
-
* @param
|
|
92
|
-
* can be
|
|
93
|
-
* @
|
|
94
|
-
*
|
|
95
|
-
|
|
106
|
+
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
107
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
108
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<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;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
117
|
+
* a node object.
|
|
118
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
119
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<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`.
|
|
96
123
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
97
|
-
* times the value should be added to the
|
|
98
|
-
* @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.
|
|
99
126
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
127
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
128
|
+
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null)
|
|
103
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);
|
|
104
136
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
109
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
110
|
-
if (key === undefined || key === null) {
|
|
137
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
138
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
139
|
+
if (key === undefined || key === null)
|
|
111
140
|
return;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
node = this.createNode(key, value, count);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
118
|
-
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
141
|
+
else
|
|
142
|
+
return this.createNode(key, value, count);
|
|
119
143
|
}
|
|
120
|
-
|
|
121
|
-
return;
|
|
122
|
-
|
|
123
|
-
return node;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
127
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
128
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
129
|
-
* class.
|
|
130
|
-
*/
|
|
131
|
-
isNode(keyOrNodeOrEntry) {
|
|
132
|
-
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
144
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
145
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
146
|
+
return;
|
|
133
147
|
}
|
|
134
148
|
/**
|
|
135
149
|
* Time Complexity: O(log n)
|
|
@@ -139,20 +153,21 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
139
153
|
* Time Complexity: O(log n)
|
|
140
154
|
* Space Complexity: O(1)
|
|
141
155
|
*
|
|
142
|
-
* The function overrides the add method of a
|
|
143
|
-
*
|
|
144
|
-
*
|
|
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 | BTNKeyOrNodeOrEntry<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 `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
161
|
+
* entry, or raw element
|
|
145
162
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
146
|
-
*
|
|
147
|
-
* method.
|
|
163
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
148
164
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
149
|
-
* be added to the
|
|
150
|
-
* added once. However, you can specify a different value for `count` if you want to add
|
|
151
|
-
* @returns
|
|
152
|
-
* 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.
|
|
153
168
|
*/
|
|
154
|
-
add(
|
|
155
|
-
const newNode = this.
|
|
169
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
170
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
156
171
|
if (newNode === undefined)
|
|
157
172
|
return false;
|
|
158
173
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
@@ -170,19 +185,19 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
170
185
|
* Time Complexity: O(log n)
|
|
171
186
|
* Space Complexity: O(1)
|
|
172
187
|
*
|
|
173
|
-
* The `delete` function in
|
|
174
|
-
*
|
|
175
|
-
* @param identifier - The identifier is the value
|
|
176
|
-
*
|
|
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
|
|
177
192
|
* function.
|
|
178
|
-
* @param {C} callback - The `callback` parameter is a function that is used to determine
|
|
179
|
-
*
|
|
180
|
-
* function takes
|
|
181
|
-
*
|
|
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
|
|
182
197
|
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
183
198
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
184
|
-
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
185
|
-
*
|
|
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
|
|
186
201
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
187
202
|
*/
|
|
188
203
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -252,7 +267,8 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
252
267
|
* Time Complexity: O(1)
|
|
253
268
|
* Space Complexity: O(1)
|
|
254
269
|
*
|
|
255
|
-
* The clear
|
|
270
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
271
|
+
* zero.
|
|
256
272
|
*/
|
|
257
273
|
clear() {
|
|
258
274
|
super.clear();
|
|
@@ -265,13 +281,14 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
265
281
|
/**
|
|
266
282
|
* Time Complexity: O(n log n)
|
|
267
283
|
* Space Complexity: O(log n)
|
|
268
|
-
*
|
|
269
284
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
270
285
|
* tree using either a recursive or iterative approach.
|
|
271
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
272
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
273
|
-
*
|
|
274
|
-
*
|
|
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.
|
|
275
292
|
*/
|
|
276
293
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
277
294
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
@@ -317,7 +334,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
317
334
|
* Time complexity: O(n)
|
|
318
335
|
* Space complexity: O(n)
|
|
319
336
|
*
|
|
320
|
-
* The
|
|
337
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
321
338
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
322
339
|
*/
|
|
323
340
|
clone() {
|
|
@@ -326,13 +343,21 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
326
343
|
return cloned;
|
|
327
344
|
}
|
|
328
345
|
/**
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
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`.
|
|
336
361
|
*/
|
|
337
362
|
_swapProperties(srcNode, destNode) {
|
|
338
363
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -356,12 +381,19 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
356
381
|
return undefined;
|
|
357
382
|
}
|
|
358
383
|
/**
|
|
384
|
+
* Time Complexity: O(1)
|
|
385
|
+
* Space Complexity: O(1)
|
|
386
|
+
*/
|
|
387
|
+
/**
|
|
388
|
+
* Time Complexity: O(1)
|
|
389
|
+
* Space Complexity: O(1)
|
|
390
|
+
*
|
|
359
391
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
360
|
-
* @param {NODE} oldNode - The
|
|
361
|
-
*
|
|
362
|
-
* @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.
|
|
363
395
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
364
|
-
* superclass,
|
|
396
|
+
* superclass, which is of type `NODE`.
|
|
365
397
|
*/
|
|
366
398
|
_replaceNode(oldNode, newNode) {
|
|
367
399
|
newNode.count = oldNode.count + newNode.count;
|
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback,
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, BTNKeyOrNodeOrEntry } from '../../types';
|
|
10
|
+
import { BTNEntry } from '../../types';
|
|
10
11
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class AVLTreeNode<K
|
|
12
|
+
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
13
|
/**
|
|
13
14
|
* The constructor function initializes a new instance of a class with a key and an optional value,
|
|
14
15
|
* and sets the height property to 0.
|
|
@@ -40,25 +41,27 @@ export declare class AVLTreeNode<K extends Comparable, V = any, NODE extends AVL
|
|
|
40
41
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
42
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
42
43
|
*/
|
|
43
|
-
export declare class AVLTree<K
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
* @param
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
44
|
+
export declare class AVLTree<K = any, V = any, R = BTNEntry<K, V>, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
45
|
+
/**
|
|
46
|
+
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
47
|
+
* entries, or raw elements.
|
|
48
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
49
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
50
|
+
* be used to initialize the AVLTree.
|
|
51
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
52
|
+
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
|
|
53
|
+
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
54
|
+
* `nodeBuilder` (
|
|
55
|
+
*/
|
|
56
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeOptions<K, V, R>);
|
|
57
|
+
/**
|
|
58
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
59
|
+
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
60
|
+
* created.
|
|
61
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
62
|
+
* value associated with the key in the node being created.
|
|
63
|
+
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
64
|
+
* type NODE.
|
|
62
65
|
*/
|
|
63
66
|
createNode(key: K, value?: V): NODE;
|
|
64
67
|
/**
|
|
@@ -68,13 +71,15 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
68
71
|
* being created.
|
|
69
72
|
* @returns a new AVLTree object.
|
|
70
73
|
*/
|
|
71
|
-
createTree(options?: AVLTreeOptions<K>): TREE;
|
|
74
|
+
createTree(options?: AVLTreeOptions<K, V, R>): TREE;
|
|
72
75
|
/**
|
|
73
|
-
* The function checks if
|
|
74
|
-
* @param
|
|
75
|
-
*
|
|
76
|
+
* The function checks if the input is an instance of AVLTreeNode.
|
|
77
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
78
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
79
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
80
|
+
* an instance of the `AVLTreeNode` class.
|
|
76
81
|
*/
|
|
77
|
-
isNode(
|
|
82
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
78
83
|
/**
|
|
79
84
|
* Time Complexity: O(log n)
|
|
80
85
|
* Space Complexity: O(1)
|
|
@@ -84,15 +89,16 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
84
89
|
* Time Complexity: O(log n)
|
|
85
90
|
* Space Complexity: O(1)
|
|
86
91
|
*
|
|
87
|
-
* The function overrides the add method of a
|
|
88
|
-
*
|
|
89
|
-
* @param
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
92
|
+
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
93
|
+
* structure, then balances the path.
|
|
94
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
95
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
96
|
+
* `RawElement`.
|
|
97
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
98
|
+
* the key or node being added to the data structure.
|
|
99
|
+
* @returns The method is returning a boolean value.
|
|
94
100
|
*/
|
|
95
|
-
add(
|
|
101
|
+
add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
96
102
|
/**
|
|
97
103
|
* Time Complexity: O(log n)
|
|
98
104
|
* Space Complexity: O(1)
|
|
@@ -101,29 +107,34 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
101
107
|
* Time Complexity: O(log n)
|
|
102
108
|
* Space Complexity: O(1)
|
|
103
109
|
*
|
|
104
|
-
* The function overrides the delete method of a binary tree
|
|
105
|
-
*
|
|
110
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
111
|
+
* to balance the tree after deletion.
|
|
106
112
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
107
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
108
|
-
*
|
|
109
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* parameter of type `NODE
|
|
113
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
113
|
+
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
|
|
114
|
+
* binary tree's node type.
|
|
115
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
116
|
+
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
|
|
117
|
+
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
|
|
114
118
|
*/
|
|
115
119
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
116
120
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
120
|
-
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
121
|
-
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
122
|
-
* node where the values from the source node will be swapped to.
|
|
123
|
-
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
124
|
-
* if either `srcNode` or `destNode` is undefined.
|
|
121
|
+
* Time Complexity: O(1)
|
|
122
|
+
* Space Complexity: O(1)
|
|
125
123
|
*/
|
|
126
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Time Complexity: O(1)
|
|
126
|
+
* Space Complexity: O(1)
|
|
127
|
+
*
|
|
128
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
129
|
+
* binary search tree.
|
|
130
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
131
|
+
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
132
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
133
|
+
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
|
|
134
|
+
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
135
|
+
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
136
|
+
*/
|
|
137
|
+
protected _swapProperties(srcNode: R | BSTNKeyOrNode<K, NODE>, destNode: R | BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
127
138
|
/**
|
|
128
139
|
* Time Complexity: O(1)
|
|
129
140
|
* Space Complexity: O(1)
|
|
@@ -133,7 +144,8 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
133
144
|
* Space Complexity: O(1)
|
|
134
145
|
*
|
|
135
146
|
* The function calculates the balance factor of a node in a binary tree.
|
|
136
|
-
* @param {NODE} node - The parameter "node" represents a node in a
|
|
147
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
|
|
148
|
+
* binary tree data structure.
|
|
137
149
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
138
150
|
* height of the left subtree from the height of the right subtree.
|
|
139
151
|
*/
|
|
@@ -159,7 +171,7 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
159
171
|
* Time Complexity: O(1)
|
|
160
172
|
* Space Complexity: O(1)
|
|
161
173
|
*
|
|
162
|
-
* The
|
|
174
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
163
175
|
* @param {NODE} A - A is a node in a binary tree.
|
|
164
176
|
*/
|
|
165
177
|
protected _balanceLL(A: NODE): void;
|
|
@@ -210,18 +222,26 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
210
222
|
*
|
|
211
223
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
212
224
|
* to restore balance in an AVL tree after inserting a node.
|
|
213
|
-
* @param {NODE} node - The `node` parameter
|
|
214
|
-
*
|
|
225
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
226
|
+
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
215
227
|
*/
|
|
216
|
-
protected _balancePath(node:
|
|
228
|
+
protected _balancePath(node: R | BTNKeyOrNodeOrEntry<K, V, NODE>): void;
|
|
217
229
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
|
|
230
|
+
* Time Complexity: O(1)
|
|
231
|
+
* Space Complexity: O(1)
|
|
232
|
+
*/
|
|
233
|
+
/**
|
|
234
|
+
* Time Complexity: O(1)
|
|
235
|
+
* Space Complexity: O(1)
|
|
236
|
+
*
|
|
237
|
+
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
238
|
+
* same as the old node.
|
|
239
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
240
|
+
* the data structure.
|
|
221
241
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
222
242
|
* the data structure.
|
|
223
|
-
* @returns the result of calling the `_replaceNode` method
|
|
224
|
-
* `oldNode` and `newNode` as arguments.
|
|
243
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
244
|
+
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
225
245
|
*/
|
|
226
246
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
227
247
|
}
|