bst-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,14 +1,14 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
|
-
BTNCallback,
|
|
4
3
|
BTNKeyOrNodeOrEntry,
|
|
5
4
|
CRUD,
|
|
5
|
+
OptBSTN,
|
|
6
6
|
RBTNColor,
|
|
7
7
|
RBTreeOptions,
|
|
8
8
|
RedBlackTreeNested,
|
|
9
|
-
RedBlackTreeNodeNested
|
|
9
|
+
RedBlackTreeNodeNested,
|
|
10
|
+
BTNEntry
|
|
10
11
|
} from '../../types';
|
|
11
|
-
import { BTNEntry } from '../../types';
|
|
12
12
|
import { BST, BSTNode } from './bst';
|
|
13
13
|
import { IBinaryTree } from '../../interfaces';
|
|
14
14
|
|
|
@@ -64,7 +64,7 @@ export class RedBlackTree<
|
|
|
64
64
|
{
|
|
65
65
|
/**
|
|
66
66
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
67
|
-
* @param
|
|
67
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
68
68
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
69
69
|
* initialize the RBTree with the provided elements.
|
|
70
70
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -73,15 +73,15 @@ export class RedBlackTree<
|
|
|
73
73
|
* depend on the implementation
|
|
74
74
|
*/
|
|
75
75
|
constructor(
|
|
76
|
-
|
|
76
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
77
77
|
options?: RBTreeOptions<K, V, R>
|
|
78
78
|
) {
|
|
79
79
|
super([], options);
|
|
80
80
|
|
|
81
81
|
this._root = this.NIL;
|
|
82
82
|
|
|
83
|
-
if (
|
|
84
|
-
this.addMany(
|
|
83
|
+
if (keysOrNodesOrEntriesOrRaws) {
|
|
84
|
+
this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
@@ -122,6 +122,8 @@ export class RedBlackTree<
|
|
|
122
122
|
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
|
|
123
123
|
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
124
124
|
iterationType: this.iterationType,
|
|
125
|
+
comparator: this._comparator,
|
|
126
|
+
toEntryFn: this._toEntryFn,
|
|
125
127
|
...options
|
|
126
128
|
}) as TREE;
|
|
127
129
|
}
|
|
@@ -131,15 +133,13 @@ export class RedBlackTree<
|
|
|
131
133
|
* Space Complexity: O(1)
|
|
132
134
|
*
|
|
133
135
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
134
|
-
* @param {
|
|
135
|
-
* `
|
|
136
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
136
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
137
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
138
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
137
139
|
* an instance of the `RedBlackTreeNode` class.
|
|
138
140
|
*/
|
|
139
|
-
override isNode(
|
|
140
|
-
|
|
141
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
142
|
-
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
141
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
142
|
+
return keyOrNodeOrEntryOrRaw instanceof RedBlackTreeNode;
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
// /**
|
|
@@ -153,27 +153,27 @@ export class RedBlackTree<
|
|
|
153
153
|
// *
|
|
154
154
|
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
155
155
|
// * valid, otherwise it returns undefined.
|
|
156
|
-
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>}
|
|
157
|
-
// * @param {V} [value] - The value associated with the key (if `
|
|
156
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRaw - The key, value, or entry to convert.
|
|
157
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRaw` is a key).
|
|
158
158
|
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
159
159
|
// */
|
|
160
|
-
// override keyValueOrEntryOrRawElementToNode(
|
|
160
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): NODE | undefined {
|
|
161
161
|
//
|
|
162
|
-
// if (
|
|
163
|
-
// if (this.isNode(
|
|
162
|
+
// if (keyOrNodeOrEntryOrRaw === null || keyOrNodeOrEntryOrRaw === undefined) return;
|
|
163
|
+
// if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
164
164
|
//
|
|
165
|
-
// if (this.
|
|
166
|
-
// const [key, entryValue] = this.
|
|
165
|
+
// if (this._toEntryFn) {
|
|
166
|
+
// const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
167
167
|
// if (this.isKey(key)) return this.createNode(key, entryValue ?? value, 'RED');
|
|
168
168
|
// }
|
|
169
169
|
//
|
|
170
|
-
// if (this.isEntry(
|
|
171
|
-
// const [key, value] =
|
|
170
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
171
|
+
// const [key, value] = keyOrNodeOrEntryOrRaw;
|
|
172
172
|
// if (key === undefined || key === null) return;
|
|
173
173
|
// else return this.createNode(key, value, 'RED');
|
|
174
174
|
// }
|
|
175
175
|
//
|
|
176
|
-
// if (this.isKey(
|
|
176
|
+
// if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'RED');
|
|
177
177
|
//
|
|
178
178
|
// return ;
|
|
179
179
|
// }
|
|
@@ -196,8 +196,8 @@ export class RedBlackTree<
|
|
|
196
196
|
*
|
|
197
197
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
198
198
|
* added.
|
|
199
|
-
* @param {
|
|
200
|
-
* `
|
|
199
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
200
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
201
201
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
202
202
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
203
203
|
* structure.
|
|
@@ -205,8 +205,8 @@ export class RedBlackTree<
|
|
|
205
205
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
206
206
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
207
207
|
*/
|
|
208
|
-
override add(
|
|
209
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
208
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
|
|
209
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
210
210
|
if (!this.isRealNode(newNode)) return false;
|
|
211
211
|
|
|
212
212
|
const insertStatus = this._insert(newNode);
|
|
@@ -227,26 +227,26 @@ export class RedBlackTree<
|
|
|
227
227
|
* Time Complexity: O(log n)
|
|
228
228
|
* Space Complexity: O(1)
|
|
229
229
|
*
|
|
230
|
-
* The function overrides the delete method
|
|
231
|
-
*
|
|
232
|
-
* @param {
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* @
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* that extends the `BTNCallback
|
|
240
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
230
|
+
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
231
|
+
* a given predicate and maintain the binary search tree properties.
|
|
232
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The `keyOrNodeOrEntryOrRaw`
|
|
233
|
+
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
234
|
+
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
235
|
+
* function that determines which node(s) should be deleted.
|
|
236
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<NODE>`
|
|
237
|
+
* objects. Each object in the array contains information about the deleted node and whether
|
|
238
|
+
* balancing is needed.
|
|
241
239
|
*/
|
|
242
|
-
override delete<
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
|
-
if (identifier === null) return [];
|
|
240
|
+
override delete(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[] {
|
|
241
|
+
if (keyOrNodeOrEntryOrRaw === null) return [];
|
|
242
|
+
|
|
247
243
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
248
|
-
|
|
249
|
-
|
|
244
|
+
let nodeToDelete: OptBSTN<NODE>;
|
|
245
|
+
if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
246
|
+
else
|
|
247
|
+
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
248
|
+
? keyOrNodeOrEntryOrRaw
|
|
249
|
+
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
250
250
|
|
|
251
251
|
if (!nodeToDelete) {
|
|
252
252
|
return results;
|
|
@@ -1,22 +1,22 @@
|
|
|
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 {
|
|
9
9
|
BinaryTreeDeleteResult,
|
|
10
10
|
BSTNKeyOrNode,
|
|
11
|
-
BTNCallback,
|
|
12
11
|
BTNKeyOrNodeOrEntry,
|
|
13
12
|
IterationType,
|
|
13
|
+
OptBSTN,
|
|
14
14
|
RBTNColor,
|
|
15
15
|
TreeMultiMapNested,
|
|
16
16
|
TreeMultiMapNodeNested,
|
|
17
|
-
TreeMultiMapOptions
|
|
17
|
+
TreeMultiMapOptions,
|
|
18
|
+
BTNEntry
|
|
18
19
|
} from '../../types';
|
|
19
|
-
import { BTNEntry } from '../../types';
|
|
20
20
|
import { IBinaryTree } from '../../interfaces';
|
|
21
21
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
22
22
|
|
|
@@ -74,7 +74,7 @@ export class TreeMultiMap<
|
|
|
74
74
|
{
|
|
75
75
|
/**
|
|
76
76
|
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
77
|
-
* @param
|
|
77
|
+
* @param keysOrNodesOrEntriesOrRaws - The parameter `keysOrNodesOrEntriesOrRaws` is an
|
|
78
78
|
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
79
79
|
* TreeMultiMap with initial data.
|
|
80
80
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -82,11 +82,11 @@ export class TreeMultiMap<
|
|
|
82
82
|
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
83
83
|
*/
|
|
84
84
|
constructor(
|
|
85
|
-
|
|
85
|
+
keysOrNodesOrEntriesOrRaws: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
86
86
|
options?: TreeMultiMapOptions<K, V, R>
|
|
87
87
|
) {
|
|
88
88
|
super([], options);
|
|
89
|
-
if (
|
|
89
|
+
if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
protected _count = 0;
|
|
@@ -142,6 +142,8 @@ export class TreeMultiMap<
|
|
|
142
142
|
override createTree(options?: TreeMultiMapOptions<K, V, R>): TREE {
|
|
143
143
|
return new TreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
144
144
|
iterationType: this.iterationType,
|
|
145
|
+
comparator: this._comparator,
|
|
146
|
+
toEntryFn: this._toEntryFn,
|
|
145
147
|
...options
|
|
146
148
|
}) as TREE;
|
|
147
149
|
}
|
|
@@ -149,8 +151,8 @@ export class TreeMultiMap<
|
|
|
149
151
|
/**
|
|
150
152
|
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
151
153
|
* node based on the input.
|
|
152
|
-
* @param {
|
|
153
|
-
* `
|
|
154
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
155
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
154
156
|
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
155
157
|
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
156
158
|
* an existing node.
|
|
@@ -159,42 +161,39 @@ export class TreeMultiMap<
|
|
|
159
161
|
* @returns either a NODE object or undefined.
|
|
160
162
|
*/
|
|
161
163
|
override keyValueOrEntryOrRawElementToNode(
|
|
162
|
-
|
|
164
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
163
165
|
value?: V,
|
|
164
166
|
count = 1
|
|
165
167
|
): NODE | undefined {
|
|
166
|
-
if (
|
|
168
|
+
if (keyOrNodeOrEntryOrRaw === undefined || keyOrNodeOrEntryOrRaw === null) return;
|
|
167
169
|
|
|
168
|
-
if (this.isNode(
|
|
170
|
+
if (this.isNode(keyOrNodeOrEntryOrRaw)) return keyOrNodeOrEntryOrRaw;
|
|
169
171
|
|
|
170
|
-
if (this.isEntry(
|
|
171
|
-
const [key, entryValue] =
|
|
172
|
+
if (this.isEntry(keyOrNodeOrEntryOrRaw)) {
|
|
173
|
+
const [key, entryValue] = keyOrNodeOrEntryOrRaw;
|
|
172
174
|
if (key === undefined || key === null) return;
|
|
173
175
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
|
|
174
176
|
}
|
|
175
177
|
|
|
176
|
-
if (this.
|
|
177
|
-
const [key, entryValue] = this.
|
|
178
|
+
if (this._toEntryFn) {
|
|
179
|
+
const [key, entryValue] = this._toEntryFn(keyOrNodeOrEntryOrRaw as R);
|
|
178
180
|
if (this.isKey(key)) return this.createNode(key, value ?? entryValue, 'BLACK', count);
|
|
179
181
|
}
|
|
180
182
|
|
|
181
|
-
if (this.isKey(
|
|
182
|
-
return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
|
|
183
|
+
if (this.isKey(keyOrNodeOrEntryOrRaw)) return this.createNode(keyOrNodeOrEntryOrRaw, value, 'BLACK', count);
|
|
183
184
|
|
|
184
185
|
return;
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
/**
|
|
188
189
|
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
189
|
-
* @param {
|
|
190
|
-
* `
|
|
191
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
190
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
191
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
192
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
192
193
|
* an instance of the `TreeMultiMapNode` class.
|
|
193
194
|
*/
|
|
194
|
-
override isNode(
|
|
195
|
-
|
|
196
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
197
|
-
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
195
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
196
|
+
return keyOrNodeOrEntryOrRaw instanceof TreeMultiMapNode;
|
|
198
197
|
}
|
|
199
198
|
|
|
200
199
|
/**
|
|
@@ -203,8 +202,8 @@ export class TreeMultiMap<
|
|
|
203
202
|
*
|
|
204
203
|
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
205
204
|
* the count and returning a boolean indicating success.
|
|
206
|
-
* @param {
|
|
207
|
-
* `
|
|
205
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The
|
|
206
|
+
* `keyOrNodeOrEntryOrRaw` parameter can accept one of the following types:
|
|
208
207
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
209
208
|
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
210
209
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
@@ -213,8 +212,8 @@ export class TreeMultiMap<
|
|
|
213
212
|
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
214
213
|
* was successful, and false otherwise.
|
|
215
214
|
*/
|
|
216
|
-
override add(
|
|
217
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
215
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
216
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value, count);
|
|
218
217
|
const orgCount = newNode?.count || 0;
|
|
219
218
|
const isSuccessAdded = super.add(newNode);
|
|
220
219
|
|
|
@@ -230,30 +229,31 @@ export class TreeMultiMap<
|
|
|
230
229
|
* Time Complexity: O(log n)
|
|
231
230
|
* Space Complexity: O(1)
|
|
232
231
|
*
|
|
233
|
-
* The function `delete`
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
* the
|
|
238
|
-
* @param
|
|
239
|
-
*
|
|
240
|
-
* `
|
|
241
|
-
*
|
|
242
|
-
* @
|
|
243
|
-
* being deleted. If set to true, the count of the node will not be taken into account when deleting
|
|
244
|
-
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
245
|
-
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
232
|
+
* The function `delete` in TypeScript overrides the deletion operation in a binary tree data
|
|
233
|
+
* structure, handling cases where nodes have children 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 or key based on which a node
|
|
236
|
+
* should be deleted from the binary tree. It can be a key, a node, or an entry.
|
|
237
|
+
* @param [ignoreCount=false] - The `ignoreCount` parameter in the `override delete` method is a
|
|
238
|
+
* boolean flag that determines whether to ignore the count of nodes when performing deletion. If
|
|
239
|
+
* `ignoreCount` is set to `true`, the method will delete the node regardless of its count. If
|
|
240
|
+
* `ignoreCount` is `false
|
|
241
|
+
* @returns The `override delete` method returns an array of `BinaryTreeDeleteResult<NODE>` objects.
|
|
246
242
|
*/
|
|
247
|
-
override delete
|
|
248
|
-
|
|
249
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
243
|
+
override delete(
|
|
244
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
250
245
|
ignoreCount = false
|
|
251
246
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
252
|
-
if (
|
|
247
|
+
if (keyOrNodeOrEntryOrRaw === null) return [];
|
|
248
|
+
|
|
253
249
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
254
|
-
callback = this._ensureCallback(identifier, callback);
|
|
255
250
|
|
|
256
|
-
|
|
251
|
+
let nodeToDelete: OptBSTN<NODE>;
|
|
252
|
+
if (this._isPredicated(keyOrNodeOrEntryOrRaw)) nodeToDelete = this.getNode(keyOrNodeOrEntryOrRaw);
|
|
253
|
+
else
|
|
254
|
+
nodeToDelete = this.isRealNode(keyOrNodeOrEntryOrRaw)
|
|
255
|
+
? keyOrNodeOrEntryOrRaw
|
|
256
|
+
: this.getNode(keyOrNodeOrEntryOrRaw);
|
|
257
257
|
|
|
258
258
|
if (!nodeToDelete) {
|
|
259
259
|
return results;
|
|
@@ -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 { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
|
@@ -564,7 +564,7 @@ export abstract class AbstractGraph<
|
|
|
564
564
|
}
|
|
565
565
|
}
|
|
566
566
|
|
|
567
|
-
getMinDist
|
|
567
|
+
if (getMinDist)
|
|
568
568
|
distMap.forEach((d, v) => {
|
|
569
569
|
if (v !== srcVertex) {
|
|
570
570
|
if (d < minDist) {
|
|
@@ -574,7 +574,7 @@ export abstract class AbstractGraph<
|
|
|
574
574
|
}
|
|
575
575
|
});
|
|
576
576
|
|
|
577
|
-
genPaths
|
|
577
|
+
if (genPaths) getPaths(minDest);
|
|
578
578
|
|
|
579
579
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
580
580
|
}
|
|
@@ -761,7 +761,7 @@ export abstract class AbstractGraph<
|
|
|
761
761
|
if (sWeight !== undefined && dWeight !== undefined) {
|
|
762
762
|
if (distMap.get(s) !== Infinity && sWeight + weight < dWeight) {
|
|
763
763
|
distMap.set(d, sWeight + weight);
|
|
764
|
-
genPath
|
|
764
|
+
if (genPath) preMap.set(d, s);
|
|
765
765
|
}
|
|
766
766
|
}
|
|
767
767
|
}
|
|
@@ -900,7 +900,7 @@ export abstract class AbstractGraph<
|
|
|
900
900
|
currentPath.push(vertex.key);
|
|
901
901
|
|
|
902
902
|
for (const neighbor of this.getNeighbors(vertex)) {
|
|
903
|
-
neighbor
|
|
903
|
+
if (neighbor) dfs(neighbor, currentPath, visited);
|
|
904
904
|
}
|
|
905
905
|
|
|
906
906
|
visited.delete(vertex);
|
|
@@ -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 { TopologicalStatus, VertexKey } from '../../types';
|
|
@@ -260,8 +260,8 @@ export class DirectedGraph<
|
|
|
260
260
|
const v1ToV2 = this.deleteEdgeSrcToDest(v1, v2);
|
|
261
261
|
const v2ToV1 = this.deleteEdgeSrcToDest(v2, v1);
|
|
262
262
|
|
|
263
|
-
v1ToV2
|
|
264
|
-
v2ToV1
|
|
263
|
+
if (v1ToV2) removed.push(v1ToV2);
|
|
264
|
+
if (v2ToV1) removed.push(v2ToV1);
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
return removed;
|
|
@@ -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 { VertexKey } from '../../types';
|
|
@@ -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 {
|
|
@@ -111,7 +111,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
|
|
|
111
111
|
* @returns A boolean value indicating whether the size of the object is 0 or not.
|
|
112
112
|
*/
|
|
113
113
|
isEmpty(): boolean {
|
|
114
|
-
return this.
|
|
114
|
+
return this._size === 0;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
/**
|
|
@@ -626,7 +626,7 @@ export class FibonacciHeap<E> {
|
|
|
626
626
|
* @returns The top element or undefined if the heap is empty.
|
|
627
627
|
*/
|
|
628
628
|
pop(): E | undefined {
|
|
629
|
-
if (this.
|
|
629
|
+
if (this._size === 0) return undefined;
|
|
630
630
|
|
|
631
631
|
const z = this.min!;
|
|
632
632
|
if (z.child) {
|
|
@@ -770,7 +770,7 @@ export class FibonacciHeap<E> {
|
|
|
770
770
|
* @protected
|
|
771
771
|
*/
|
|
772
772
|
protected _consolidate(): void {
|
|
773
|
-
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this.
|
|
773
|
+
const A: (FibonacciHeapNode<E> | undefined)[] = new Array(this._size);
|
|
774
774
|
const elements = this.consumeLinkedList(this.root);
|
|
775
775
|
let x: FibonacciHeapNode<E> | undefined,
|
|
776
776
|
y: FibonacciHeapNode<E> | undefined,
|
|
@@ -798,7 +798,7 @@ export class FibonacciHeap<E> {
|
|
|
798
798
|
A[d] = x;
|
|
799
799
|
}
|
|
800
800
|
|
|
801
|
-
for (let i = 0; i < this.
|
|
801
|
+
for (let i = 0; i < this._size; i++) {
|
|
802
802
|
if (A[i] && this.comparator(A[i]!.element, this.min!.element) <= 0) {
|
|
803
803
|
this._min = A[i]!;
|
|
804
804
|
}
|
|
@@ -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 { DoublyLinkedListOptions, ElementCallback } from '../../types';
|
|
@@ -267,7 +267,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
267
267
|
* or the linked list is empty, it will return undefined.
|
|
268
268
|
*/
|
|
269
269
|
at(index: number): E | undefined {
|
|
270
|
-
if (index < 0 || index >= this.
|
|
270
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
271
271
|
let current = this.head;
|
|
272
272
|
for (let i = 0; i < index; i++) {
|
|
273
273
|
current = current!.next;
|
|
@@ -287,7 +287,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
287
287
|
* valid range of the linked list, otherwise it returns `undefined`.
|
|
288
288
|
*/
|
|
289
289
|
getNodeAt(index: number): DoublyLinkedListNode<E> | undefined {
|
|
290
|
-
if (index < 0 || index >= this.
|
|
290
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
291
291
|
let current = this.head;
|
|
292
292
|
for (let i = 0; i < index; i++) {
|
|
293
293
|
current = current!.next;
|
|
@@ -331,12 +331,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
331
331
|
* if the index is out of bounds.
|
|
332
332
|
*/
|
|
333
333
|
addAt(index: number, value: E): boolean {
|
|
334
|
-
if (index < 0 || index > this.
|
|
334
|
+
if (index < 0 || index > this._size) return false;
|
|
335
335
|
if (index === 0) {
|
|
336
336
|
this.unshift(value);
|
|
337
337
|
return true;
|
|
338
338
|
}
|
|
339
|
-
if (index === this.
|
|
339
|
+
if (index === this._size) {
|
|
340
340
|
this.push(value);
|
|
341
341
|
return true;
|
|
342
342
|
}
|
|
@@ -442,12 +442,12 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
442
442
|
* bounds.
|
|
443
443
|
*/
|
|
444
444
|
deleteAt(index: number): boolean {
|
|
445
|
-
if (index < 0 || index >= this.
|
|
445
|
+
if (index < 0 || index >= this._size) return false;
|
|
446
446
|
if (index === 0) {
|
|
447
447
|
this.shift();
|
|
448
448
|
return true;
|
|
449
449
|
}
|
|
450
|
-
if (index === this.
|
|
450
|
+
if (index === this._size - 1) {
|
|
451
451
|
this.pop();
|
|
452
452
|
return true;
|
|
453
453
|
}
|
|
@@ -505,7 +505,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
505
505
|
* @returns A boolean value is being returned.
|
|
506
506
|
*/
|
|
507
507
|
isEmpty(): boolean {
|
|
508
|
-
return this.
|
|
508
|
+
return this._size === 0;
|
|
509
509
|
}
|
|
510
510
|
|
|
511
511
|
/**
|
|
@@ -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 { ElementCallback, SinglyLinkedListOptions } from '../../types';
|
|
@@ -237,7 +237,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
237
237
|
* `undefined` if the index is out of bounds.
|
|
238
238
|
*/
|
|
239
239
|
at(index: number): E | undefined {
|
|
240
|
-
if (index < 0 || index >= this.
|
|
240
|
+
if (index < 0 || index >= this._size) return undefined;
|
|
241
241
|
let current = this.head;
|
|
242
242
|
for (let i = 0; i < index; i++) {
|
|
243
243
|
current = current!.next;
|
|
@@ -274,12 +274,12 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
274
274
|
* bounds.
|
|
275
275
|
*/
|
|
276
276
|
deleteAt(index: number): boolean {
|
|
277
|
-
if (index < 0 || index >= this.
|
|
277
|
+
if (index < 0 || index >= this._size) return false;
|
|
278
278
|
if (index === 0) {
|
|
279
279
|
this.shift();
|
|
280
280
|
return true;
|
|
281
281
|
}
|
|
282
|
-
if (index === this.
|
|
282
|
+
if (index === this._size - 1) {
|
|
283
283
|
this.pop();
|
|
284
284
|
return true;
|
|
285
285
|
}
|
|
@@ -348,12 +348,12 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
348
348
|
* if the index is out of bounds.
|
|
349
349
|
*/
|
|
350
350
|
addAt(index: number, value: E): boolean {
|
|
351
|
-
if (index < 0 || index > this.
|
|
351
|
+
if (index < 0 || index > this._size) return false;
|
|
352
352
|
if (index === 0) {
|
|
353
353
|
this.unshift(value);
|
|
354
354
|
return true;
|
|
355
355
|
}
|
|
356
|
-
if (index === this.
|
|
356
|
+
if (index === this._size) {
|
|
357
357
|
this.push(value);
|
|
358
358
|
return true;
|
|
359
359
|
}
|
|
@@ -372,7 +372,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
|
|
|
372
372
|
* @returns A boolean value indicating whether the length of the object is equal to 0.
|
|
373
373
|
*/
|
|
374
374
|
isEmpty(): boolean {
|
|
375
|
-
return this.
|
|
375
|
+
return this._size === 0;
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
/**
|