linked-list-typed 1.47.4 → 1.47.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 (72) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +8 -8
  2. package/dist/data-structures/binary-tree/avl-tree.js +23 -15
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +65 -28
  4. package/dist/data-structures/binary-tree/binary-tree.js +66 -82
  5. package/dist/data-structures/binary-tree/bst.d.ts +38 -37
  6. package/dist/data-structures/binary-tree/bst.js +56 -40
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +11 -7
  8. package/dist/data-structures/binary-tree/rb-tree.js +26 -17
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +16 -16
  10. package/dist/data-structures/binary-tree/tree-multimap.js +31 -22
  11. package/dist/data-structures/graph/abstract-graph.js +1 -1
  12. package/dist/data-structures/hash/hash-map.d.ts +3 -3
  13. package/dist/data-structures/hash/hash-map.js +10 -4
  14. package/dist/data-structures/hash/hash-table.d.ts +9 -4
  15. package/dist/data-structures/hash/hash-table.js +50 -5
  16. package/dist/data-structures/heap/heap.d.ts +25 -22
  17. package/dist/data-structures/heap/heap.js +101 -41
  18. package/dist/data-structures/heap/max-heap.d.ts +2 -5
  19. package/dist/data-structures/heap/max-heap.js +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -5
  21. package/dist/data-structures/heap/min-heap.js +2 -2
  22. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +58 -57
  23. package/dist/data-structures/linked-list/doubly-linked-list.js +125 -119
  24. package/dist/data-structures/linked-list/singly-linked-list.d.ts +38 -37
  25. package/dist/data-structures/linked-list/singly-linked-list.js +65 -60
  26. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -5
  27. package/dist/data-structures/priority-queue/max-priority-queue.js +2 -2
  28. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -5
  29. package/dist/data-structures/priority-queue/min-priority-queue.js +2 -2
  30. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -5
  31. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  32. package/dist/data-structures/queue/deque.d.ts +50 -49
  33. package/dist/data-structures/queue/deque.js +81 -71
  34. package/dist/data-structures/queue/queue.d.ts +46 -0
  35. package/dist/data-structures/queue/queue.js +80 -0
  36. package/dist/data-structures/stack/stack.d.ts +20 -6
  37. package/dist/data-structures/stack/stack.js +65 -8
  38. package/dist/data-structures/trie/trie.d.ts +5 -0
  39. package/dist/data-structures/trie/trie.js +47 -0
  40. package/dist/interfaces/binary-tree.d.ts +3 -1
  41. package/dist/types/common.d.ts +2 -0
  42. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/bst.d.ts +2 -2
  44. package/dist/types/data-structures/heap/heap.d.ts +4 -1
  45. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +2 -1
  46. package/package.json +2 -2
  47. package/src/data-structures/binary-tree/avl-tree.ts +27 -17
  48. package/src/data-structures/binary-tree/binary-tree.ts +114 -97
  49. package/src/data-structures/binary-tree/bst.ts +67 -47
  50. package/src/data-structures/binary-tree/rb-tree.ts +34 -20
  51. package/src/data-structures/binary-tree/tree-multimap.ts +43 -25
  52. package/src/data-structures/graph/abstract-graph.ts +1 -1
  53. package/src/data-structures/hash/hash-map.ts +13 -7
  54. package/src/data-structures/hash/hash-table.ts +59 -9
  55. package/src/data-structures/heap/heap.ts +115 -46
  56. package/src/data-structures/heap/max-heap.ts +5 -5
  57. package/src/data-structures/heap/min-heap.ts +5 -5
  58. package/src/data-structures/linked-list/doubly-linked-list.ts +137 -128
  59. package/src/data-structures/linked-list/singly-linked-list.ts +72 -64
  60. package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
  61. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -12
  62. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  63. package/src/data-structures/queue/deque.ts +86 -75
  64. package/src/data-structures/queue/queue.ts +88 -0
  65. package/src/data-structures/stack/stack.ts +75 -10
  66. package/src/data-structures/trie/trie.ts +53 -0
  67. package/src/interfaces/binary-tree.ts +13 -1
  68. package/src/types/common.ts +5 -1
  69. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  70. package/src/types/data-structures/binary-tree/bst.ts +2 -3
  71. package/src/types/data-structures/heap/heap.ts +3 -1
  72. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -1
@@ -205,5 +205,51 @@ export declare class Queue<E = any> {
205
205
  * @returns The `clone()` method is returning a new instance of the `Queue` class.
206
206
  */
207
207
  clone(): Queue<E>;
208
+ print(): void;
208
209
  [Symbol.iterator](): Generator<E, void, unknown>;
210
+ /**
211
+ * Time Complexity: O(n)
212
+ * Space Complexity: O(1)
213
+ */
214
+ /**
215
+ * Time Complexity: O(n)
216
+ * Space Complexity: O(1)
217
+ *
218
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
219
+ * each element.
220
+ * @param callback - The callback parameter is a function that will be called for each element in the
221
+ * deque. It takes three parameters:
222
+ */
223
+ forEach(callback: (element: E, index: number, queue: this) => void): void;
224
+ /**
225
+ * Time Complexity: O(n)
226
+ * Space Complexity: O(n)
227
+ */
228
+ /**
229
+ * Time Complexity: O(n)
230
+ * Space Complexity: O(n)
231
+ *
232
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
233
+ * predicate function.
234
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
235
+ * `index`, and `deque`.
236
+ * @returns The `filter` method is returning a new `Queue` object that contains only the elements
237
+ * that satisfy the given `predicate` function.
238
+ */
239
+ filter(predicate: (element: E, index: number, queue: this) => boolean): Queue<E>;
240
+ /**
241
+ * Time Complexity: O(n)
242
+ * Space Complexity: O(n)
243
+ */
244
+ /**
245
+ * Time Complexity: O(n)
246
+ * Space Complexity: O(n)
247
+ *
248
+ * The `map` function takes a callback function and applies it to each element in the deque,
249
+ * returning a new deque with the results.
250
+ * @param callback - The `callback` parameter is a function that takes three arguments:
251
+ * @returns The `map` method is returning a new `Queue` object with the transformed elements.
252
+ */
253
+ map<T>(callback: (element: E, index: number, queue: this) => T): Queue<T>;
254
+ reduce<T>(callback: (accumulator: T, element: E, index: number, queue: this) => T, initialValue: T): T;
209
255
  }
@@ -265,10 +265,90 @@ class Queue {
265
265
  clone() {
266
266
  return new Queue(this.nodes.slice(this.offset));
267
267
  }
268
+ print() {
269
+ console.log([...this]);
270
+ }
268
271
  *[Symbol.iterator]() {
269
272
  for (const item of this.nodes) {
270
273
  yield item;
271
274
  }
272
275
  }
276
+ /**
277
+ * Time Complexity: O(n)
278
+ * Space Complexity: O(1)
279
+ */
280
+ /**
281
+ * Time Complexity: O(n)
282
+ * Space Complexity: O(1)
283
+ *
284
+ * The `forEach` function iterates over each element in a deque and applies a callback function to
285
+ * each element.
286
+ * @param callback - The callback parameter is a function that will be called for each element in the
287
+ * deque. It takes three parameters:
288
+ */
289
+ forEach(callback) {
290
+ let index = 0;
291
+ for (const el of this) {
292
+ callback(el, index, this);
293
+ index++;
294
+ }
295
+ }
296
+ /**
297
+ * Time Complexity: O(n)
298
+ * Space Complexity: O(n)
299
+ */
300
+ /**
301
+ * Time Complexity: O(n)
302
+ * Space Complexity: O(n)
303
+ *
304
+ * The `filter` function creates a new deque containing only the elements that satisfy the given
305
+ * predicate function.
306
+ * @param predicate - The `predicate` parameter is a function that takes three arguments: `element`,
307
+ * `index`, and `deque`.
308
+ * @returns The `filter` method is returning a new `Queue` object that contains only the elements
309
+ * that satisfy the given `predicate` function.
310
+ */
311
+ filter(predicate) {
312
+ const newDeque = new Queue([]);
313
+ let index = 0;
314
+ for (const el of this) {
315
+ if (predicate(el, index, this)) {
316
+ newDeque.push(el);
317
+ }
318
+ index++;
319
+ }
320
+ return newDeque;
321
+ }
322
+ /**
323
+ * Time Complexity: O(n)
324
+ * Space Complexity: O(n)
325
+ */
326
+ /**
327
+ * Time Complexity: O(n)
328
+ * Space Complexity: O(n)
329
+ *
330
+ * The `map` function takes a callback function and applies it to each element in the deque,
331
+ * returning a new deque with the results.
332
+ * @param callback - The `callback` parameter is a function that takes three arguments:
333
+ * @returns The `map` method is returning a new `Queue` object with the transformed elements.
334
+ */
335
+ map(callback) {
336
+ const newDeque = new Queue([]);
337
+ let index = 0;
338
+ for (const el of this) {
339
+ newDeque.push(callback(el, index, this));
340
+ index++;
341
+ }
342
+ return newDeque;
343
+ }
344
+ reduce(callback, initialValue) {
345
+ let accumulator = initialValue;
346
+ let index = 0;
347
+ for (const el of this) {
348
+ accumulator = callback(accumulator, el, index, this);
349
+ index++;
350
+ }
351
+ return accumulator;
352
+ }
273
353
  }
274
354
  exports.Queue = Queue;
@@ -10,13 +10,18 @@ export declare class Stack<E = any> {
10
10
  * of elements of type `E`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
11
11
  * is provided and is an array, it is assigned to the `_elements
12
12
  */
13
- constructor(elements?: E[]);
13
+ constructor(elements?: Iterable<E>);
14
14
  protected _elements: E[];
15
15
  get elements(): E[];
16
16
  /**
17
17
  * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
18
18
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
19
19
  */
20
+ /**
21
+ * The size() function returns the number of elements in an array.
22
+ * @returns The size of the elements array.
23
+ */
24
+ get size(): number;
20
25
  /**
21
26
  * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
22
27
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
@@ -32,11 +37,6 @@ export declare class Stack<E = any> {
32
37
  * @returns A boolean value indicating whether the `_elements` array is empty or not.
33
38
  */
34
39
  isEmpty(): boolean;
35
- /**
36
- * The size() function returns the number of elements in an array.
37
- * @returns The size of the elements array.
38
- */
39
- size(): number;
40
40
  /**
41
41
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
42
42
  * Space Complexity: O(1), as it does not use any additional space.
@@ -103,4 +103,18 @@ export declare class Stack<E = any> {
103
103
  * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
104
104
  */
105
105
  clone(): Stack<E>;
106
+ /**
107
+ * Custom iterator for the Stack class.
108
+ * @returns An iterator object.
109
+ */
110
+ [Symbol.iterator](): Generator<E, void, unknown>;
111
+ /**
112
+ * Applies a function to each element of the stack.
113
+ * @param {function(E): void} callback - A function to apply to each element.
114
+ */
115
+ forEach(callback: (element: E, index: number, stack: this) => void): void;
116
+ filter(predicate: (element: E, index: number, stack: this) => boolean): Stack<E>;
117
+ map<T>(callback: (element: E, index: number, stack: this) => T): Stack<T>;
118
+ reduce<T>(callback: (accumulator: T, element: E, index: number, stack: this) => T, initialValue: T): T;
119
+ print(): void;
106
120
  }
@@ -14,7 +14,12 @@ class Stack {
14
14
  * is provided and is an array, it is assigned to the `_elements
15
15
  */
16
16
  constructor(elements) {
17
- this._elements = Array.isArray(elements) ? elements : [];
17
+ this._elements = [];
18
+ if (elements) {
19
+ for (const el of elements) {
20
+ this.push(el);
21
+ }
22
+ }
18
23
  }
19
24
  get elements() {
20
25
  return this._elements;
@@ -23,6 +28,13 @@ class Stack {
23
28
  * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
24
29
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
25
30
  */
31
+ /**
32
+ * The size() function returns the number of elements in an array.
33
+ * @returns The size of the elements array.
34
+ */
35
+ get size() {
36
+ return this.elements.length;
37
+ }
26
38
  /**
27
39
  * Time Complexity: O(n), where n is the number of elements in the input array. Similar to the constructor, it requires iterating through each element.
28
40
  * Space Complexity: O(n), as it creates a new stack with the elements from the input array.
@@ -42,13 +54,6 @@ class Stack {
42
54
  isEmpty() {
43
55
  return this.elements.length === 0;
44
56
  }
45
- /**
46
- * The size() function returns the number of elements in an array.
47
- * @returns The size of the elements array.
48
- */
49
- size() {
50
- return this.elements.length;
51
- }
52
57
  /**
53
58
  * Time Complexity: O(1), as it only involves accessing the last element of the array.
54
59
  * Space Complexity: O(1), as it does not use any additional space.
@@ -132,5 +137,57 @@ class Stack {
132
137
  clone() {
133
138
  return new Stack(this.elements.slice());
134
139
  }
140
+ /**
141
+ * Custom iterator for the Stack class.
142
+ * @returns An iterator object.
143
+ */
144
+ *[Symbol.iterator]() {
145
+ for (let i = 0; i < this.elements.length; i++) {
146
+ yield this.elements[i];
147
+ }
148
+ }
149
+ /**
150
+ * Applies a function to each element of the stack.
151
+ * @param {function(E): void} callback - A function to apply to each element.
152
+ */
153
+ forEach(callback) {
154
+ let index = 0;
155
+ for (const el of this) {
156
+ callback(el, index, this);
157
+ index++;
158
+ }
159
+ }
160
+ filter(predicate) {
161
+ const newStack = new Stack();
162
+ let index = 0;
163
+ for (const el of this) {
164
+ if (predicate(el, index, this)) {
165
+ newStack.push(el);
166
+ }
167
+ index++;
168
+ }
169
+ return newStack;
170
+ }
171
+ map(callback) {
172
+ const newStack = new Stack();
173
+ let index = 0;
174
+ for (const el of this) {
175
+ newStack.push(callback(el, index, this));
176
+ index++;
177
+ }
178
+ return newStack;
179
+ }
180
+ reduce(callback, initialValue) {
181
+ let accumulator = initialValue;
182
+ let index = 0;
183
+ for (const el of this) {
184
+ accumulator = callback(accumulator, el, index, this);
185
+ index++;
186
+ }
187
+ return accumulator;
188
+ }
189
+ print() {
190
+ console.log([...this]);
191
+ }
135
192
  }
136
193
  exports.Stack = Stack;
@@ -140,6 +140,11 @@ export declare class Trie {
140
140
  * @returns {string[]} an array of strings.
141
141
  */
142
142
  getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
143
+ [Symbol.iterator](): IterableIterator<string>;
144
+ forEach(callback: (word: string, index: number, trie: this) => void): void;
145
+ filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
146
+ map(callback: (word: string, index: number, trie: this) => string): Trie;
147
+ reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
143
148
  /**
144
149
  * Time Complexity: O(M), where M is the length of the input string.
145
150
  * Space Complexity: O(1) - Constant space.
@@ -305,6 +305,53 @@ class Trie {
305
305
  dfs(startNode, prefix);
306
306
  return words;
307
307
  }
308
+ *[Symbol.iterator]() {
309
+ function* _dfs(node, path) {
310
+ if (node.isEnd) {
311
+ yield path;
312
+ }
313
+ for (const [char, childNode] of node.children) {
314
+ yield* _dfs(childNode, path + char);
315
+ }
316
+ }
317
+ yield* _dfs(this.root, '');
318
+ }
319
+ forEach(callback) {
320
+ let index = 0;
321
+ for (const word of this) {
322
+ callback(word, index, this);
323
+ index++;
324
+ }
325
+ }
326
+ filter(predicate) {
327
+ const results = [];
328
+ let index = 0;
329
+ for (const word of this) {
330
+ if (predicate(word, index, this)) {
331
+ results.push(word);
332
+ }
333
+ index++;
334
+ }
335
+ return results;
336
+ }
337
+ map(callback) {
338
+ const newTrie = new Trie();
339
+ let index = 0;
340
+ for (const word of this) {
341
+ newTrie.add(callback(word, index, this));
342
+ index++;
343
+ }
344
+ return newTrie;
345
+ }
346
+ reduce(callback, initialValue) {
347
+ let accumulator = initialValue;
348
+ let index = 0;
349
+ for (const word of this) {
350
+ accumulator = callback(accumulator, word, index, this);
351
+ index++;
352
+ }
353
+ return accumulator;
354
+ }
308
355
  /**
309
356
  * Time Complexity: O(M), where M is the length of the input string.
310
357
  * Space Complexity: O(1) - Constant space.
@@ -1,7 +1,9 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeNested, BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
2
+ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys } from '../types';
3
3
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
4
4
  createNode(key: BTNKey, value?: N['value']): N;
5
+ createTree(options?: Partial<BinaryTreeOptions>): TREE;
6
+ init(elements: IterableEntriesOrKeys<V>): void;
5
7
  add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
6
8
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
7
9
  }
@@ -1,3 +1,4 @@
1
+ import { BTNKey } from "./data-structures";
1
2
  export type Comparator<T> = (a: T, b: T) => number;
2
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
3
4
  export type BTNCallback<N, D = any> = (node: N) => D;
@@ -18,3 +19,4 @@ export type BinaryTreePrintOptions = {
18
19
  isShowNull?: boolean;
19
20
  isShowRedBlackNIL?: boolean;
20
21
  };
22
+ export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
@@ -26,6 +26,6 @@ export type BiTreeDeleteResult<N> = {
26
26
  export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
27
27
  export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
28
28
  export type BinaryTreeOptions = {
29
- iterationType?: IterationType;
29
+ iterationType: IterationType;
30
30
  };
31
31
  export type NodeDisplayLayout = [string[], number, number, number];
@@ -1,8 +1,8 @@
1
1
  import { BST, BSTNode } from '../../../data-structures';
2
2
  import type { BinaryTreeOptions, BTNKey } from './binary-tree';
3
- export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
3
+ import { Comparator } from "../../common";
4
4
  export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
5
  export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
6
6
  export type BSTOptions = BinaryTreeOptions & {
7
- comparator?: BSTComparator;
7
+ comparator: Comparator<BTNKey>;
8
8
  };
@@ -1 +1,4 @@
1
- export {};
1
+ import { Comparator } from "../../common";
2
+ export type HeapOptions<T> = {
3
+ comparator: Comparator<T>;
4
+ };
@@ -1 +1,2 @@
1
- export {};
1
+ import { HeapOptions } from "../heap";
2
+ export type PriorityQueueOptions<T> = HeapOptions<T> & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "linked-list-typed",
3
- "version": "1.47.4",
3
+ "version": "1.47.6",
4
4
  "description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -66,6 +66,6 @@
66
66
  "typescript": "^4.9.5"
67
67
  },
68
68
  "dependencies": {
69
- "data-structure-typed": "^1.47.4"
69
+ "data-structure-typed": "^1.47.6"
70
70
  }
71
71
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
10
- import { BTNCallback, IterationType } from '../../types';
10
+ import { BTNCallback, IterableEntriesOrKeys } from '../../types';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
 
13
13
  export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
@@ -23,21 +23,15 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
23
23
  extends BST<V, N, TREE>
24
24
  implements IBinaryTree<V, N, TREE> {
25
25
 
26
- override options: AVLTreeOptions;
27
-
28
26
  /**
29
27
  * This is a constructor function for an AVL tree data structure in TypeScript.
30
28
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
31
29
  * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
32
30
  * options.
33
31
  */
34
- constructor(options?: AVLTreeOptions) {
35
- super(options);
36
- if (options) {
37
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
38
- } else {
39
- this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
40
- }
32
+ constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<AVLTreeOptions>) {
33
+ super([], options);
34
+ if (elements) this.init(elements);
41
35
  }
42
36
 
43
37
  /**
@@ -54,14 +48,12 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
54
48
  }
55
49
 
56
50
  override createTree(options?: AVLTreeOptions): TREE {
57
- return new AVLTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
51
+ return new AVLTree<V, N, TREE>([], {
52
+ iterationType: this.iterationType,
53
+ comparator: this.comparator, ...options
54
+ }) as TREE;
58
55
  }
59
56
 
60
- /**
61
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
62
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
63
- */
64
-
65
57
  /**
66
58
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
67
59
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
@@ -82,7 +74,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
82
74
  }
83
75
 
84
76
  /**
85
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
77
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
86
78
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
87
79
  */
88
80
 
@@ -115,6 +107,24 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
115
107
  return deletedResults;
116
108
  }
117
109
 
110
+ /**
111
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
112
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
113
+ */
114
+
115
+ init(elements: IterableEntriesOrKeys<V>): void {
116
+ if (elements) {
117
+ for (const entryOrKey of elements) {
118
+ if (Array.isArray(entryOrKey)) {
119
+ const [key, value] = entryOrKey;
120
+ this.add(key, value);
121
+ } else {
122
+ this.add(entryOrKey);
123
+ }
124
+ }
125
+ }
126
+ }
127
+
118
128
  /**
119
129
  * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
120
130
  * tree.