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,439 +5,218 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
9
|
-
AVLTreeMultiMapNested,
|
|
10
|
-
AVLTreeMultiMapNodeNested,
|
|
11
|
-
AVLTreeMultiMapOptions,
|
|
12
|
-
BinaryTreeDeleteResult,
|
|
13
|
-
BSTNOptKeyOrNode,
|
|
14
|
-
BTNRep,
|
|
15
|
-
IterationType
|
|
16
|
-
} from '../../types';
|
|
17
|
-
import { IBinaryTree } from '../../interfaces';
|
|
8
|
+
import { AVLTreeMultiMapOptions, BTNOptKeyOrNull, BTNRep, OptNodeOrNull } from '../../types';
|
|
18
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
19
11
|
|
|
20
|
-
export class AVLTreeMultiMapNode<
|
|
21
|
-
K = any,
|
|
22
|
-
V = any,
|
|
23
|
-
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
|
|
24
|
-
> extends AVLTreeNode<K, V, NODE> {
|
|
12
|
+
export class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
25
13
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @param {
|
|
32
|
-
*
|
|
33
|
-
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
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`.
|
|
34
21
|
*/
|
|
35
|
-
constructor(key: K, value
|
|
22
|
+
constructor(key: K, value: V[]) {
|
|
36
23
|
super(key, value);
|
|
37
|
-
this.count = count;
|
|
38
24
|
}
|
|
39
25
|
|
|
40
|
-
|
|
26
|
+
override parent?: AVLTreeMultiMapNode<K, V> = undefined;
|
|
41
27
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
get count(): number {
|
|
47
|
-
return this._count;
|
|
28
|
+
override _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
|
|
29
|
+
|
|
30
|
+
override get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
|
|
31
|
+
return this._left;
|
|
48
32
|
}
|
|
49
33
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
34
|
+
override set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
|
|
35
|
+
if (v) {
|
|
36
|
+
v.parent = this;
|
|
37
|
+
}
|
|
38
|
+
this._left = v;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
override _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>> = undefined;
|
|
42
|
+
|
|
43
|
+
override get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>> {
|
|
44
|
+
return this._right;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>) {
|
|
48
|
+
if (v) {
|
|
49
|
+
v.parent = this;
|
|
50
|
+
}
|
|
51
|
+
this._right = v;
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
|
|
60
55
|
/**
|
|
61
|
-
*
|
|
56
|
+
*
|
|
62
57
|
*/
|
|
63
|
-
export class AVLTreeMultiMap<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
R = object,
|
|
67
|
-
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
68
|
-
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
69
|
-
K,
|
|
70
|
-
V,
|
|
71
|
-
R,
|
|
72
|
-
NODE,
|
|
73
|
-
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
74
|
-
>
|
|
75
|
-
>
|
|
76
|
-
extends AVLTree<K, V, R, NODE, TREE>
|
|
77
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
58
|
+
export class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
59
|
+
extends AVLTree<K, V[], R, MK, MV[], MR>
|
|
60
|
+
implements IBinaryTree<K, V[], R, MK, MV, MR>
|
|
78
61
|
{
|
|
79
62
|
/**
|
|
80
|
-
* The constructor initializes
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
63
|
+
* The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
|
|
64
|
+
* and options.
|
|
65
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
66
|
+
* iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
|
|
67
|
+
* AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
|
|
68
|
+
* the AVLTreeMulti
|
|
69
|
+
* @param [options] - The `options` parameter in the constructor is of type
|
|
70
|
+
* `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
|
|
71
|
+
* additional options for configuring the AVLTreeMultiMap instance.
|
|
86
72
|
*/
|
|
87
73
|
constructor(
|
|
88
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
89
|
-
options?: AVLTreeMultiMapOptions<K, V, R>
|
|
74
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R> = [],
|
|
75
|
+
options?: AVLTreeMultiMapOptions<K, V[], R>
|
|
90
76
|
) {
|
|
91
|
-
super([], options);
|
|
92
|
-
if (keysNodesEntriesOrRaws)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
protected _count = 0;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
99
|
-
* search.
|
|
100
|
-
* @returns the sum of the count property of all nodes in the tree.
|
|
101
|
-
*/
|
|
102
|
-
get count(): number {
|
|
103
|
-
return this._count;
|
|
77
|
+
super([], { ...options, isMapMode: true });
|
|
78
|
+
if (keysNodesEntriesOrRaws) {
|
|
79
|
+
this.addMany(keysNodesEntriesOrRaws);
|
|
80
|
+
}
|
|
104
81
|
}
|
|
105
82
|
|
|
106
83
|
/**
|
|
107
|
-
* Time Complexity: O(
|
|
84
|
+
* Time Complexity: O(1)
|
|
108
85
|
* Space Complexity: O(1)
|
|
109
86
|
*
|
|
110
|
-
* The function
|
|
111
|
-
*
|
|
112
|
-
* @
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
|
|
122
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
123
|
-
* which is a generic type that can be replaced with any specific type when using the function.
|
|
124
|
-
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
125
|
-
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
126
|
-
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
127
|
-
* key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
|
|
128
|
-
* calling the `createNode` method. If provided, it specifies the initial count for the node.
|
|
129
|
-
* @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
|
|
130
|
-
*/
|
|
131
|
-
override createNode(key: K, value?: V, count?: number): NODE {
|
|
132
|
-
return new AVLTreeMultiMapNode(key, this._isMapMode ? undefined : value, count) as NODE;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
137
|
-
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
138
|
-
* configuration options for creating the AVLTreeMultiMap. It can have the following properties:
|
|
139
|
-
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
140
|
-
* object.
|
|
141
|
-
*/
|
|
142
|
-
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
143
|
-
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
87
|
+
* The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
|
|
88
|
+
* specified options.
|
|
89
|
+
* @param [options] - The `options` parameter in the `createTree` function is of type
|
|
90
|
+
* `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
|
|
91
|
+
* `K`, `V[]`, and `R`. The function creates a new `AVL
|
|
92
|
+
* @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
|
|
93
|
+
* provided options.
|
|
94
|
+
*/
|
|
95
|
+
override createTree(options?: AVLTreeMultiMapOptions<K, V[], R>) {
|
|
96
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR>([], {
|
|
144
97
|
iterationType: this.iterationType,
|
|
145
|
-
|
|
146
|
-
comparator: this._comparator,
|
|
98
|
+
specifyComparable: this._specifyComparable,
|
|
147
99
|
toEntryFn: this._toEntryFn,
|
|
100
|
+
isReverse: this._isReverse,
|
|
148
101
|
...options
|
|
149
|
-
})
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
154
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
155
|
-
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
156
|
-
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
157
|
-
* an instance of the `AVLTreeMultiMapNode` class.
|
|
158
|
-
*/
|
|
159
|
-
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
160
|
-
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
165
|
-
* a node object.
|
|
166
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
167
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
168
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
169
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
170
|
-
* value is provided, it will default to `undefined`.
|
|
171
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
172
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
173
|
-
* @returns either a NODE object or undefined.
|
|
174
|
-
*/
|
|
175
|
-
override keyValueNodeEntryRawToNodeAndValue(
|
|
176
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
177
|
-
value?: V,
|
|
178
|
-
count = 1
|
|
179
|
-
): [NODE | undefined, V | undefined] {
|
|
180
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
181
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
182
|
-
|
|
183
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
184
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
185
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
186
|
-
const finalValue = value ?? entryValue;
|
|
187
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
191
|
-
|
|
192
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
193
|
-
if (this._toEntryFn) {
|
|
194
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
195
|
-
const finalValue = value ?? entryValue;
|
|
196
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
197
|
-
}
|
|
198
|
-
return [undefined, undefined];
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return [undefined, undefined];
|
|
102
|
+
});
|
|
202
103
|
}
|
|
203
104
|
|
|
204
105
|
/**
|
|
205
|
-
* Time Complexity: O(
|
|
106
|
+
* Time Complexity: O(1)
|
|
206
107
|
* Space Complexity: O(1)
|
|
207
108
|
*
|
|
208
|
-
* The function overrides the
|
|
209
|
-
* and
|
|
210
|
-
* @param {
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
215
|
-
* data structure. It is an optional parameter, so it can be omitted if not needed.
|
|
216
|
-
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
217
|
-
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
218
|
-
* be added once. However, you can specify a different value for `count` if you want to add
|
|
219
|
-
* @returns a boolean value.
|
|
109
|
+
* The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
|
|
110
|
+
* specified key and an empty array of values.
|
|
111
|
+
* @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
|
|
112
|
+
* that will be created in the AVLTreeMultiMap.
|
|
113
|
+
* @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
|
|
114
|
+
* empty array.
|
|
220
115
|
*/
|
|
221
|
-
override
|
|
222
|
-
|
|
223
|
-
if (newNode === undefined) return false;
|
|
224
|
-
|
|
225
|
-
const orgNodeCount = newNode?.count || 0;
|
|
226
|
-
const inserted = super.add(newNode, newValue);
|
|
227
|
-
if (inserted) {
|
|
228
|
-
this._count += orgNodeCount;
|
|
229
|
-
}
|
|
230
|
-
return true;
|
|
116
|
+
override createNode(key: K): AVLTreeMultiMapNode<K, V> {
|
|
117
|
+
return new AVLTreeMultiMapNode<K, V>(key, []);
|
|
231
118
|
}
|
|
232
119
|
|
|
120
|
+
override add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean;
|
|
121
|
+
|
|
122
|
+
override add(key: K, value: V): boolean;
|
|
123
|
+
|
|
233
124
|
/**
|
|
234
125
|
* Time Complexity: O(log n)
|
|
235
|
-
* Space Complexity: O(
|
|
126
|
+
* Space Complexity: O(log n)
|
|
236
127
|
*
|
|
237
|
-
* The function overrides the
|
|
238
|
-
*
|
|
239
|
-
* @param {BTNRep<K, V,
|
|
240
|
-
* parameter in the `
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* @param [
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* `
|
|
247
|
-
*
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
let needBalanced: NODE | undefined = undefined,
|
|
261
|
-
orgCurrent: NODE | undefined = curr;
|
|
128
|
+
* The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
|
|
129
|
+
* tree multi-map.
|
|
130
|
+
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
131
|
+
* parameter in the `override add` method can be either a key-value pair entry or just a key. If it
|
|
132
|
+
* is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
|
|
133
|
+
* `values`
|
|
134
|
+
* @param {V} [value] - The `value` parameter in the `override add` method represents the value that
|
|
135
|
+
* you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
|
|
136
|
+
* with a specific key.
|
|
137
|
+
* @returns The `override add` method is returning a boolean value, which indicates whether the
|
|
138
|
+
* addition operation was successful or not.
|
|
139
|
+
*/
|
|
140
|
+
override add(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value?: V): boolean {
|
|
141
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.add(keyNodeOrEntry);
|
|
142
|
+
|
|
143
|
+
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
144
|
+
if (key === undefined || key === null) return false;
|
|
145
|
+
|
|
146
|
+
const existingValues = this.get(key);
|
|
147
|
+
if (existingValues !== undefined && values !== undefined) {
|
|
148
|
+
for (const value of values) existingValues.push(value);
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
262
151
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
152
|
+
const existingNode = this.getNode(key);
|
|
153
|
+
if (this.isRealNode(existingNode)) {
|
|
154
|
+
if (existingValues === undefined) {
|
|
155
|
+
super.add(key, values);
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
if (values !== undefined) {
|
|
159
|
+
for (const value of values) existingValues.push(value);
|
|
160
|
+
return true;
|
|
270
161
|
} else {
|
|
271
|
-
|
|
272
|
-
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
273
|
-
parent.left = curr.right;
|
|
274
|
-
} else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
|
|
275
|
-
parent.right = curr.right;
|
|
276
|
-
}
|
|
277
|
-
needBalanced = parent;
|
|
162
|
+
return false;
|
|
278
163
|
}
|
|
279
164
|
} else {
|
|
280
|
-
|
|
281
|
-
if (leftSubTreeRightMost) {
|
|
282
|
-
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
283
|
-
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
284
|
-
if (parentOfLeftSubTreeMax) {
|
|
285
|
-
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
286
|
-
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
287
|
-
} else {
|
|
288
|
-
parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
|
|
289
|
-
}
|
|
290
|
-
needBalanced = parentOfLeftSubTreeMax;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
165
|
+
return super.add(key, values);
|
|
293
166
|
}
|
|
294
|
-
|
|
295
|
-
// TODO How to handle when the count of target node is lesser than current node's count
|
|
296
|
-
if (orgCurrent) this._count -= orgCurrent.count;
|
|
297
|
-
}
|
|
167
|
+
};
|
|
298
168
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
this._balancePath(needBalanced);
|
|
169
|
+
if (this.isEntry(keyNodeOrEntry)) {
|
|
170
|
+
const [key, values] = keyNodeOrEntry;
|
|
171
|
+
return _commonAdd(key, value !== undefined ? [value] : values);
|
|
303
172
|
}
|
|
304
173
|
|
|
305
|
-
return
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Time Complexity: O(1)
|
|
310
|
-
* Space Complexity: O(1)
|
|
311
|
-
*
|
|
312
|
-
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
313
|
-
* zero.
|
|
314
|
-
*/
|
|
315
|
-
override clear() {
|
|
316
|
-
super.clear();
|
|
317
|
-
this._count = 0;
|
|
174
|
+
return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
|
|
318
175
|
}
|
|
319
176
|
|
|
320
177
|
/**
|
|
321
|
-
* Time Complexity: O(
|
|
178
|
+
* Time Complexity: O(log n)
|
|
322
179
|
* Space Complexity: O(log n)
|
|
323
|
-
*
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
* the
|
|
329
|
-
* @
|
|
330
|
-
*
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
buildBalanceBST(l, m - 1);
|
|
347
|
-
buildBalanceBST(m + 1, r);
|
|
348
|
-
};
|
|
180
|
+
*
|
|
181
|
+
* The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
|
|
182
|
+
* structure and deletes the entire node if no values are left for that key.
|
|
183
|
+
* @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
184
|
+
* parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
|
|
185
|
+
* pair in the AVLTreeMultiMapNode, or just the key itself.
|
|
186
|
+
* @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
|
|
187
|
+
* value that you want to delete from the multi-map data structure associated with a particular key.
|
|
188
|
+
* The function checks if the value exists in the array of values associated with the key, and if
|
|
189
|
+
* found, removes it from the array.
|
|
190
|
+
* @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
|
|
191
|
+
* `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
|
|
192
|
+
* the value was not found in the array, it returns `false`.
|
|
193
|
+
*/
|
|
194
|
+
deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean {
|
|
195
|
+
const values = this.get(keyNodeOrEntry);
|
|
196
|
+
if (Array.isArray(values)) {
|
|
197
|
+
const index = values.indexOf(value);
|
|
198
|
+
if (index === -1) return false;
|
|
199
|
+
values.splice(index, 1);
|
|
200
|
+
|
|
201
|
+
// If no values left, remove the entire node
|
|
202
|
+
if (values.length === 0) this.delete(keyNodeOrEntry);
|
|
349
203
|
|
|
350
|
-
buildBalanceBST(0, n - 1);
|
|
351
|
-
return true;
|
|
352
|
-
} else {
|
|
353
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
354
|
-
while (stack.length > 0) {
|
|
355
|
-
const popped = stack.pop();
|
|
356
|
-
if (popped) {
|
|
357
|
-
const [l, r] = popped;
|
|
358
|
-
if (l <= r) {
|
|
359
|
-
const m = l + Math.floor((r - l) / 2);
|
|
360
|
-
const midNode = sorted[m];
|
|
361
|
-
if (this._isMapMode) this.add(midNode.key, undefined, midNode.count);
|
|
362
|
-
else this.add(midNode.key, midNode.value, midNode.count);
|
|
363
|
-
stack.push([m + 1, r]);
|
|
364
|
-
stack.push([l, m - 1]);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
204
|
return true;
|
|
369
205
|
}
|
|
206
|
+
return false;
|
|
370
207
|
}
|
|
371
208
|
|
|
372
209
|
/**
|
|
373
|
-
* Time
|
|
374
|
-
* Space
|
|
210
|
+
* Time Complexity: O(n)
|
|
211
|
+
* Space Complexity: O(n)
|
|
375
212
|
*
|
|
376
|
-
* The function overrides the
|
|
377
|
-
*
|
|
213
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
214
|
+
* structure.
|
|
215
|
+
* @returns A cloned tree object is being returned.
|
|
378
216
|
*/
|
|
379
|
-
override clone()
|
|
217
|
+
override clone() {
|
|
380
218
|
const cloned = this.createTree();
|
|
381
|
-
|
|
382
|
-
else this.bfs(node => cloned.add(node.key, node.value, node.count));
|
|
383
|
-
if (this._isMapMode) cloned._store = this._store;
|
|
219
|
+
this._clone(cloned);
|
|
384
220
|
return cloned;
|
|
385
221
|
}
|
|
386
|
-
|
|
387
|
-
/**
|
|
388
|
-
* Time Complexity: O(1)
|
|
389
|
-
* Space Complexity: O(1)
|
|
390
|
-
*
|
|
391
|
-
* The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
|
|
392
|
-
* in a binary search tree.
|
|
393
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
|
|
394
|
-
* that will be swapped with the `destNode`.
|
|
395
|
-
* @param {R | BSTNOptKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
|
|
396
|
-
* node where the properties will be swapped with the source node.
|
|
397
|
-
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
398
|
-
* If either `srcNode` or `destNode` is undefined, it returns `undefined`.
|
|
399
|
-
*/
|
|
400
|
-
protected override _swapProperties(
|
|
401
|
-
srcNode: R | BSTNOptKeyOrNode<K, NODE>,
|
|
402
|
-
destNode: R | BSTNOptKeyOrNode<K, NODE>
|
|
403
|
-
): NODE | undefined {
|
|
404
|
-
srcNode = this.ensureNode(srcNode);
|
|
405
|
-
destNode = this.ensureNode(destNode);
|
|
406
|
-
if (srcNode && destNode) {
|
|
407
|
-
const { key, value, count, height } = destNode;
|
|
408
|
-
const tempNode = this.createNode(key, value, count);
|
|
409
|
-
if (tempNode) {
|
|
410
|
-
tempNode.height = height;
|
|
411
|
-
|
|
412
|
-
destNode.key = srcNode.key;
|
|
413
|
-
if (!this._isMapMode) destNode.value = srcNode.value;
|
|
414
|
-
destNode.count = srcNode.count;
|
|
415
|
-
destNode.height = srcNode.height;
|
|
416
|
-
|
|
417
|
-
srcNode.key = tempNode.key;
|
|
418
|
-
if (!this._isMapMode) srcNode.value = tempNode.value;
|
|
419
|
-
srcNode.count = tempNode.count;
|
|
420
|
-
srcNode.height = tempNode.height;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
return destNode;
|
|
424
|
-
}
|
|
425
|
-
return undefined;
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Time Complexity: O(1)
|
|
430
|
-
* Space Complexity: O(1)
|
|
431
|
-
*
|
|
432
|
-
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
433
|
-
* @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
|
|
434
|
-
* data structure. It is of type NODE.
|
|
435
|
-
* @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
|
|
436
|
-
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
437
|
-
* superclass, which is of type `NODE`.
|
|
438
|
-
*/
|
|
439
|
-
protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
440
|
-
newNode.count = oldNode.count + newNode.count;
|
|
441
|
-
return super._replaceNode(oldNode, newNode);
|
|
442
|
-
}
|
|
443
222
|
}
|