graph-typed 1.52.6 → 1.52.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/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +591 -402
- package/dist/data-structures/binary-tree/binary-tree.js +690 -604
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +122 -125
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +42 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +27 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +45 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +46 -50
- package/src/data-structures/binary-tree/avl-tree.ts +68 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +716 -748
- package/src/data-structures/binary-tree/bst.ts +137 -146
- package/src/data-structures/binary-tree/rb-tree.ts +46 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +49 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +2 -1
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
@@ -11,11 +11,10 @@ import type {
|
|
|
11
11
|
AVLTreeMultiMapOptions,
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNKeyOrNode,
|
|
14
|
-
BTNCallback,
|
|
15
14
|
BTNKeyOrNodeOrEntry,
|
|
16
|
-
IterationType
|
|
15
|
+
IterationType,
|
|
16
|
+
BTNEntry
|
|
17
17
|
} from '../../types';
|
|
18
|
-
import { BTNEntry } from '../../types';
|
|
19
18
|
import { IBinaryTree } from '../../interfaces';
|
|
20
19
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
21
20
|
|
|
@@ -80,18 +79,18 @@ export class AVLTreeMultiMap<
|
|
|
80
79
|
{
|
|
81
80
|
/**
|
|
82
81
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
83
|
-
* @param
|
|
82
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
84
83
|
* iterable object that can contain either keys, nodes, entries, or raw elements.
|
|
85
84
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
86
85
|
* behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
|
|
87
86
|
* `compareValues` functions to define custom comparison logic for keys and values, respectively.
|
|
88
87
|
*/
|
|
89
88
|
constructor(
|
|
90
|
-
|
|
89
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
91
90
|
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
92
91
|
) {
|
|
93
92
|
super([], options);
|
|
94
|
-
if (
|
|
93
|
+
if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
protected _count = 0;
|
|
@@ -144,29 +143,28 @@ export class AVLTreeMultiMap<
|
|
|
144
143
|
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
145
144
|
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
146
145
|
iterationType: this.iterationType,
|
|
147
|
-
comparator: this.
|
|
146
|
+
comparator: this._comparator,
|
|
147
|
+
toEntryFn: this._toEntryFn,
|
|
148
148
|
...options
|
|
149
149
|
}) as TREE;
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
/**
|
|
153
153
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
154
|
-
* @param {
|
|
155
|
-
* `
|
|
156
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
154
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
155
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
156
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
157
157
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
158
158
|
*/
|
|
159
|
-
override isNode(
|
|
160
|
-
|
|
161
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
162
|
-
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
|
|
159
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
160
|
+
return keyOrNodeOrEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
163
161
|
}
|
|
164
162
|
|
|
165
163
|
/**
|
|
166
164
|
* The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
|
|
167
165
|
* a node object.
|
|
168
|
-
* @param {
|
|
169
|
-
* `
|
|
166
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
167
|
+
* `keyOrNodeOrEntryOrRaw` parameter can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
170
168
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
171
169
|
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
172
170
|
* value is provided, it will default to `undefined`.
|
|
@@ -175,25 +173,25 @@ export class AVLTreeMultiMap<
|
|
|
175
173
|
* @returns either a NODE object or undefined.
|
|
176
174
|
*/
|
|
177
175
|
override keyValueOrEntryOrRawElementToNode(
|
|
178
|
-
|
|
176
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
179
177
|
value?: V,
|
|
180
178
|
count = 1
|
|
181
179
|
): NODE | undefined {
|
|
182
|
-
if (
|
|
183
|
-
if (this.isNode(
|
|
180
|
+
if (keyOrNodeOrEntryOrRaw === undefined || keyOrNodeOrEntryOrRaw === null) return;
|
|
181
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
184
182
|
|
|
185
|
-
if (this.isEntry(
|
|
186
|
-
const [key, entryValue] =
|
|
183
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
184
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
187
185
|
if (key === undefined || key === null) return;
|
|
188
186
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, count);
|
|
189
187
|
}
|
|
190
188
|
|
|
191
|
-
if (this.
|
|
192
|
-
const [key, entryValue] = this.
|
|
189
|
+
if (this._toEntryFn) {
|
|
190
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
193
191
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, count);
|
|
194
192
|
}
|
|
195
193
|
|
|
196
|
-
if (this.isKey(
|
|
194
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
197
195
|
|
|
198
196
|
return;
|
|
199
197
|
}
|
|
@@ -204,8 +202,8 @@ export class AVLTreeMultiMap<
|
|
|
204
202
|
*
|
|
205
203
|
* The function overrides the add method of a TypeScript class to add a new node to a data structure
|
|
206
204
|
* and update the count.
|
|
207
|
-
* @param {
|
|
208
|
-
* `
|
|
205
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
206
|
+
* `keyOrNodeOrEntryOrRaw` parameter can accept a value of type `R`, which can be any type. It
|
|
209
207
|
* can also accept a value of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
|
|
210
208
|
* entry, or raw element
|
|
211
209
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
@@ -215,8 +213,8 @@ export class AVLTreeMultiMap<
|
|
|
215
213
|
* be added once. However, you can specify a different value for `count` if you want to add
|
|
216
214
|
* @returns a boolean value.
|
|
217
215
|
*/
|
|
218
|
-
override add(
|
|
219
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
216
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
217
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
220
218
|
if (newNode === undefined) return false;
|
|
221
219
|
|
|
222
220
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -231,31 +229,29 @@ export class AVLTreeMultiMap<
|
|
|
231
229
|
* Time Complexity: O(log n)
|
|
232
230
|
* Space Complexity: O(1)
|
|
233
231
|
*
|
|
234
|
-
* The
|
|
235
|
-
*
|
|
236
|
-
* @param
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
232
|
+
* The function overrides the delete method in a binary tree data structure, handling deletion of
|
|
233
|
+
* nodes and maintaining balance in the tree.
|
|
234
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `predicate`
|
|
235
|
+
* parameter in the `delete` method is used to specify the condition for deleting a node from the
|
|
236
|
+
* binary tree. It can be a key, node, or entry that determines which
|
|
237
|
+
* node(s) should be deleted.
|
|
238
|
+
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
239
|
+
* boolean flag that determines whether to ignore the count of the node being deleted. If
|
|
240
|
+
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
241
|
+
* `ignoreCount` is set to
|
|
242
|
+
* @returns The `delete` method overrides the default delete behavior in a binary tree data
|
|
243
|
+
* structure. It takes a predicate or node to be deleted and an optional flag to ignore count. The
|
|
244
|
+
* method returns an array of `BinaryTreeDeleteResult` objects, each containing information about the
|
|
245
|
+
* deleted node and whether balancing is needed in the tree.
|
|
248
246
|
*/
|
|
249
|
-
override delete
|
|
250
|
-
|
|
251
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
247
|
+
override delete(
|
|
248
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
252
249
|
ignoreCount = false
|
|
253
250
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
254
251
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
255
252
|
if (!this.root) return deletedResult;
|
|
256
|
-
callback = this._ensureCallback(identifier, callback);
|
|
257
253
|
|
|
258
|
-
const curr: NODE | undefined = this.getNode(
|
|
254
|
+
const curr: NODE | undefined = this.getNode(keyOrNodeOrEntryOrRaw) ?? undefined;
|
|
259
255
|
if (!curr) return deletedResult;
|
|
260
256
|
|
|
261
257
|
const parent: NODE | undefined = curr?.parent ? curr.parent : undefined;
|
|
@@ -293,7 +289,7 @@ export class AVLTreeMultiMap<
|
|
|
293
289
|
}
|
|
294
290
|
}
|
|
295
291
|
}
|
|
296
|
-
this._size = this.
|
|
292
|
+
this._size = this._size - 1;
|
|
297
293
|
// TODO How to handle when the count of target node is lesser than current node's count
|
|
298
294
|
if (orgCurrent) this._count -= orgCurrent.count;
|
|
299
295
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
@@ -12,10 +12,9 @@ import type {
|
|
|
12
12
|
AVLTreeOptions,
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
BTNKeyOrNodeOrEntry,
|
|
16
|
+
BTNEntry
|
|
17
17
|
} from '../../types';
|
|
18
|
-
import { BTNEntry } from '../../types';
|
|
19
18
|
import { IBinaryTree } from '../../interfaces';
|
|
20
19
|
|
|
21
20
|
export class AVLTreeNode<
|
|
@@ -78,7 +77,7 @@ export class AVLTree<
|
|
|
78
77
|
/**
|
|
79
78
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
80
79
|
* entries, or raw elements.
|
|
81
|
-
* @param
|
|
80
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
82
81
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
83
82
|
* be used to initialize the AVLTree.
|
|
84
83
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -87,11 +86,11 @@ export class AVLTree<
|
|
|
87
86
|
* `nodeBuilder` (
|
|
88
87
|
*/
|
|
89
88
|
constructor(
|
|
90
|
-
|
|
89
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
91
90
|
options?: AVLTreeOptions<K, V, R>
|
|
92
91
|
) {
|
|
93
92
|
super([], options);
|
|
94
|
-
if (
|
|
93
|
+
if (keysOrNodesOrEntriesOrRaws) super.addMany(keysOrNodesOrEntriesOrRaws);
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
/**
|
|
@@ -117,22 +116,21 @@ export class AVLTree<
|
|
|
117
116
|
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
118
117
|
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
119
118
|
iterationType: this.iterationType,
|
|
120
|
-
comparator: this.
|
|
119
|
+
comparator: this._comparator,
|
|
120
|
+
toEntryFn: this._toEntryFn,
|
|
121
121
|
...options
|
|
122
122
|
}) as TREE;
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
127
|
-
* @param {
|
|
128
|
-
* `
|
|
129
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
127
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
128
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
129
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
130
130
|
* an instance of the `AVLTreeNode` class.
|
|
131
131
|
*/
|
|
132
|
-
override isNode(
|
|
133
|
-
|
|
134
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
135
|
-
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
132
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
133
|
+
return keyOrNodeOrEntryOrRaw instanceof AVLTreeNode;
|
|
136
134
|
}
|
|
137
135
|
|
|
138
136
|
/**
|
|
@@ -141,17 +139,17 @@ export class AVLTree<
|
|
|
141
139
|
*
|
|
142
140
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
143
141
|
* structure, then balances the path.
|
|
144
|
-
* @param {
|
|
145
|
-
* `
|
|
142
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
143
|
+
* `keyOrNodeOrEntryOrRaw` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
146
144
|
* `RawElement`.
|
|
147
145
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
148
146
|
* the key or node being added to the data structure.
|
|
149
147
|
* @returns The method is returning a boolean value.
|
|
150
148
|
*/
|
|
151
|
-
override add(
|
|
152
|
-
if (
|
|
153
|
-
const inserted = super.add(
|
|
154
|
-
if (inserted) this._balancePath(
|
|
149
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
|
|
150
|
+
if (keyOrNodeOrEntryOrRaw === null) return false;
|
|
151
|
+
const inserted = super.add(keyOrNodeOrEntryOrRaw, value);
|
|
152
|
+
if (inserted) this._balancePath(keyOrNodeOrEntryOrRaw);
|
|
155
153
|
return inserted;
|
|
156
154
|
}
|
|
157
155
|
|
|
@@ -159,20 +157,17 @@ export class AVLTree<
|
|
|
159
157
|
* Time Complexity: O(log n)
|
|
160
158
|
* Space Complexity: O(1)
|
|
161
159
|
*
|
|
162
|
-
* The function overrides the delete method
|
|
163
|
-
*
|
|
164
|
-
* @param
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
160
|
+
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
161
|
+
* balances the tree if necessary.
|
|
162
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
163
|
+
* parameter in the `override delete` method can be one of the following types:
|
|
164
|
+
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
165
|
+
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
166
|
+
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
167
|
+
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
170
168
|
*/
|
|
171
|
-
override delete<
|
|
172
|
-
|
|
173
|
-
callback: C = this._DEFAULT_CALLBACK as C
|
|
174
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
175
|
-
const deletedResults = super.delete(identifier, callback);
|
|
169
|
+
override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
170
|
+
const deletedResults = super.delete(keyOrNodeOrEntryOrRaw);
|
|
176
171
|
for (const { needBalanced } of deletedResults) {
|
|
177
172
|
if (needBalanced) {
|
|
178
173
|
this._balancePath(needBalanced);
|
|
@@ -339,8 +334,8 @@ export class AVLTree<
|
|
|
339
334
|
}
|
|
340
335
|
|
|
341
336
|
this._updateHeight(A);
|
|
342
|
-
B
|
|
343
|
-
C
|
|
337
|
+
if (B) this._updateHeight(B);
|
|
338
|
+
if (C) this._updateHeight(C);
|
|
344
339
|
}
|
|
345
340
|
|
|
346
341
|
/**
|
|
@@ -378,7 +373,7 @@ export class AVLTree<
|
|
|
378
373
|
B.left = A;
|
|
379
374
|
}
|
|
380
375
|
this._updateHeight(A);
|
|
381
|
-
B
|
|
376
|
+
if (B) this._updateHeight(B);
|
|
382
377
|
}
|
|
383
378
|
|
|
384
379
|
/**
|
|
@@ -427,8 +422,8 @@ export class AVLTree<
|
|
|
427
422
|
if (C) C.right = B;
|
|
428
423
|
|
|
429
424
|
this._updateHeight(A);
|
|
430
|
-
B
|
|
431
|
-
C
|
|
425
|
+
if (B) this._updateHeight(B);
|
|
426
|
+
if (C) this._updateHeight(C);
|
|
432
427
|
}
|
|
433
428
|
|
|
434
429
|
/**
|
|
@@ -437,46 +432,48 @@ export class AVLTree<
|
|
|
437
432
|
*
|
|
438
433
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
439
434
|
* to restore balance in an AVL tree after inserting a node.
|
|
440
|
-
* @param {
|
|
435
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
441
436
|
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
442
437
|
*/
|
|
443
|
-
protected _balancePath(node:
|
|
438
|
+
protected _balancePath(node: BTNKeyOrNodeOrEntry<K, V, NODE> | R): void {
|
|
444
439
|
node = this.ensureNode(node);
|
|
445
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
440
|
+
const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
|
|
446
441
|
for (let i = 0; i < path.length; i++) {
|
|
447
442
|
// second O(log n)
|
|
448
443
|
const A = path[i];
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
if (
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
444
|
+
if (A) {
|
|
445
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
446
|
+
this._updateHeight(A); // first O(1)
|
|
447
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
448
|
+
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
449
|
+
switch (
|
|
450
|
+
this._balanceFactor(A) // second O(1)
|
|
451
|
+
) {
|
|
452
|
+
case -2:
|
|
453
|
+
if (A && A.left) {
|
|
454
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
455
|
+
// second O(1)
|
|
456
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
457
|
+
this._balanceLL(A);
|
|
458
|
+
} else {
|
|
459
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
460
|
+
this._balanceLR(A);
|
|
461
|
+
}
|
|
465
462
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
463
|
+
break;
|
|
464
|
+
case +2:
|
|
465
|
+
if (A && A.right) {
|
|
466
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
467
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
468
|
+
this._balanceRR(A);
|
|
469
|
+
} else {
|
|
470
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
471
|
+
this._balanceRL(A);
|
|
472
|
+
}
|
|
476
473
|
}
|
|
477
|
-
|
|
474
|
+
}
|
|
475
|
+
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
478
476
|
}
|
|
479
|
-
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
480
477
|
}
|
|
481
478
|
}
|
|
482
479
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { getMSB } from '../../utils';
|