doubly-linked-list-typed 1.54.3 → 2.0.1

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 (71) hide show
  1. package/README.md +7 -7
  2. package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
  3. package/dist/data-structures/base/iterable-element-base.js +14 -11
  4. package/dist/data-structures/base/linear-base.d.ts +277 -0
  5. package/dist/data-structures/base/linear-base.js +552 -0
  6. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -8
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +50 -37
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +64 -0
  9. package/dist/data-structures/binary-tree/avl-tree.js +64 -0
  10. package/dist/data-structures/binary-tree/binary-tree.js +5 -5
  11. package/dist/data-structures/binary-tree/bst.js +11 -11
  12. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +175 -14
  13. package/dist/data-structures/binary-tree/tree-multi-map.js +210 -40
  14. package/dist/data-structures/graph/abstract-graph.js +16 -16
  15. package/dist/data-structures/hash/hash-map.d.ts +46 -0
  16. package/dist/data-structures/hash/hash-map.js +46 -0
  17. package/dist/data-structures/heap/heap.d.ts +3 -11
  18. package/dist/data-structures/heap/heap.js +0 -10
  19. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  20. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  21. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  22. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  23. package/dist/data-structures/linked-list/singly-linked-list.d.ts +145 -75
  24. package/dist/data-structures/linked-list/singly-linked-list.js +283 -169
  25. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  26. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  27. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  28. package/dist/data-structures/queue/deque.d.ts +130 -91
  29. package/dist/data-structures/queue/deque.js +269 -169
  30. package/dist/data-structures/queue/queue.d.ts +131 -40
  31. package/dist/data-structures/queue/queue.js +181 -50
  32. package/dist/data-structures/stack/stack.d.ts +124 -11
  33. package/dist/data-structures/stack/stack.js +121 -10
  34. package/dist/data-structures/trie/trie.d.ts +4 -3
  35. package/dist/data-structures/trie/trie.js +3 -0
  36. package/dist/types/data-structures/base/base.d.ts +9 -4
  37. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  38. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  39. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  40. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  41. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  42. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  43. package/package.json +2 -2
  44. package/src/data-structures/base/iterable-element-base.ts +29 -20
  45. package/src/data-structures/base/linear-base.ts +649 -0
  46. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +51 -36
  47. package/src/data-structures/binary-tree/avl-tree.ts +64 -0
  48. package/src/data-structures/binary-tree/binary-tree.ts +5 -5
  49. package/src/data-structures/binary-tree/bst.ts +9 -9
  50. package/src/data-structures/binary-tree/tree-multi-map.ts +214 -40
  51. package/src/data-structures/graph/abstract-graph.ts +16 -16
  52. package/src/data-structures/hash/hash-map.ts +46 -0
  53. package/src/data-structures/heap/heap.ts +3 -14
  54. package/src/data-structures/heap/max-heap.ts +2 -2
  55. package/src/data-structures/heap/min-heap.ts +2 -2
  56. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  57. package/src/data-structures/linked-list/singly-linked-list.ts +307 -185
  58. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  59. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  60. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  61. package/src/data-structures/queue/deque.ts +286 -183
  62. package/src/data-structures/queue/queue.ts +196 -63
  63. package/src/data-structures/stack/stack.ts +124 -18
  64. package/src/data-structures/trie/trie.ts +7 -3
  65. package/src/types/data-structures/base/base.ts +17 -8
  66. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  67. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  68. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  69. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  70. package/src/types/data-structures/queue/deque.ts +2 -3
  71. package/src/types/data-structures/queue/queue.ts +2 -2
@@ -6,75 +6,102 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { ElementCallback, SinglyLinkedListOptions } from '../../types';
9
- import { IterableElementBase } from '../base';
10
- export declare class SinglyLinkedListNode<E = any> {
9
+ import { LinearLinkedBase, LinkedListNode } from '../base/linear-base';
10
+ export declare class SinglyLinkedListNode<E = any> extends LinkedListNode<E> {
11
11
  /**
12
12
  * The constructor function initializes an instance of a class with a given value and sets the next property to undefined.
13
13
  * @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
14
14
  * will be stored in the node of a linked list.
15
15
  */
16
16
  constructor(value: E);
17
- protected _value: E;
18
- /**
19
- * The function returns the value of a protected variable.
20
- * @returns The value of the variable `_value` is being returned.
21
- */
22
- get value(): E;
23
- /**
24
- * The above function sets the value of a variable.
25
- * @param {E} value - The parameter "value" is of type E, which means it can be any type.
26
- */
27
- set value(value: E);
28
17
  protected _next: SinglyLinkedListNode<E> | undefined;
29
- /**
30
- * The `next` function returns the next node in a singly linked list.
31
- * @returns The `next` property is being returned. It can be either a `SinglyLinkedListNode<E>`
32
- * object or `undefined`.
33
- */
34
18
  get next(): SinglyLinkedListNode<E> | undefined;
35
- /**
36
- * The "next" property of a SinglyLinkedListNode is set to the provided value.
37
- * @param {SinglyLinkedListNode<E> | undefined} value - The `value` parameter is of type
38
- * `SinglyLinkedListNode<E> | undefined`. This means that it can accept either a
39
- * `SinglyLinkedListNode` object or `undefined` as its value.
40
- */
41
19
  set next(value: SinglyLinkedListNode<E> | undefined);
42
20
  }
43
21
  /**
22
+ * 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
23
+ * 2. Bidirectional Traversal: Unlike doubly linked lists, singly linked lists can be easily traversed forwards but not backwards.
24
+ * 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
25
+ * 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
26
+ * Caution: Although our linked list classes provide methods such as at, setAt, addAt, and indexOf that are based on array indices, their time complexity, like that of the native Array.lastIndexOf, is 𝑂(𝑛). If you need to use these methods frequently, you might want to consider other data structures, such as Deque or Queue (designed for random access). Similarly, since the native Array.shift method has a time complexity of 𝑂(𝑛), using an array to simulate a queue can be inefficient. In such cases, you should use Queue or Deque, as these data structures leverage deferred array rearrangement, effectively reducing the average time complexity to 𝑂(1).
27
+ *
28
+ * @example
29
+ * // implementation of a basic text editor
30
+ * class TextEditor {
31
+ * private content: SinglyLinkedList<string>;
32
+ * private cursorIndex: number;
33
+ * private undoStack: Stack<{ operation: string; data?: any }>;
34
+ *
35
+ * constructor() {
36
+ * this.content = new SinglyLinkedList<string>();
37
+ * this.cursorIndex = 0; // Cursor starts at the beginning
38
+ * this.undoStack = new Stack<{ operation: string; data?: any }>(); // Stack to keep track of operations for undo
39
+ * }
40
+ *
41
+ * insert(char: string) {
42
+ * this.content.addAt(this.cursorIndex, char);
43
+ * this.cursorIndex++;
44
+ * this.undoStack.push({ operation: 'insert', data: { index: this.cursorIndex - 1 } });
45
+ * }
46
+ *
47
+ * delete() {
48
+ * if (this.cursorIndex === 0) return; // Nothing to delete
49
+ * const deleted = this.content.deleteAt(this.cursorIndex - 1);
50
+ * this.cursorIndex--;
51
+ * this.undoStack.push({ operation: 'delete', data: { index: this.cursorIndex, char: deleted } });
52
+ * }
53
+ *
54
+ * moveCursor(index: number) {
55
+ * this.cursorIndex = Math.max(0, Math.min(index, this.content.length));
56
+ * }
57
+ *
58
+ * undo() {
59
+ * if (this.undoStack.size === 0) return; // No operations to undo
60
+ * const lastAction = this.undoStack.pop();
61
+ *
62
+ * if (lastAction!.operation === 'insert') {
63
+ * this.content.deleteAt(lastAction!.data.index);
64
+ * this.cursorIndex = lastAction!.data.index;
65
+ * } else if (lastAction!.operation === 'delete') {
66
+ * this.content.addAt(lastAction!.data.index, lastAction!.data.char);
67
+ * this.cursorIndex = lastAction!.data.index + 1;
68
+ * }
69
+ * }
44
70
  *
71
+ * getText(): string {
72
+ * return [...this.content].join('');
73
+ * }
74
+ * }
75
+ *
76
+ * // Example Usage
77
+ * const editor = new TextEditor();
78
+ * editor.insert('H');
79
+ * editor.insert('e');
80
+ * editor.insert('l');
81
+ * editor.insert('l');
82
+ * editor.insert('o');
83
+ * console.log(editor.getText()); // 'Hello' // Output: "Hello"
84
+ *
85
+ * editor.delete();
86
+ * console.log(editor.getText()); // 'Hell' // Output: "Hell"
87
+ *
88
+ * editor.undo();
89
+ * console.log(editor.getText()); // 'Hello' // Output: "Hello"
90
+ *
91
+ * editor.moveCursor(1);
92
+ * editor.insert('a');
93
+ * console.log(editor.getText()); // 'Haello'
45
94
  */
46
- export declare class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R, SinglyLinkedList<E, R>> {
95
+ export declare class SinglyLinkedList<E = any, R = any> extends LinearLinkedBase<E, R, SinglyLinkedListNode<E>> {
47
96
  constructor(elements?: Iterable<E> | Iterable<R> | Iterable<SinglyLinkedListNode<E>>, options?: SinglyLinkedListOptions<E, R>);
48
97
  protected _head: SinglyLinkedListNode<E> | undefined;
49
- /**
50
- * The `head` function returns the first node of a singly linked list.
51
- * @returns The method is returning either a SinglyLinkedListNode object or undefined.
52
- */
53
98
  get head(): SinglyLinkedListNode<E> | undefined;
54
99
  protected _tail: SinglyLinkedListNode<E> | undefined;
55
- /**
56
- * The `tail` function returns the last node of a singly linked list.
57
- * @returns The method is returning either a SinglyLinkedListNode object or undefined.
58
- */
59
100
  get tail(): SinglyLinkedListNode<E> | undefined;
60
- /**
61
- * The above function returns the value of the first element in a linked list, or undefined if the
62
- * list is empty.
63
- * @returns The value of the first node in the linked list, or undefined if the linked list is empty.
64
- */
65
101
  get first(): E | undefined;
66
- /**
67
- * The function returns the value of the last element in a linked list, or undefined if the list is
68
- * empty.
69
- * @returns The value of the last node in the linked list, or undefined if the linked list is empty.
70
- */
71
102
  get last(): E | undefined;
72
- protected _size: number;
73
- /**
74
- * The function returns the size of an object.
75
- * @returns The size of the object, which is a number.
76
- */
77
- get size(): number;
103
+ protected _length: number;
104
+ get length(): number;
78
105
  /**
79
106
  * Time Complexity: O(n)
80
107
  * Space Complexity: O(n)
@@ -210,7 +237,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
210
237
  * @returns The method `deleteAt` returns the value of the node that was deleted, or `undefined` if the index is out of
211
238
  * bounds.
212
239
  */
213
- deleteAt(index: number): boolean;
240
+ deleteAt(index: number): E | undefined;
214
241
  /**
215
242
  * Time Complexity: O(n)
216
243
  * Space Complexity: O(1)
@@ -238,6 +265,21 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
238
265
  * successfully added at the specified index, and `false` if the index is out of bounds.
239
266
  */
240
267
  addAt(index: number, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
268
+ /**
269
+ * Time Complexity: O(n)
270
+ * Space Complexity: O(1)
271
+ *
272
+ * The function setAt(index, value) updates the value at a specified index in a data structure if the
273
+ * index exists.
274
+ * @param {number} index - The `index` parameter in the `setAt` method refers to the position in the
275
+ * data structure where you want to set a new value.
276
+ * @param {E} value - The `value` parameter in the `setAt` method represents the new value that you
277
+ * want to set at the specified index in the data structure.
278
+ * @returns The `setAt` method returns a boolean value - `true` if the value at the specified index
279
+ * is successfully updated, and `false` if the index is out of bounds (i.e., the node at that index
280
+ * does not exist).
281
+ */
282
+ setAt(index: number, value: E): boolean;
241
283
  /**
242
284
  * Time Complexity: O(1)
243
285
  * Space Complexity: O(1)
@@ -254,14 +296,6 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
254
296
  * The `clear` function resets the linked list by setting the head, tail, and length to undefined and 0 respectively.
255
297
  */
256
298
  clear(): void;
257
- /**
258
- * Time Complexity: O(n)
259
- * Space Complexity: O(n)
260
- *
261
- * The `toArray` function converts a linked list into an array.
262
- * @returns The `toArray()` method is returning an array of type `E[]`.
263
- */
264
- toArray(): E[];
265
299
  /**
266
300
  * Time Complexity: O(n)
267
301
  * Space Complexity: O(1)
@@ -270,20 +304,6 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
270
304
  * @returns The reverse() method does not return anything. It has a return type of void.
271
305
  */
272
306
  reverse(): this;
273
- /**
274
- * Time Complexity: O(n)
275
- * Space Complexity: O(1)
276
- *
277
- * The `indexOf` function in TypeScript searches for a specific element or node in a singly linked
278
- * list and returns its index if found.
279
- * @param {E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)} elementNodeOrPredicate
280
- * elementNodeOrPredicate - The `elementNodeOrPredicate` parameter in the `indexOf` method can be one
281
- * of the following types:
282
- * @returns The `indexOf` method returns the index of the first occurrence of the element that
283
- * matches the provided predicate in the singly linked list. If no matching element is found, it
284
- * returns -1.
285
- */
286
- indexOf(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): number;
287
307
  /**
288
308
  * Time Complexity: O(n)
289
309
  * Space Complexity: O(1)
@@ -332,6 +352,26 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
332
352
  * was not found.
333
353
  */
334
354
  addAfter(existingElementOrNode: E | SinglyLinkedListNode<E>, newElementOrNode: E | SinglyLinkedListNode<E>): boolean;
355
+ /**
356
+ * Time Complexity: O(n)
357
+ * Space Complexity: O(1)
358
+ *
359
+ * The function `splice` in TypeScript overrides the default behavior to remove and insert elements
360
+ * in a singly linked list while handling boundary cases.
361
+ * @param {number} start - The `start` parameter in the `splice` method indicates the index at which
362
+ * to start modifying the list. It specifies the position where elements will be added or removed.
363
+ * @param {number} [deleteCount=0] - The `deleteCount` parameter in the `splice` method specifies the
364
+ * number of elements to remove from the array starting at the specified `start` index. If
365
+ * `deleteCount` is not provided, it defaults to 0, meaning no elements will be removed but new
366
+ * elements can still be inserted at
367
+ * @param {E[]} items - The `items` parameter in the `splice` method represents the elements to be
368
+ * inserted into the list at the specified `start` index. These elements will be inserted in place of
369
+ * the elements that are removed from the list. The `splice` method allows you to add new elements to
370
+ * the list while
371
+ * @returns The `splice` method is returning a `SinglyLinkedList` containing the elements that were
372
+ * removed from the original list during the splice operation.
373
+ */
374
+ splice(start: number, deleteCount?: number, ...items: E[]): this;
335
375
  /**
336
376
  * Time Complexity: O(n)
337
377
  * Space Complexity: O(1)
@@ -353,7 +393,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
353
393
  * @returns The `clone()` method is returning a new instance of the `SinglyLinkedList` class, which
354
394
  * is a clone of the original list.
355
395
  */
356
- clone(): SinglyLinkedList<E, R>;
396
+ clone(): this;
357
397
  /**
358
398
  * Time Complexity: O(n)
359
399
  * Space Complexity: O(n)
@@ -371,7 +411,7 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
371
411
  * @returns The `filter` method is returning a new `SinglyLinkedList` object that contains the
372
412
  * elements that pass the filter condition specified by the `callback` function.
373
413
  */
374
- filter(callback: ElementCallback<E, R, boolean, SinglyLinkedList<E, R>>, thisArg?: any): SinglyLinkedList<E, R>;
414
+ filter(callback: ElementCallback<E, R, boolean>, thisArg?: any): SinglyLinkedList<E, R>;
375
415
  /**
376
416
  * Time Complexity: O(n)
377
417
  * Space Complexity: O(n)
@@ -392,11 +432,31 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
392
432
  * value of
393
433
  * @returns a new instance of the `SinglyLinkedList` class with the mapped elements.
394
434
  */
395
- map<EM, RM>(callback: ElementCallback<E, R, EM, SinglyLinkedList<E, R>>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
435
+ map<EM, RM>(callback: ElementCallback<E, R, EM>, toElementFn?: (rawElement: RM) => EM, thisArg?: any): SinglyLinkedList<EM, RM>;
436
+ /**
437
+ * The function `_createInstance` returns a new instance of `SinglyLinkedList` with the specified
438
+ * options.
439
+ * @param [options] - The `options` parameter in the `_createInstance` method is of type
440
+ * `SinglyLinkedListOptions<E, R>`, which is used to configure the behavior of the `SinglyLinkedList`
441
+ * instance being created. It is an optional parameter, meaning it can be omitted when calling the
442
+ * method.
443
+ * @returns An instance of the `SinglyLinkedList` class with an empty array and the provided options
444
+ * is being returned.
445
+ */
446
+ protected _createInstance(options?: SinglyLinkedListOptions<E, R>): this;
396
447
  /**
397
448
  * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
398
449
  */
399
450
  protected _getIterator(): IterableIterator<E>;
451
+ /**
452
+ * The function returns an iterator that iterates over the elements of a collection in reverse order.
453
+ */
454
+ protected _getReverseIterator(): IterableIterator<E>;
455
+ /**
456
+ * The function `_getNodeIterator` returns an iterator that iterates over the nodes of a singly
457
+ * linked list.
458
+ */
459
+ protected _getNodeIterator(): IterableIterator<SinglyLinkedListNode<E>>;
400
460
  /**
401
461
  * The _isPredicate function in TypeScript checks if the input is a function that takes a
402
462
  * SinglyLinkedListNode as an argument and returns a boolean.
@@ -427,4 +487,14 @@ export declare class SinglyLinkedList<E = any, R = any> extends IterableElementB
427
487
  * a function is returned that checks if a given node's value is equal to the input
428
488
  */
429
489
  protected _ensurePredicate(elementNodeOrPredicate: E | SinglyLinkedListNode<E> | ((node: SinglyLinkedListNode<E>) => boolean)): (node: SinglyLinkedListNode<E>) => boolean;
490
+ /**
491
+ * The function `_getPrevNode` returns the node before a given node in a singly linked list.
492
+ * @param node - The `node` parameter in the `_getPrevNode` method is a reference to a node in a
493
+ * singly linked list. The method is used to find the node that comes before the given node in the
494
+ * linked list.
495
+ * @returns The `_getPrevNode` method returns either the previous node of the input node in a singly
496
+ * linked list or `undefined` if the input node is the head of the list or if the input node is not
497
+ * found in the list.
498
+ */
499
+ protected _getPrevNode(node: SinglyLinkedListNode<E>): SinglyLinkedListNode<E> | undefined;
430
500
  }