data-structure-typed 1.47.5 → 1.47.7
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/.github/ISSUE_TEMPLATE/bug_report.md +10 -7
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/release-package.yml +1 -1
- package/CHANGELOG.md +1 -1
- package/CODE_OF_CONDUCT.md +32 -10
- package/COMMANDS.md +3 -1
- package/CONTRIBUTING.md +4 -3
- package/README.md +103 -28
- package/SECURITY.md +1 -1
- package/benchmark/report.html +46 -1
- package/benchmark/report.json +563 -8
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +46 -29
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +182 -184
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/cjs/data-structures/binary-tree/bst.js +168 -169
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +77 -31
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +66 -136
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/cjs/data-structures/hash/hash-map.js +5 -8
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/cjs/data-structures/heap/heap.js +52 -34
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/max-heap.js +2 -2
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/cjs/data-structures/heap/min-heap.js +2 -2
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/cjs/data-structures/queue/deque.js +3 -0
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/cjs/data-structures/queue/queue.js +3 -0
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/cjs/data-structures/stack/stack.js +10 -2
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/cjs/data-structures/trie/trie.js +19 -4
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/cjs/types/common.d.ts +7 -0
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/cjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/cjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +36 -18
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +49 -30
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +158 -129
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +182 -194
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +73 -63
- package/dist/mjs/data-structures/binary-tree/bst.js +171 -170
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -17
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +81 -33
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +29 -40
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +70 -138
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -6
- package/dist/mjs/data-structures/hash/hash-map.js +5 -8
- package/dist/mjs/data-structures/heap/heap.d.ts +19 -21
- package/dist/mjs/data-structures/heap/heap.js +53 -35
- package/dist/mjs/data-structures/heap/max-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/max-heap.js +2 -2
- package/dist/mjs/data-structures/heap/min-heap.d.ts +2 -5
- package/dist/mjs/data-structures/heap/min-heap.js +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +9 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +8 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +2 -2
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +2 -5
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +1 -0
- package/dist/mjs/data-structures/queue/deque.js +3 -0
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -0
- package/dist/mjs/data-structures/queue/queue.js +3 -0
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -1
- package/dist/mjs/data-structures/stack/stack.js +10 -2
- package/dist/mjs/data-structures/trie/trie.d.ts +3 -0
- package/dist/mjs/data-structures/trie/trie.js +20 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +4 -2
- package/dist/mjs/types/common.d.ts +7 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +1 -2
- package/dist/mjs/types/data-structures/heap/heap.d.ts +4 -1
- package/dist/mjs/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
- package/dist/umd/data-structure-typed.js +629 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +61 -31
- package/src/data-structures/binary-tree/binary-tree.ts +283 -254
- package/src/data-structures/binary-tree/bst.ts +193 -170
- package/src/data-structures/binary-tree/rb-tree.ts +87 -32
- package/src/data-structures/binary-tree/tree-multimap.ts +76 -136
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/data-structures/heap/heap.ts +57 -39
- package/src/data-structures/heap/max-heap.ts +5 -5
- package/src/data-structures/heap/min-heap.ts +5 -5
- package/src/data-structures/linked-list/doubly-linked-list.ts +10 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +9 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
- package/src/data-structures/priority-queue/priority-queue.ts +3 -3
- package/src/data-structures/queue/deque.ts +4 -0
- package/src/data-structures/queue/queue.ts +4 -0
- package/src/data-structures/stack/stack.ts +12 -3
- package/src/data-structures/trie/trie.ts +23 -4
- package/src/interfaces/binary-tree.ts +14 -2
- package/src/types/common.ts +15 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +2 -3
- package/src/types/data-structures/hash/hash-map.ts +1 -2
- package/src/types/data-structures/heap/heap.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
- package/test/integration/{all-in-one.ts → all-in-one.test.ts} +1 -1
- package/test/integration/index.html +87 -0
- package/test/performance/data-structures/comparison/comparison.test.ts +7 -6
- package/test/performance/data-structures/heap/heap.test.ts +2 -2
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -57
- package/test/unit/data-structures/binary-tree/bst.test.ts +49 -54
- package/test/unit/data-structures/binary-tree/overall.test.ts +17 -18
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +118 -66
- package/test/unit/data-structures/heap/heap.test.ts +2 -2
- package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +4 -3
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +9 -10
- package/test/unit/data-structures/stack/stack.test.ts +2 -2
- package/test/unit/unrestricted-interconversion.test.ts +156 -0
- package/tsconfig-cjs.json +1 -1
- package/test/integration/conversion.test.ts +0 -0
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
8
|
+
import { RBTNColor } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { BinaryTreeNode } from './binary-tree';
|
|
11
11
|
export class RedBlackTreeNode extends BSTNode {
|
|
@@ -24,21 +24,22 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
24
24
|
*/
|
|
25
25
|
export class RedBlackTree extends BST {
|
|
26
26
|
Sentinel = new RedBlackTreeNode(NaN);
|
|
27
|
-
options;
|
|
28
27
|
/**
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
28
|
+
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
29
|
+
* initializes the tree with optional elements and options.
|
|
30
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
|
|
31
|
+
* objects. It represents the initial elements that will be added to the RBTree during its
|
|
32
|
+
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
33
|
+
* elements to the
|
|
34
|
+
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
35
|
+
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
36
|
+
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
32
37
|
*/
|
|
33
|
-
constructor(options) {
|
|
34
|
-
super(options);
|
|
35
|
-
if (options) {
|
|
36
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
40
|
-
}
|
|
38
|
+
constructor(elements, options) {
|
|
39
|
+
super([], options);
|
|
41
40
|
this._root = this.Sentinel;
|
|
41
|
+
if (elements)
|
|
42
|
+
super.addMany(elements);
|
|
42
43
|
}
|
|
43
44
|
_root;
|
|
44
45
|
get root() {
|
|
@@ -48,40 +49,63 @@ export class RedBlackTree extends BST {
|
|
|
48
49
|
get size() {
|
|
49
50
|
return this._size;
|
|
50
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
54
|
+
* @param {BTNKey} key - The key parameter is the key value associated with the node. It is used to
|
|
55
|
+
* identify and compare nodes in the Red-Black Tree.
|
|
56
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
57
|
+
* associated with the node. It is of type `V`, which is a generic type that can be replaced with any
|
|
58
|
+
* specific type when using the `createNode` method.
|
|
59
|
+
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
60
|
+
* Red-Black Tree. It can be either "RED" or "BLACK". By default, the color is set to "BLACK".
|
|
61
|
+
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
62
|
+
* value, and color.
|
|
63
|
+
*/
|
|
51
64
|
createNode(key, value, color = RBTNColor.BLACK) {
|
|
52
65
|
return new RedBlackTreeNode(key, value, color);
|
|
53
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* The function creates a Red-Black Tree with the specified options and returns it.
|
|
69
|
+
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
70
|
+
* passed to the `createTree` function. It is used to customize the behavior of the `RedBlackTree`
|
|
71
|
+
* class.
|
|
72
|
+
* @returns a new instance of a RedBlackTree object.
|
|
73
|
+
*/
|
|
54
74
|
createTree(options) {
|
|
55
|
-
return new RedBlackTree(
|
|
75
|
+
return new RedBlackTree([], {
|
|
76
|
+
iterationType: this.iterationType,
|
|
77
|
+
comparator: this.comparator, ...options
|
|
78
|
+
});
|
|
56
79
|
}
|
|
57
80
|
/**
|
|
58
81
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
59
82
|
* Space Complexity: O(1)
|
|
60
83
|
*/
|
|
61
84
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
|
|
67
|
-
* following types:
|
|
68
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
69
|
-
* key in the node being added to the Red-Black Tree.
|
|
70
|
-
* @returns The method returns either a node (`N`) or `undefined`.
|
|
85
|
+
* The function adds a node to a Red-Black Tree data structure.
|
|
86
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
87
|
+
* @returns The method `add` returns either an instance of `N` (the node that was added) or
|
|
88
|
+
* `undefined`.
|
|
71
89
|
*/
|
|
72
|
-
add(
|
|
90
|
+
add(keyOrNodeOrEntry) {
|
|
73
91
|
let node;
|
|
74
|
-
if (this.isNodeKey(
|
|
75
|
-
node = this.createNode(
|
|
92
|
+
if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
93
|
+
node = this.createNode(keyOrNodeOrEntry, undefined, RBTNColor.RED);
|
|
76
94
|
}
|
|
77
|
-
else if (
|
|
78
|
-
node =
|
|
95
|
+
else if (keyOrNodeOrEntry instanceof RedBlackTreeNode) {
|
|
96
|
+
node = keyOrNodeOrEntry;
|
|
79
97
|
}
|
|
80
|
-
else if (
|
|
98
|
+
else if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
81
99
|
return;
|
|
82
100
|
}
|
|
83
|
-
else if (
|
|
84
|
-
|
|
101
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
102
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
103
|
+
if (key === undefined || key === null) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
node = this.createNode(key, value, RBTNColor.RED);
|
|
108
|
+
}
|
|
85
109
|
}
|
|
86
110
|
else {
|
|
87
111
|
return;
|
|
@@ -100,6 +124,9 @@ export class RedBlackTree extends BST {
|
|
|
100
124
|
x = x?.right;
|
|
101
125
|
}
|
|
102
126
|
else {
|
|
127
|
+
if (node !== x) {
|
|
128
|
+
this._replaceNode(x, node);
|
|
129
|
+
}
|
|
103
130
|
return;
|
|
104
131
|
}
|
|
105
132
|
}
|
|
@@ -203,6 +230,10 @@ export class RedBlackTree extends BST {
|
|
|
203
230
|
// TODO
|
|
204
231
|
return ans;
|
|
205
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
235
|
+
* Space Complexity: O(1)
|
|
236
|
+
*/
|
|
206
237
|
isRealNode(node) {
|
|
207
238
|
return node !== this.Sentinel && node !== undefined;
|
|
208
239
|
}
|
|
@@ -231,10 +262,10 @@ export class RedBlackTree extends BST {
|
|
|
231
262
|
* `getNodes` method, which is called within the `getNode` method.
|
|
232
263
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
233
264
|
*/
|
|
234
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.
|
|
265
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
235
266
|
if (identifier instanceof BinaryTreeNode)
|
|
236
267
|
callback = (node => node);
|
|
237
|
-
beginRoot = this.
|
|
268
|
+
beginRoot = this.ensureNode(beginRoot);
|
|
238
269
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
239
270
|
}
|
|
240
271
|
/**
|
|
@@ -284,6 +315,10 @@ export class RedBlackTree extends BST {
|
|
|
284
315
|
}
|
|
285
316
|
return y;
|
|
286
317
|
}
|
|
318
|
+
/**
|
|
319
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
320
|
+
* Space Complexity: O(1)
|
|
321
|
+
*/
|
|
287
322
|
clear() {
|
|
288
323
|
this._root = this.Sentinel;
|
|
289
324
|
this._size = 0;
|
|
@@ -517,4 +552,17 @@ export class RedBlackTree extends BST {
|
|
|
517
552
|
}
|
|
518
553
|
this.root.color = RBTNColor.BLACK;
|
|
519
554
|
}
|
|
555
|
+
/**
|
|
556
|
+
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
557
|
+
* @param {N} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
558
|
+
* data structure. It is of type `N`, which is the type of the nodes in the data structure.
|
|
559
|
+
* @param {N} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
560
|
+
* data structure.
|
|
561
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
562
|
+
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
563
|
+
*/
|
|
564
|
+
_replaceNode(oldNode, newNode) {
|
|
565
|
+
newNode.color = oldNode.color;
|
|
566
|
+
return super._replaceNode(oldNode, newNode);
|
|
567
|
+
}
|
|
520
568
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
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';
|
|
8
|
+
import type { BSTNodeKeyOrNode, BTNKey, BTNodeExemplar, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
9
|
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
@@ -27,14 +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
|
-
options
|
|
31
|
-
/**
|
|
32
|
-
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
33
|
-
* merge duplicated values.
|
|
34
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
35
|
-
* TreeMultimap.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options?: TreeMultimapOptions);
|
|
30
|
+
constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<TreeMultimapOptions>);
|
|
38
31
|
private _count;
|
|
39
32
|
get count(): number;
|
|
40
33
|
/**
|
|
@@ -56,38 +49,33 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
56
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.
|
|
57
50
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
58
51
|
*
|
|
59
|
-
* The `add` function
|
|
60
|
-
*
|
|
61
|
-
* @param
|
|
62
|
-
* following types:
|
|
63
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
64
|
-
* 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:
|
|
65
55
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
66
|
-
* times the key
|
|
67
|
-
*
|
|
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`.
|
|
68
59
|
*/
|
|
69
|
-
add(
|
|
60
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | undefined;
|
|
70
61
|
/**
|
|
71
|
-
* Time Complexity: O(
|
|
72
|
-
* Space Complexity: O(1) - constant space, as it
|
|
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.
|
|
63
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
73
64
|
*/
|
|
74
65
|
/**
|
|
75
|
-
* 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.
|
|
76
67
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
77
68
|
*
|
|
78
|
-
* The function
|
|
79
|
-
*
|
|
80
|
-
* @param
|
|
81
|
-
*
|
|
82
|
-
* @
|
|
83
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
84
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
85
|
-
* @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`.
|
|
86
74
|
*/
|
|
87
|
-
addMany(
|
|
75
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<V, N>>): (N | undefined)[];
|
|
88
76
|
/**
|
|
89
|
-
* Time Complexity: O(
|
|
90
|
-
* Space Complexity: O(1) - constant space, as it
|
|
77
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
78
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
91
79
|
*/
|
|
92
80
|
/**
|
|
93
81
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -100,10 +88,10 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
100
88
|
* values:
|
|
101
89
|
* @returns a boolean value.
|
|
102
90
|
*/
|
|
103
|
-
perfectlyBalance(iterationType?: IterationType
|
|
91
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
104
92
|
/**
|
|
105
|
-
* Time Complexity: O(
|
|
106
|
-
* Space Complexity: O(
|
|
93
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
94
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
107
95
|
*/
|
|
108
96
|
/**
|
|
109
97
|
* 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.
|
|
@@ -126,8 +114,8 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
126
114
|
*/
|
|
127
115
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
|
|
128
116
|
/**
|
|
129
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
130
|
-
* Space Complexity: O(
|
|
117
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
118
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
131
119
|
*/
|
|
132
120
|
/**
|
|
133
121
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -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
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FamilyPosition, IterationType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultimapNode extends AVLTreeNode {
|
|
4
4
|
count;
|
|
@@ -21,25 +21,17 @@ export class TreeMultimapNode extends AVLTreeNode {
|
|
|
21
21
|
* 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.
|
|
22
22
|
*/
|
|
23
23
|
export class TreeMultimap extends AVLTree {
|
|
24
|
-
options
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
29
|
-
* TreeMultimap.
|
|
30
|
-
*/
|
|
31
|
-
constructor(options = { iterationType: IterationType.ITERATIVE }) {
|
|
32
|
-
super(options);
|
|
33
|
-
if (options) {
|
|
34
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options };
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
38
|
-
}
|
|
24
|
+
constructor(elements, options) {
|
|
25
|
+
super([], options);
|
|
26
|
+
if (elements)
|
|
27
|
+
this.addMany(elements);
|
|
39
28
|
}
|
|
40
29
|
_count = 0;
|
|
30
|
+
// TODO the _count is not accurate after nodes count modified
|
|
41
31
|
get count() {
|
|
42
|
-
|
|
32
|
+
let sum = 0;
|
|
33
|
+
this.subTreeTraverse(node => sum += node.count);
|
|
34
|
+
return sum;
|
|
43
35
|
}
|
|
44
36
|
/**
|
|
45
37
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
@@ -54,7 +46,10 @@ export class TreeMultimap extends AVLTree {
|
|
|
54
46
|
return new TreeMultimapNode(key, value, count);
|
|
55
47
|
}
|
|
56
48
|
createTree(options) {
|
|
57
|
-
return new TreeMultimap(
|
|
49
|
+
return new TreeMultimap([], {
|
|
50
|
+
iterationType: this.iterationType,
|
|
51
|
+
comparator: this.comparator, ...options
|
|
52
|
+
});
|
|
58
53
|
}
|
|
59
54
|
/**
|
|
60
55
|
* 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.
|
|
@@ -64,131 +59,64 @@ export class TreeMultimap extends AVLTree {
|
|
|
64
59
|
* 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.
|
|
65
60
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
66
61
|
*
|
|
67
|
-
* The `add` function
|
|
68
|
-
*
|
|
69
|
-
* @param
|
|
70
|
-
* following types:
|
|
71
|
-
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
72
|
-
* being added to the tree. It is an optional parameter, so it can be omitted if not needed.
|
|
62
|
+
* The `add` function overrides the base class `add` function to add a new node to the tree multimap
|
|
63
|
+
* and update the count.
|
|
64
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
73
65
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
74
|
-
* times the key
|
|
75
|
-
*
|
|
66
|
+
* times the key or node or entry should be added to the multimap. If not provided, the default value
|
|
67
|
+
* is 1.
|
|
68
|
+
* @returns either a node (`N`) or `undefined`.
|
|
76
69
|
*/
|
|
77
|
-
add(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
82
|
-
newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
|
|
70
|
+
add(keyOrNodeOrEntry, count = 1) {
|
|
71
|
+
let newNode;
|
|
72
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
73
|
+
return;
|
|
83
74
|
}
|
|
84
|
-
else if (
|
|
85
|
-
newNode =
|
|
75
|
+
else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
|
|
76
|
+
newNode = keyOrNodeOrEntry;
|
|
86
77
|
}
|
|
87
|
-
else {
|
|
88
|
-
newNode = this.createNode(
|
|
78
|
+
else if (this.isNodeKey(keyOrNodeOrEntry)) {
|
|
79
|
+
newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
|
|
89
80
|
}
|
|
90
|
-
if (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
81
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
82
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
83
|
+
if (key === undefined || key === null) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
newNode = this.createNode(key, value, count);
|
|
88
|
+
}
|
|
96
89
|
}
|
|
97
90
|
else {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
cur.value = newNode.value;
|
|
105
|
-
cur.count += newNode.count;
|
|
106
|
-
this._count += newNode.count;
|
|
107
|
-
traversing = false;
|
|
108
|
-
inserted = cur;
|
|
109
|
-
}
|
|
110
|
-
else if (this._compare(cur.key, newNode.key) === CP.gt) {
|
|
111
|
-
// Traverse left of the node
|
|
112
|
-
if (cur.left === undefined) {
|
|
113
|
-
//Add to the left of the current node
|
|
114
|
-
cur.left = newNode;
|
|
115
|
-
this._size = this.size + 1;
|
|
116
|
-
this._count += newNode.count;
|
|
117
|
-
traversing = false;
|
|
118
|
-
inserted = cur.left;
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
//Traverse the left of the current node
|
|
122
|
-
if (cur.left)
|
|
123
|
-
cur = cur.left;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else if (this._compare(cur.key, newNode.key) === CP.lt) {
|
|
127
|
-
// Traverse right of the node
|
|
128
|
-
if (cur.right === undefined) {
|
|
129
|
-
//Add to the right of the current node
|
|
130
|
-
cur.right = newNode;
|
|
131
|
-
this._size = this.size + 1;
|
|
132
|
-
this._count += newNode.count;
|
|
133
|
-
traversing = false;
|
|
134
|
-
inserted = cur.right;
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
//Traverse the left of the current node
|
|
138
|
-
if (cur.right)
|
|
139
|
-
cur = cur.right;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
// TODO may need to support undefined inserted
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
traversing = false;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const orgNodeCount = newNode?.count || 0;
|
|
94
|
+
const inserted = super.add(newNode);
|
|
95
|
+
if (inserted) {
|
|
96
|
+
this._count += orgNodeCount;
|
|
151
97
|
}
|
|
152
|
-
if (inserted)
|
|
153
|
-
this._balancePath(inserted);
|
|
154
98
|
return inserted;
|
|
155
99
|
}
|
|
156
100
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(1) - constant space, as it
|
|
101
|
+
* 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.
|
|
102
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
159
103
|
*/
|
|
160
104
|
/**
|
|
161
|
-
* Time Complexity: O(k log n) - logarithmic time
|
|
105
|
+
* 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.
|
|
162
106
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
163
107
|
*
|
|
164
|
-
* The function
|
|
165
|
-
*
|
|
166
|
-
* @param
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
169
|
-
* keys or nodes being added. It is used to associate data with each key or node being added to the
|
|
170
|
-
* TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
|
|
171
|
-
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
108
|
+
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
109
|
+
* structure.
|
|
110
|
+
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
111
|
+
* either keys, nodes, or entries.
|
|
112
|
+
* @returns The method is returning an array of type `N | undefined`.
|
|
172
113
|
*/
|
|
173
|
-
addMany(
|
|
174
|
-
|
|
175
|
-
for (let i = 0; i < keysOrNodes.length; i++) {
|
|
176
|
-
const keyOrNode = keysOrNodes[i];
|
|
177
|
-
if (keyOrNode instanceof TreeMultimapNode) {
|
|
178
|
-
inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
if (keyOrNode === undefined) {
|
|
182
|
-
inserted.push(this.add(NaN, undefined, 0));
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
|
186
|
-
}
|
|
187
|
-
return inserted;
|
|
114
|
+
addMany(keysOrNodesOrEntries) {
|
|
115
|
+
return super.addMany(keysOrNodesOrEntries);
|
|
188
116
|
}
|
|
189
117
|
/**
|
|
190
|
-
* Time Complexity: O(
|
|
191
|
-
* Space Complexity: O(1) - constant space, as it
|
|
118
|
+
* Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
|
|
119
|
+
* Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
|
|
192
120
|
*/
|
|
193
121
|
/**
|
|
194
122
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
@@ -201,7 +129,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
201
129
|
* values:
|
|
202
130
|
* @returns a boolean value.
|
|
203
131
|
*/
|
|
204
|
-
perfectlyBalance(iterationType = this.
|
|
132
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
205
133
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
206
134
|
if (sorted.length < 1)
|
|
207
135
|
return false;
|
|
@@ -212,7 +140,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
212
140
|
return;
|
|
213
141
|
const m = l + Math.floor((r - l) / 2);
|
|
214
142
|
const midNode = sorted[m];
|
|
215
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
143
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
216
144
|
buildBalanceBST(l, m - 1);
|
|
217
145
|
buildBalanceBST(m + 1, r);
|
|
218
146
|
};
|
|
@@ -228,7 +156,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
228
156
|
if (l <= r) {
|
|
229
157
|
const m = l + Math.floor((r - l) / 2);
|
|
230
158
|
const midNode = sorted[m];
|
|
231
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
159
|
+
this.add([midNode.key, midNode.value], midNode.count);
|
|
232
160
|
stack.push([m + 1, r]);
|
|
233
161
|
stack.push([l, m - 1]);
|
|
234
162
|
}
|
|
@@ -238,8 +166,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
238
166
|
}
|
|
239
167
|
}
|
|
240
168
|
/**
|
|
241
|
-
* Time Complexity: O(
|
|
242
|
-
* Space Complexity: O(
|
|
169
|
+
* Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
|
|
170
|
+
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
243
171
|
*/
|
|
244
172
|
/**
|
|
245
173
|
* 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.
|
|
@@ -294,7 +222,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
294
222
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
|
|
295
223
|
if (leftSubTreeRightMost) {
|
|
296
224
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
297
|
-
orgCurrent = this.
|
|
225
|
+
orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
|
|
298
226
|
if (parentOfLeftSubTreeMax) {
|
|
299
227
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
|
|
300
228
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -318,8 +246,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
318
246
|
return deletedResult;
|
|
319
247
|
}
|
|
320
248
|
/**
|
|
321
|
-
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree.
|
|
322
|
-
* Space Complexity: O(
|
|
249
|
+
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
250
|
+
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
323
251
|
*/
|
|
324
252
|
/**
|
|
325
253
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
@@ -344,7 +272,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
344
272
|
* added, or `undefined` if no node was added.
|
|
345
273
|
*/
|
|
346
274
|
_addTo(newNode, parent) {
|
|
347
|
-
parent = this.
|
|
275
|
+
parent = this.ensureNode(parent);
|
|
348
276
|
if (parent) {
|
|
349
277
|
if (parent.left === undefined) {
|
|
350
278
|
parent.left = newNode;
|
|
@@ -371,7 +299,7 @@ export class TreeMultimap extends AVLTree {
|
|
|
371
299
|
}
|
|
372
300
|
}
|
|
373
301
|
/**
|
|
374
|
-
* The `
|
|
302
|
+
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
375
303
|
* @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
376
304
|
* which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
|
|
377
305
|
* @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
|
|
@@ -379,9 +307,9 @@ export class TreeMultimap extends AVLTree {
|
|
|
379
307
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
380
308
|
* if either `srcNode` or `destNode` is undefined.
|
|
381
309
|
*/
|
|
382
|
-
|
|
383
|
-
srcNode = this.
|
|
384
|
-
destNode = this.
|
|
310
|
+
_swapProperties(srcNode, destNode) {
|
|
311
|
+
srcNode = this.ensureNode(srcNode);
|
|
312
|
+
destNode = this.ensureNode(destNode);
|
|
385
313
|
if (srcNode && destNode) {
|
|
386
314
|
const { key, value, count, height } = destNode;
|
|
387
315
|
const tempNode = this.createNode(key, value, count);
|
|
@@ -400,4 +328,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
400
328
|
}
|
|
401
329
|
return undefined;
|
|
402
330
|
}
|
|
331
|
+
_replaceNode(oldNode, newNode) {
|
|
332
|
+
newNode.count = oldNode.count + newNode.count;
|
|
333
|
+
return super._replaceNode(oldNode, newNode);
|
|
334
|
+
}
|
|
403
335
|
}
|