data-structure-typed 1.48.0 → 1.48.2

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 (144) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +24 -18
  3. package/benchmark/report.html +16 -16
  4. package/benchmark/report.json +188 -308
  5. package/dist/cjs/data-structures/base/index.d.ts +1 -0
  6. package/dist/cjs/data-structures/base/index.js +18 -0
  7. package/dist/cjs/data-structures/base/index.js.map +1 -0
  8. package/dist/cjs/data-structures/base/iterable-base.d.ts +232 -0
  9. package/dist/cjs/data-structures/base/iterable-base.js +313 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -0
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
  14. package/dist/cjs/data-structures/binary-tree/binary-tree.js +91 -88
  15. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  16. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  19. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  20. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +16 -0
  21. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  22. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +44 -6
  23. package/dist/cjs/data-structures/graph/abstract-graph.js +50 -27
  24. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  25. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  26. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  27. package/dist/cjs/data-structures/hash/hash-map.d.ts +160 -44
  28. package/dist/cjs/data-structures/hash/hash-map.js +314 -82
  29. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  30. package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
  31. package/dist/cjs/data-structures/heap/heap.d.ts +50 -7
  32. package/dist/cjs/data-structures/heap/heap.js +60 -30
  33. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  34. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  35. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  36. package/dist/cjs/data-structures/index.d.ts +1 -0
  37. package/dist/cjs/data-structures/index.js +1 -0
  38. package/dist/cjs/data-structures/index.js.map +1 -1
  39. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  40. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  41. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  42. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  43. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +40 -73
  44. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  45. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  46. package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
  47. package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
  48. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  49. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  50. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  51. package/dist/cjs/data-structures/queue/deque.d.ts +29 -51
  52. package/dist/cjs/data-structures/queue/deque.js +36 -71
  53. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  54. package/dist/cjs/data-structures/queue/queue.d.ts +49 -48
  55. package/dist/cjs/data-structures/queue/queue.js +69 -82
  56. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  57. package/dist/cjs/data-structures/stack/stack.d.ts +43 -10
  58. package/dist/cjs/data-structures/stack/stack.js +50 -31
  59. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  60. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  61. package/dist/cjs/data-structures/trie/trie.d.ts +41 -6
  62. package/dist/cjs/data-structures/trie/trie.js +53 -32
  63. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  64. package/dist/cjs/types/data-structures/base/base.d.ts +5 -0
  65. package/dist/cjs/types/data-structures/base/base.js +3 -0
  66. package/dist/cjs/types/data-structures/base/base.js.map +1 -0
  67. package/dist/cjs/types/data-structures/base/index.d.ts +1 -0
  68. package/dist/cjs/types/data-structures/base/index.js +18 -0
  69. package/dist/cjs/types/data-structures/base/index.js.map +1 -0
  70. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -0
  71. package/dist/cjs/types/data-structures/index.d.ts +1 -0
  72. package/dist/cjs/types/data-structures/index.js +1 -0
  73. package/dist/cjs/types/data-structures/index.js.map +1 -1
  74. package/dist/cjs/utils/utils.js.map +1 -1
  75. package/dist/mjs/data-structures/base/index.d.ts +1 -0
  76. package/dist/mjs/data-structures/base/index.js +1 -0
  77. package/dist/mjs/data-structures/base/iterable-base.d.ts +232 -0
  78. package/dist/mjs/data-structures/base/iterable-base.js +307 -0
  79. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +45 -40
  80. package/dist/mjs/data-structures/binary-tree/binary-tree.js +91 -88
  81. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +12 -0
  82. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +16 -0
  83. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +44 -6
  84. package/dist/mjs/data-structures/graph/abstract-graph.js +52 -27
  85. package/dist/mjs/data-structures/hash/hash-map.d.ts +160 -44
  86. package/dist/mjs/data-structures/hash/hash-map.js +310 -80
  87. package/dist/mjs/data-structures/heap/heap.d.ts +50 -7
  88. package/dist/mjs/data-structures/heap/heap.js +60 -30
  89. package/dist/mjs/data-structures/index.d.ts +1 -0
  90. package/dist/mjs/data-structures/index.js +1 -0
  91. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +38 -51
  92. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +46 -73
  93. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +32 -51
  94. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +40 -73
  95. package/dist/mjs/data-structures/queue/deque.d.ts +29 -51
  96. package/dist/mjs/data-structures/queue/deque.js +36 -71
  97. package/dist/mjs/data-structures/queue/queue.d.ts +49 -48
  98. package/dist/mjs/data-structures/queue/queue.js +66 -79
  99. package/dist/mjs/data-structures/stack/stack.d.ts +43 -10
  100. package/dist/mjs/data-structures/stack/stack.js +50 -31
  101. package/dist/mjs/data-structures/trie/trie.d.ts +41 -6
  102. package/dist/mjs/data-structures/trie/trie.js +53 -32
  103. package/dist/mjs/types/data-structures/base/base.d.ts +5 -0
  104. package/dist/mjs/types/data-structures/base/base.js +1 -0
  105. package/dist/mjs/types/data-structures/base/index.d.ts +1 -0
  106. package/dist/mjs/types/data-structures/base/index.js +1 -0
  107. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -0
  108. package/dist/mjs/types/data-structures/index.d.ts +1 -0
  109. package/dist/mjs/types/data-structures/index.js +1 -0
  110. package/dist/umd/data-structure-typed.js +1104 -575
  111. package/dist/umd/data-structure-typed.min.js +2 -2
  112. package/dist/umd/data-structure-typed.min.js.map +1 -1
  113. package/package.json +2 -2
  114. package/src/data-structures/base/index.ts +1 -0
  115. package/src/data-structures/base/iterable-base.ts +329 -0
  116. package/src/data-structures/binary-tree/binary-tree.ts +98 -93
  117. package/src/data-structures/binary-tree/tree-multimap.ts +18 -0
  118. package/src/data-structures/graph/abstract-graph.ts +55 -28
  119. package/src/data-structures/hash/hash-map.ts +334 -83
  120. package/src/data-structures/heap/heap.ts +63 -36
  121. package/src/data-structures/index.ts +1 -0
  122. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -79
  123. package/src/data-structures/linked-list/singly-linked-list.ts +45 -80
  124. package/src/data-structures/queue/deque.ts +40 -82
  125. package/src/data-structures/queue/queue.ts +72 -87
  126. package/src/data-structures/stack/stack.ts +53 -34
  127. package/src/data-structures/trie/trie.ts +58 -35
  128. package/src/types/data-structures/base/base.ts +6 -0
  129. package/src/types/data-structures/base/index.ts +1 -0
  130. package/src/types/data-structures/hash/hash-map.ts +2 -0
  131. package/src/types/data-structures/index.ts +1 -0
  132. package/test/integration/avl-tree.test.ts +2 -2
  133. package/test/integration/bst.test.ts +21 -25
  134. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +17 -12
  135. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +24 -8
  136. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -9
  137. package/test/unit/data-structures/binary-tree/bst.test.ts +24 -8
  138. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +8 -8
  139. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +82 -1
  140. package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
  141. package/test/unit/data-structures/hash/hash-map.test.ts +312 -18
  142. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +28 -0
  143. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +25 -0
  144. package/test/unit/data-structures/queue/deque.test.ts +25 -0
@@ -0,0 +1,232 @@
1
+ import { ElementCallback, PairCallback, ReduceElementCallback, ReducePairCallback } from "../../types";
2
+ export declare abstract class IterablePairBase<K = any, V = any> {
3
+ /**
4
+ * Time Complexity: O(n)
5
+ * Space Complexity: O(1)
6
+ */
7
+ /**
8
+ * Time Complexity: O(n)
9
+ * Space Complexity: O(1)
10
+ *
11
+ * The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
12
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
13
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
14
+ * parameter is used to pass any additional arguments to the `_getIterator` method.
15
+ */
16
+ [Symbol.iterator](...args: any[]): IterableIterator<[K, V]>;
17
+ /**
18
+ * Time Complexity: O(n)
19
+ * Space Complexity: O(n)
20
+ */
21
+ /**
22
+ * Time Complexity: O(n)
23
+ * Space Complexity: O(n)
24
+ *
25
+ * The function returns an iterator that yields key-value pairs from the object, where the value can
26
+ * be undefined.
27
+ */
28
+ entries(): IterableIterator<[K, V | undefined]>;
29
+ /**
30
+ * Time Complexity: O(n)
31
+ * Space Complexity: O(n)
32
+ */
33
+ /**
34
+ * Time Complexity: O(n)
35
+ * Space Complexity: O(n)
36
+ *
37
+ * The function returns an iterator that yields the keys of a data structure.
38
+ */
39
+ keys(): IterableIterator<K>;
40
+ /**
41
+ * Time Complexity: O(n)
42
+ * Space Complexity: O(n)
43
+ */
44
+ /**
45
+ * Time Complexity: O(n)
46
+ * Space Complexity: O(n)
47
+ *
48
+ * The function returns an iterator that yields the values of a collection.
49
+ */
50
+ values(): IterableIterator<V>;
51
+ /**
52
+ * Time Complexity: O(n)
53
+ * Space Complexity: O(1)
54
+ */
55
+ /**
56
+ * Time Complexity: O(n)
57
+ * Space Complexity: O(1)
58
+ *
59
+ * The `every` function checks if every element in a collection satisfies a given condition.
60
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
61
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
62
+ * met for the current element in the iteration.
63
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
64
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
65
+ * passed as the first argument to the `predicate` function. If `thisArg` is not provided
66
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
67
+ * the collection satisfies the provided predicate function, and `false` otherwise.
68
+ */
69
+ every(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean;
70
+ /**
71
+ * Time Complexity: O(n)
72
+ * Space Complexity: O(1)
73
+ */
74
+ /**
75
+ * Time Complexity: O(n)
76
+ * Space Complexity: O(1)
77
+ *
78
+ * The "some" function iterates over a collection and returns true if at least one element satisfies
79
+ * a given predicate.
80
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
81
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
82
+ * met for the current element in the iteration.
83
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
84
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
85
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
86
+ * @returns a boolean value. It returns true if the predicate function returns true for any pair in
87
+ * the collection, and false otherwise.
88
+ */
89
+ some(predicate: PairCallback<K, V, boolean>, thisArg?: any): boolean;
90
+ /**
91
+ * Time Complexity: O(n)
92
+ * Space Complexity: O(1)
93
+ */
94
+ /**
95
+ * Time Complexity: O(n)
96
+ * Space Complexity: O(1)
97
+ *
98
+ * The `forEach` function iterates over each key-value pair in a collection and executes a callback
99
+ * function for each pair.
100
+ * @param callbackfn - The callback function that will be called for each element in the collection.
101
+ * It takes four parameters: the value of the current element, the key of the current element, the
102
+ * index of the current element, and the collection itself.
103
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
104
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
105
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
106
+ */
107
+ forEach(callbackfn: PairCallback<K, V, void>, thisArg?: any): void;
108
+ /**
109
+ * Time Complexity: O(n)
110
+ * Space Complexity: O(1)
111
+ */
112
+ /**
113
+ * Time Complexity: O(n)
114
+ * Space Complexity: O(1)
115
+ *
116
+ * The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
117
+ * accumulating a single value.
118
+ * @param callbackfn - The callback function that will be called for each element in the collection.
119
+ * It takes four arguments: the current accumulator value, the current value of the element, the key
120
+ * of the element, and the index of the element in the collection. It should return the updated
121
+ * accumulator value.
122
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
123
+ * is the value that will be used as the first argument to the `callbackfn` function when reducing
124
+ * the elements of the collection.
125
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
126
+ * all the elements in the collection.
127
+ */
128
+ reduce<U>(callbackfn: ReducePairCallback<K, V, U>, initialValue: U): U;
129
+ protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
130
+ }
131
+ export declare abstract class IterableElementBase<V> {
132
+ /**
133
+ * Time Complexity: O(n)
134
+ * Space Complexity: O(1)
135
+ */
136
+ /**
137
+ * Time Complexity: O(n)
138
+ * Space Complexity: O(1)
139
+ *
140
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
141
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
142
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
143
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
144
+ */
145
+ [Symbol.iterator](...args: any[]): IterableIterator<V>;
146
+ /**
147
+ * Time Complexity: O(n)
148
+ * Space Complexity: O(n)
149
+ */
150
+ /**
151
+ * Time Complexity: O(n)
152
+ * Space Complexity: O(n)
153
+ *
154
+ * The function returns an iterator that yields all the values in the object.
155
+ */
156
+ values(): IterableIterator<V>;
157
+ /**
158
+ * Time Complexity: O(n)
159
+ * Space Complexity: O(1)
160
+ */
161
+ /**
162
+ * Time Complexity: O(n)
163
+ * Space Complexity: O(1)
164
+ *
165
+ * The `every` function checks if every element in the array satisfies a given predicate.
166
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
167
+ * the current element being processed, its index, and the array it belongs to. It should return a
168
+ * boolean value indicating whether the element satisfies a certain condition or not.
169
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
170
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
171
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
172
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
173
+ * the array satisfies the provided predicate function, and `false` otherwise.
174
+ */
175
+ every(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean;
176
+ /**
177
+ * Time Complexity: O(n)
178
+ * Space Complexity: O(1)
179
+ */
180
+ /**
181
+ * Time Complexity: O(n)
182
+ * Space Complexity: O(1)
183
+ *
184
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
185
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
186
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
187
+ * element satisfies the condition.
188
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
189
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
190
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
191
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
192
+ * in the collection, and false otherwise.
193
+ */
194
+ some(predicate: ElementCallback<V, boolean>, thisArg?: any): boolean;
195
+ /**
196
+ * Time Complexity: O(n)
197
+ * Space Complexity: O(1)
198
+ */
199
+ /**
200
+ * Time Complexity: O(n)
201
+ * Space Complexity: O(1)
202
+ *
203
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
204
+ * function for each element.
205
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
206
+ * the array. It takes three arguments: the current element being processed, the index of the current
207
+ * element, and the array that forEach was called upon.
208
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
209
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
210
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
211
+ */
212
+ forEach(callbackfn: ElementCallback<V, void>, thisArg?: any): void;
213
+ /**
214
+ * Time Complexity: O(n)
215
+ * Space Complexity: O(1)
216
+ */
217
+ /**
218
+ * Time Complexity: O(n)
219
+ * Space Complexity: O(1)
220
+ *
221
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
222
+ * function to reduce them into a single value.
223
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
224
+ * the array. It takes four arguments:
225
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
226
+ * is the value that the accumulator starts with before the reduction operation begins.
227
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
228
+ * all the elements in the array and applying the callback function to each element.
229
+ */
230
+ reduce<U>(callbackfn: ReduceElementCallback<V, U>, initialValue: U): U;
231
+ protected abstract _getIterator(...args: any[]): IterableIterator<V>;
232
+ }
@@ -0,0 +1,307 @@
1
+ export class IterablePairBase {
2
+ /**
3
+ * Time Complexity: O(n)
4
+ * Space Complexity: O(1)
5
+ */
6
+ /**
7
+ * Time Complexity: O(n)
8
+ * Space Complexity: O(1)
9
+ *
10
+ * The function is an implementation of the Symbol.iterator method that returns an iterable iterator.
11
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
12
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
13
+ * parameter is used to pass any additional arguments to the `_getIterator` method.
14
+ */
15
+ *[Symbol.iterator](...args) {
16
+ yield* this._getIterator(...args);
17
+ }
18
+ /**
19
+ * Time Complexity: O(n)
20
+ * Space Complexity: O(n)
21
+ */
22
+ /**
23
+ * Time Complexity: O(n)
24
+ * Space Complexity: O(n)
25
+ *
26
+ * The function returns an iterator that yields key-value pairs from the object, where the value can
27
+ * be undefined.
28
+ */
29
+ *entries() {
30
+ for (const item of this) {
31
+ yield item;
32
+ }
33
+ }
34
+ /**
35
+ * Time Complexity: O(n)
36
+ * Space Complexity: O(n)
37
+ */
38
+ /**
39
+ * Time Complexity: O(n)
40
+ * Space Complexity: O(n)
41
+ *
42
+ * The function returns an iterator that yields the keys of a data structure.
43
+ */
44
+ *keys() {
45
+ for (const item of this) {
46
+ yield item[0];
47
+ }
48
+ }
49
+ /**
50
+ * Time Complexity: O(n)
51
+ * Space Complexity: O(n)
52
+ */
53
+ /**
54
+ * Time Complexity: O(n)
55
+ * Space Complexity: O(n)
56
+ *
57
+ * The function returns an iterator that yields the values of a collection.
58
+ */
59
+ *values() {
60
+ for (const item of this) {
61
+ yield item[1];
62
+ }
63
+ }
64
+ /**
65
+ * Time Complexity: O(n)
66
+ * Space Complexity: O(1)
67
+ */
68
+ /**
69
+ * Time Complexity: O(n)
70
+ * Space Complexity: O(1)
71
+ *
72
+ * The `every` function checks if every element in a collection satisfies a given condition.
73
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
74
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
75
+ * met for the current element in the iteration.
76
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
77
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
78
+ * passed as the first argument to the `predicate` function. If `thisArg` is not provided
79
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
80
+ * the collection satisfies the provided predicate function, and `false` otherwise.
81
+ */
82
+ every(predicate, thisArg) {
83
+ let index = 0;
84
+ for (const item of this) {
85
+ if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
86
+ return false;
87
+ }
88
+ }
89
+ return true;
90
+ }
91
+ /**
92
+ * Time Complexity: O(n)
93
+ * Space Complexity: O(1)
94
+ */
95
+ /**
96
+ * Time Complexity: O(n)
97
+ * Space Complexity: O(1)
98
+ *
99
+ * The "some" function iterates over a collection and returns true if at least one element satisfies
100
+ * a given predicate.
101
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
102
+ * `value`, `key`, and `index`. It should return a boolean value indicating whether the condition is
103
+ * met for the current element in the iteration.
104
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
105
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
106
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
107
+ * @returns a boolean value. It returns true if the predicate function returns true for any pair in
108
+ * the collection, and false otherwise.
109
+ */
110
+ some(predicate, thisArg) {
111
+ let index = 0;
112
+ for (const item of this) {
113
+ if (predicate.call(thisArg, item[1], item[0], index++, this)) {
114
+ return true;
115
+ }
116
+ }
117
+ return false;
118
+ }
119
+ /**
120
+ * Time Complexity: O(n)
121
+ * Space Complexity: O(1)
122
+ */
123
+ /**
124
+ * Time Complexity: O(n)
125
+ * Space Complexity: O(1)
126
+ *
127
+ * The `forEach` function iterates over each key-value pair in a collection and executes a callback
128
+ * function for each pair.
129
+ * @param callbackfn - The callback function that will be called for each element in the collection.
130
+ * It takes four parameters: the value of the current element, the key of the current element, the
131
+ * index of the current element, and the collection itself.
132
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
133
+ * specify the value of `this` within the callback function. If `thisArg` is provided, it will be
134
+ * used as the `this` value when calling the callback function. If `thisArg` is not provided, `
135
+ */
136
+ forEach(callbackfn, thisArg) {
137
+ let index = 0;
138
+ for (const item of this) {
139
+ const [key, value] = item;
140
+ callbackfn.call(thisArg, value, key, index++, this);
141
+ }
142
+ }
143
+ /**
144
+ * Time Complexity: O(n)
145
+ * Space Complexity: O(1)
146
+ */
147
+ /**
148
+ * Time Complexity: O(n)
149
+ * Space Complexity: O(1)
150
+ *
151
+ * The `reduce` function iterates over key-value pairs and applies a callback function to each pair,
152
+ * accumulating a single value.
153
+ * @param callbackfn - The callback function that will be called for each element in the collection.
154
+ * It takes four arguments: the current accumulator value, the current value of the element, the key
155
+ * of the element, and the index of the element in the collection. It should return the updated
156
+ * accumulator value.
157
+ * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It
158
+ * is the value that will be used as the first argument to the `callbackfn` function when reducing
159
+ * the elements of the collection.
160
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
161
+ * all the elements in the collection.
162
+ */
163
+ reduce(callbackfn, initialValue) {
164
+ let accumulator = initialValue;
165
+ let index = 0;
166
+ for (const item of this) {
167
+ const [key, value] = item;
168
+ accumulator = callbackfn(accumulator, value, key, index++, this);
169
+ }
170
+ return accumulator;
171
+ }
172
+ }
173
+ export class IterableElementBase {
174
+ /**
175
+ * Time Complexity: O(n)
176
+ * Space Complexity: O(1)
177
+ */
178
+ /**
179
+ * Time Complexity: O(n)
180
+ * Space Complexity: O(1)
181
+ *
182
+ * The function is an implementation of the Symbol.iterator method that returns an IterableIterator.
183
+ * @param {any[]} args - The `args` parameter in the code snippet represents a rest parameter. It
184
+ * allows the function to accept any number of arguments as an array. In this case, the `args`
185
+ * parameter is used to pass any number of arguments to the `_getIterator` method.
186
+ */
187
+ *[Symbol.iterator](...args) {
188
+ yield* this._getIterator(...args);
189
+ }
190
+ /**
191
+ * Time Complexity: O(n)
192
+ * Space Complexity: O(n)
193
+ */
194
+ /**
195
+ * Time Complexity: O(n)
196
+ * Space Complexity: O(n)
197
+ *
198
+ * The function returns an iterator that yields all the values in the object.
199
+ */
200
+ *values() {
201
+ for (const item of this) {
202
+ yield item;
203
+ }
204
+ }
205
+ /**
206
+ * Time Complexity: O(n)
207
+ * Space Complexity: O(1)
208
+ */
209
+ /**
210
+ * Time Complexity: O(n)
211
+ * Space Complexity: O(1)
212
+ *
213
+ * The `every` function checks if every element in the array satisfies a given predicate.
214
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
215
+ * the current element being processed, its index, and the array it belongs to. It should return a
216
+ * boolean value indicating whether the element satisfies a certain condition or not.
217
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
218
+ * to be used as `this` when executing the `predicate` function. If `thisArg` is provided, it will be
219
+ * passed as the `this` value to the `predicate` function. If `thisArg` is
220
+ * @returns The `every` method is returning a boolean value. It returns `true` if every element in
221
+ * the array satisfies the provided predicate function, and `false` otherwise.
222
+ */
223
+ every(predicate, thisArg) {
224
+ let index = 0;
225
+ for (const item of this) {
226
+ if (!predicate.call(thisArg, item, index++, this)) {
227
+ return false;
228
+ }
229
+ }
230
+ return true;
231
+ }
232
+ /**
233
+ * Time Complexity: O(n)
234
+ * Space Complexity: O(1)
235
+ */
236
+ /**
237
+ * Time Complexity: O(n)
238
+ * Space Complexity: O(1)
239
+ *
240
+ * The "some" function checks if at least one element in a collection satisfies a given predicate.
241
+ * @param predicate - The `predicate` parameter is a callback function that takes three arguments:
242
+ * `value`, `index`, and `array`. It should return a boolean value indicating whether the current
243
+ * element satisfies the condition.
244
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
245
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
246
+ * it will be passed as the `this` value to the `predicate` function. If `thisArg
247
+ * @returns a boolean value. It returns true if the predicate function returns true for any element
248
+ * in the collection, and false otherwise.
249
+ */
250
+ some(predicate, thisArg) {
251
+ let index = 0;
252
+ for (const item of this) {
253
+ if (predicate.call(thisArg, item, index++, this)) {
254
+ return true;
255
+ }
256
+ }
257
+ return false;
258
+ }
259
+ /**
260
+ * Time Complexity: O(n)
261
+ * Space Complexity: O(1)
262
+ */
263
+ /**
264
+ * Time Complexity: O(n)
265
+ * Space Complexity: O(1)
266
+ *
267
+ * The `forEach` function iterates over each element in an array-like object and calls a callback
268
+ * function for each element.
269
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
270
+ * the array. It takes three arguments: the current element being processed, the index of the current
271
+ * element, and the array that forEach was called upon.
272
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
273
+ * to be used as `this` when executing the `callbackfn` function. If `thisArg` is provided, it will
274
+ * be passed as the `this` value to the `callbackfn` function. If `thisArg
275
+ */
276
+ forEach(callbackfn, thisArg) {
277
+ let index = 0;
278
+ for (const item of this) {
279
+ callbackfn.call(thisArg, item, index++, this);
280
+ }
281
+ }
282
+ /**
283
+ * Time Complexity: O(n)
284
+ * Space Complexity: O(1)
285
+ */
286
+ /**
287
+ * Time Complexity: O(n)
288
+ * Space Complexity: O(1)
289
+ *
290
+ * The `reduce` function iterates over the elements of an array-like object and applies a callback
291
+ * function to reduce them into a single value.
292
+ * @param callbackfn - The callbackfn parameter is a function that will be called for each element in
293
+ * the array. It takes four arguments:
294
+ * @param {U} initialValue - The initialValue parameter is the initial value of the accumulator. It
295
+ * is the value that the accumulator starts with before the reduction operation begins.
296
+ * @returns The `reduce` method is returning the final value of the accumulator after iterating over
297
+ * all the elements in the array and applying the callback function to each element.
298
+ */
299
+ reduce(callbackfn, initialValue) {
300
+ let accumulator = initialValue;
301
+ let index = 0;
302
+ for (const item of this) {
303
+ accumulator = callbackfn(accumulator, item, index++, this);
304
+ }
305
+ return accumulator;
306
+ }
307
+ }
@@ -6,8 +6,9 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNKey, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types';
9
- import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
9
+ import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout, PairCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ import { IterablePairBase } from "../base";
11
12
  /**
12
13
  * Represents a node in a binary tree.
13
14
  * @template V - The type of data stored in the node.
@@ -41,7 +42,7 @@ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = Bi
41
42
  * 8. Full Trees: Every node has either 0 or 2 children.
42
43
  * 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
43
44
  */
44
- export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> implements IBinaryTree<V, N, TREE> {
45
+ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>, TREE extends BinaryTree<V, N, TREE> = BinaryTree<V, N, BinaryTreeNested<V, N>>> extends IterablePairBase<BTNKey, V | undefined> implements IBinaryTree<V, N, TREE> {
45
46
  iterationType: IterationType;
46
47
  /**
47
48
  * The constructor function initializes a binary tree object with optional elements and options.
@@ -462,54 +463,57 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
462
463
  morris<C extends BTNCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: BTNodeKeyOrNode<N>): ReturnType<C>[];
463
464
  /**
464
465
  * Time complexity: O(n)
465
- * Space complexity: O(1)
466
+ * Space complexity: O(n)
466
467
  */
467
468
  /**
468
- * The `forEach` function iterates over each entry in a tree and calls a callback function with the
469
- * entry and the tree as arguments.
470
- * @param callback - The callback parameter is a function that will be called for each entry in the
471
- * tree. It takes two parameters: entry and tree.
469
+ * Time complexity: O(n)
470
+ * Space complexity: O(n)
471
+ *
472
+ * The `clone` function creates a new tree object and copies all the nodes from the original tree to
473
+ * the new tree.
474
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
472
475
  */
473
- forEach(callback: (entry: [BTNKey, V | undefined], tree: this) => void): void;
476
+ clone(): TREE;
474
477
  /**
475
- * The `filter` function creates a new tree by iterating over the entries of the current tree and
476
- * adding the entries that satisfy the given predicate.
477
- * @param predicate - The `predicate` parameter is a function that takes two arguments: `entry` and
478
- * `tree`.
479
- * @returns The `filter` method is returning a new tree object that contains only the entries that
480
- * satisfy the given predicate function.
478
+ * Time Complexity: O(n)
479
+ * Space Complexity: O(n)
481
480
  */
482
- filter(predicate: (entry: [BTNKey, V | undefined], tree: this) => boolean): TREE;
483
481
  /**
484
- * The `map` function creates a new tree by applying a callback function to each entry in the current
485
- * tree.
486
- * @param callback - The callback parameter is a function that takes two arguments: entry and tree.
487
- * @returns The `map` method is returning a new tree object.
488
- */
489
- map(callback: (entry: [BTNKey, V | undefined], tree: this) => V): TREE;
482
+ * Time Complexity: O(n)
483
+ * Space Complexity: O(n)
484
+ *
485
+ * The `filter` function creates a new tree by iterating over the elements of the current tree and
486
+ * adding only the elements that satisfy the given predicate function.
487
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `value`,
488
+ * `key`, and `index`. It should return a boolean value indicating whether the pair should be
489
+ * included in the filtered tree or not.
490
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that specifies the value
491
+ * to be used as the `this` value when executing the `predicate` function. If `thisArg` is provided,
492
+ * it will be passed as the first argument to the `predicate` function. If `thisArg` is
493
+ * @returns The `filter` method is returning a new tree object that contains the key-value pairs that
494
+ * pass the given predicate function.
495
+ */
496
+ filter(predicate: PairCallback<BTNKey, V | undefined, boolean>, thisArg?: any): TREE;
490
497
  /**
491
- * The `reduce` function iterates over the entries of a tree and applies a callback function to each
492
- * entry, accumulating a single value.
493
- * @param callback - The callback parameter is a function that takes three arguments: accumulator,
494
- * entry, and tree. It is called for each entry in the tree and is used to accumulate a single value
495
- * based on the logic defined in the callback function.
496
- * @param {T} initialValue - The initialValue parameter is the initial value of the accumulator. It
497
- * is the value that will be passed as the first argument to the callback function when reducing the
498
- * elements of the tree.
499
- * @returns The `reduce` method is returning the final value of the accumulator after iterating over
500
- * all the entries in the tree and applying the callback function to each entry.
498
+ * Time Complexity: O(n)
499
+ * Space Complexity: O(n)
501
500
  */
502
- reduce<T>(callback: (accumulator: T, entry: [BTNKey, V | undefined], tree: this) => T, initialValue: T): T;
503
501
  /**
504
- * The above function is an iterator for a binary tree that can be used to traverse the tree in
505
- * either an iterative or recursive manner.
506
- * @param node - The `node` parameter represents the current node in the binary tree from which the
507
- * iteration starts. It is an optional parameter with a default value of `this.root`, which means
508
- * that if no node is provided, the iteration will start from the root of the binary tree.
509
- * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
510
- * binary tree nodes in a specific order.
502
+ * Time Complexity: O(n)
503
+ * Space Complexity: O(n)
504
+ *
505
+ * The `map` function creates a new tree by applying a callback function to each key-value pair in
506
+ * the original tree.
507
+ * @param callback - The callback parameter is a function that will be called for each key-value pair
508
+ * in the tree. It takes four arguments: the value of the current pair, the key of the current pair,
509
+ * the index of the current pair, and a reference to the tree itself. The callback function should
510
+ * return a new
511
+ * @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
512
+ * specify the value of `this` within the callback function. If you pass a value for `thisArg`, it
513
+ * will be used as the `this` value when the callback function is called. If you don't pass a value
514
+ * @returns The `map` method is returning a new tree object.
511
515
  */
512
- [Symbol.iterator](node?: N | null | undefined): Generator<[BTNKey, V | undefined], void, undefined>;
516
+ map(callback: PairCallback<BTNKey, V | undefined, V>, thisArg?: any): TREE;
513
517
  /**
514
518
  * The `print` function is used to display a binary tree structure in a visually appealing way.
515
519
  * @param {BTNKey | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `BTNKey | N | null |
@@ -518,6 +522,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
518
522
  * @param {BinaryTreePrintOptions} [options={ isShowUndefined: false, isShowNull: false, isShowRedBlackNIL: false}] - Options object that controls printing behavior. You can specify whether to display undefined, null, or sentinel nodes.
519
523
  */
520
524
  print(beginRoot?: BTNodeKeyOrNode<N>, options?: BinaryTreePrintOptions): void;
525
+ protected _getIterator(node?: N | null | undefined): IterableIterator<[BTNKey, V | undefined]>;
521
526
  protected _displayAux(node: N | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
522
527
  protected _defaultOneParamCallback: (node: N) => number;
523
528
  /**