heap-typed 1.51.7 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -80
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +103 -74
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +116 -93
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +90 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +318 -233
- package/dist/data-structures/binary-tree/binary-tree.js +492 -392
- package/dist/data-structures/binary-tree/bst.d.ts +204 -251
- package/dist/data-structures/binary-tree/bst.js +256 -358
- package/dist/data-structures/binary-tree/rb-tree.d.ts +74 -85
- package/dist/data-structures/binary-tree/rb-tree.js +111 -119
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -76
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +27 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -100
- package/src/data-structures/binary-tree/avl-tree.ts +109 -80
- package/src/data-structures/binary-tree/binary-tree.ts +556 -433
- package/src/data-structures/binary-tree/bst.ts +286 -375
- package/src/data-structures/binary-tree/rb-tree.ts +132 -125
- package/src/data-structures/binary-tree/tree-multi-map.ts +129 -102
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +11 -9
- package/src/types/common.ts +2 -3
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -3
- package/src/types/data-structures/binary-tree/avl-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +7 -6
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -3
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
-
import {
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, IterationType, KeyOrNodeOrEntry, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
+
import { BTNEntry } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
12
|
-
export declare class TreeMultiMapNode<K
|
|
12
|
+
export declare class TreeMultiMapNode<K extends Comparable, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
15
15
|
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
@@ -36,17 +36,17 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
|
|
|
36
36
|
*/
|
|
37
37
|
set count(value: number);
|
|
38
38
|
}
|
|
39
|
-
export declare class TreeMultiMap<K
|
|
39
|
+
export declare class TreeMultiMap<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>> extends RedBlackTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
40
40
|
/**
|
|
41
|
-
* The constructor function initializes a
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
41
|
+
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
42
|
+
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
43
|
+
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
44
|
+
* TreeMultiMap with initial data.
|
|
45
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
46
|
+
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
47
|
+
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
48
48
|
*/
|
|
49
|
-
constructor(
|
|
49
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultiMapOptions<K, V, R>);
|
|
50
50
|
protected _count: number;
|
|
51
51
|
/**
|
|
52
52
|
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
@@ -69,48 +69,47 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
69
69
|
/**
|
|
70
70
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
71
71
|
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
72
|
-
* which is a generic type representing the
|
|
73
|
-
* @param {V} [value] - The `value` parameter
|
|
74
|
-
* node. It is
|
|
75
|
-
* function. If provided, it should be of type `V`.
|
|
72
|
+
* which is a generic type representing the type of keys in the tree.
|
|
73
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
74
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
76
75
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
77
76
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
78
77
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
79
78
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
80
79
|
* with a key in the tree.
|
|
81
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
80
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
82
81
|
*/
|
|
83
82
|
createNode(key: K, value?: V, color?: RBTNColor, count?: number): NODE;
|
|
84
83
|
/**
|
|
85
84
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
86
85
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
87
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
88
|
-
*
|
|
86
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
87
|
+
* R>`.
|
|
89
88
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
90
|
-
* existing `iterationType`
|
|
89
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
91
90
|
*/
|
|
92
|
-
createTree(options?: TreeMultiMapOptions<K>): TREE;
|
|
91
|
+
createTree(options?: TreeMultiMapOptions<K, V, R>): TREE;
|
|
93
92
|
/**
|
|
94
|
-
* The function `
|
|
95
|
-
*
|
|
96
|
-
* @param
|
|
97
|
-
*
|
|
98
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
93
|
+
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
94
|
+
* node based on the input.
|
|
95
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
96
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
97
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
98
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
99
|
+
* an existing node.
|
|
100
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
101
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
102
|
+
* @returns either a NODE object or undefined.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
|
|
105
105
|
/**
|
|
106
|
-
* The function
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* of the `TreeMultiMapNode` class.
|
|
106
|
+
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
107
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
108
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
109
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
110
|
+
* an instance of the `TreeMultiMapNode` class.
|
|
112
111
|
*/
|
|
113
|
-
isNode(
|
|
112
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
114
113
|
/**
|
|
115
114
|
* Time Complexity: O(log n)
|
|
116
115
|
* Space Complexity: O(1)
|
|
@@ -119,16 +118,19 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
119
118
|
* Time Complexity: O(log n)
|
|
120
119
|
* Space Complexity: O(1)
|
|
121
120
|
*
|
|
122
|
-
* The function overrides the add method
|
|
123
|
-
*
|
|
121
|
+
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
122
|
+
* the count and returning a boolean indicating success.
|
|
123
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
124
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
124
125
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
125
|
-
* data structure.
|
|
126
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
126
127
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
127
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
128
|
-
*
|
|
129
|
-
* @returns a boolean value.
|
|
128
|
+
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
129
|
+
* for `count`, the key-value pair will be added once.
|
|
130
|
+
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
131
|
+
* was successful, and false otherwise.
|
|
130
132
|
*/
|
|
131
|
-
add(
|
|
133
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
|
|
132
134
|
/**
|
|
133
135
|
* Time Complexity: O(log n)
|
|
134
136
|
* Space Complexity: O(1)
|
|
@@ -137,20 +139,18 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
137
139
|
* Time Complexity: O(log n)
|
|
138
140
|
* Space Complexity: O(1)
|
|
139
141
|
*
|
|
140
|
-
* The `delete`
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
153
|
-
* target node will be decremented
|
|
142
|
+
* The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
|
|
143
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
|
|
144
|
+
* key that is used to identify the node that needs to be deleted from the binary tree. It can be of
|
|
145
|
+
* any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
146
|
+
* the node to be deleted
|
|
147
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
148
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
149
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
|
|
150
|
+
* specific node or when performing other operations on the
|
|
151
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
152
|
+
* being deleted. If set to true, the count of the node will not be taken into account when deleting
|
|
153
|
+
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
154
154
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
155
155
|
*/
|
|
156
156
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
@@ -176,10 +176,12 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
176
176
|
*
|
|
177
177
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
178
178
|
* tree using either a recursive or iterative approach.
|
|
179
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
180
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
181
|
-
*
|
|
182
|
-
*
|
|
179
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
180
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
181
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified by the
|
|
182
|
+
* `iterationType` property of the current object.
|
|
183
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
184
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
183
185
|
*/
|
|
184
186
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
185
187
|
/**
|
|
@@ -195,24 +197,38 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
195
197
|
*/
|
|
196
198
|
clone(): TREE;
|
|
197
199
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
*
|
|
200
|
+
* Time Complexity: O(1)
|
|
201
|
+
* Space Complexity: O(1)
|
|
202
|
+
*/
|
|
203
|
+
/**
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
206
|
+
*
|
|
207
|
+
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
208
|
+
* in a binary search tree.
|
|
209
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
210
|
+
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
211
|
+
* instance of the `BSTNKeyOrNode<K, NODE>` class.
|
|
212
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
213
|
+
* node where the properties will be swapped with the source node.
|
|
203
214
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
204
|
-
* If
|
|
205
|
-
|
|
206
|
-
|
|
215
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
216
|
+
*/
|
|
217
|
+
protected _swapProperties(srcNode: R | BSTNKeyOrNode<K, NODE>, destNode: R | BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
218
|
+
/**
|
|
219
|
+
* Time Complexity: O(1)
|
|
220
|
+
* Space Complexity: O(1)
|
|
207
221
|
*/
|
|
208
|
-
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
209
222
|
/**
|
|
223
|
+
* Time Complexity: O(1)
|
|
224
|
+
* Space Complexity: O(1)
|
|
225
|
+
*
|
|
210
226
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
211
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
212
|
-
*
|
|
213
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
227
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
|
|
228
|
+
* structure.
|
|
229
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
214
230
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
215
|
-
* superclass,
|
|
231
|
+
* superclass, which is of type `NODE`.
|
|
216
232
|
*/
|
|
217
233
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
218
234
|
}
|
|
@@ -39,19 +39,19 @@ class TreeMultiMapNode extends rb_tree_1.RedBlackTreeNode {
|
|
|
39
39
|
exports.TreeMultiMapNode = TreeMultiMapNode;
|
|
40
40
|
class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
41
41
|
/**
|
|
42
|
-
* The constructor function initializes a
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
42
|
+
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
43
|
+
* @param keysOrNodesOrEntriesOrRawElements - The parameter `keysOrNodesOrEntriesOrRawElements` is an
|
|
44
|
+
* iterable that can contain keys, nodes, entries, or raw elements. It is used to initialize the
|
|
45
|
+
* TreeMultiMap with initial data.
|
|
46
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
47
|
+
* behavior of the `TreeMultiMap` constructor. It can include properties such as `compareKeys` and
|
|
48
|
+
* `compareValues`, which are functions used to compare keys and values respectively.
|
|
49
49
|
*/
|
|
50
|
-
constructor(
|
|
50
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
51
51
|
super([], options);
|
|
52
52
|
this._count = 0;
|
|
53
|
-
if (
|
|
54
|
-
this.addMany(
|
|
53
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
54
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
55
55
|
}
|
|
56
56
|
// TODO the _count is not accurate after nodes count modified
|
|
57
57
|
/**
|
|
@@ -81,16 +81,15 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
81
81
|
/**
|
|
82
82
|
* The function creates a new TreeMultiMapNode with the specified key, value, color, and count.
|
|
83
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 representing the
|
|
85
|
-
* @param {V} [value] - The `value` parameter
|
|
86
|
-
* node. It is
|
|
87
|
-
* function. If provided, it should be of type `V`.
|
|
84
|
+
* which is a generic type representing the type of keys in the tree.
|
|
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.
|
|
88
87
|
* @param {RBTNColor} [color=BLACK] - The color parameter is used to specify the color of the node in
|
|
89
88
|
* a Red-Black Tree. It can have two possible values: 'RED' or 'BLACK'. The default value is 'BLACK'.
|
|
90
89
|
* @param {number} [count] - The `count` parameter represents the number of occurrences of a key in
|
|
91
90
|
* the tree. It is an optional parameter and is used to keep track of the number of values associated
|
|
92
91
|
* with a key in the tree.
|
|
93
|
-
* @returns A new instance of the TreeMultiMapNode class
|
|
92
|
+
* @returns A new instance of the TreeMultiMapNode class, casted as NODE.
|
|
94
93
|
*/
|
|
95
94
|
createNode(key, value, color = 'BLACK', count) {
|
|
96
95
|
return new TreeMultiMapNode(key, value, count, color);
|
|
@@ -98,60 +97,56 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
98
97
|
/**
|
|
99
98
|
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
100
99
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
101
|
-
* configuration options for creating the `TreeMultiMap`. It
|
|
102
|
-
*
|
|
100
|
+
* configuration options for creating the `TreeMultiMap`. It is of type `TreeMultiMapOptions<K, V,
|
|
101
|
+
* R>`.
|
|
103
102
|
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
104
|
-
* existing `iterationType`
|
|
103
|
+
* existing `iterationType` property. The returned value is casted as `TREE`.
|
|
105
104
|
*/
|
|
106
105
|
createTree(options) {
|
|
107
106
|
return new TreeMultiMap([], Object.assign({ iterationType: this.iterationType }, options));
|
|
108
107
|
}
|
|
109
108
|
/**
|
|
110
|
-
* The function `
|
|
111
|
-
*
|
|
112
|
-
* @param
|
|
113
|
-
*
|
|
114
|
-
* @param {V} [value] - The `value` parameter is an optional value
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
109
|
+
* The function `keyValueOrEntryOrRawElementToNode` takes in a key, value, and count and returns a
|
|
110
|
+
* node based on the input.
|
|
111
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
112
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
113
|
+
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
114
|
+
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
115
|
+
* an existing node.
|
|
116
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
117
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
118
|
+
* @returns either a NODE object or undefined.
|
|
119
119
|
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
120
|
+
keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
121
|
+
if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null)
|
|
123
122
|
return;
|
|
123
|
+
if (this.isNode(keyOrNodeOrEntryOrRawElement))
|
|
124
|
+
return keyOrNodeOrEntryOrRawElement;
|
|
125
|
+
if (this.toEntryFn) {
|
|
126
|
+
const [key] = this.toEntryFn(keyOrNodeOrEntryOrRawElement);
|
|
127
|
+
if (key)
|
|
128
|
+
return this.getNodeByKey(key);
|
|
124
129
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
129
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
130
|
-
if (key === undefined || key === null) {
|
|
130
|
+
if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
131
|
+
const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
132
|
+
if (key === undefined || key === null)
|
|
131
133
|
return;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
node = this.createNode(key, value, 'BLACK', count);
|
|
135
|
-
}
|
|
134
|
+
else
|
|
135
|
+
return this.createNode(key, value, 'BLACK', count);
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
else {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
return node;
|
|
137
|
+
if (this.isKey(keyOrNodeOrEntryOrRawElement))
|
|
138
|
+
return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'BLACK', count);
|
|
139
|
+
return;
|
|
144
140
|
}
|
|
145
141
|
/**
|
|
146
|
-
* The function
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
* of the `TreeMultiMapNode` class.
|
|
142
|
+
* The function checks if the input is an instance of the TreeMultiMapNode class.
|
|
143
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
144
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
145
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
146
|
+
* an instance of the `TreeMultiMapNode` class.
|
|
152
147
|
*/
|
|
153
|
-
isNode(
|
|
154
|
-
return
|
|
148
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
149
|
+
return keyOrNodeOrEntryOrRawElement instanceof TreeMultiMapNode;
|
|
155
150
|
}
|
|
156
151
|
/**
|
|
157
152
|
* Time Complexity: O(log n)
|
|
@@ -161,17 +156,20 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
161
156
|
* Time Complexity: O(log n)
|
|
162
157
|
* Space Complexity: O(1)
|
|
163
158
|
*
|
|
164
|
-
* The function overrides the add method
|
|
165
|
-
*
|
|
159
|
+
* The function overrides the add method of a class and adds a new node to a data structure, updating
|
|
160
|
+
* the count and returning a boolean indicating success.
|
|
161
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
|
|
162
|
+
* `keyOrNodeOrEntryOrRawElement` parameter can accept one of the following types:
|
|
166
163
|
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
167
|
-
* data structure.
|
|
164
|
+
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
168
165
|
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
169
|
-
* be added to the data structure. By default, it is set to 1, meaning that
|
|
170
|
-
*
|
|
171
|
-
* @returns a boolean value.
|
|
166
|
+
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
167
|
+
* for `count`, the key-value pair will be added once.
|
|
168
|
+
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
169
|
+
* was successful, and false otherwise.
|
|
172
170
|
*/
|
|
173
|
-
add(
|
|
174
|
-
const newNode = this.
|
|
171
|
+
add(keyOrNodeOrEntryOrRawElement, value, count = 1) {
|
|
172
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
|
|
175
173
|
const orgCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
176
174
|
const isSuccessAdded = super.add(newNode);
|
|
177
175
|
if (isSuccessAdded) {
|
|
@@ -190,20 +188,18 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
190
188
|
* Time Complexity: O(log n)
|
|
191
189
|
* Space Complexity: O(1)
|
|
192
190
|
*
|
|
193
|
-
* The `delete`
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*
|
|
204
|
-
*
|
|
205
|
-
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
206
|
-
* target node will be decremented
|
|
191
|
+
* The function `delete` is used to remove a node from a binary tree and fix the tree if necessary.
|
|
192
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value or
|
|
193
|
+
* key that is used to identify the node that needs to be deleted from the binary tree. It can be of
|
|
194
|
+
* any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
195
|
+
* the node to be deleted
|
|
196
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
197
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
198
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function is used to compare nodes when searching for a
|
|
199
|
+
* specific node or when performing other operations on the
|
|
200
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
201
|
+
* being deleted. If set to true, the count of the node will not be taken into account when deleting
|
|
202
|
+
* it. If set to false, the count of the node will be decremented by 1 before deleting it.
|
|
207
203
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
208
204
|
*/
|
|
209
205
|
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
@@ -319,10 +315,12 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
319
315
|
*
|
|
320
316
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
321
317
|
* tree using either a recursive or iterative approach.
|
|
322
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
323
|
-
* type of iteration to use when building the balanced binary search tree. It
|
|
324
|
-
*
|
|
325
|
-
*
|
|
318
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
319
|
+
* specifies the type of iteration to use when building the balanced binary search tree. It has a
|
|
320
|
+
* default value of `this.iterationType`, which means it will use the iteration type specified by the
|
|
321
|
+
* `iterationType` property of the current object.
|
|
322
|
+
* @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
|
|
323
|
+
* balancing operation is successful, and `false` if there are no nodes to balance.
|
|
326
324
|
*/
|
|
327
325
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
328
326
|
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
@@ -377,15 +375,22 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
377
375
|
return cloned;
|
|
378
376
|
}
|
|
379
377
|
/**
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
*
|
|
378
|
+
* Time Complexity: O(1)
|
|
379
|
+
* Space Complexity: O(1)
|
|
380
|
+
*/
|
|
381
|
+
/**
|
|
382
|
+
* Time Complexity: O(1)
|
|
383
|
+
* Space Complexity: O(1)
|
|
384
|
+
*
|
|
385
|
+
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
386
|
+
* in a binary search tree.
|
|
387
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
388
|
+
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
389
|
+
* instance of the `BSTNKeyOrNode<K, NODE>` class.
|
|
390
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
391
|
+
* node where the properties will be swapped with the source node.
|
|
385
392
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
386
|
-
* If
|
|
387
|
-
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
388
|
-
* If either `srcNode` or `destNode` is
|
|
393
|
+
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
389
394
|
*/
|
|
390
395
|
_swapProperties(srcNode, destNode) {
|
|
391
396
|
srcNode = this.ensureNode(srcNode);
|
|
@@ -409,12 +414,19 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
409
414
|
return undefined;
|
|
410
415
|
}
|
|
411
416
|
/**
|
|
417
|
+
* Time Complexity: O(1)
|
|
418
|
+
* Space Complexity: O(1)
|
|
419
|
+
*/
|
|
420
|
+
/**
|
|
421
|
+
* Time Complexity: O(1)
|
|
422
|
+
* Space Complexity: O(1)
|
|
423
|
+
*
|
|
412
424
|
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
413
|
-
* @param {NODE} oldNode - The `oldNode` parameter is
|
|
414
|
-
*
|
|
415
|
-
* @param {NODE} newNode - The `newNode` parameter is an
|
|
425
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
|
|
426
|
+
* structure.
|
|
427
|
+
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
416
428
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
417
|
-
* superclass,
|
|
429
|
+
* superclass, which is of type `NODE`.
|
|
418
430
|
*/
|
|
419
431
|
_replaceNode(oldNode, newNode) {
|
|
420
432
|
newNode.count = oldNode.count + newNode.count;
|
|
@@ -231,9 +231,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
231
231
|
getMinPathBetween(v1: VO | VertexKey, v2: VO | VertexKey, isWeight?: boolean, isDFS?: boolean): VO[] | undefined;
|
|
232
232
|
/**
|
|
233
233
|
* Dijkstra algorithm time: O(VE) space: O(VO + EO)
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
/**
|
|
234
|
+
*/
|
|
235
|
+
/**
|
|
237
236
|
* Time Complexity: O(V^2 + E) - Quadratic time in the worst case (no heap optimization).
|
|
238
237
|
* Space Complexity: O(V + E) - Depends on the implementation (Dijkstra's algorithm).
|
|
239
238
|
*/
|
|
@@ -264,9 +263,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
264
263
|
* 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.
|
|
265
264
|
* 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.
|
|
266
265
|
*
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
/**
|
|
266
|
+
*/
|
|
267
|
+
/**
|
|
270
268
|
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
271
269
|
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
272
270
|
*/
|
|
@@ -295,9 +293,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
295
293
|
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
296
294
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
297
295
|
* one to rest pairs
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
/**
|
|
296
|
+
*/
|
|
297
|
+
/**
|
|
301
298
|
* Time Complexity: O(V * E) - Quadratic time in the worst case (Bellman-Ford algorithm).
|
|
302
299
|
* Space Complexity: O(V + E) - Depends on the implementation (Bellman-Ford algorithm).
|
|
303
300
|
*
|
|
@@ -325,9 +322,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
325
322
|
};
|
|
326
323
|
/**
|
|
327
324
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
/**
|
|
325
|
+
*/
|
|
326
|
+
/**
|
|
331
327
|
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
332
328
|
* 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.
|
|
333
329
|
*/
|
|
@@ -343,9 +339,8 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
343
339
|
* Not support graph with negative weight cycle
|
|
344
340
|
* all pairs
|
|
345
341
|
* 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.
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
/**
|
|
342
|
+
*/
|
|
343
|
+
/**
|
|
349
344
|
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
350
345
|
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
351
346
|
*
|