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
|
@@ -9,19 +9,19 @@ import type {
|
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
11
|
BTNCallback,
|
|
12
|
-
|
|
12
|
+
BTNKeyOrNodeOrEntry,
|
|
13
13
|
IterationType,
|
|
14
|
-
KeyOrNodeOrEntry,
|
|
15
14
|
RBTNColor,
|
|
16
15
|
TreeMultiMapNested,
|
|
17
16
|
TreeMultiMapNodeNested,
|
|
18
17
|
TreeMultiMapOptions
|
|
19
18
|
} from '../../types';
|
|
19
|
+
import { BTNEntry } from '../../types';
|
|
20
20
|
import { IBinaryTree } from '../../interfaces';
|
|
21
21
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
22
22
|
|
|
23
23
|
export class TreeMultiMapNode<
|
|
24
|
-
K
|
|
24
|
+
K = any,
|
|
25
25
|
V = any,
|
|
26
26
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
|
|
27
27
|
> extends RedBlackTreeNode<K, V, NODE> {
|
|
@@ -63,25 +63,29 @@ export class TreeMultiMapNode<
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export class TreeMultiMap<
|
|
66
|
-
K
|
|
66
|
+
K = any,
|
|
67
67
|
V = any,
|
|
68
|
+
R = BTNEntry<K, V>,
|
|
68
69
|
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
|
|
69
|
-
TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>
|
|
70
|
+
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
70
71
|
>
|
|
71
|
-
extends RedBlackTree<K, V, NODE, TREE>
|
|
72
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
72
|
+
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
73
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
73
74
|
/**
|
|
74
|
-
* The constructor function initializes a
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
75
|
+
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
76
|
+
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
77
|
+
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
78
|
+
* TreeMultiMap with initial data.
|
|
79
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
80
|
+
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
81
|
+
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
81
82
|
*/
|
|
82
|
-
constructor(
|
|
83
|
+
constructor(
|
|
84
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
85
|
+
options?: TreeMultiMapOptions<K, V, R>
|
|
86
|
+
) {
|
|
83
87
|
super([], options);
|
|
84
|
-
if (
|
|
88
|
+
if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
protected _count = 0;
|
|
@@ -117,16 +121,15 @@ export class TreeMultiMap<
|
|
|
117
121
|
/**
|
|
118
122
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
119
123
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
120
|
-
* which is a generic type representing the
|
|
121
|
-
* @param {V} [value] - The `value` parameter
|
|
122
|
-
* node. It is
|
|
123
|
-
* function. If provided, it should be of type `V`.
|
|
124
|
+
* which is a generic type representing the type of keys in the tree.
|
|
125
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
126
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
124
127
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
125
128
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
126
129
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
127
130
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
128
131
|
* with a key in the tree.
|
|
129
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
132
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
130
133
|
*/
|
|
131
134
|
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK', count?: number): NODE {
|
|
132
135
|
return new TreeMultiMapNode(key, value, count, color) as NODE;
|
|
@@ -135,64 +138,67 @@ export class TreeMultiMap<
|
|
|
135
138
|
/**
|
|
136
139
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
137
140
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
138
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
139
|
-
*
|
|
141
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
142
|
+
* R>`.
|
|
140
143
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
141
|
-
* existing `iterationType`
|
|
144
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
142
145
|
*/
|
|
143
|
-
override createTree(options?: TreeMultiMapOptions<K>): TREE {
|
|
144
|
-
return new TreeMultiMap<K, V, NODE, TREE>([], {
|
|
146
|
+
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
|
|
147
|
+
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
145
148
|
iterationType: this.iterationType,
|
|
146
149
|
...options
|
|
147
150
|
}) as TREE;
|
|
148
151
|
}
|
|
149
152
|
|
|
150
153
|
/**
|
|
151
|
-
* The function `
|
|
152
|
-
*
|
|
153
|
-
* @param
|
|
154
|
-
*
|
|
155
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
154
|
+
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
155
|
+
* node based on the input.
|
|
156
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
157
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
158
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
159
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
160
|
+
* an existing node.
|
|
161
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
162
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
163
|
+
* @returns either a NODE object or undefined.
|
|
160
164
|
*/
|
|
161
|
-
override
|
|
162
|
-
|
|
165
|
+
override keyValueOrEntryOrRawElementToNode(
|
|
166
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>,
|
|
163
167
|
value?: V,
|
|
164
168
|
count = 1
|
|
165
169
|
): NODE | undefined {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (key === undefined || key === null) {
|
|
174
|
-
return;
|
|
175
|
-
} else {
|
|
176
|
-
node = this.createNode(key, value, 'BLACK', count);
|
|
177
|
-
}
|
|
178
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
179
|
-
node = this.createNode(keyOrNodeOrEntry, value, 'BLACK', count);
|
|
180
|
-
} else {
|
|
181
|
-
return;
|
|
170
|
+
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
|
|
171
|
+
|
|
172
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
173
|
+
|
|
174
|
+
if (this.toEntryFn) {
|
|
175
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
176
|
+
if (key) return this.getNodeByKey(key);
|
|
182
177
|
}
|
|
183
|
-
|
|
178
|
+
|
|
179
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
180
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
181
|
+
if (key === undefined || key === null) return;
|
|
182
|
+
else return this.createNode(key, value, 'BLACK', count);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
186
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
|
|
187
|
+
|
|
188
|
+
return;
|
|
184
189
|
}
|
|
185
190
|
|
|
186
191
|
/**
|
|
187
|
-
* The function
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
* of the `TreeMultiMapNode` class.
|
|
192
|
+
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
193
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
194
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
195
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
196
|
+
* an instance of the `TreeMultiMapNode` class.
|
|
193
197
|
*/
|
|
194
|
-
override isNode(
|
|
195
|
-
|
|
198
|
+
override isNode(
|
|
199
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
200
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
201
|
+
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
196
202
|
}
|
|
197
203
|
|
|
198
204
|
/**
|
|
@@ -204,17 +210,20 @@ export class TreeMultiMap<
|
|
|
204
210
|
* Time Complexity: O(log n)
|
|
205
211
|
* Space Complexity: O(1)
|
|
206
212
|
*
|
|
207
|
-
* The function overrides the add method
|
|
208
|
-
*
|
|
213
|
+
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
214
|
+
* the count and returning a boolean indicating success.
|
|
215
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
216
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
209
217
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
210
|
-
* data structure.
|
|
218
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
211
219
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
212
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
213
|
-
*
|
|
214
|
-
* @returns a boolean value.
|
|
220
|
+
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
221
|
+
* for `count`, the key-value pair will be added once.
|
|
222
|
+
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
223
|
+
* was successful, and false otherwise.
|
|
215
224
|
*/
|
|
216
|
-
override add(
|
|
217
|
-
const newNode = this.
|
|
225
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
226
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
218
227
|
const orgCount = newNode?.count || 0;
|
|
219
228
|
const isSuccessAdded = super.add(newNode);
|
|
220
229
|
|
|
@@ -235,20 +244,18 @@ export class TreeMultiMap<
|
|
|
235
244
|
* Time Complexity: O(log n)
|
|
236
245
|
* Space Complexity: O(1)
|
|
237
246
|
*
|
|
238
|
-
* The `delete`
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
251
|
-
* target node will be decremented
|
|
247
|
+
* The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
|
|
248
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
|
|
249
|
+
* key that is used to identify the node that needs to be deleted from the binary tree. It can be of
|
|
250
|
+
* any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
251
|
+
* the node to be deleted
|
|
252
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
253
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
254
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
|
|
255
|
+
* specific node or when performing other operations on the
|
|
256
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
257
|
+
* being deleted. If set to true, the count of the node will not be taken into account when deleting
|
|
258
|
+
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
252
259
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
253
260
|
*/
|
|
254
261
|
override delete<C extends BTNCallback<NODE>>(
|
|
@@ -372,10 +379,12 @@ export class TreeMultiMap<
|
|
|
372
379
|
*
|
|
373
380
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
374
381
|
* tree using either a recursive or iterative approach.
|
|
375
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
376
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
377
|
-
*
|
|
378
|
-
*
|
|
382
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
383
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
384
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified by the
|
|
385
|
+
* `iterationType` property of the current object.
|
|
386
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
387
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
379
388
|
*/
|
|
380
389
|
override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
381
390
|
const sorted = this.dfs(node => node, 'IN'),
|
|
@@ -434,19 +443,27 @@ export class TreeMultiMap<
|
|
|
434
443
|
}
|
|
435
444
|
|
|
436
445
|
/**
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
446
|
+
* Time Complexity: O(1)
|
|
447
|
+
* Space Complexity: O(1)
|
|
448
|
+
*/
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Time Complexity: O(1)
|
|
452
|
+
* Space Complexity: O(1)
|
|
453
|
+
*
|
|
454
|
+
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
455
|
+
* in a binary search tree.
|
|
456
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
457
|
+
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
458
|
+
* instance of the `BSTNKeyOrNode<K, NODE>` class.
|
|
459
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
460
|
+
* node where the properties will be swapped with the source node.
|
|
442
461
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
443
|
-
* If
|
|
444
|
-
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
445
|
-
* If either `srcNode` or `destNode` is
|
|
462
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
446
463
|
*/
|
|
447
464
|
protected override _swapProperties(
|
|
448
|
-
srcNode: BSTNKeyOrNode<K, NODE>,
|
|
449
|
-
destNode: BSTNKeyOrNode<K, NODE>
|
|
465
|
+
srcNode: R | BSTNKeyOrNode<K, NODE>,
|
|
466
|
+
destNode: R | BSTNKeyOrNode<K, NODE>
|
|
450
467
|
): NODE | undefined {
|
|
451
468
|
srcNode = this.ensureNode(srcNode);
|
|
452
469
|
destNode = this.ensureNode(destNode);
|
|
@@ -473,14 +490,22 @@ export class TreeMultiMap<
|
|
|
473
490
|
}
|
|
474
491
|
|
|
475
492
|
/**
|
|
493
|
+
* Time Complexity: O(1)
|
|
494
|
+
* Space Complexity: O(1)
|
|
495
|
+
*/
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Time Complexity: O(1)
|
|
499
|
+
* Space Complexity: O(1)
|
|
500
|
+
*
|
|
476
501
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
477
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
478
|
-
*
|
|
479
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
502
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
|
|
503
|
+
* structure.
|
|
504
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
480
505
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
481
|
-
* superclass,
|
|
506
|
+
* superclass, which is of type `NODE`.
|
|
482
507
|
*/
|
|
483
|
-
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
508
|
+
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
484
509
|
newNode.count = oldNode.count + newNode.count;
|
|
485
510
|
return super._replaceNode(oldNode, newNode);
|
|
486
511
|
}
|
|
@@ -496,9 +496,9 @@ export abstract class AbstractGraph<
|
|
|
496
496
|
|
|
497
497
|
/**
|
|
498
498
|
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
499
|
-
|
|
499
|
+
*/
|
|
500
500
|
|
|
501
|
-
|
|
501
|
+
/**
|
|
502
502
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
503
503
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
504
504
|
*/
|
|
@@ -639,9 +639,9 @@ export abstract class AbstractGraph<
|
|
|
639
639
|
* Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edgeMap.
|
|
640
640
|
* The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(VO^3), where VO is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
|
|
641
641
|
*
|
|
642
|
-
|
|
642
|
+
*/
|
|
643
643
|
|
|
644
|
-
|
|
644
|
+
/**
|
|
645
645
|
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
646
646
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
647
647
|
*/
|
|
@@ -777,9 +777,9 @@ export abstract class AbstractGraph<
|
|
|
777
777
|
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
778
778
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
779
779
|
* one to rest pairs
|
|
780
|
-
|
|
780
|
+
*/
|
|
781
781
|
|
|
782
|
-
|
|
782
|
+
/**
|
|
783
783
|
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
784
784
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
785
785
|
*
|
|
@@ -887,9 +887,9 @@ export abstract class AbstractGraph<
|
|
|
887
887
|
|
|
888
888
|
/**
|
|
889
889
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
890
|
-
|
|
890
|
+
*/
|
|
891
891
|
|
|
892
|
-
|
|
892
|
+
/**
|
|
893
893
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
894
894
|
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
895
895
|
*/
|
|
@@ -907,9 +907,9 @@ export abstract class AbstractGraph<
|
|
|
907
907
|
* Not support graph with negative weight cycle
|
|
908
908
|
* all pairs
|
|
909
909
|
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
910
|
-
|
|
910
|
+
*/
|
|
911
911
|
|
|
912
|
-
|
|
912
|
+
/**
|
|
913
913
|
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
914
914
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
915
915
|
*
|
|
@@ -261,7 +261,8 @@ export class DirectedGraph<
|
|
|
261
261
|
if (vertex) {
|
|
262
262
|
const neighbors = this.getNeighbors(vertex);
|
|
263
263
|
for (const neighbor of neighbors) {
|
|
264
|
-
this._inEdgeMap.delete(neighbor);
|
|
264
|
+
// this._inEdgeMap.delete(neighbor);
|
|
265
|
+
this.deleteEdgeSrcToDest(vertex, neighbor);
|
|
265
266
|
}
|
|
266
267
|
this._outEdgeMap.delete(vertex);
|
|
267
268
|
this._inEdgeMap.delete(vertex);
|