doubly-linked-list-typed 1.52.0 → 1.52.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 (51) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +11 -11
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +6 -6
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +11 -11
  4. package/dist/data-structures/binary-tree/avl-tree.js +6 -6
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +97 -97
  6. package/dist/data-structures/binary-tree/binary-tree.js +52 -52
  7. package/dist/data-structures/binary-tree/bst.d.ts +35 -35
  8. package/dist/data-structures/binary-tree/bst.js +17 -17
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +8 -8
  10. package/dist/data-structures/binary-tree/rb-tree.js +6 -6
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +10 -10
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +5 -5
  13. package/dist/data-structures/graph/directed-graph.js +2 -1
  14. package/dist/data-structures/queue/deque.d.ts +7 -0
  15. package/dist/data-structures/queue/deque.js +16 -1
  16. package/dist/data-structures/queue/queue.d.ts +18 -1
  17. package/dist/data-structures/queue/queue.js +32 -7
  18. package/dist/interfaces/binary-tree.d.ts +3 -3
  19. package/dist/types/common.d.ts +1 -22
  20. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +18 -1
  21. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -0
  22. package/dist/types/data-structures/queue/deque.d.ts +1 -0
  23. package/dist/types/data-structures/queue/queue.d.ts +3 -1
  24. package/package.json +2 -2
  25. package/src/data-structures/base/iterable-element-base.ts +2 -2
  26. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  27. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -24
  28. package/src/data-structures/binary-tree/avl-tree.ts +20 -19
  29. package/src/data-structures/binary-tree/binary-tree.ts +162 -157
  30. package/src/data-structures/binary-tree/bst.ts +54 -50
  31. package/src/data-structures/binary-tree/rb-tree.ts +18 -17
  32. package/src/data-structures/binary-tree/tree-multi-map.ts +18 -17
  33. package/src/data-structures/graph/abstract-graph.ts +15 -14
  34. package/src/data-structures/graph/directed-graph.ts +9 -7
  35. package/src/data-structures/graph/undirected-graph.ts +7 -6
  36. package/src/data-structures/hash/hash-map.ts +4 -4
  37. package/src/data-structures/heap/heap.ts +1 -1
  38. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  39. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  40. package/src/data-structures/queue/deque.ts +18 -4
  41. package/src/data-structures/queue/queue.ts +38 -8
  42. package/src/data-structures/stack/stack.ts +1 -1
  43. package/src/data-structures/trie/trie.ts +1 -1
  44. package/src/interfaces/binary-tree.ts +3 -3
  45. package/src/types/common.ts +2 -24
  46. package/src/types/data-structures/binary-tree/binary-tree.ts +21 -1
  47. package/src/types/data-structures/binary-tree/bst.ts +7 -0
  48. package/src/types/data-structures/graph/abstract-graph.ts +8 -8
  49. package/src/types/data-structures/queue/deque.ts +4 -1
  50. package/src/types/data-structures/queue/queue.ts +3 -1
  51. package/src/types/utils/utils.ts +4 -4
@@ -367,7 +367,7 @@ export class Heap<E = any, R = any> extends IterableElementBase<E, R, Heap<E, R>
367
367
  /**
368
368
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
369
369
  */
370
- protected* _getIterator(): IterableIterator<E> {
370
+ protected *_getIterator(): IterableIterator<E> {
371
371
  for (const element of this.elements) {
372
372
  yield element;
373
373
  }
@@ -803,7 +803,7 @@ export class DoublyLinkedList<E = any, R = any> extends IterableElementBase<E, R
803
803
  /**
804
804
  * The function returns an iterator that iterates over the values of a linked list.
805
805
  */
806
- protected* _getIterator(): IterableIterator<E> {
806
+ protected *_getIterator(): IterableIterator<E> {
807
807
  let current = this.head;
808
808
 
809
809
  while (current) {
@@ -751,7 +751,7 @@ export class SinglyLinkedList<E = any, R = any> extends IterableElementBase<E, R
751
751
  /**
752
752
  * The function `_getIterator` returns an iterable iterator that yields the values of a linked list.
753
753
  */
754
- protected* _getIterator(): IterableIterator<E> {
754
+ protected *_getIterator(): IterableIterator<E> {
755
755
  let current = this.head;
756
756
 
757
757
  while (current) {
@@ -32,8 +32,9 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
32
32
  super(options);
33
33
 
34
34
  if (options) {
35
- const { bucketSize } = options;
35
+ const { bucketSize, maxLen } = options;
36
36
  if (typeof bucketSize === 'number') this._bucketSize = bucketSize;
37
+ if (typeof maxLen === 'number' && maxLen > 0 && maxLen % 1 === 0) this._maxLen = maxLen;
37
38
  }
38
39
 
39
40
  let _size: number;
@@ -73,6 +74,17 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
73
74
  return this._bucketSize;
74
75
  }
75
76
 
77
+ protected _maxLen: number = -1;
78
+
79
+ /**
80
+ * The maxLen function returns the max length of the deque.
81
+ *
82
+ * @return The max length of the deque
83
+ */
84
+ get maxLen() {
85
+ return this._maxLen;
86
+ }
87
+
76
88
  protected _bucketFirst = 0;
77
89
 
78
90
  /**
@@ -193,6 +205,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
193
205
  }
194
206
  this._size += 1;
195
207
  this._buckets[this._bucketLast][this._lastInBucket] = element;
208
+ if (this._maxLen > 0 && this._size > this._maxLen) this.shift();
196
209
  return true;
197
210
  }
198
211
 
@@ -257,6 +270,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
257
270
  }
258
271
  this._size += 1;
259
272
  this._buckets[this._bucketFirst][this._firstInBucket] = element;
273
+ if (this._maxLen > 0 && this._size > this._maxLen) this.pop();
260
274
  return true;
261
275
  }
262
276
 
@@ -330,7 +344,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
330
344
  /**
331
345
  * The below function is a generator that yields elements from a collection one by one.
332
346
  */
333
- * begin(): Generator<E> {
347
+ *begin(): Generator<E> {
334
348
  let index = 0;
335
349
  while (index < this.size) {
336
350
  yield this.at(index);
@@ -342,7 +356,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
342
356
  * The function `reverseBegin()` is a generator that yields elements in reverse order starting from
343
357
  * the last element.
344
358
  */
345
- * reverseBegin(): Generator<E> {
359
+ *reverseBegin(): Generator<E> {
346
360
  let index = this.size - 1;
347
361
  while (index >= 0) {
348
362
  yield this.at(index);
@@ -834,7 +848,7 @@ export class Deque<E = any, R = any> extends IterableElementBase<E, R, Deque<E,
834
848
  * The above function is an implementation of the iterator protocol in TypeScript, allowing the
835
849
  * object to be iterated over using a for...of loop.
836
850
  */
837
- protected* _getIterator(): IterableIterator<E> {
851
+ protected *_getIterator(): IterableIterator<E> {
838
852
  for (let i = 0; i < this.size; ++i) {
839
853
  yield this.at(i);
840
854
  }
@@ -19,6 +19,12 @@ import { SinglyLinkedList } from '../linked-list';
19
19
  export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E, R>> {
20
20
  constructor(elements: Iterable<E> | Iterable<R> = [], options?: QueueOptions<E, R>) {
21
21
  super(options);
22
+
23
+ if (options) {
24
+ const { autoCompactRatio = 0.5 } = options;
25
+ this._autoCompactRatio = autoCompactRatio;
26
+ }
27
+
22
28
  if (elements) {
23
29
  for (const el of elements) {
24
30
  if (this.toElementFn) this.push(this.toElementFn(el as R));
@@ -89,6 +95,25 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
89
95
  return this.size > 0 ? this.elements[this.elements.length - 1] : undefined;
90
96
  }
91
97
 
98
+ _autoCompactRatio: number = 0.5;
99
+
100
+ /**
101
+ * This function returns the value of the autoCompactRatio property.
102
+ * @returns The `autoCompactRatio` property of the object, which is a number.
103
+ */
104
+ get autoCompactRatio(): number {
105
+ return this._autoCompactRatio;
106
+ }
107
+
108
+ /**
109
+ * The above function sets the autoCompactRatio property to a specified number in TypeScript.
110
+ * @param {number} v - The parameter `v` represents the value that will be assigned to the
111
+ * `_autoCompactRatio` property.
112
+ */
113
+ set autoCompactRatio(v: number) {
114
+ this._autoCompactRatio = v;
115
+ }
116
+
92
117
  /**
93
118
  * Time Complexity: O(n)
94
119
  * Space Complexity: O(n)
@@ -100,7 +125,6 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
100
125
  *
101
126
  * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
102
127
  * @public
103
- * @static
104
128
  * @param {E[]} elements - The "elements" parameter is an array of elements of type E.
105
129
  * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
106
130
  * array.
@@ -146,12 +170,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
146
170
  const first = this.first;
147
171
  this._offset += 1;
148
172
 
149
- if (this.offset * 2 < this.elements.length) return first;
150
-
151
- // only delete dequeued elements when reaching half size
152
- // to decrease latency of shifting elements.
153
- this._elements = this.elements.slice(this.offset);
154
- this._offset = 0;
173
+ if (this.offset / this.elements.length > this.autoCompactRatio) this.compact();
155
174
  return first;
156
175
  }
157
176
 
@@ -238,6 +257,17 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
238
257
  this._offset = 0;
239
258
  }
240
259
 
260
+ /**
261
+ * The `compact` function in TypeScript slices the elements array based on the offset and resets the
262
+ * offset to zero.
263
+ * @returns The `compact()` method is returning a boolean value of `true`.
264
+ */
265
+ compact(): boolean {
266
+ this._elements = this.elements.slice(this.offset);
267
+ this._offset = 0;
268
+ return true;
269
+ }
270
+
241
271
  /**
242
272
  * Time Complexity: O(n)
243
273
  * Space Complexity: O(n)
@@ -318,7 +348,7 @@ export class Queue<E = any, R = any> extends IterableElementBase<E, R, Queue<E,
318
348
  *
319
349
  * The function `_getIterator` returns an iterable iterator for the elements in the class.
320
350
  */
321
- protected* _getIterator(): IterableIterator<E> {
351
+ protected *_getIterator(): IterableIterator<E> {
322
352
  for (const item of this.elements.slice(this.offset)) {
323
353
  yield item;
324
354
  }
@@ -274,7 +274,7 @@ export class Stack<E = any, R = any> extends IterableElementBase<E, R, Stack<E,
274
274
  * Custom iterator for the Stack class.
275
275
  * @returns An iterator object.
276
276
  */
277
- protected* _getIterator(): IterableIterator<E> {
277
+ protected *_getIterator(): IterableIterator<E> {
278
278
  for (let i = 0; i < this.elements.length; i++) {
279
279
  yield this.elements[i];
280
280
  }
@@ -560,7 +560,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
560
560
  * The function `_getIterator` returns an iterable iterator that performs a depth-first search on a
561
561
  * trie data structure and yields all the paths to the end nodes.
562
562
  */
563
- protected* _getIterator(): IterableIterator<string> {
563
+ protected *_getIterator(): IterableIterator<string> {
564
564
  function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
565
565
  if (node.isEnd) {
566
566
  yield path;
@@ -5,7 +5,7 @@ import type {
5
5
  BinaryTreeNodeNested,
6
6
  BinaryTreeOptions,
7
7
  BTNCallback,
8
- KeyOrNodeOrEntry
8
+ BTNKeyOrNodeOrEntry
9
9
  } from '../types';
10
10
 
11
11
  export interface IBinaryTree<
@@ -19,9 +19,9 @@ export interface IBinaryTree<
19
19
 
20
20
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): TREE;
21
21
 
22
- add(keyOrNodeOrEntryOrRawElement: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
22
+ add(keyOrNodeOrEntryOrRawElement: BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
23
23
 
24
- addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
24
+ addMany(nodes: Iterable<BTNKeyOrNodeOrEntry<K, V, NODE>>, values?: Iterable<V | undefined>): boolean[];
25
25
 
26
26
  delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<NODE>[];
27
27
  }
@@ -1,11 +1,5 @@
1
1
  export type CP = 1 | -1 | 0;
2
2
 
3
- /**
4
- * Enum representing different loop types.
5
- *
6
- * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
7
- * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
8
- */
9
3
  export type IterationType = 'ITERATIVE' | 'RECURSIVE';
10
4
 
11
5
  export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
@@ -16,8 +10,6 @@ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
16
10
 
17
11
  export type NodeDisplayLayout = [string[], number, number, number];
18
12
 
19
- export type BTNCallback<N, D = any> = (node: N) => D;
20
-
21
13
  export interface IterableWithSize<T> extends Iterable<T> {
22
14
  size: number | ((...args: any[]) => number);
23
15
  }
@@ -26,22 +18,8 @@ export interface IterableWithLength<T> extends Iterable<T> {
26
18
  length: number | ((...args: any[]) => number);
27
19
  }
28
20
 
29
- export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
30
-
31
- export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
32
-
33
- export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
34
-
35
- export type BTNKeyOrNode<K, N> = K | null | undefined | N;
21
+ export type OptValue<V> = V | undefined;
36
22
 
37
- export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
38
-
39
- export type BTNodePureKeyOrNode<K, N> = K | N;
40
-
41
- export type BTNPureKeyOrNodeOrEntry<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
42
-
43
- export type BSTNKeyOrNode<K, N> = K | undefined | N;
44
-
45
- export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
23
+ export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
46
24
 
47
25
  export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
@@ -1,5 +1,5 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
2
- import { BTNEntry, IterationType } from '../../common';
2
+ import { IterationType, OptValue } from '../../common';
3
3
 
4
4
  export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5
5
 
@@ -9,3 +9,23 @@ export type BinaryTreeOptions<K, V, R> = {
9
9
  iterationType?: IterationType;
10
10
  toEntryFn?: (rawElement: R) => BTNEntry<K, V>;
11
11
  }
12
+
13
+ export type BinaryTreePrintOptions = { isShowUndefined?: boolean; isShowNull?: boolean; isShowRedBlackNIL?: boolean };
14
+
15
+ export type OptBTNOrNull<NODE> = NODE | null | undefined;
16
+
17
+ export type OptBTNKeyOrNull<K> = K | null | undefined;
18
+
19
+ export type BTNEntry<K, V> = [OptBTNKeyOrNull<K>, OptValue<V>];
20
+
21
+ export type BTNKeyOrNode<K, NODE> = OptBTNKeyOrNull<K> | NODE;
22
+
23
+ export type BTNKeyOrNodeOrEntry<K, V, NODE> = BTNEntry<K, V> | BTNKeyOrNode<K, NODE>;
24
+
25
+ export type BTNPureKeyOrNode<K, NODE> = K | NODE;
26
+
27
+ export type BTNPureKeyOrNodeOrEntry<K, V, NODE> = [K, OptValue<V>] | BTNPureKeyOrNode<K, NODE>;
28
+
29
+ export type BinaryTreeDeleteResult<NODE> = { deleted: OptBTNOrNull<NODE>; needBalanced: OptBTNOrNull<NODE> };
30
+
31
+ export type BTNCallback<NODE, D = any> = (node: NODE) => D;
@@ -9,3 +9,10 @@ export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R,
9
9
  export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
10
10
  comparator?: Comparator<K>
11
11
  }
12
+
13
+ export type OptBSTNKey<K> = K | undefined;
14
+
15
+ export type OptBSTN<NODE> = NODE | undefined;
16
+
17
+ export type BSTNKeyOrNode<K, NODE> = OptBSTNKey<K> | NODE;
18
+
@@ -2,12 +2,12 @@ export type VertexKey = string | number;
2
2
 
3
3
  export type DijkstraResult<V> =
4
4
  | {
5
- distMap: Map<V, number>;
6
- distPaths?: Map<V, V[]>;
7
- preMap: Map<V, V | undefined>;
8
- seen: Set<V>;
9
- paths: V[][];
10
- minDist: number;
11
- minPath: V[];
12
- }
5
+ distMap: Map<V, number>;
6
+ distPaths?: Map<V, V[]>;
7
+ preMap: Map<V, V | undefined>;
8
+ seen: Set<V>;
9
+ paths: V[][];
10
+ minDist: number;
11
+ minPath: V[];
12
+ }
13
13
  | undefined;
@@ -1,3 +1,6 @@
1
1
  import { IterableElementBaseOptions } from '../base';
2
2
 
3
- export type DequeOptions<E, R> = { bucketSize?: number } & IterableElementBaseOptions<E, R>;
3
+ export type DequeOptions<E, R> = {
4
+ bucketSize?: number;
5
+ maxLen?: number;
6
+ } & IterableElementBaseOptions<E, R>;
@@ -1,3 +1,5 @@
1
1
  import { IterableElementBaseOptions } from '../base';
2
2
 
3
- export type QueueOptions<E, R> = IterableElementBaseOptions<E, R> & {};
3
+ export type QueueOptions<E, R> = IterableElementBaseOptions<E, R> & {
4
+ autoCompactRatio?: number;
5
+ };
@@ -13,9 +13,9 @@ export type Comparable =
13
13
  | bigint
14
14
  | boolean
15
15
  | ({ [key in string]: any } & {
16
- valueOf(): Comparable;
17
- })
16
+ valueOf(): Comparable;
17
+ })
18
18
  | ({ [key in string]: any } & {
19
- toString(): Comparable;
20
- })
19
+ toString(): Comparable;
20
+ })
21
21
  | (() => Comparable);