min-heap-typed 1.50.1 → 1.50.3
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/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -9,18 +9,136 @@
|
|
|
9
9
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
10
10
|
|
|
11
11
|
export class SegmentTreeNode {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
14
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
15
|
+
* by this node in a segment tree.
|
|
16
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
17
|
+
* node in a segment tree.
|
|
18
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
19
|
+
* the segment tree node.
|
|
20
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
21
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
22
|
+
*/
|
|
19
23
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
24
|
+
this._start = start;
|
|
25
|
+
this._end = end;
|
|
26
|
+
this._sum = sum;
|
|
27
|
+
this._value = value || undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected _start = 0;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The function returns the value of the protected variable _start.
|
|
34
|
+
* @returns The start value, which is of type number.
|
|
35
|
+
*/
|
|
36
|
+
get start(): number {
|
|
37
|
+
return this._start;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The above function sets the value of the "start" property.
|
|
42
|
+
* @param {number} value - The value parameter is of type number.
|
|
43
|
+
*/
|
|
44
|
+
set start(value: number) {
|
|
45
|
+
this._start = value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected _end = 0;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The function returns the value of the protected variable `_end`.
|
|
52
|
+
* @returns The value of the protected property `_end`.
|
|
53
|
+
*/
|
|
54
|
+
get end(): number {
|
|
55
|
+
return this._end;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The above function sets the value of the "end" property.
|
|
60
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
61
|
+
* property.
|
|
62
|
+
*/
|
|
63
|
+
set end(value: number) {
|
|
64
|
+
this._end = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected _value: SegmentTreeNodeVal | undefined = undefined;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the value of a segment tree node.
|
|
71
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
72
|
+
*/
|
|
73
|
+
get value(): SegmentTreeNodeVal | undefined {
|
|
74
|
+
return this._value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The function sets the value of a segment tree node.
|
|
79
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
80
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
81
|
+
*/
|
|
82
|
+
set value(value: SegmentTreeNodeVal | undefined) {
|
|
83
|
+
this._value = value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected _sum = 0;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The function returns the value of the sum property.
|
|
90
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
91
|
+
*/
|
|
92
|
+
get sum(): number {
|
|
93
|
+
return this._sum;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The above function sets the value of the sum property.
|
|
98
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
99
|
+
*/
|
|
100
|
+
set sum(value: number) {
|
|
101
|
+
this._sum = value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
protected _left: SegmentTreeNode | undefined = undefined;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The function returns the left child of a segment tree node.
|
|
108
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
109
|
+
* `SegmentTreeNode` or `undefined`.
|
|
110
|
+
*/
|
|
111
|
+
get left(): SegmentTreeNode | undefined {
|
|
112
|
+
return this._left;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
117
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
118
|
+
* undefined.
|
|
119
|
+
*/
|
|
120
|
+
set left(value: SegmentTreeNode | undefined) {
|
|
121
|
+
this._left = value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
protected _right: SegmentTreeNode | undefined = undefined;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The function returns the right child of a segment tree node.
|
|
128
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
129
|
+
*/
|
|
130
|
+
get right(): SegmentTreeNode | undefined {
|
|
131
|
+
return this._right;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The function sets the right child of a segment tree node.
|
|
136
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
137
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
138
|
+
* value.
|
|
139
|
+
*/
|
|
140
|
+
set right(value: SegmentTreeNode | undefined) {
|
|
141
|
+
this._right = value;
|
|
24
142
|
}
|
|
25
143
|
}
|
|
26
144
|
|
|
@@ -51,24 +169,40 @@ export class SegmentTree {
|
|
|
51
169
|
|
|
52
170
|
protected _values: number[] = [];
|
|
53
171
|
|
|
172
|
+
/**
|
|
173
|
+
* The function returns an array of numbers.
|
|
174
|
+
* @returns An array of numbers is being returned.
|
|
175
|
+
*/
|
|
54
176
|
get values(): number[] {
|
|
55
177
|
return this._values;
|
|
56
178
|
}
|
|
57
179
|
|
|
58
180
|
protected _start = 0;
|
|
59
181
|
|
|
182
|
+
/**
|
|
183
|
+
* The function returns the value of the protected variable _start.
|
|
184
|
+
* @returns The start value, which is of type number.
|
|
185
|
+
*/
|
|
60
186
|
get start(): number {
|
|
61
187
|
return this._start;
|
|
62
188
|
}
|
|
63
189
|
|
|
64
190
|
protected _end: number;
|
|
65
191
|
|
|
192
|
+
/**
|
|
193
|
+
* The function returns the value of the protected variable `_end`.
|
|
194
|
+
* @returns The value of the protected property `_end`.
|
|
195
|
+
*/
|
|
66
196
|
get end(): number {
|
|
67
197
|
return this._end;
|
|
68
198
|
}
|
|
69
199
|
|
|
70
200
|
protected _root: SegmentTreeNode | undefined;
|
|
71
201
|
|
|
202
|
+
/**
|
|
203
|
+
* The function returns the root node of a segment tree.
|
|
204
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
205
|
+
*/
|
|
72
206
|
get root(): SegmentTreeNode | undefined {
|
|
73
207
|
return this._root;
|
|
74
208
|
}
|
|
@@ -21,10 +21,8 @@ import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
|
21
21
|
export class TreeMultimapNode<
|
|
22
22
|
K = any,
|
|
23
23
|
V = any,
|
|
24
|
-
|
|
25
|
-
> extends AVLTreeNode<K, V,
|
|
26
|
-
count: number;
|
|
27
|
-
|
|
24
|
+
NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
|
|
25
|
+
> extends AVLTreeNode<K, V, NODE> {
|
|
28
26
|
/**
|
|
29
27
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
30
28
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -39,6 +37,25 @@ export class TreeMultimapNode<
|
|
|
39
37
|
super(key, value);
|
|
40
38
|
this.count = count;
|
|
41
39
|
}
|
|
40
|
+
|
|
41
|
+
protected _count: number = 1;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The function returns the value of the protected variable _count.
|
|
45
|
+
* @returns The count property of the object, which is of type number.
|
|
46
|
+
*/
|
|
47
|
+
get count(): number {
|
|
48
|
+
return this._count;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The above function sets the value of the count property.
|
|
53
|
+
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
54
|
+
* numeric value.
|
|
55
|
+
*/
|
|
56
|
+
set count(value: number) {
|
|
57
|
+
this._count = value;
|
|
58
|
+
}
|
|
42
59
|
}
|
|
43
60
|
|
|
44
61
|
/**
|
|
@@ -47,19 +64,24 @@ export class TreeMultimapNode<
|
|
|
47
64
|
export class TreeMultimap<
|
|
48
65
|
K = any,
|
|
49
66
|
V = any,
|
|
50
|
-
|
|
51
|
-
TREE extends TreeMultimap<K, V,
|
|
67
|
+
NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
|
|
68
|
+
TREE extends TreeMultimap<K, V, NODE, TREE> = TreeMultimap<K, V, NODE, TreeMultimapNested<K, V, NODE>>
|
|
52
69
|
>
|
|
53
|
-
extends AVLTree<K, V,
|
|
54
|
-
implements IBinaryTree<K, V,
|
|
55
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
70
|
+
extends AVLTree<K, V, NODE, TREE>
|
|
71
|
+
implements IBinaryTree<K, V, NODE, TREE> {
|
|
72
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultimapOptions<K>) {
|
|
56
73
|
super([], options);
|
|
57
74
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
58
75
|
}
|
|
59
76
|
|
|
60
|
-
|
|
77
|
+
protected _count = 0;
|
|
61
78
|
|
|
62
79
|
// TODO the _count is not accurate after nodes count modified
|
|
80
|
+
/**
|
|
81
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
82
|
+
* search.
|
|
83
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
84
|
+
*/
|
|
63
85
|
get count(): number {
|
|
64
86
|
let sum = 0;
|
|
65
87
|
this.dfs(node => (sum += node.count));
|
|
@@ -70,17 +92,26 @@ export class TreeMultimap<
|
|
|
70
92
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
71
93
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
72
94
|
* distinguish one node from another in the tree.
|
|
73
|
-
* @param {
|
|
95
|
+
* @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
|
|
74
96
|
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
75
97
|
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
76
98
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
77
99
|
*/
|
|
78
|
-
override createNode(key: K, value?: V, count?: number):
|
|
79
|
-
return new TreeMultimapNode(key, value, count) as
|
|
100
|
+
override createNode(key: K, value?: V, count?: number): NODE {
|
|
101
|
+
return new TreeMultimapNode(key, value, count) as NODE;
|
|
80
102
|
}
|
|
81
103
|
|
|
104
|
+
/**
|
|
105
|
+
* The function creates a new TreeMultimap object with the specified options and returns it.
|
|
106
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
107
|
+
* configuration options for creating the `TreeMultimap` object. It can include properties such as
|
|
108
|
+
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
109
|
+
* the tree, respectively. These properties can be
|
|
110
|
+
* @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
|
|
111
|
+
* default options. The returned value is casted as `TREE`.
|
|
112
|
+
*/
|
|
82
113
|
override createTree(options?: TreeMultimapOptions<K>): TREE {
|
|
83
|
-
return new TreeMultimap<K, V,
|
|
114
|
+
return new TreeMultimap<K, V, NODE, TREE>([], {
|
|
84
115
|
iterationType: this.iterationType,
|
|
85
116
|
variant: this.variant,
|
|
86
117
|
...options
|
|
@@ -88,18 +119,22 @@ export class TreeMultimap<
|
|
|
88
119
|
}
|
|
89
120
|
|
|
90
121
|
/**
|
|
91
|
-
* The function `
|
|
92
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
122
|
+
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
123
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
|
|
93
124
|
* can be one of the following:
|
|
94
125
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
95
126
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
96
127
|
* it defaults to `undefined`.
|
|
97
128
|
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
98
129
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
99
|
-
* @returns a node of type `
|
|
130
|
+
* @returns a node of type `NODE` or `undefined`.
|
|
100
131
|
*/
|
|
101
|
-
override
|
|
102
|
-
|
|
132
|
+
override keyValueOrEntryToNode(
|
|
133
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
134
|
+
value?: V,
|
|
135
|
+
count = 1
|
|
136
|
+
): NODE | undefined {
|
|
137
|
+
let node: NODE | undefined;
|
|
103
138
|
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
104
139
|
return;
|
|
105
140
|
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
@@ -121,18 +156,17 @@ export class TreeMultimap<
|
|
|
121
156
|
|
|
122
157
|
/**
|
|
123
158
|
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
124
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
159
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
125
160
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
126
161
|
* class.
|
|
127
162
|
*/
|
|
128
|
-
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
163
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
129
164
|
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
130
165
|
}
|
|
131
166
|
|
|
132
167
|
/**
|
|
133
168
|
* Time Complexity: O(log n)
|
|
134
169
|
* Space Complexity: O(1)
|
|
135
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
136
170
|
*/
|
|
137
171
|
|
|
138
172
|
/**
|
|
@@ -151,8 +185,8 @@ export class TreeMultimap<
|
|
|
151
185
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
152
186
|
* was not successful.
|
|
153
187
|
*/
|
|
154
|
-
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
155
|
-
const newNode = this.
|
|
188
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
|
|
189
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
|
|
156
190
|
if (newNode === undefined) return false;
|
|
157
191
|
|
|
158
192
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -164,88 +198,12 @@ export class TreeMultimap<
|
|
|
164
198
|
}
|
|
165
199
|
|
|
166
200
|
/**
|
|
167
|
-
* Time Complexity: O(
|
|
168
|
-
* Space Complexity: O(1)
|
|
169
|
-
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
170
|
-
*/
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Time Complexity: O(k log n)
|
|
174
|
-
* Space Complexity: O(1)
|
|
175
|
-
*
|
|
176
|
-
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
177
|
-
* structure.
|
|
178
|
-
* @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
|
|
179
|
-
* either keys, nodes, or entries.
|
|
180
|
-
* @returns The method is returning an array of type `N | undefined`.
|
|
181
|
-
*/
|
|
182
|
-
override addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[] {
|
|
183
|
-
return super.addMany(keysOrNodesOrEntries);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Time Complexity: O(n log n)
|
|
188
|
-
* Space Complexity: O(n)
|
|
189
|
-
* 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. linear space, as it creates an array to store the sorted nodes.
|
|
190
|
-
*/
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Time Complexity: O(n log n)
|
|
194
|
-
* Space Complexity: O(n)
|
|
195
|
-
*
|
|
196
|
-
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
197
|
-
* tree using either a recursive or iterative approach.
|
|
198
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
199
|
-
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
200
|
-
* values:
|
|
201
|
-
* @returns a boolean value.
|
|
202
|
-
*/
|
|
203
|
-
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
204
|
-
const sorted = this.dfs(node => node, 'in'),
|
|
205
|
-
n = sorted.length;
|
|
206
|
-
if (sorted.length < 1) return false;
|
|
207
|
-
|
|
208
|
-
this.clear();
|
|
209
|
-
|
|
210
|
-
if (iterationType === IterationType.RECURSIVE) {
|
|
211
|
-
const buildBalanceBST = (l: number, r: number) => {
|
|
212
|
-
if (l > r) return;
|
|
213
|
-
const m = l + Math.floor((r - l) / 2);
|
|
214
|
-
const midNode = sorted[m];
|
|
215
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
216
|
-
buildBalanceBST(l, m - 1);
|
|
217
|
-
buildBalanceBST(m + 1, r);
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
buildBalanceBST(0, n - 1);
|
|
221
|
-
return true;
|
|
222
|
-
} else {
|
|
223
|
-
const stack: [[number, number]] = [[0, n - 1]];
|
|
224
|
-
while (stack.length > 0) {
|
|
225
|
-
const popped = stack.pop();
|
|
226
|
-
if (popped) {
|
|
227
|
-
const [l, r] = popped;
|
|
228
|
-
if (l <= r) {
|
|
229
|
-
const m = l + Math.floor((r - l) / 2);
|
|
230
|
-
const midNode = sorted[m];
|
|
231
|
-
this.add(midNode.key, midNode.value, midNode.count);
|
|
232
|
-
stack.push([m + 1, r]);
|
|
233
|
-
stack.push([l, m - 1]);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Time Complexity: O(k log n)
|
|
201
|
+
* Time Complexity: O(log n)
|
|
243
202
|
* Space Complexity: O(1)
|
|
244
|
-
* 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. constant space, as it doesn't use additional data structures that scale with input size.
|
|
245
203
|
*/
|
|
246
204
|
|
|
247
205
|
/**
|
|
248
|
-
* Time Complexity: O(
|
|
206
|
+
* Time Complexity: O(log n)
|
|
249
207
|
* Space Complexity: O(1)
|
|
250
208
|
*
|
|
251
209
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
@@ -261,22 +219,22 @@ export class TreeMultimap<
|
|
|
261
219
|
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
262
220
|
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
263
221
|
* decremented by 1 and
|
|
264
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
222
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
265
223
|
*/
|
|
266
|
-
override delete<C extends BTNCallback<
|
|
224
|
+
override delete<C extends BTNCallback<NODE>>(
|
|
267
225
|
identifier: ReturnType<C>,
|
|
268
226
|
callback: C = this._defaultOneParamCallback as C,
|
|
269
227
|
ignoreCount = false
|
|
270
|
-
): BinaryTreeDeleteResult<
|
|
271
|
-
const deletedResult: BinaryTreeDeleteResult<
|
|
228
|
+
): BinaryTreeDeleteResult<NODE>[] {
|
|
229
|
+
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
272
230
|
if (!this.root) return deletedResult;
|
|
273
231
|
|
|
274
|
-
const curr:
|
|
232
|
+
const curr: NODE | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
275
233
|
if (!curr) return deletedResult;
|
|
276
234
|
|
|
277
|
-
const parent:
|
|
278
|
-
let needBalanced:
|
|
279
|
-
orgCurrent:
|
|
235
|
+
const parent: NODE | undefined = curr?.parent ? curr.parent : undefined;
|
|
236
|
+
let needBalanced: NODE | undefined = undefined,
|
|
237
|
+
orgCurrent: NODE | undefined = curr;
|
|
280
238
|
|
|
281
239
|
if (curr.count > 1 && !ignoreCount) {
|
|
282
240
|
curr.count--;
|
|
@@ -339,6 +297,60 @@ export class TreeMultimap<
|
|
|
339
297
|
this._count = 0;
|
|
340
298
|
}
|
|
341
299
|
|
|
300
|
+
/**
|
|
301
|
+
* Time Complexity: O(n log n)
|
|
302
|
+
* Space Complexity: O(log n)
|
|
303
|
+
*/
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Time Complexity: O(n log n)
|
|
307
|
+
* Space Complexity: O(log n)
|
|
308
|
+
*
|
|
309
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
310
|
+
* tree using either a recursive or iterative approach.
|
|
311
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
312
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
313
|
+
* values:
|
|
314
|
+
* @returns a boolean value.
|
|
315
|
+
*/
|
|
316
|
+
override perfectlyBalance(iterationType = this.iterationType): boolean {
|
|
317
|
+
const sorted = this.dfs(node => node, 'in'),
|
|
318
|
+
n = sorted.length;
|
|
319
|
+
if (sorted.length < 1) return false;
|
|
320
|
+
|
|
321
|
+
this.clear();
|
|
322
|
+
|
|
323
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
324
|
+
const buildBalanceBST = (l: number, r: number) => {
|
|
325
|
+
if (l > r) return;
|
|
326
|
+
const m = l + Math.floor((r - l) / 2);
|
|
327
|
+
const midNode = sorted[m];
|
|
328
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
329
|
+
buildBalanceBST(l, m - 1);
|
|
330
|
+
buildBalanceBST(m + 1, r);
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
buildBalanceBST(0, n - 1);
|
|
334
|
+
return true;
|
|
335
|
+
} else {
|
|
336
|
+
const stack: [[number, number]] = [[0, n - 1]];
|
|
337
|
+
while (stack.length > 0) {
|
|
338
|
+
const popped = stack.pop();
|
|
339
|
+
if (popped) {
|
|
340
|
+
const [l, r] = popped;
|
|
341
|
+
if (l <= r) {
|
|
342
|
+
const m = l + Math.floor((r - l) / 2);
|
|
343
|
+
const midNode = sorted[m];
|
|
344
|
+
this.add(midNode.key, midNode.value, midNode.count);
|
|
345
|
+
stack.push([m + 1, r]);
|
|
346
|
+
stack.push([l, m - 1]);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
342
354
|
/**
|
|
343
355
|
* Time complexity: O(n)
|
|
344
356
|
* Space complexity: O(n)
|
|
@@ -359,14 +371,17 @@ export class TreeMultimap<
|
|
|
359
371
|
|
|
360
372
|
/**
|
|
361
373
|
* The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
|
|
362
|
-
* @param {K |
|
|
363
|
-
* which the values will be swapped. It can be of type `K`, `
|
|
364
|
-
* @param {K |
|
|
374
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
|
|
375
|
+
* which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
|
|
376
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
365
377
|
* node where the values from the source node will be swapped to.
|
|
366
378
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
367
379
|
* if either `srcNode` or `destNode` is undefined.
|
|
368
380
|
*/
|
|
369
|
-
protected override _swapProperties(
|
|
381
|
+
protected override _swapProperties(
|
|
382
|
+
srcNode: BSTNKeyOrNode<K, NODE>,
|
|
383
|
+
destNode: BSTNKeyOrNode<K, NODE>
|
|
384
|
+
): NODE | undefined {
|
|
370
385
|
srcNode = this.ensureNode(srcNode);
|
|
371
386
|
destNode = this.ensureNode(destNode);
|
|
372
387
|
if (srcNode && destNode) {
|
|
@@ -391,7 +406,15 @@ export class TreeMultimap<
|
|
|
391
406
|
return undefined;
|
|
392
407
|
}
|
|
393
408
|
|
|
394
|
-
|
|
409
|
+
/**
|
|
410
|
+
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
411
|
+
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
|
|
412
|
+
* needs to be replaced in a data structure.
|
|
413
|
+
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
|
|
414
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
415
|
+
* superclass, after updating the `count` property of the `newNode` object.
|
|
416
|
+
*/
|
|
417
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
395
418
|
newNode.count = oldNode.count + newNode.count;
|
|
396
419
|
return super._replaceNode(oldNode, newNode);
|
|
397
420
|
}
|