data-structure-typed 1.49.5 → 1.49.6
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/CHANGELOG.md +1 -1
- package/README.md +14 -23
- package/benchmark/report.html +14 -23
- package/benchmark/report.json +163 -256
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/cjs/types/common.d.ts +3 -3
- package/dist/cjs/types/common.js +2 -2
- package/dist/cjs/types/common.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
- package/dist/mjs/types/common.d.ts +3 -3
- package/dist/mjs/types/common.js +2 -2
- package/dist/umd/data-structure-typed.js +497 -419
- 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 +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
- package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
- package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
- package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
- package/test/performance/data-structures/heap/heap.test.ts +5 -18
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
- package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
- package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
- package/test/performance/data-structures/queue/queue.test.ts +8 -25
- package/test/performance/data-structures/stack/stack.test.ts +6 -18
- package/test/performance/data-structures/trie/trie.test.ts +2 -6
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
- package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
- package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
- package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
- package/test/performance/data-structures/matrix/vector2d.test.ts +0 -0
|
@@ -21,10 +21,10 @@ 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
|
-
constructor(
|
|
24
|
+
constructor(nodes, options) {
|
|
25
25
|
super([], options);
|
|
26
|
-
if (
|
|
27
|
-
this.addMany(
|
|
26
|
+
if (nodes)
|
|
27
|
+
this.addMany(nodes);
|
|
28
28
|
}
|
|
29
29
|
_count = 0;
|
|
30
30
|
// TODO the _count is not accurate after nodes count modified
|
|
@@ -53,26 +53,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* The function
|
|
57
|
-
* @param
|
|
58
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
59
|
-
* class.
|
|
60
|
-
*/
|
|
61
|
-
isNode(exemplar) {
|
|
62
|
-
return exemplar instanceof TreeMultimapNode;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
66
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
67
|
-
* data type.
|
|
68
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
69
|
-
*/
|
|
70
|
-
isNotNodeInstance(potentialKey) {
|
|
71
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
75
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
56
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
57
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
76
58
|
* can be one of the following:
|
|
77
59
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
78
60
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -81,16 +63,16 @@ export class TreeMultimap extends AVLTree {
|
|
|
81
63
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
82
64
|
* @returns a node of type `N` or `undefined`.
|
|
83
65
|
*/
|
|
84
|
-
exemplarToNode(
|
|
66
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
85
67
|
let node;
|
|
86
|
-
if (
|
|
68
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
87
69
|
return;
|
|
88
70
|
}
|
|
89
|
-
else if (this.isNode(
|
|
90
|
-
node =
|
|
71
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
72
|
+
node = keyOrNodeOrEntry;
|
|
91
73
|
}
|
|
92
|
-
else if (this.isEntry(
|
|
93
|
-
const [key, value] =
|
|
74
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
75
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
94
76
|
if (key === undefined || key === null) {
|
|
95
77
|
return;
|
|
96
78
|
}
|
|
@@ -98,8 +80,8 @@ export class TreeMultimap extends AVLTree {
|
|
|
98
80
|
node = this.createNode(key, value, count);
|
|
99
81
|
}
|
|
100
82
|
}
|
|
101
|
-
else if (this.isNotNodeInstance(
|
|
102
|
-
node = this.createNode(
|
|
83
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
84
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
103
85
|
}
|
|
104
86
|
else {
|
|
105
87
|
return;
|
|
@@ -107,12 +89,31 @@ export class TreeMultimap extends AVLTree {
|
|
|
107
89
|
return node;
|
|
108
90
|
}
|
|
109
91
|
/**
|
|
110
|
-
*
|
|
111
|
-
*
|
|
92
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
93
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
94
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
95
|
+
* class.
|
|
112
96
|
*/
|
|
97
|
+
isNode(keyOrNodeOrEntry) {
|
|
98
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
99
|
+
}
|
|
113
100
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
101
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
102
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
103
|
+
* data type.
|
|
104
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
105
|
+
*/
|
|
106
|
+
isNotNodeInstance(potentialKey) {
|
|
107
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Time Complexity: O(log n)
|
|
111
|
+
* Space Complexity: O(1)
|
|
112
|
+
* 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.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* Time Complexity: O(log n)
|
|
116
|
+
* Space Complexity: O(1)
|
|
116
117
|
*
|
|
117
118
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
118
119
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -129,21 +130,22 @@ export class TreeMultimap extends AVLTree {
|
|
|
129
130
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
130
131
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
131
132
|
if (newNode === undefined)
|
|
132
|
-
return;
|
|
133
|
+
return false;
|
|
133
134
|
const orgNodeCount = newNode?.count || 0;
|
|
134
135
|
const inserted = super.add(newNode);
|
|
135
136
|
if (inserted) {
|
|
136
137
|
this._count += orgNodeCount;
|
|
137
138
|
}
|
|
138
|
-
return
|
|
139
|
+
return true;
|
|
139
140
|
}
|
|
140
141
|
/**
|
|
141
|
-
* Time Complexity: O(k log n)
|
|
142
|
-
* Space Complexity: O(1)
|
|
142
|
+
* Time Complexity: O(k log n)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
|
+
* 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.
|
|
143
145
|
*/
|
|
144
146
|
/**
|
|
145
|
-
* Time Complexity: O(k log n)
|
|
146
|
-
* Space Complexity: O(1)
|
|
147
|
+
* Time Complexity: O(k log n)
|
|
148
|
+
* Space Complexity: O(1)
|
|
147
149
|
*
|
|
148
150
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
149
151
|
* structure.
|
|
@@ -155,12 +157,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
155
157
|
return super.addMany(keysOrNodesOrEntries);
|
|
156
158
|
}
|
|
157
159
|
/**
|
|
158
|
-
* Time Complexity: O(
|
|
159
|
-
* Space Complexity: O(
|
|
160
|
+
* Time Complexity: O(n log n)
|
|
161
|
+
* Space Complexity: O(n)
|
|
162
|
+
* 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.
|
|
160
163
|
*/
|
|
161
164
|
/**
|
|
162
|
-
* Time Complexity: O(n log n)
|
|
163
|
-
* Space Complexity: O(n)
|
|
165
|
+
* Time Complexity: O(n log n)
|
|
166
|
+
* Space Complexity: O(n)
|
|
164
167
|
*
|
|
165
168
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
166
169
|
* tree using either a recursive or iterative approach.
|
|
@@ -206,12 +209,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
206
209
|
}
|
|
207
210
|
}
|
|
208
211
|
/**
|
|
209
|
-
* Time Complexity: O(k log n)
|
|
210
|
-
* Space Complexity: O(1)
|
|
212
|
+
* Time Complexity: O(k log n)
|
|
213
|
+
* Space Complexity: O(1)
|
|
214
|
+
* 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.
|
|
211
215
|
*/
|
|
212
216
|
/**
|
|
213
|
-
* Time Complexity: O(log n)
|
|
214
|
-
* Space Complexity: O(1)
|
|
217
|
+
* Time Complexity: O(k log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
215
219
|
*
|
|
216
220
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
217
221
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -286,10 +290,13 @@ export class TreeMultimap extends AVLTree {
|
|
|
286
290
|
return deletedResult;
|
|
287
291
|
}
|
|
288
292
|
/**
|
|
289
|
-
* Time Complexity: O(
|
|
290
|
-
* Space Complexity: O(
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
291
295
|
*/
|
|
292
296
|
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
293
300
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
294
301
|
*/
|
|
295
302
|
clear() {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback,
|
|
2
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } from '../types';
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry:
|
|
7
|
-
addMany(nodes: Iterable<
|
|
6
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
7
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
9
9
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare enum BSTVariant {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
STANDARD = "STANDARD",
|
|
3
|
+
INVERSE = "INVERSE"
|
|
4
4
|
}
|
|
5
5
|
export declare enum CP {
|
|
6
6
|
lt = "lt",
|
|
@@ -44,7 +44,7 @@ export type BinaryTreePrintOptions = {
|
|
|
44
44
|
};
|
|
45
45
|
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
46
46
|
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
47
|
-
export type
|
|
47
|
+
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
48
48
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
49
49
|
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
50
50
|
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
package/dist/mjs/types/common.js
CHANGED