data-structure-typed 1.51.8 → 1.51.9

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 (106) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +13 -13
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +13 -385
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
  9. package/dist/cjs/data-structures/binary-tree/avl-tree.js +78 -59
  10. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
  12. package/dist/cjs/data-structures/binary-tree/binary-tree.js +471 -361
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/bst.d.ts +198 -200
  15. package/dist/cjs/data-structures/binary-tree/bst.js +215 -249
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
  18. package/dist/cjs/data-structures/binary-tree/rb-tree.js +107 -98
  19. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  21. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  22. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  23. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +10 -15
  24. package/dist/cjs/data-structures/graph/abstract-graph.js +10 -15
  25. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/hash/hash-map.d.ts +31 -38
  27. package/dist/cjs/data-structures/hash/hash-map.js +40 -55
  28. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  29. package/dist/cjs/data-structures/queue/deque.d.ts +2 -3
  30. package/dist/cjs/data-structures/queue/deque.js +2 -3
  31. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  33. package/dist/cjs/data-structures/trie/trie.d.ts +1 -1
  34. package/dist/cjs/data-structures/trie/trie.js +1 -1
  35. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  36. package/dist/cjs/interfaces/binary-tree.d.ts +6 -6
  37. package/dist/cjs/types/common.d.ts +1 -2
  38. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  39. package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  40. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  41. package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +4 -4
  42. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  43. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  44. package/dist/cjs/utils/utils.js +3 -5
  45. package/dist/cjs/utils/utils.js.map +1 -1
  46. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
  47. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
  48. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +80 -60
  49. package/dist/mjs/data-structures/binary-tree/avl-tree.js +78 -59
  50. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +316 -224
  51. package/dist/mjs/data-structures/binary-tree/binary-tree.js +472 -362
  52. package/dist/mjs/data-structures/binary-tree/bst.d.ts +198 -200
  53. package/dist/mjs/data-structures/binary-tree/bst.js +219 -255
  54. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +71 -72
  55. package/dist/mjs/data-structures/binary-tree/rb-tree.js +107 -98
  56. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
  57. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +105 -93
  58. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +10 -15
  59. package/dist/mjs/data-structures/graph/abstract-graph.js +10 -15
  60. package/dist/mjs/data-structures/hash/hash-map.d.ts +31 -38
  61. package/dist/mjs/data-structures/hash/hash-map.js +41 -55
  62. package/dist/mjs/data-structures/queue/deque.d.ts +2 -3
  63. package/dist/mjs/data-structures/queue/deque.js +2 -3
  64. package/dist/mjs/data-structures/trie/trie.d.ts +1 -1
  65. package/dist/mjs/data-structures/trie/trie.js +1 -1
  66. package/dist/mjs/interfaces/binary-tree.d.ts +6 -6
  67. package/dist/mjs/types/common.d.ts +1 -2
  68. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
  69. package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
  70. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
  71. package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +4 -4
  72. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
  73. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
  74. package/dist/mjs/utils/utils.js +3 -5
  75. package/dist/umd/data-structure-typed.js +1340 -1181
  76. package/dist/umd/data-structure-typed.min.js +2 -2
  77. package/dist/umd/data-structure-typed.min.js.map +1 -1
  78. package/package.json +6 -6
  79. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
  80. package/src/data-structures/binary-tree/avl-tree.ts +94 -66
  81. package/src/data-structures/binary-tree/binary-tree.ts +530 -398
  82. package/src/data-structures/binary-tree/bst.ts +251 -270
  83. package/src/data-structures/binary-tree/rb-tree.ts +121 -100
  84. package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
  85. package/src/data-structures/graph/abstract-graph.ts +10 -10
  86. package/src/data-structures/hash/hash-map.ts +42 -49
  87. package/src/data-structures/queue/deque.ts +2 -2
  88. package/src/data-structures/queue/queue.ts +1 -1
  89. package/src/data-structures/trie/trie.ts +2 -2
  90. package/src/interfaces/binary-tree.ts +8 -7
  91. package/src/types/common.ts +1 -2
  92. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
  93. package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
  95. package/src/types/data-structures/binary-tree/bst.ts +4 -4
  96. package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
  97. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
  98. package/src/utils/utils.ts +3 -3
  99. package/test/integration/avl-tree.test.ts +2 -2
  100. package/test/integration/bst.test.ts +3 -3
  101. package/test/integration/index.html +25 -11
  102. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +68 -0
  103. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +56 -0
  104. package/test/unit/data-structures/graph/directed-graph.test.ts +2 -2
  105. package/test/utils/big-o.ts +12 -6
  106. package/tsconfig-base.json +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.51.8",
3
+ "version": "1.51.9",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -66,11 +66,11 @@
66
66
  "@typescript-eslint/eslint-plugin": "^6.7.4",
67
67
  "@typescript-eslint/parser": "^6.7.4",
68
68
  "auto-changelog": "^2.4.0",
69
- "avl-tree-typed": "^1.51.7",
69
+ "avl-tree-typed": "^1.51.8",
70
70
  "benchmark": "^2.1.4",
71
- "binary-tree-typed": "^1.51.7",
72
- "bst-typed": "^1.51.7",
73
- "data-structure-typed": "^1.51.7",
71
+ "binary-tree-typed": "^1.51.8",
72
+ "bst-typed": "^1.51.8",
73
+ "data-structure-typed": "^1.51.8",
74
74
  "dependency-cruiser": "^14.1.0",
75
75
  "doctoc": "^2.2.1",
76
76
  "eslint": "^8.50.0",
@@ -79,7 +79,7 @@
79
79
  "eslint-import-resolver-typescript": "^3.6.1",
80
80
  "eslint-plugin-import": "^2.28.1",
81
81
  "fast-glob": "^3.3.1",
82
- "heap-typed": "^1.51.7",
82
+ "heap-typed": "^1.51.8",
83
83
  "istanbul-badges-readme": "^1.8.5",
84
84
  "jest": "^29.7.0",
85
85
  "js-sdsl": "^4.4.2",
@@ -16,6 +16,7 @@ import type {
16
16
  IterationType,
17
17
  KeyOrNodeOrEntry
18
18
  } from '../../types';
19
+ import { BTNEntry } from '../../types';
19
20
  import { IBinaryTree } from '../../interfaces';
20
21
  import { AVLTree, AVLTreeNode } from './avl-tree';
21
22
 
@@ -65,19 +66,36 @@ export class AVLTreeMultiMapNode<
65
66
  export class AVLTreeMultiMap<
66
67
  K extends Comparable,
67
68
  V = any,
69
+ R = BTNEntry<K, V>,
68
70
  NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
69
- TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
71
+ TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
72
+ K,
73
+ V,
74
+ R,
75
+ NODE,
76
+ AVLTreeMultiMapNested<K, V, R, NODE>
77
+ >
70
78
  >
71
- extends AVLTree<K, V, NODE, TREE>
72
- implements IBinaryTree<K, V, NODE, TREE> {
73
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
79
+ extends AVLTree<K, V, R, NODE, TREE>
80
+ implements IBinaryTree<K, V, R, NODE, TREE> {
81
+ /**
82
+ * The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
83
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
84
+ * iterable object that can contain either keys, nodes, entries, or raw elements.
85
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
86
+ * behavior of the AVLTreeMultiMap. It can include properties such as `compareKeys` and
87
+ * `compareValues` functions to define custom comparison logic for keys and values, respectively.
88
+ */
89
+ constructor(
90
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
91
+ options?: AVLTreeMultiMapOptions<K, V, R>
92
+ ) {
74
93
  super([], options);
75
- if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
94
+ if (keysOrNodesOrEntriesOrRawElements) this.addMany(keysOrNodesOrEntriesOrRawElements);
76
95
  }
77
96
 
78
97
  protected _count = 0;
79
98
 
80
- // TODO the _count is not accurate after nodes count modified
81
99
  /**
82
100
  * The function calculates the sum of the count property of all nodes in a tree using depth-first
83
101
  * search.
@@ -107,20 +125,29 @@ export class AVLTreeMultiMap<
107
125
  }
108
126
 
109
127
  /**
110
- * The function creates a new BSTNode with the given key, value, and count.
111
- * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
112
- * distinguish one node from another in the tree.
113
- * @param {NODE} value - The `value` parameter represents the value that will be stored in the binary search tree node.
114
- * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
115
- * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
116
- * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
128
+ * The function creates a new AVLTreeMultiMapNode with the specified key, value, and count.
129
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
130
+ * which is a generic type that can be replaced with any specific type when using the function.
131
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
132
+ * associated with the key in the node. It is of type `V`, which can be any data type.
133
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a
134
+ * key-value pair in the AVLTreeMultiMapNode. It is an optional parameter, so it can be omitted when
135
+ * calling the `createNode` method. If provided, it specifies the initial count for the node.
136
+ * @returns a new instance of the AVLTreeMultiMapNode class, casted as NODE.
117
137
  */
118
138
  override createNode(key: K, value?: V, count?: number): NODE {
119
139
  return new AVLTreeMultiMapNode(key, value, count) as NODE;
120
140
  }
121
141
 
122
- override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
123
- return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
142
+ /**
143
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
144
+ * @param [options] - The `options` parameter is an optional object that contains additional
145
+ * configuration options for creating the AVLTreeMultiMap. It can have the following properties:
146
+ * @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
147
+ * object.
148
+ */
149
+ override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
150
+ return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
124
151
  iterationType: this.iterationType,
125
152
  comparator: this.comparator,
126
153
  ...options
@@ -128,49 +155,52 @@ export class AVLTreeMultiMap<
128
155
  }
129
156
 
130
157
  /**
131
- * The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
132
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
133
- * can be one of the following:
134
- * @param {V} [value] - The `value` parameter is an optional argument that represents the value
135
- * associated with the node. It is of type `V`, which can be any data type. If no value is provided,
136
- * it defaults to `undefined`.
158
+ * The function checks if the input is an instance of AVLTreeMultiMapNode.
159
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
160
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
161
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
162
+ * an instance of the `AVLTreeMultiMapNode` class.
163
+ */
164
+ override isNode(
165
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
166
+ ): keyOrNodeOrEntryOrRawElement is NODE {
167
+ return keyOrNodeOrEntryOrRawElement instanceof AVLTreeMultiMapNode;
168
+ }
169
+
170
+ /**
171
+ * The function `keyValueOrEntryOrRawElementToNode` converts a key, value, entry, or raw element into
172
+ * a node object.
173
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
174
+ * `keyOrNodeOrEntryOrRawElement` parameter can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
175
+ * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
176
+ * `override` function. It represents the value associated with the key in the data structure. If no
177
+ * value is provided, it will default to `undefined`.
137
178
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
138
- * times the value should be added to the node. If not provided, it defaults to 1.
139
- * @returns a node of type `NODE` or `undefined`.
179
+ * times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
180
+ * @returns either a NODE object or undefined.
140
181
  */
141
- override keyValueOrEntryToNode(
142
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
182
+ override keyValueOrEntryOrRawElementToNode(
183
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>,
143
184
  value?: V,
144
185
  count = 1
145
186
  ): NODE | undefined {
146
- let node: NODE | undefined;
147
- if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
148
- return;
149
- } else if (this.isNode(keyOrNodeOrEntry)) {
150
- node = keyOrNodeOrEntry;
151
- } else if (this.isEntry(keyOrNodeOrEntry)) {
152
- const [key, value] = keyOrNodeOrEntry;
153
- if (key === undefined || key === null) {
154
- return;
155
- } else {
156
- node = this.createNode(key, value, count);
157
- }
158
- } else if (!this.isNode(keyOrNodeOrEntry)) {
159
- node = this.createNode(keyOrNodeOrEntry, value, count);
160
- } else {
161
- return;
187
+ if (keyOrNodeOrEntryOrRawElement === undefined || keyOrNodeOrEntryOrRawElement === null) return;
188
+ if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
189
+
190
+ if (this.toEntryFn) {
191
+ const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
192
+ if (key) return this.createNode(key, entryValue ?? value, count);
162
193
  }
163
- return node;
164
- }
165
194
 
166
- /**
167
- * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
168
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
169
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
170
- * class.
171
- */
172
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
173
- return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
195
+ if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
196
+ const [key, value] = keyOrNodeOrEntryOrRawElement;
197
+ if (key === undefined || key === null) return;
198
+ else return this.createNode(key, value, count);
199
+ }
200
+
201
+ if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, count);
202
+
203
+ return;
174
204
  }
175
205
 
176
206
  /**
@@ -182,20 +212,21 @@ export class AVLTreeMultiMap<
182
212
  * Time Complexity: O(log n)
183
213
  * Space Complexity: O(1)
184
214
  *
185
- * The function overrides the add method of a binary tree node and adds a new node to the tree.
186
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
187
- * entry. It represents the key, node, or entry that you want to add to the binary tree.
215
+ * The function overrides the add method of a TypeScript class to add a new node to a data structure
216
+ * and update the count.
217
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The
218
+ * `keyOrNodeOrEntryOrRawElement` parameter can accept a value of type `R`, which can be any type. It
219
+ * can also accept a value of type `KeyOrNodeOrEntry<K, V, NODE>`, which represents a key, node,
220
+ * entry, or raw element
188
221
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
189
- * binary tree node. It is an optional parameter, meaning it can be omitted when calling the `add`
190
- * method.
222
+ * data structure. It is an optional parameter, so it can be omitted if not needed.
191
223
  * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
192
- * be added to the binary tree. By default, it is set to 1, meaning that the key-value pair will be
193
- * added once. However, you can specify a different value for `count` if you want to add
194
- * @returns The method is returning either the newly inserted node or `undefined` if the insertion
195
- * was not successful.
224
+ * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
225
+ * be added once. However, you can specify a different value for `count` if you want to add
226
+ * @returns a boolean value.
196
227
  */
197
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
198
- const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
228
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
229
+ const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value, count);
199
230
  if (newNode === undefined) return false;
200
231
 
201
232
  const orgNodeCount = newNode?.count || 0;
@@ -215,19 +246,19 @@ export class AVLTreeMultiMap<
215
246
  * Time Complexity: O(log n)
216
247
  * Space Complexity: O(1)
217
248
  *
218
- * The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
219
- * account the count of the node and balancing the tree if necessary.
220
- * @param identifier - The identifier is the value or key that is used to identify the node that
221
- * needs to be deleted from the binary tree. It can be of any type that is returned by the callback
249
+ * The `delete` function in a binary tree data structure deletes a node based on its identifier and
250
+ * returns the deleted node along with the parent node that needs to be balanced.
251
+ * @param identifier - The identifier parameter is the value used to identify the node that needs to
252
+ * be deleted from the binary tree. It can be of any type and is the return type of the callback
222
253
  * function.
223
- * @param {C} callback - The `callback` parameter is a function that is used to determine if a node
224
- * should be deleted. It is optional and defaults to a default callback function. The `callback`
225
- * function takes one parameter, which is the identifier of the node, and returns a value that is
226
- * used to identify the node to
254
+ * @param {C} callback - The `callback` parameter is a function that is used to determine the
255
+ * equality of nodes in the binary tree. It is optional and has a default value of
256
+ * `this._DEFAULT_CALLBACK`. The `callback` function takes a single argument, which is the identifier
257
+ * of a node, and returns a value that
227
258
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
228
259
  * being deleted. If set to true, the count of the node will not be considered and the node will be
229
- * deleted regardless of its count. If set to false (default), the count of the node will be
230
- * decremented by 1 and
260
+ * deleted regardless of its count. If set to false (default), the count of the node will be taken
261
+ * into account and the node
231
262
  * @returns an array of `BinaryTreeDeleteResult<NODE>`.
232
263
  */
233
264
  override delete<C extends BTNCallback<NODE>>(
@@ -300,7 +331,8 @@ export class AVLTreeMultiMap<
300
331
  * Time Complexity: O(1)
301
332
  * Space Complexity: O(1)
302
333
  *
303
- * The clear() function clears the contents of a data structure and sets the count to zero.
334
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
335
+ * zero.
304
336
  */
305
337
  override clear() {
306
338
  super.clear();
@@ -315,13 +347,14 @@ export class AVLTreeMultiMap<
315
347
  /**
316
348
  * Time Complexity: O(n log n)
317
349
  * Space Complexity: O(log n)
318
- *
319
350
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
320
351
  * tree using either a recursive or iterative approach.
321
- * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
322
- * type of iteration to use when building the balanced binary search tree. It can have two possible
323
- * values:
324
- * @returns a boolean value.
352
+ * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
353
+ * specifies the type of iteration to use when building the balanced binary search tree. It has a
354
+ * default value of `this.iterationType`, which means it will use the iteration type currently set in
355
+ * the object.
356
+ * @returns The function `perfectlyBalance` returns a boolean value. It returns `true` if the
357
+ * balancing operation is successful, and `false` if there are no nodes to balance.
325
358
  */
326
359
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
327
360
  const sorted = this.dfs(node => node, 'IN'),
@@ -370,7 +403,7 @@ export class AVLTreeMultiMap<
370
403
  * Time complexity: O(n)
371
404
  * Space complexity: O(n)
372
405
  *
373
- * The `clone` function creates a deep copy of a tree object.
406
+ * The function overrides the clone method to create a deep copy of a tree object.
374
407
  * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
375
408
  */
376
409
  override clone(): TREE {
@@ -380,17 +413,26 @@ export class AVLTreeMultiMap<
380
413
  }
381
414
 
382
415
  /**
383
- * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
384
- * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node from
385
- * which the values will be swapped. It can be of type `K`, `NODE`, or `undefined`.
386
- * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
387
- * node where the values from the source node will be swapped to.
388
- * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
389
- * if either `srcNode` or `destNode` is undefined.
416
+ * Time Complexity: O(1)
417
+ * Space Complexity: O(1)
418
+ */
419
+
420
+ /**
421
+ * Time Complexity: O(1)
422
+ * Space Complexity: O(1)
423
+ *
424
+ * The `_swapProperties` function swaps the properties (key, value, count, height) between two nodes
425
+ * in a binary search tree.
426
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents the source node
427
+ * that will be swapped with the `destNode`.
428
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter represents the destination
429
+ * node where the properties will be swapped with the source node.
430
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
431
+ * If either `srcNode` or `destNode` is undefined, it returns `undefined`.
390
432
  */
391
433
  protected override _swapProperties(
392
- srcNode: BSTNKeyOrNode<K, NODE>,
393
- destNode: BSTNKeyOrNode<K, NODE>
434
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
435
+ destNode: R | BSTNKeyOrNode<K, NODE>
394
436
  ): NODE | undefined {
395
437
  srcNode = this.ensureNode(srcNode);
396
438
  destNode = this.ensureNode(destNode);
@@ -417,12 +459,20 @@ export class AVLTreeMultiMap<
417
459
  }
418
460
 
419
461
  /**
462
+ * Time Complexity: O(1)
463
+ * Space Complexity: O(1)
464
+ */
465
+
466
+ /**
467
+ * Time Complexity: O(1)
468
+ * Space Complexity: O(1)
469
+ *
420
470
  * The function replaces an old node with a new node and updates the count property of the new node.
421
- * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
422
- * needs to be replaced in a data structure.
423
- * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
471
+ * @param {NODE} oldNode - The oldNode parameter represents the node that needs to be replaced in the
472
+ * data structure. It is of type NODE.
473
+ * @param {NODE} newNode - The `newNode` parameter is an instance of the `NODE` class.
424
474
  * @returns The method is returning the result of calling the `_replaceNode` method from the
425
- * superclass, after updating the `count` property of the `newNode` object.
475
+ * superclass, which is of type `NODE`.
426
476
  */
427
477
  protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
428
478
  newNode.count = oldNode.count + newNode.count;
@@ -16,6 +16,7 @@ import type {
16
16
  Comparable,
17
17
  KeyOrNodeOrEntry
18
18
  } from '../../types';
19
+ import { BTNEntry } from '../../types';
19
20
  import { IBinaryTree } from '../../interfaces';
20
21
 
21
22
  export class AVLTreeNode<
@@ -68,33 +69,39 @@ export class AVLTreeNode<
68
69
  export class AVLTree<
69
70
  K extends Comparable,
70
71
  V = any,
72
+ R = BTNEntry<K, V>,
71
73
  NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
72
- TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>
74
+ TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>
73
75
  >
74
- extends BST<K, V, NODE, TREE>
75
- implements IBinaryTree<K, V, NODE, TREE> {
76
+ extends BST<K, V, R, NODE, TREE>
77
+ implements IBinaryTree<K, V, R, NODE, TREE> {
76
78
  /**
77
- * The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
78
- * @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
79
- * objects. It represents a collection of nodes that will be added to the AVL tree during
80
- * initialization.
81
- * @param [options] - The `options` parameter is an optional object that allows you to customize the
82
- * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
83
- * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
79
+ * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
80
+ * entries, or raw elements.
81
+ * @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
82
+ * iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
83
+ * be used to initialize the AVLTree.
84
+ * @param [options] - The `options` parameter is an optional object that can be used to customize the
85
+ * behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
86
+ * keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
87
+ * `nodeBuilder` (
84
88
  */
85
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeOptions<K>) {
89
+ constructor(
90
+ keysOrNodesOrEntriesOrRawElements: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>> = [],
91
+ options?: AVLTreeOptions<K, V, R>
92
+ ) {
86
93
  super([], options);
87
- if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
94
+ if (keysOrNodesOrEntriesOrRawElements) super.addMany(keysOrNodesOrEntriesOrRawElements);
88
95
  }
89
96
 
90
97
  /**
91
- * The function creates a new AVL tree node with the specified key and value.
92
- * @param {K} key - The key parameter is the key value that will be associated with
93
- * the new node. It is used to determine the position of the node in the binary search tree.
94
- * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
95
- * type `V`, which means it can be any value that is assignable to the `value` property of the
96
- * node type `NODE`.
97
- * @returns a new AVLTreeNode object with the specified key and value.
98
+ * The function creates a new AVL tree node with the given key and value.
99
+ * @param {K} key - The key parameter is of type K, which represents the key of the node being
100
+ * created.
101
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
102
+ * value associated with the key in the node being created.
103
+ * @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
104
+ * type NODE.
98
105
  */
99
106
  override createNode(key: K, value?: V): NODE {
100
107
  return new AVLTreeNode<K, V, NODE>(key, value) as NODE;
@@ -107,8 +114,8 @@ export class AVLTree<
107
114
  * being created.
108
115
  * @returns a new AVLTree object.
109
116
  */
110
- override createTree(options?: AVLTreeOptions<K>): TREE {
111
- return new AVLTree<K, V, NODE, TREE>([], {
117
+ override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
118
+ return new AVLTree<K, V, R, NODE, TREE>([], {
112
119
  iterationType: this.iterationType,
113
120
  comparator: this.comparator,
114
121
  ...options
@@ -116,12 +123,16 @@ export class AVLTree<
116
123
  }
117
124
 
118
125
  /**
119
- * The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
120
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
121
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
126
+ * The function checks if the input is an instance of AVLTreeNode.
127
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
128
+ * `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
129
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
130
+ * an instance of the `AVLTreeNode` class.
122
131
  */
123
- override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
124
- return keyOrNodeOrEntry instanceof AVLTreeNode;
132
+ override isNode(
133
+ keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>
134
+ ): keyOrNodeOrEntryOrRawElement is NODE {
135
+ return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
125
136
  }
126
137
 
127
138
  /**
@@ -134,18 +145,19 @@ export class AVLTree<
134
145
  * Time Complexity: O(log n)
135
146
  * Space Complexity: O(1)
136
147
  *
137
- * The function overrides the add method of a binary tree node and balances the tree after inserting
138
- * a new node.
139
- * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
140
- * entry.
141
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
142
- * being added to the binary tree.
143
- * @returns The method is returning either the inserted node or undefined.
148
+ * The function overrides the add method of a class and inserts a key-value pair into a data
149
+ * structure, then balances the path.
150
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
151
+ * `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
152
+ * `RawElement`.
153
+ * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
154
+ * the key or node being added to the data structure.
155
+ * @returns The method is returning a boolean value.
144
156
  */
145
- override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
146
- if (keyOrNodeOrEntry === null) return false;
147
- const inserted = super.add(keyOrNodeOrEntry, value);
148
- if (inserted) this._balancePath(keyOrNodeOrEntry);
157
+ override add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
158
+ if (keyOrNodeOrEntryOrRawElement === null) return false;
159
+ const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
160
+ if (inserted) this._balancePath(keyOrNodeOrEntryOrRawElement);
149
161
  return inserted;
150
162
  }
151
163
 
@@ -158,16 +170,14 @@ export class AVLTree<
158
170
  * Time Complexity: O(log n)
159
171
  * Space Complexity: O(1)
160
172
  *
161
- * The function overrides the delete method of a binary tree, performs the deletion, and then
162
- * balances the tree if necessary.
173
+ * The function overrides the delete method of a binary tree class and performs additional operations
174
+ * to balance the tree after deletion.
163
175
  * @param identifier - The `identifier` parameter is the value or condition used to identify the
164
- * node(s) to be deleted from the binary tree. It can be of any type and is the return type of the
165
- * `callback` function.
166
- * @param {C} callback - The `callback` parameter is a function that will be called for each node
167
- * that is deleted from the binary tree. It is an optional parameter and if not provided, it will
168
- * default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
169
- * parameter of type `NODE
170
- * @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
176
+ * node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
177
+ * binary tree's node type.
178
+ * @param {C} callback - The `callback` parameter is a function that will be used to determine if a
179
+ * node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
180
+ * @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
171
181
  */
172
182
  override delete<C extends BTNCallback<NODE>>(
173
183
  identifier: ReturnType<C>,
@@ -183,18 +193,26 @@ export class AVLTree<
183
193
  }
184
194
 
185
195
  /**
186
- * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
187
- * tree.
188
- * @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
189
- * needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
190
- * @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
191
- * node where the values from the source node will be swapped to.
192
- * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
193
- * if either `srcNode` or `destNode` is undefined.
196
+ * Time Complexity: O(1)
197
+ * Space Complexity: O(1)
198
+ */
199
+
200
+ /**
201
+ * Time Complexity: O(1)
202
+ * Space Complexity: O(1)
203
+ *
204
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
205
+ * binary search tree.
206
+ * @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
207
+ * object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
208
+ * @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
209
+ * `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
210
+ * @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
211
+ * `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
194
212
  */
195
213
  protected override _swapProperties(
196
- srcNode: BSTNKeyOrNode<K, NODE>,
197
- destNode: BSTNKeyOrNode<K, NODE>
214
+ srcNode: R | BSTNKeyOrNode<K, NODE>,
215
+ destNode: R | BSTNKeyOrNode<K, NODE>
198
216
  ): NODE | undefined {
199
217
  const srcNodeEnsured = this.ensureNode(srcNode);
200
218
  const destNodeEnsured = this.ensureNode(destNode);
@@ -230,7 +248,8 @@ export class AVLTree<
230
248
  * Space Complexity: O(1)
231
249
  *
232
250
  * The function calculates the balance factor of a node in a binary tree.
233
- * @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
251
+ * @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
252
+ * binary tree data structure.
234
253
  * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
235
254
  * height of the left subtree from the height of the right subtree.
236
255
  */
@@ -275,7 +294,7 @@ export class AVLTree<
275
294
  * Time Complexity: O(1)
276
295
  * Space Complexity: O(1)
277
296
  *
278
- * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
297
+ * The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
279
298
  * @param {NODE} A - A is a node in a binary tree.
280
299
  */
281
300
  protected _balanceLL(A: NODE): void {
@@ -470,10 +489,10 @@ export class AVLTree<
470
489
  *
471
490
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
472
491
  * to restore balance in an AVL tree after inserting a node.
473
- * @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
474
- * AVL tree that needs to be balanced.
492
+ * @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
493
+ * `KeyOrNodeOrEntry<K, V, NODE>`.
475
494
  */
476
- protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void {
495
+ protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void {
477
496
  node = this.ensureNode(node);
478
497
  const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
479
498
  for (let i = 0; i < path.length; i++) {
@@ -514,13 +533,22 @@ export class AVLTree<
514
533
  }
515
534
 
516
535
  /**
517
- * The function replaces an old node with a new node while preserving the height of the old node.
518
- * @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
519
- * `newNode`.
536
+ * Time Complexity: O(1)
537
+ * Space Complexity: O(1)
538
+ */
539
+
540
+ /**
541
+ * Time Complexity: O(1)
542
+ * Space Complexity: O(1)
543
+ *
544
+ * The function replaces an old node with a new node and sets the height of the new node to be the
545
+ * same as the old node.
546
+ * @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
547
+ * the data structure.
520
548
  * @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
521
549
  * the data structure.
522
- * @returns the result of calling the `_replaceNode` method on the superclass, passing in the
523
- * `oldNode` and `newNode` as arguments.
550
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
551
+ * superclass, with the `oldNode` and `newNode` as arguments.
524
552
  */
525
553
  protected override _replaceNode(oldNode: NODE, newNode: NODE): NODE {
526
554
  newNode.height = oldNode.height;