queue-typed 1.47.6 → 1.47.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
- package/dist/data-structures/binary-tree/avl-tree.js +45 -36
- package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
- package/dist/data-structures/binary-tree/binary-tree.js +133 -119
- package/dist/data-structures/binary-tree/bst.d.ts +53 -44
- package/dist/data-structures/binary-tree/bst.js +137 -154
- package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
- package/dist/data-structures/binary-tree/rb-tree.js +70 -33
- package/dist/data-structures/binary-tree/segment-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/segment-tree.js +7 -7
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +30 -30
- package/dist/data-structures/graph/directed-graph.d.ts +24 -24
- package/dist/data-structures/graph/directed-graph.js +28 -28
- package/dist/data-structures/graph/undirected-graph.d.ts +14 -14
- package/dist/data-structures/graph/undirected-graph.js +18 -18
- package/dist/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/data-structures/hash/hash-map.js +5 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +28 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +33 -33
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +21 -21
- package/dist/data-structures/linked-list/singly-linked-list.js +27 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -4
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +13 -13
- package/dist/data-structures/stack/stack.d.ts +6 -6
- package/dist/data-structures/stack/stack.js +7 -7
- package/dist/data-structures/trie/trie.d.ts +3 -0
- package/dist/data-structures/trie/trie.js +19 -4
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +6 -1
- package/dist/types/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +59 -39
- package/src/data-structures/binary-tree/binary-tree.ts +192 -180
- package/src/data-structures/binary-tree/bst.ts +157 -154
- package/src/data-structures/binary-tree/rb-tree.ts +78 -37
- package/src/data-structures/binary-tree/segment-tree.ts +10 -10
- package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
- package/src/data-structures/graph/abstract-graph.ts +46 -46
- package/src/data-structures/graph/directed-graph.ts +40 -40
- package/src/data-structures/graph/undirected-graph.ts +26 -26
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/linked-list/doubly-linked-list.ts +45 -45
- package/src/data-structures/linked-list/singly-linked-list.ts +38 -38
- package/src/data-structures/linked-list/skip-linked-list.ts +4 -4
- package/src/data-structures/queue/queue.ts +13 -13
- package/src/data-structures/stack/stack.ts +9 -9
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +11 -1
- package/src/types/data-structures/graph/abstract-graph.ts +2 -2
- package/src/types/data-structures/hash/hash-map.ts +1 -2
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback, BTNKey,
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -22,29 +22,53 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
22
22
|
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
23
23
|
Sentinel: N;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
|
+
* initializes the tree with optional elements and options.
|
|
27
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
28
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
29
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
|
+
* elements to the
|
|
31
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
32
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
|
+
*/
|
|
35
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<RBTreeOptions>);
|
|
30
36
|
protected _root: N;
|
|
31
37
|
get root(): N;
|
|
32
38
|
protected _size: number;
|
|
33
39
|
get size(): number;
|
|
40
|
+
/**
|
|
41
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
42
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
43
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
44
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
45
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
46
|
+
* specific type when using the `createNode` method.
|
|
47
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
48
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
49
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
50
|
+
* value, and color.
|
|
51
|
+
*/
|
|
34
52
|
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
53
|
+
/**
|
|
54
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
55
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
56
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
57
|
+
* class.
|
|
58
|
+
* @returns a new instance of a RedBlackTree object.
|
|
59
|
+
*/
|
|
35
60
|
createTree(options?: RBTreeOptions): TREE;
|
|
36
61
|
/**
|
|
37
62
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
38
63
|
* Space Complexity: O(1)
|
|
39
|
-
*
|
|
40
|
-
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
41
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
42
|
-
* following types:
|
|
43
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
44
|
-
* key in the node being added to the Red-Black Tree.
|
|
45
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
46
64
|
*/
|
|
47
|
-
|
|
65
|
+
/**
|
|
66
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
67
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
68
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
69
|
+
* `undefined`.
|
|
70
|
+
*/
|
|
71
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined;
|
|
48
72
|
/**
|
|
49
73
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
50
74
|
* Space Complexity: O(1)
|
|
@@ -105,7 +129,6 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
105
129
|
* Space Complexity: O(1)
|
|
106
130
|
*/
|
|
107
131
|
clear(): void;
|
|
108
|
-
init(elements: IterableEntriesOrKeys<V>): void;
|
|
109
132
|
protected _setRoot(v: N): void;
|
|
110
133
|
/**
|
|
111
134
|
* Time Complexity: O(1)
|
|
@@ -170,4 +193,14 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
170
193
|
* red-black tree.
|
|
171
194
|
*/
|
|
172
195
|
protected _fixInsert(k: N): void;
|
|
196
|
+
/**
|
|
197
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
198
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
199
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
200
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
201
|
+
* data structure.
|
|
202
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
203
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
204
|
+
*/
|
|
205
|
+
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
173
206
|
}
|
|
@@ -27,9 +27,15 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
27
27
|
*/
|
|
28
28
|
class RedBlackTree extends bst_1.BST {
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
31
|
+
* initializes the tree with optional elements and options.
|
|
32
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
33
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
34
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
35
|
+
* elements to the
|
|
36
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
37
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
38
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
33
39
|
*/
|
|
34
40
|
constructor(elements, options) {
|
|
35
41
|
super([], options);
|
|
@@ -37,7 +43,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
37
43
|
this._size = 0;
|
|
38
44
|
this._root = this.Sentinel;
|
|
39
45
|
if (elements)
|
|
40
|
-
|
|
46
|
+
super.addMany(elements);
|
|
41
47
|
}
|
|
42
48
|
get root() {
|
|
43
49
|
return this._root;
|
|
@@ -45,36 +51,60 @@ class RedBlackTree extends bst_1.BST {
|
|
|
45
51
|
get size() {
|
|
46
52
|
return this._size;
|
|
47
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
56
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
57
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
58
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
60
|
+
* specific type when using the `createNode` method.
|
|
61
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
62
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
63
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
64
|
+
* value, and color.
|
|
65
|
+
*/
|
|
48
66
|
createNode(key, value, color = types_1.RBTNColor.BLACK) {
|
|
49
67
|
return new RedBlackTreeNode(key, value, color);
|
|
50
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
71
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
72
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
73
|
+
* class.
|
|
74
|
+
* @returns a new instance of a RedBlackTree object.
|
|
75
|
+
*/
|
|
51
76
|
createTree(options) {
|
|
52
77
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
53
78
|
}
|
|
54
79
|
/**
|
|
55
80
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
56
81
|
* Space Complexity: O(1)
|
|
57
|
-
*
|
|
58
|
-
* The `add` function adds a new node to a Red-Black Tree data structure.
|
|
59
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
60
|
-
* following types:
|
|
61
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
62
|
-
* key in the node being added to the Red-Black Tree.
|
|
63
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
64
82
|
*/
|
|
65
|
-
|
|
83
|
+
/**
|
|
84
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
85
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
86
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
87
|
+
* `undefined`.
|
|
88
|
+
*/
|
|
89
|
+
add(keyOrNodeOrEntry) {
|
|
66
90
|
let node;
|
|
67
|
-
if (this.isNodeKey(
|
|
68
|
-
node = this.createNode(
|
|
91
|
+
if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
92
|
+
node = this.createNode(keyOrNodeOrEntry, undefined, types_1.RBTNColor.RED);
|
|
69
93
|
}
|
|
70
|
-
else if (
|
|
71
|
-
node =
|
|
94
|
+
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
95
|
+
node = keyOrNodeOrEntry;
|
|
72
96
|
}
|
|
73
|
-
else if (
|
|
97
|
+
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
74
98
|
return;
|
|
75
99
|
}
|
|
76
|
-
else if (
|
|
77
|
-
|
|
100
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
101
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
102
|
+
if (key === undefined || key === null) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
107
|
+
}
|
|
78
108
|
}
|
|
79
109
|
else {
|
|
80
110
|
return;
|
|
@@ -93,6 +123,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
93
123
|
x = x === null || x === void 0 ? void 0 : x.right;
|
|
94
124
|
}
|
|
95
125
|
else {
|
|
126
|
+
if (node !== x) {
|
|
127
|
+
this._replaceNode(x, node);
|
|
128
|
+
}
|
|
96
129
|
return;
|
|
97
130
|
}
|
|
98
131
|
}
|
|
@@ -203,6 +236,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
203
236
|
isRealNode(node) {
|
|
204
237
|
return node !== this.Sentinel && node !== undefined;
|
|
205
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
241
|
+
* Space Complexity: O(1)
|
|
242
|
+
*/
|
|
206
243
|
/**
|
|
207
244
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
208
245
|
* Space Complexity: O(1)
|
|
@@ -228,7 +265,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
228
265
|
var _a;
|
|
229
266
|
if (identifier instanceof binary_tree_1.BinaryTreeNode)
|
|
230
267
|
callback = (node => node);
|
|
231
|
-
beginRoot = this.
|
|
268
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
232
269
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
233
270
|
}
|
|
234
271
|
/**
|
|
@@ -287,19 +324,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
287
324
|
this._root = this.Sentinel;
|
|
288
325
|
this._size = 0;
|
|
289
326
|
}
|
|
290
|
-
init(elements) {
|
|
291
|
-
if (elements) {
|
|
292
|
-
for (const entryOrKey of elements) {
|
|
293
|
-
if (Array.isArray(entryOrKey)) {
|
|
294
|
-
const [key, value] = entryOrKey;
|
|
295
|
-
this.add(key, value);
|
|
296
|
-
}
|
|
297
|
-
else {
|
|
298
|
-
this.add(entryOrKey);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
327
|
_setRoot(v) {
|
|
304
328
|
if (v) {
|
|
305
329
|
v.parent = undefined;
|
|
@@ -529,5 +553,18 @@ class RedBlackTree extends bst_1.BST {
|
|
|
529
553
|
}
|
|
530
554
|
this.root.color = types_1.RBTNColor.BLACK;
|
|
531
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
558
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
559
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
560
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
561
|
+
* data structure.
|
|
562
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
563
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
564
|
+
*/
|
|
565
|
+
_replaceNode(oldNode, newNode) {
|
|
566
|
+
newNode.color = oldNode.color;
|
|
567
|
+
return super._replaceNode(oldNode, newNode);
|
|
568
|
+
}
|
|
532
569
|
}
|
|
533
570
|
exports.RedBlackTree = RedBlackTree;
|
|
@@ -9,11 +9,11 @@ import type { SegmentTreeNodeVal } from '../../types';
|
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
10
|
start: number;
|
|
11
11
|
end: number;
|
|
12
|
-
value: SegmentTreeNodeVal |
|
|
12
|
+
value: SegmentTreeNodeVal | undefined;
|
|
13
13
|
sum: number;
|
|
14
|
-
left: SegmentTreeNode |
|
|
15
|
-
right: SegmentTreeNode |
|
|
16
|
-
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal |
|
|
14
|
+
left: SegmentTreeNode | undefined;
|
|
15
|
+
right: SegmentTreeNode | undefined;
|
|
16
|
+
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
|
|
17
17
|
}
|
|
18
18
|
export declare class SegmentTree {
|
|
19
19
|
/**
|
|
@@ -32,8 +32,8 @@ export declare class SegmentTree {
|
|
|
32
32
|
get start(): number;
|
|
33
33
|
protected _end: number;
|
|
34
34
|
get end(): number;
|
|
35
|
-
protected _root: SegmentTreeNode |
|
|
36
|
-
get root(): SegmentTreeNode |
|
|
35
|
+
protected _root: SegmentTreeNode | undefined;
|
|
36
|
+
get root(): SegmentTreeNode | undefined;
|
|
37
37
|
/**
|
|
38
38
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
39
39
|
* the sum of values to each segment.
|
|
@@ -12,14 +12,14 @@ class SegmentTreeNode {
|
|
|
12
12
|
constructor(start, end, sum, value) {
|
|
13
13
|
this.start = 0;
|
|
14
14
|
this.end = 0;
|
|
15
|
-
this.value =
|
|
15
|
+
this.value = undefined;
|
|
16
16
|
this.sum = 0;
|
|
17
|
-
this.left =
|
|
18
|
-
this.right =
|
|
17
|
+
this.left = undefined;
|
|
18
|
+
this.right = undefined;
|
|
19
19
|
this.start = start;
|
|
20
20
|
this.end = end;
|
|
21
21
|
this.sum = sum;
|
|
22
|
-
this.value = value ||
|
|
22
|
+
this.value = value || undefined;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
exports.SegmentTreeNode = SegmentTreeNode;
|
|
@@ -45,7 +45,7 @@ class SegmentTree {
|
|
|
45
45
|
this._root = this.build(start, end);
|
|
46
46
|
}
|
|
47
47
|
else {
|
|
48
|
-
this._root =
|
|
48
|
+
this._root = undefined;
|
|
49
49
|
this._values = [];
|
|
50
50
|
}
|
|
51
51
|
}
|
|
@@ -96,7 +96,7 @@ class SegmentTree {
|
|
|
96
96
|
* @returns The function does not return anything.
|
|
97
97
|
*/
|
|
98
98
|
updateNode(index, sum, value) {
|
|
99
|
-
const root = this.root ||
|
|
99
|
+
const root = this.root || undefined;
|
|
100
100
|
if (!root) {
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
@@ -132,7 +132,7 @@ class SegmentTree {
|
|
|
132
132
|
* @returns The function `querySumByRange` returns a number.
|
|
133
133
|
*/
|
|
134
134
|
querySumByRange(indexA, indexB) {
|
|
135
|
-
const root = this.root ||
|
|
135
|
+
const root = this.root || undefined;
|
|
136
136
|
if (!root) {
|
|
137
137
|
return 0;
|
|
138
138
|
}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback,
|
|
8
|
+
import type { BSTNodeKeyOrNode, BTNKey, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -27,13 +27,7 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
-
|
|
31
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
32
|
-
* merge duplicated values.
|
|
33
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
34
|
-
* TreeMultimap.
|
|
35
|
-
*/
|
|
36
|
-
constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<TreeMultimapOptions>);
|
|
30
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>);
|
|
37
31
|
private _count;
|
|
38
32
|
get count(): number;
|
|
39
33
|
/**
|
|
@@ -47,39 +41,38 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
47
41
|
*/
|
|
48
42
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
49
43
|
createTree(options?: TreeMultimapOptions): TREE;
|
|
44
|
+
/**
|
|
45
|
+
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
46
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
47
|
+
*/
|
|
50
48
|
/**
|
|
51
49
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
52
50
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
53
51
|
*
|
|
54
|
-
* The `add` function
|
|
55
|
-
*
|
|
56
|
-
* @param
|
|
57
|
-
* following types:
|
|
58
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
59
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
52
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
53
|
+
* and update the count.
|
|
54
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
60
55
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
61
|
-
* times the key
|
|
62
|
-
*
|
|
56
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
57
|
+
* is 1.
|
|
58
|
+
* @returns either a node (`N`) or `undefined`.
|
|
63
59
|
*/
|
|
64
|
-
add(
|
|
60
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
65
61
|
/**
|
|
66
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
62
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
67
63
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
68
64
|
*/
|
|
69
65
|
/**
|
|
70
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
66
|
+
* Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
71
67
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
72
68
|
*
|
|
73
|
-
* The function
|
|
74
|
-
*
|
|
75
|
-
* @param
|
|
76
|
-
*
|
|
77
|
-
* @
|
|
78
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
79
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
80
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
69
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
70
|
+
* structure.
|
|
71
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
72
|
+
* either keys, nodes, or entries.
|
|
73
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
81
74
|
*/
|
|
82
|
-
addMany(
|
|
75
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
|
|
83
76
|
/**
|
|
84
77
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
85
78
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -128,11 +121,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
128
121
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
129
122
|
*/
|
|
130
123
|
clear(): void;
|
|
131
|
-
/**
|
|
132
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
|
|
133
|
-
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
134
|
-
*/
|
|
135
|
-
init(elements: IterableEntriesOrKeys<V>): void;
|
|
136
124
|
/**
|
|
137
125
|
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
138
126
|
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
@@ -148,9 +136,9 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
148
136
|
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
|
149
137
|
* added, or `undefined` if no node was added.
|
|
150
138
|
*/
|
|
151
|
-
protected _addTo(newNode: N | undefined, parent:
|
|
139
|
+
protected _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<N>): N | undefined;
|
|
152
140
|
/**
|
|
153
|
-
* The `
|
|
141
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
154
142
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
155
143
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
156
144
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -158,5 +146,6 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
158
146
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
159
147
|
* if either `srcNode` or `destNode` is undefined.
|
|
160
148
|
*/
|
|
161
|
-
protected
|
|
149
|
+
protected _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined;
|
|
150
|
+
protected _replaceNode(oldNode: N, newNode: N): N;
|
|
162
151
|
}
|