red-black-tree-typed 1.53.6 → 1.54.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/README.md +52 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -170
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -331
- package/dist/data-structures/binary-tree/avl-tree.d.ts +103 -69
- package/dist/data-structures/binary-tree/avl-tree.js +131 -71
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +309 -208
- package/dist/data-structures/binary-tree/binary-tree.js +382 -300
- package/dist/data-structures/binary-tree/bst.d.ts +245 -127
- package/dist/data-structures/binary-tree/bst.js +366 -163
- package/dist/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/data-structures/binary-tree/index.js +3 -1
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +286 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +181 -108
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -170
- package/dist/data-structures/binary-tree/tree-multi-map.js +145 -367
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +64 -19
- package/dist/data-structures/linked-list/doubly-linked-list.js +92 -31
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +48 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +74 -27
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +111 -10
- package/dist/data-structures/trie/trie.js +123 -18
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -5
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +152 -373
- package/src/data-structures/binary-tree/avl-tree.ts +164 -106
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +563 -447
- package/src/data-structures/binary-tree/bst.ts +433 -237
- package/src/data-structures/binary-tree/index.ts +3 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +224 -146
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +159 -401
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +186 -118
- package/src/data-structures/linked-list/singly-linked-list.ts +81 -28
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -17
- package/src/index.ts +4 -3
- package/src/interfaces/binary-tree.ts +10 -21
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -6
- package/src/types/data-structures/binary-tree/bst.ts +8 -7
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -205
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
|
@@ -5,465 +5,223 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
BSTNOptKeyOrNode,
|
|
11
|
-
BTNRep,
|
|
12
|
-
IterationType,
|
|
13
|
-
OptNode,
|
|
14
|
-
RBTNColor,
|
|
15
|
-
TreeMultiMapNested,
|
|
16
|
-
TreeMultiMapNodeNested,
|
|
17
|
-
TreeMultiMapOptions
|
|
18
|
-
} from '../../types';
|
|
8
|
+
import type { BTNOptKeyOrNull, BTNRep, OptNodeOrNull, TreeMultiMapOptions } from '../../types';
|
|
9
|
+
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
19
10
|
import { IBinaryTree } from '../../interfaces';
|
|
20
|
-
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
21
11
|
|
|
22
|
-
export class TreeMultiMapNode<
|
|
23
|
-
K = any,
|
|
24
|
-
V = any,
|
|
25
|
-
NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
|
|
26
|
-
> extends RedBlackTreeNode<K, V, NODE> {
|
|
12
|
+
export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
|
|
27
13
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* used to
|
|
31
|
-
*
|
|
32
|
-
* associated
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* in the Red-Black Tree. It is an optional parameter with a default value of 1.
|
|
36
|
-
* @param {RBTNColor} [color=BLACK] - The `color` parameter is used to specify the color of the node
|
|
37
|
-
* in a Red-Black Tree. It is optional and has a default value of `'BLACK'`.
|
|
14
|
+
* This TypeScript constructor initializes an object with a key of type K and an array of values of
|
|
15
|
+
* type V.
|
|
16
|
+
* @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
|
|
17
|
+
* data being stored in the data structure. It helps in quickly accessing or retrieving the
|
|
18
|
+
* associated value in the data structure.
|
|
19
|
+
* @param {V[]} value - The `value` parameter in the constructor represents an array of values of
|
|
20
|
+
* type `V`.
|
|
38
21
|
*/
|
|
39
|
-
constructor(key: K, value
|
|
40
|
-
super(key, value
|
|
41
|
-
this.count = count;
|
|
22
|
+
constructor(key: K, value: V[]) {
|
|
23
|
+
super(key, value);
|
|
42
24
|
}
|
|
43
25
|
|
|
44
|
-
|
|
26
|
+
override parent?: TreeMultiMapNode<K, V> = undefined;
|
|
45
27
|
|
|
46
|
-
|
|
47
|
-
* The function returns the value of the private variable _count.
|
|
48
|
-
* @returns The count property of the object, which is of type number.
|
|
49
|
-
*/
|
|
50
|
-
get count(): number {
|
|
51
|
-
return this._count;
|
|
52
|
-
}
|
|
28
|
+
override _left?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
|
|
53
29
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
57
|
-
* numeric value.
|
|
58
|
-
*/
|
|
59
|
-
set count(value: number) {
|
|
60
|
-
this._count = value;
|
|
30
|
+
override get left(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
|
|
31
|
+
return this._left;
|
|
61
32
|
}
|
|
62
|
-
}
|
|
63
33
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
TREE extends TreeMultiMap<K, V, R, NODE, TREE> = TreeMultiMap<K, V, R, NODE, TreeMultiMapNested<K, V, R, NODE>>
|
|
70
|
-
>
|
|
71
|
-
extends RedBlackTree<K, V, R, NODE, TREE>
|
|
72
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
73
|
-
{
|
|
74
|
-
/**
|
|
75
|
-
* The constructor function initializes a TreeMultiMap object with optional initial data.
|
|
76
|
-
* @param keysNodesEntriesOrRaws - The parameter `keysNodesEntriesOrRaws` 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.
|
|
82
|
-
*/
|
|
83
|
-
constructor(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE>> = [], options?: TreeMultiMapOptions<K, V, R>) {
|
|
84
|
-
super([], options);
|
|
85
|
-
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
34
|
+
override set left(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
|
|
35
|
+
if (v) {
|
|
36
|
+
v.parent = this;
|
|
37
|
+
}
|
|
38
|
+
this._left = v;
|
|
86
39
|
}
|
|
87
40
|
|
|
88
|
-
|
|
41
|
+
override _right?: OptNodeOrNull<TreeMultiMapNode<K, V>> = undefined;
|
|
89
42
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
93
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
94
|
-
*/
|
|
95
|
-
get count(): number {
|
|
96
|
-
return this._count;
|
|
43
|
+
override get right(): OptNodeOrNull<TreeMultiMapNode<K, V>> {
|
|
44
|
+
return this._right;
|
|
97
45
|
}
|
|
98
46
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
* search.
|
|
105
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
106
|
-
*/
|
|
107
|
-
getComputedCount(): number {
|
|
108
|
-
let sum = 0;
|
|
109
|
-
this.dfs(node => (sum += node.count));
|
|
110
|
-
return sum;
|
|
47
|
+
override set right(v: OptNodeOrNull<TreeMultiMapNode<K, V>>) {
|
|
48
|
+
if (v) {
|
|
49
|
+
v.parent = this;
|
|
50
|
+
}
|
|
51
|
+
this._right = v;
|
|
111
52
|
}
|
|
53
|
+
}
|
|
112
54
|
|
|
55
|
+
/**
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // Find elements in a range
|
|
59
|
+
* const tmm = new TreeMultiMap<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
60
|
+
* console.log(tmm.search(new Range(5, 10))); // [5, 10, 7]
|
|
61
|
+
* console.log(tmm.search(new Range(4, 12))); // [5, 10, 12, 7]
|
|
62
|
+
* console.log(tmm.search(new Range(15, 20))); // [15, 18]
|
|
63
|
+
*/
|
|
64
|
+
export class TreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
65
|
+
extends RedBlackTree<K, V[], R, MK, MV[], MR>
|
|
66
|
+
implements IBinaryTree<K, V[], R, MK, MV, MR>
|
|
67
|
+
{
|
|
113
68
|
/**
|
|
114
|
-
* The
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
69
|
+
* The constructor initializes an TreeMultiMap with the provided keys, nodes, entries, or raw data
|
|
70
|
+
* and options.
|
|
71
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
72
|
+
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
|
|
73
|
+
* TreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
|
|
74
|
+
* the RedBlackTreeMulti
|
|
75
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
76
|
+
* `TreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
|
|
77
|
+
* additional options for configuring the TreeMultiMap instance.
|
|
78
|
+
*/
|
|
79
|
+
constructor(
|
|
80
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], TreeMultiMapNode<K, V>> | R> = [],
|
|
81
|
+
options?: TreeMultiMapOptions<K, V[], R>
|
|
82
|
+
) {
|
|
83
|
+
super([], { ...options, isMapMode: true });
|
|
84
|
+
if (keysNodesEntriesOrRaws) {
|
|
85
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
86
|
+
}
|
|
128
87
|
}
|
|
129
88
|
|
|
130
89
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
90
|
+
* Time Complexity: O(1)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
93
|
+
* The `createTree` function in TypeScript overrides the default implementation to create a new
|
|
94
|
+
* TreeMultiMap with specified options.
|
|
95
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type
|
|
96
|
+
* `TreeMultiMapOptions<K, V[], R>`. This parameter allows you to pass additional configuration
|
|
97
|
+
* options when creating a new `TreeMultiMap` instance. It includes properties such as
|
|
98
|
+
* `iterationType`, `specifyComparable
|
|
99
|
+
* @returns A new instance of `TreeMultiMap` is being returned, with an empty array as the initial
|
|
100
|
+
* data and the provided options merged with the existing properties of the current object.
|
|
101
|
+
*/
|
|
102
|
+
override createTree(options?: TreeMultiMapOptions<K, V[], R>) {
|
|
103
|
+
return new TreeMultiMap<K, V, R, MK, MV, MR>([], {
|
|
140
104
|
iterationType: this.iterationType,
|
|
141
|
-
|
|
142
|
-
comparator: this._comparator,
|
|
105
|
+
specifyComparable: this._specifyComparable,
|
|
143
106
|
toEntryFn: this._toEntryFn,
|
|
107
|
+
isReverse: this._isReverse,
|
|
144
108
|
...options
|
|
145
|
-
})
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` takes in a key, value, and count and returns a
|
|
150
|
-
* node based on the input.
|
|
151
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
152
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
153
|
-
* @param {V} [value] - The `value` parameter is an optional value that represents the value
|
|
154
|
-
* associated with the key in the node. It is used when creating a new node or updating the value of
|
|
155
|
-
* an existing node.
|
|
156
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
157
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
158
|
-
* @returns either a NODE object or undefined.
|
|
159
|
-
*/
|
|
160
|
-
override keyValueNodeEntryRawToNodeAndValue(
|
|
161
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
162
|
-
value?: V,
|
|
163
|
-
count = 1
|
|
164
|
-
): [NODE | undefined, V | undefined] {
|
|
165
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
166
|
-
|
|
167
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
168
|
-
|
|
169
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
170
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
171
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
172
|
-
const finalValue = value ?? entryValue;
|
|
173
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (this._toEntryFn) {
|
|
177
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
178
|
-
const finalValue = value ?? entryValue;
|
|
179
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, 'BLACK', count), finalValue];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, 'BLACK', count), value];
|
|
183
|
-
|
|
184
|
-
return [undefined, undefined];
|
|
109
|
+
});
|
|
185
110
|
}
|
|
186
111
|
|
|
187
112
|
/**
|
|
188
|
-
*
|
|
189
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
190
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
191
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
192
|
-
* an instance of the `TreeMultiMapNode` class.
|
|
193
|
-
*/
|
|
194
|
-
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
195
|
-
return keyNodeEntryOrRaw instanceof TreeMultiMapNode;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Time Complexity: O(log n)
|
|
113
|
+
* Time Complexity: O(1)
|
|
200
114
|
* Space Complexity: O(1)
|
|
201
115
|
*
|
|
202
|
-
* The function overrides the
|
|
203
|
-
*
|
|
204
|
-
* @param {
|
|
205
|
-
*
|
|
206
|
-
* @
|
|
207
|
-
*
|
|
208
|
-
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
209
|
-
* be added to the data structure. By default, it is set to 1, meaning that if no value is provided
|
|
210
|
-
* for `count`, the key-value pair will be added once.
|
|
211
|
-
* @returns The method is returning a boolean value. It returns true if the addition of the new node
|
|
212
|
-
* was successful, and false otherwise.
|
|
116
|
+
* The function `createNode` overrides the method to create a new `TreeMultiMapNode` with a specified
|
|
117
|
+
* key and an empty array of values.
|
|
118
|
+
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
|
|
119
|
+
* that will be created in the TreeMultiMap data structure.
|
|
120
|
+
* @returns A new instance of `TreeMultiMapNode<K, V>` is being returned, with the specified key and
|
|
121
|
+
* an empty array as its value.
|
|
213
122
|
*/
|
|
214
|
-
override
|
|
215
|
-
|
|
216
|
-
const orgCount = newNode?.count || 0;
|
|
217
|
-
const isSuccessAdded = super.add(newNode, newValue);
|
|
218
|
-
|
|
219
|
-
if (isSuccessAdded) {
|
|
220
|
-
this._count += orgCount;
|
|
221
|
-
return true;
|
|
222
|
-
} else {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
123
|
+
override createNode(key: K): TreeMultiMapNode<K, V> {
|
|
124
|
+
return new TreeMultiMapNode<K, V>(key, []);
|
|
225
125
|
}
|
|
226
126
|
|
|
127
|
+
override add(node: BTNRep<K, V[], TreeMultiMapNode<K, V>>): boolean;
|
|
128
|
+
|
|
129
|
+
override add(key: K, value: V): boolean;
|
|
130
|
+
|
|
227
131
|
/**
|
|
228
132
|
* Time Complexity: O(log n)
|
|
229
|
-
* Space Complexity: O(
|
|
133
|
+
* Space Complexity: O(log n)
|
|
230
134
|
*
|
|
231
|
-
* The function `
|
|
232
|
-
*
|
|
233
|
-
* @param {BTNRep<K, V,
|
|
234
|
-
* parameter in the `
|
|
235
|
-
*
|
|
236
|
-
* @param [
|
|
237
|
-
*
|
|
238
|
-
*
|
|
239
|
-
* `
|
|
240
|
-
*
|
|
241
|
-
*/
|
|
242
|
-
override
|
|
243
|
-
if (
|
|
244
|
-
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return results;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
let originalColor = nodeToDelete.color;
|
|
256
|
-
let replacementNode: NODE | undefined;
|
|
257
|
-
|
|
258
|
-
if (!this.isRealNode(nodeToDelete.left)) {
|
|
259
|
-
replacementNode = nodeToDelete.right;
|
|
260
|
-
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
261
|
-
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
262
|
-
this._count -= nodeToDelete.count;
|
|
263
|
-
} else {
|
|
264
|
-
nodeToDelete.count--;
|
|
265
|
-
this._count--;
|
|
266
|
-
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
267
|
-
return results;
|
|
268
|
-
}
|
|
269
|
-
} else if (!this.isRealNode(nodeToDelete.right)) {
|
|
270
|
-
replacementNode = nodeToDelete.left;
|
|
271
|
-
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
272
|
-
this._transplant(nodeToDelete, nodeToDelete.left);
|
|
273
|
-
this._count -= nodeToDelete.count;
|
|
274
|
-
} else {
|
|
275
|
-
nodeToDelete.count--;
|
|
276
|
-
this._count--;
|
|
277
|
-
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
278
|
-
return results;
|
|
135
|
+
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to a
|
|
136
|
+
* TreeMultiMapNode, handling different input types and scenarios.
|
|
137
|
+
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
138
|
+
* parameter in the `override add` method can be either a `BTNRep` object containing a key, an array
|
|
139
|
+
* of values, and a `TreeMultiMapNode`, or just a key.
|
|
140
|
+
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that
|
|
141
|
+
* you want to add to the TreeMultiMap. If the key is already present in the map, the new value will
|
|
142
|
+
* be added to the existing list of values associated with that key. If the key is not present,
|
|
143
|
+
* @returns The `add` method is returning a boolean value, which indicates whether the operation was
|
|
144
|
+
* successful or not.
|
|
145
|
+
*/
|
|
146
|
+
override add(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value?: V): boolean {
|
|
147
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
148
|
+
|
|
149
|
+
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
150
|
+
if (key === undefined || key === null) return false;
|
|
151
|
+
|
|
152
|
+
const existingValues = this.get(key);
|
|
153
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
154
|
+
for (const value of values) existingValues.push(value);
|
|
155
|
+
return true;
|
|
279
156
|
}
|
|
280
|
-
} else {
|
|
281
|
-
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
282
|
-
if (successor) {
|
|
283
|
-
originalColor = successor.color;
|
|
284
|
-
replacementNode = successor.right;
|
|
285
157
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if (ignoreCount || nodeToDelete.count <= 1) {
|
|
292
|
-
this._transplant(successor, successor.right);
|
|
293
|
-
this._count -= nodeToDelete.count;
|
|
294
|
-
} else {
|
|
295
|
-
nodeToDelete.count--;
|
|
296
|
-
this._count--;
|
|
297
|
-
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
298
|
-
return results;
|
|
299
|
-
}
|
|
300
|
-
successor.right = nodeToDelete.right;
|
|
301
|
-
if (this.isRealNode(successor.right)) {
|
|
302
|
-
successor.right.parent = successor;
|
|
303
|
-
}
|
|
158
|
+
const existingNode = this.getNode(key);
|
|
159
|
+
if (this.isRealNode(existingNode)) {
|
|
160
|
+
if (existingValues === undefined) {
|
|
161
|
+
super.add(key, values);
|
|
162
|
+
return true;
|
|
304
163
|
}
|
|
305
|
-
if (
|
|
306
|
-
|
|
307
|
-
|
|
164
|
+
if (values !== undefined) {
|
|
165
|
+
for (const value of values) existingValues.push(value);
|
|
166
|
+
return true;
|
|
308
167
|
} else {
|
|
309
|
-
|
|
310
|
-
this._count--;
|
|
311
|
-
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
312
|
-
return results;
|
|
313
|
-
}
|
|
314
|
-
successor.left = nodeToDelete.left;
|
|
315
|
-
if (this.isRealNode(successor.left)) {
|
|
316
|
-
successor.left.parent = successor;
|
|
168
|
+
return false;
|
|
317
169
|
}
|
|
318
|
-
|
|
170
|
+
} else {
|
|
171
|
+
return super.add(key, values);
|
|
319
172
|
}
|
|
320
|
-
}
|
|
321
|
-
this._size--;
|
|
173
|
+
};
|
|
322
174
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
175
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
176
|
+
const [key, values] = keyNodeOrEntry;
|
|
177
|
+
return _commonAdd(key, value !== undefined ? [value] : values);
|
|
326
178
|
}
|
|
327
179
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
return results;
|
|
180
|
+
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
|
|
331
181
|
}
|
|
332
182
|
|
|
333
183
|
/**
|
|
334
|
-
* Time Complexity: O(
|
|
335
|
-
* Space Complexity: O(1)
|
|
336
|
-
*
|
|
337
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
338
|
-
* zero.
|
|
339
|
-
*/
|
|
340
|
-
override clear() {
|
|
341
|
-
super.clear();
|
|
342
|
-
this._count = 0;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(n log n)
|
|
184
|
+
* Time Complexity: O(log n)
|
|
347
185
|
* Space Complexity: O(log n)
|
|
348
186
|
*
|
|
349
|
-
* The `
|
|
350
|
-
*
|
|
351
|
-
* @param {
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
* `
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
187
|
+
* The function `deleteValue` removes a specific value from a key in a TreeMultiMap data structure
|
|
188
|
+
* and deletes the entire node if no values are left for that key.
|
|
189
|
+
* @param {BTNRep<K, V[], TreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
190
|
+
* parameter in the `deleteValue` function can be either a `BTNRep` object containing a key and an
|
|
191
|
+
* array of values, or just a key itself.
|
|
192
|
+
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
193
|
+
* value that you want to remove from the multi-map data structure associated with a particular key.
|
|
194
|
+
* The function checks if the value exists in the array of values associated with the key, and if
|
|
195
|
+
* found, removes it from the array.
|
|
196
|
+
* @returns The `deleteValue` function returns a boolean value - `true` if the specified `value` was
|
|
197
|
+
* successfully deleted from the values associated with the `keyNodeOrEntry`, and `false` otherwise.
|
|
198
|
+
*/
|
|
199
|
+
deleteValue(keyNodeOrEntry: BTNRep<K, V[], TreeMultiMapNode<K, V>> | K, value: V): boolean {
|
|
200
|
+
const values = this.get(keyNodeOrEntry);
|
|
201
|
+
if (Array.isArray(values)) {
|
|
202
|
+
const index = values.indexOf(value);
|
|
203
|
+
if (index === -1) return false;
|
|
204
|
+
values.splice(index, 1);
|
|
205
|
+
|
|
206
|
+
// If no values left, remove the entire node
|
|
207
|
+
if (values.length === 0) this.delete(keyNodeOrEntry);
|
|
364
208
|
|
|
365
|
-
if (iterationType === 'RECURSIVE') {
|
|
366
|
-
const buildBalanceBST = (l: number, r: number) => {
|
|
367
|
-
if (l > r) return;
|
|
368
|
-
const m = l + Math.floor((r - l) / 2);
|
|
369
|
-
const midNode = sorted[m];
|
|
370
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
371
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
372
|
-
buildBalanceBST(l, m - 1);
|
|
373
|
-
buildBalanceBST(m + 1, r);
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
buildBalanceBST(0, n - 1);
|
|
377
|
-
return true;
|
|
378
|
-
} else {
|
|
379
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
380
|
-
while (stack.length > 0) {
|
|
381
|
-
const popped = stack.pop();
|
|
382
|
-
if (popped) {
|
|
383
|
-
const [l, r] = popped;
|
|
384
|
-
if (l <= r) {
|
|
385
|
-
const m = l + Math.floor((r - l) / 2);
|
|
386
|
-
const midNode = sorted[m];
|
|
387
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
388
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
389
|
-
stack.push([m + 1, r]);
|
|
390
|
-
stack.push([l, m - 1]);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
209
|
return true;
|
|
395
210
|
}
|
|
211
|
+
return false;
|
|
396
212
|
}
|
|
397
213
|
|
|
398
214
|
/**
|
|
399
|
-
* Time
|
|
400
|
-
* Space
|
|
215
|
+
* Time Complexity: O(n)
|
|
216
|
+
* Space Complexity: O(n)
|
|
401
217
|
*
|
|
402
|
-
* The function overrides the
|
|
403
|
-
*
|
|
218
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
219
|
+
* structure.
|
|
220
|
+
* @returns The `cloned` object is being returned.
|
|
404
221
|
*/
|
|
405
|
-
override clone()
|
|
222
|
+
override clone() {
|
|
406
223
|
const cloned = this.createTree();
|
|
407
|
-
this.
|
|
408
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
224
|
+
this._clone(cloned);
|
|
409
225
|
return cloned;
|
|
410
226
|
}
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Time Complexity: O(1)
|
|
414
|
-
* Space Complexity: O(1)
|
|
415
|
-
*
|
|
416
|
-
* The `_swapProperties` function swaps the properties (key, value, count, color) between two nodes
|
|
417
|
-
* in a binary search tree.
|
|
418
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
419
|
-
* that will be swapped with the `destNode`. It can be either an instance of the `R` class or an
|
|
420
|
-
* instance of the `BSTNOptKeyOrNode<K, NODE>` class.
|
|
421
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
422
|
-
* node where the properties will be swapped with the source node.
|
|
423
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
424
|
-
* If either `srcNode` or `destNode` is undefined, it returns undefined.
|
|
425
|
-
*/
|
|
426
|
-
protected override _swapProperties(
|
|
427
|
-
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
428
|
-
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
429
|
-
): NODE | undefined {
|
|
430
|
-
srcNode = this.ensureNode(srcNode);
|
|
431
|
-
destNode = this.ensureNode(destNode);
|
|
432
|
-
if (srcNode && destNode) {
|
|
433
|
-
const { key, value, count, color } = destNode;
|
|
434
|
-
const tempNode = this.createNode(key, value, color, count);
|
|
435
|
-
if (tempNode) {
|
|
436
|
-
tempNode.color = color;
|
|
437
|
-
|
|
438
|
-
destNode.key = srcNode.key;
|
|
439
|
-
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
440
|
-
destNode.count = srcNode.count;
|
|
441
|
-
destNode.color = srcNode.color;
|
|
442
|
-
|
|
443
|
-
srcNode.key = tempNode.key;
|
|
444
|
-
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
445
|
-
srcNode.count = tempNode.count;
|
|
446
|
-
srcNode.color = tempNode.color;
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
return destNode;
|
|
450
|
-
}
|
|
451
|
-
return undefined;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* Time Complexity: O(1)
|
|
456
|
-
* Space Complexity: O(1)
|
|
457
|
-
*
|
|
458
|
-
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
459
|
-
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace in the data
|
|
460
|
-
* structure.
|
|
461
|
-
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
462
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
463
|
-
* superclass, which is of type `NODE`.
|
|
464
|
-
*/
|
|
465
|
-
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
466
|
-
newNode.count = oldNode.count + newNode.count;
|
|
467
|
-
return super._replaceNode(oldNode, newNode);
|
|
468
|
-
}
|
|
469
227
|
}
|
|
@@ -947,7 +947,7 @@ export abstract class AbstractGraph<
|
|
|
947
947
|
const filtered: [VertexKey, V | undefined][] = [];
|
|
948
948
|
let index = 0;
|
|
949
949
|
for (const [key, value] of this) {
|
|
950
|
-
if (predicate.call(thisArg,
|
|
950
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
951
951
|
filtered.push([key, value]);
|
|
952
952
|
}
|
|
953
953
|
index++;
|
|
@@ -972,7 +972,7 @@ export abstract class AbstractGraph<
|
|
|
972
972
|
const mapped: T[] = [];
|
|
973
973
|
let index = 0;
|
|
974
974
|
for (const [key, value] of this) {
|
|
975
|
-
mapped.push(callback.call(thisArg,
|
|
975
|
+
mapped.push(callback.call(thisArg, key, value, index, this));
|
|
976
976
|
index++;
|
|
977
977
|
}
|
|
978
978
|
return mapped;
|