linked-list-typed 1.38.9 → 1.39.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 (67) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +2 -2
  2. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  3. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  4. package/dist/data-structures/binary-tree/binary-tree.d.ts +86 -32
  5. package/dist/data-structures/binary-tree/binary-tree.js +8 -8
  6. package/dist/data-structures/binary-tree/bst.d.ts +6 -6
  7. package/dist/data-structures/binary-tree/bst.js +2 -2
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
  9. package/dist/data-structures/graph/abstract-graph.d.ts +4 -4
  10. package/dist/data-structures/graph/abstract-graph.js +5 -5
  11. package/dist/data-structures/graph/directed-graph.d.ts +4 -4
  12. package/dist/data-structures/graph/directed-graph.js +6 -6
  13. package/dist/data-structures/graph/map-graph.d.ts +1 -1
  14. package/dist/data-structures/graph/map-graph.js +1 -1
  15. package/dist/data-structures/graph/undirected-graph.d.ts +3 -3
  16. package/dist/data-structures/graph/undirected-graph.js +4 -4
  17. package/dist/data-structures/heap/heap.d.ts +10 -5
  18. package/dist/data-structures/heap/heap.js +10 -10
  19. package/dist/data-structures/heap/max-heap.d.ts +4 -1
  20. package/dist/data-structures/heap/max-heap.js +9 -7
  21. package/dist/data-structures/heap/min-heap.d.ts +4 -1
  22. package/dist/data-structures/heap/min-heap.js +9 -7
  23. package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
  24. package/dist/data-structures/matrix/matrix2d.js +3 -7
  25. package/dist/data-structures/matrix/vector2d.d.ts +0 -1
  26. package/dist/data-structures/matrix/vector2d.js +0 -1
  27. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  28. package/dist/data-structures/priority-queue/max-priority-queue.js +9 -7
  29. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  30. package/dist/data-structures/priority-queue/min-priority-queue.js +9 -7
  31. package/dist/data-structures/priority-queue/priority-queue.d.ts +5 -2
  32. package/dist/data-structures/priority-queue/priority-queue.js +2 -2
  33. package/dist/interfaces/binary-tree.d.ts +2 -2
  34. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  35. package/dist/types/helpers.d.ts +1 -4
  36. package/package.json +2 -2
  37. package/src/data-structures/binary-tree/avl-tree.ts +5 -4
  38. package/src/data-structures/binary-tree/binary-indexed-tree.ts +21 -1
  39. package/src/data-structures/binary-tree/binary-tree.ts +37 -93
  40. package/src/data-structures/binary-tree/bst.ts +11 -17
  41. package/src/data-structures/binary-tree/rb-tree.ts +2 -1
  42. package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
  43. package/src/data-structures/graph/abstract-graph.ts +16 -15
  44. package/src/data-structures/graph/directed-graph.ts +8 -7
  45. package/src/data-structures/graph/map-graph.ts +2 -2
  46. package/src/data-structures/graph/undirected-graph.ts +9 -8
  47. package/src/data-structures/hash/hash-map.ts +1 -1
  48. package/src/data-structures/hash/tree-map.ts +1 -2
  49. package/src/data-structures/hash/tree-set.ts +1 -2
  50. package/src/data-structures/heap/heap.ts +12 -12
  51. package/src/data-structures/heap/max-heap.ts +8 -6
  52. package/src/data-structures/heap/min-heap.ts +8 -6
  53. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  54. package/src/data-structures/matrix/matrix.ts +1 -1
  55. package/src/data-structures/matrix/matrix2d.ts +1 -3
  56. package/src/data-structures/matrix/vector2d.ts +1 -4
  57. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
  58. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
  59. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  60. package/src/data-structures/queue/deque.ts +4 -5
  61. package/src/data-structures/queue/queue.ts +1 -1
  62. package/src/interfaces/binary-tree.ts +2 -2
  63. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
  64. package/src/types/data-structures/matrix/navigator.ts +1 -1
  65. package/src/types/helpers.ts +1 -7
  66. package/src/types/utils/utils.ts +1 -1
  67. package/src/types/utils/validate-type.ts +2 -2
@@ -7,12 +7,16 @@
7
7
 
8
8
  import type {Comparator, DFSOrderPattern} from '../../types';
9
9
 
10
- export class Heap<E> {
10
+ export class Heap<E = any> {
11
11
  protected nodes: E[] = [];
12
12
  protected readonly comparator: Comparator<E>;
13
13
 
14
- constructor(comparator: Comparator<E>) {
15
- this.comparator = comparator;
14
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
15
+ this.comparator = options.comparator;
16
+ if (options.nodes && options.nodes.length > 0) {
17
+ this.nodes = options.nodes;
18
+ this.fix();
19
+ }
16
20
  }
17
21
 
18
22
  /**
@@ -32,15 +36,11 @@ export class Heap<E> {
32
36
 
33
37
  /**
34
38
  * Static method that creates a binary heap from an array of nodes and a comparison function.
35
- * @param nodes
36
- * @param comparator - Comparison function.
37
39
  * @returns A new Heap instance.
40
+ * @param options
38
41
  */
39
- static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
40
- const binaryHeap = new Heap<E>(comparator);
41
- binaryHeap.nodes = [...nodes];
42
- binaryHeap.fix(); // Fix heap properties
43
- return binaryHeap;
42
+ static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
43
+ return new Heap<E>(options);
44
44
  }
45
45
 
46
46
  /**
@@ -180,7 +180,7 @@ export class Heap<E> {
180
180
  * @returns A new Heap instance containing the same elements.
181
181
  */
182
182
  clone(): Heap<E> {
183
- const clonedHeap = new Heap<E>(this.comparator);
183
+ const clonedHeap = new Heap<E>({comparator: this.comparator});
184
184
  clonedHeap.nodes = [...this.nodes];
185
185
  return clonedHeap;
186
186
  }
@@ -269,7 +269,7 @@ export class FibonacciHeapNode<E> {
269
269
 
270
270
  export class FibonacciHeap<E> {
271
271
  root?: FibonacciHeapNode<E>;
272
- size: number = 0;
272
+ size = 0;
273
273
  protected min?: FibonacciHeapNode<E>;
274
274
  protected readonly comparator: Comparator<E>;
275
275
 
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return b - a;
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return b - a;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -11,14 +11,16 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- comparator: Comparator<E> = (a: E, b: E) => {
15
- if (!(typeof a === 'number' && typeof b === 'number')) {
16
- throw new Error('The a, b params of compare function must be number');
17
- } else {
18
- return a - b;
14
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
15
+ comparator: (a: E, b: E) => {
16
+ if (!(typeof a === 'number' && typeof b === 'number')) {
17
+ throw new Error('The a, b params of compare function must be number');
18
+ } else {
19
+ return a - b;
20
+ }
19
21
  }
20
22
  }
21
23
  ) {
22
- super(comparator);
24
+ super(options);
23
25
  }
24
26
  }
@@ -485,7 +485,7 @@ export class SinglyLinkedList<E = any> {
485
485
  return count;
486
486
  }
487
487
 
488
- * [Symbol.iterator]() {
488
+ *[Symbol.iterator]() {
489
489
  let current = this.head;
490
490
 
491
491
  while (current) {
@@ -14,7 +14,7 @@ export class MatrixNTI2D<V = any> {
14
14
  * given initial value or 0 if not provided.
15
15
  * @param options - An object containing the following properties:
16
16
  */
17
- constructor(options: { row: number; col: number; initialVal?: V }) {
17
+ constructor(options: {row: number; col: number; initialVal?: V}) {
18
18
  const {row, col, initialVal} = options;
19
19
  this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
20
20
  }
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import Vector2D from './vector2d';
8
+ import {Vector2D} from './vector2d';
9
9
 
10
10
  export class Matrix2D {
11
11
  private readonly _matrix: number[][];
@@ -209,5 +209,3 @@ export class Matrix2D {
209
209
  return new Vector2D(this._matrix[0][0], this._matrix[1][0]);
210
210
  }
211
211
  }
212
-
213
- export default Matrix2D;
@@ -10,8 +10,7 @@ export class Vector2D {
10
10
  public x: number = 0,
11
11
  public y: number = 0,
12
12
  public w: number = 1 // needed for matrix multiplication
13
- ) {
14
- }
13
+ ) {}
15
14
 
16
15
  /**
17
16
  * The function checks if the x and y values of a point are both zero.
@@ -313,5 +312,3 @@ export class Vector2D {
313
312
  this.y = 0;
314
313
  }
315
314
  }
316
-
317
- export default Vector2D;
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return b - a;
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return b - a;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -10,14 +10,16 @@ import type {Comparator} from '../../types';
10
10
 
11
11
  export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
12
12
  constructor(
13
- compare: Comparator<E> = (a: E, b: E) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- } else {
17
- return a - b;
13
+ options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ comparator: (a: E, b: E) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ } else {
18
+ return a - b;
19
+ }
18
20
  }
19
21
  }
20
22
  ) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
@@ -9,8 +9,8 @@
9
9
  import {Heap} from '../heap';
10
10
  import {Comparator} from '../../types';
11
11
 
12
- export class PriorityQueue<E> extends Heap<E> {
13
- constructor(comparator: Comparator<E>) {
14
- super(comparator);
12
+ export class PriorityQueue<E = any> extends Heap<E> {
13
+ constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
14
+ super(options);
15
15
  }
16
16
  }
@@ -9,8 +9,7 @@ import {DoublyLinkedList} from '../linked-list';
9
9
 
10
10
  // O(n) time complexity of obtaining the value
11
11
  // O(1) time complexity of adding at the beginning and the end
12
- export class Deque<E = any> extends DoublyLinkedList<E> {
13
- }
12
+ export class Deque<E = any> extends DoublyLinkedList<E> {}
14
13
 
15
14
  // O(1) time complexity of obtaining the value
16
15
  // O(n) time complexity of adding at the beginning and the end
@@ -20,9 +19,9 @@ export class ObjectDeque<E = number> {
20
19
  if (capacity !== undefined) this._capacity = capacity;
21
20
  }
22
21
 
23
- private _nodes: { [key: number]: E } = {};
22
+ private _nodes: {[key: number]: E} = {};
24
23
 
25
- get nodes(): { [p: number]: E } {
24
+ get nodes(): {[p: number]: E} {
26
25
  return this._nodes;
27
26
  }
28
27
 
@@ -157,7 +156,7 @@ export class ObjectDeque<E = number> {
157
156
  return this._size <= 0;
158
157
  }
159
158
 
160
- protected _seNodes(value: { [p: number]: E }) {
159
+ protected _seNodes(value: {[p: number]: E}) {
161
160
  this._nodes = value;
162
161
  }
163
162
 
@@ -183,7 +183,7 @@ export class Queue<E = any> {
183
183
  return new Queue(this.nodes.slice(this.offset));
184
184
  }
185
185
 
186
- * [Symbol.iterator]() {
186
+ *[Symbol.iterator]() {
187
187
  for (const item of this.nodes) {
188
188
  yield item;
189
189
  }
@@ -1,10 +1,10 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types';
2
+ import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback} from '../types';
3
3
 
4
4
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
5
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
6
6
 
7
7
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
8
8
 
9
- delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
9
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
10
10
  }
@@ -24,10 +24,6 @@ export enum FamilyPosition {
24
24
 
25
25
  export type BinaryTreeNodeKey = number;
26
26
 
27
- export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
28
-
29
- export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
30
-
31
27
  export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null };
32
28
 
33
29
  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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@@ -1,6 +1,6 @@
1
1
  export type Direction = 'up' | 'right' | 'down' | 'left';
2
2
 
3
- export type Turning = { [key in Direction]: Direction };
3
+ export type Turning = {[key in Direction]: Direction};
4
4
 
5
5
  export type NavigatorParams<T = any> = {
6
6
  matrix: T[][];
@@ -1,14 +1,8 @@
1
- import {BinaryTreeNodeKey} from './data-structures';
2
-
3
1
  export type Comparator<T> = (a: T, b: T) => number;
4
2
 
5
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
6
4
 
7
- export type MapCallback<N, D = any> = (node: N) => D;
8
-
9
- export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
10
-
11
- export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
5
+ export type OneParamCallback<N, D = any> = (node: N) => D;
12
6
 
13
7
  export enum CP {
14
8
  lt = 'lt',
@@ -1,5 +1,5 @@
1
1
  export type ToThunkFn = () => ReturnType<TrlFn>;
2
- export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: symbol };
2
+ export type Thunk = () => ReturnType<ToThunkFn> & {__THUNK__: symbol};
3
3
  export type TrlFn = (...args: any[]) => any;
4
4
  export type TrlAsyncFn = (...args: any[]) => any;
5
5
 
@@ -1,6 +1,6 @@
1
- export type KeyValueObject = { [key: string]: any };
1
+ export type KeyValueObject = {[key: string]: any};
2
2
 
3
- export type KeyValueObjectWithKey = { [key: string]: any; key: string | number | symbol };
3
+ export type KeyValueObjectWithKey = {[key: string]: any; key: string | number | symbol};
4
4
 
5
5
  export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
6
6