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.
Files changed (68) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +14 -23
  3. package/benchmark/report.html +14 -23
  4. package/benchmark/report.json +163 -256
  5. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  6. package/dist/cjs/data-structures/binary-tree/avl-tree.js +55 -49
  7. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +192 -149
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +83 -71
  12. package/dist/cjs/data-structures/binary-tree/bst.js +113 -89
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js +62 -59
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  18. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +58 -51
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  20. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  21. package/dist/cjs/types/common.d.ts +3 -3
  22. package/dist/cjs/types/common.js +2 -2
  23. package/dist/cjs/types/common.js.map +1 -1
  24. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +53 -48
  25. package/dist/mjs/data-structures/binary-tree/avl-tree.js +55 -49
  26. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +153 -130
  27. package/dist/mjs/data-structures/binary-tree/binary-tree.js +192 -149
  28. package/dist/mjs/data-structures/binary-tree/bst.d.ts +83 -71
  29. package/dist/mjs/data-structures/binary-tree/bst.js +113 -89
  30. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +37 -35
  31. package/dist/mjs/data-structures/binary-tree/rb-tree.js +62 -59
  32. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +46 -39
  33. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +58 -51
  34. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  35. package/dist/mjs/types/common.d.ts +3 -3
  36. package/dist/mjs/types/common.js +2 -2
  37. package/dist/umd/data-structure-typed.js +497 -419
  38. package/dist/umd/data-structure-typed.min.js +2 -2
  39. package/dist/umd/data-structure-typed.min.js.map +1 -1
  40. package/package.json +1 -1
  41. package/src/data-structures/binary-tree/avl-tree.ts +58 -53
  42. package/src/data-structures/binary-tree/binary-tree.ts +253 -205
  43. package/src/data-structures/binary-tree/bst.ts +125 -104
  44. package/src/data-structures/binary-tree/rb-tree.ts +66 -64
  45. package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
  46. package/src/interfaces/binary-tree.ts +3 -3
  47. package/src/types/common.ts +3 -3
  48. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -12
  49. package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +37 -0
  50. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +6 -16
  51. package/test/performance/data-structures/binary-tree/bst.test.ts +5 -13
  52. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +5 -15
  53. package/test/performance/data-structures/comparison/comparison.test.ts +13 -36
  54. package/test/performance/data-structures/graph/directed-graph.test.ts +3 -14
  55. package/test/performance/data-structures/hash/hash-map.test.ts +11 -34
  56. package/test/performance/data-structures/heap/heap.test.ts +5 -18
  57. package/test/performance/data-structures/linked-list/doubly-linked-list.test.ts +0 -1
  58. package/test/performance/data-structures/linked-list/singly-linked-list.test.ts +0 -2
  59. package/test/performance/data-structures/priority-queue/max-priority-queue.test.ts +2 -4
  60. package/test/performance/data-structures/priority-queue/priority-queue.test.ts +4 -14
  61. package/test/performance/data-structures/queue/queue.test.ts +8 -25
  62. package/test/performance/data-structures/stack/stack.test.ts +6 -18
  63. package/test/performance/data-structures/trie/trie.test.ts +2 -6
  64. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +6 -5
  65. package/test/unit/data-structures/binary-tree/bst.test.ts +17 -1
  66. package/test/performance/data-structures/binary-tree/overall.test.ts +0 -0
  67. package/test/performance/data-structures/matrix/matrix2d.test.ts +0 -0
  68. 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(elements, options) {
24
+ constructor(nodes, options) {
25
25
  super([], options);
26
- if (elements)
27
- this.addMany(elements);
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 checks if an exemplar is an instance of the TreeMultimapNode class.
57
- * @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
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(exemplar, value, count = 1) {
66
+ exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
85
67
  let node;
86
- if (exemplar === undefined || exemplar === null) {
68
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
87
69
  return;
88
70
  }
89
- else if (this.isNode(exemplar)) {
90
- node = exemplar;
71
+ else if (this.isNode(keyOrNodeOrEntry)) {
72
+ node = keyOrNodeOrEntry;
91
73
  }
92
- else if (this.isEntry(exemplar)) {
93
- const [key, value] = exemplar;
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(exemplar)) {
102
- node = this.createNode(exemplar, value, count);
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
- * 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.
111
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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
- * 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.
115
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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 inserted;
139
+ return true;
139
140
  }
140
141
  /**
141
- * 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.
142
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
146
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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(1) - constant time, as it performs basic pointer assignments.
159
- * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
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) - 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.
163
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
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) - 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.
210
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
214
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
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(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.
290
- * Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
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, BTNExemplar } from '../types';
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: BTNExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
7
- addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
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
- MIN = "MIN",
3
- MAX = "MAX"
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 BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
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;
@@ -1,7 +1,7 @@
1
1
  export var BSTVariant;
2
2
  (function (BSTVariant) {
3
- BSTVariant["MIN"] = "MIN";
4
- BSTVariant["MAX"] = "MAX";
3
+ BSTVariant["STANDARD"] = "STANDARD";
4
+ BSTVariant["INVERSE"] = "INVERSE";
5
5
  })(BSTVariant || (BSTVariant = {}));
6
6
  export var CP;
7
7
  (function (CP) {