min-heap-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
@@ -5,10 +5,13 @@
5
5
  * @license MIT License
6
6
  */
7
7
  import type { Comparator, DFSOrderPattern } from '../../types';
8
- export declare class Heap<E> {
8
+ export declare class Heap<E = any> {
9
9
  protected nodes: E[];
10
10
  protected readonly comparator: Comparator<E>;
11
- constructor(comparator: Comparator<E>);
11
+ constructor(options: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  /**
13
16
  * Get the size (number of elements) of the heap.
14
17
  */
@@ -20,11 +23,13 @@ export declare class Heap<E> {
20
23
  get leaf(): E | undefined;
21
24
  /**
22
25
  * Static method that creates a binary heap from an array of nodes and a comparison function.
23
- * @param nodes
24
- * @param comparator - Comparison function.
25
26
  * @returns A new Heap instance.
27
+ * @param options
26
28
  */
27
- static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E>;
29
+ static heapify<E>(options: {
30
+ nodes: E[];
31
+ comparator: Comparator<E>;
32
+ }): Heap<E>;
28
33
  /**
29
34
  * Insert an element into the heap and maintain the heap properties.
30
35
  * @param element - The element to be inserted.
@@ -8,9 +8,13 @@
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
10
10
  class Heap {
11
- constructor(comparator) {
11
+ constructor(options) {
12
12
  this.nodes = [];
13
- this.comparator = comparator;
13
+ this.comparator = options.comparator;
14
+ if (options.nodes && options.nodes.length > 0) {
15
+ this.nodes = options.nodes;
16
+ this.fix();
17
+ }
14
18
  }
15
19
  /**
16
20
  * Get the size (number of elements) of the heap.
@@ -28,15 +32,11 @@ class Heap {
28
32
  }
29
33
  /**
30
34
  * Static method that creates a binary heap from an array of nodes and a comparison function.
31
- * @param nodes
32
- * @param comparator - Comparison function.
33
35
  * @returns A new Heap instance.
36
+ * @param options
34
37
  */
35
- static heapify(nodes, comparator) {
36
- const binaryHeap = new Heap(comparator);
37
- binaryHeap.nodes = [...nodes];
38
- binaryHeap.fix(); // Fix heap properties
39
- return binaryHeap;
38
+ static heapify(options) {
39
+ return new Heap(options);
40
40
  }
41
41
  /**
42
42
  * Insert an element into the heap and maintain the heap properties.
@@ -161,7 +161,7 @@ class Heap {
161
161
  * @returns A new Heap instance containing the same elements.
162
162
  */
163
163
  clone() {
164
- const clonedHeap = new Heap(this.comparator);
164
+ const clonedHeap = new Heap({ comparator: this.comparator });
165
165
  clonedHeap.nodes = [...this.nodes];
166
166
  return clonedHeap;
167
167
  }
@@ -8,5 +8,8 @@
8
8
  import { Heap } from './heap';
9
9
  import type { Comparator } from '../../types';
10
10
  export declare class MaxHeap<E = any> extends Heap<E> {
11
- constructor(comparator?: Comparator<E>);
11
+ constructor(options?: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  }
@@ -10,15 +10,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.MaxHeap = void 0;
11
11
  const heap_1 = require("./heap");
12
12
  class MaxHeap extends heap_1.Heap {
13
- constructor(comparator = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return b - a;
13
+ constructor(options = {
14
+ comparator: (a, b) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ }
18
+ else {
19
+ return b - a;
20
+ }
19
21
  }
20
22
  }) {
21
- super(comparator);
23
+ super(options);
22
24
  }
23
25
  }
24
26
  exports.MaxHeap = MaxHeap;
@@ -8,5 +8,8 @@
8
8
  import { Heap } from './heap';
9
9
  import type { Comparator } from '../../types';
10
10
  export declare class MinHeap<E = any> extends Heap<E> {
11
- constructor(comparator?: Comparator<E>);
11
+ constructor(options?: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  }
@@ -10,15 +10,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.MinHeap = void 0;
11
11
  const heap_1 = require("./heap");
12
12
  class MinHeap extends heap_1.Heap {
13
- constructor(comparator = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return a - b;
13
+ constructor(options = {
14
+ comparator: (a, b) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ }
18
+ else {
19
+ return a - b;
20
+ }
19
21
  }
20
22
  }) {
21
- super(comparator);
23
+ super(options);
22
24
  }
23
25
  }
24
26
  exports.MinHeap = MinHeap;
@@ -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
  export declare class Matrix2D {
10
10
  private readonly _matrix;
11
11
  /**
@@ -105,4 +105,3 @@ export declare class Matrix2D {
105
105
  */
106
106
  toVector(): Vector2D;
107
107
  }
108
- export default Matrix2D;
@@ -1,7 +1,4 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.Matrix2D = void 0;
7
4
  /**
@@ -11,7 +8,7 @@ exports.Matrix2D = void 0;
11
8
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
12
9
  * @license MIT License
13
10
  */
14
- const vector2d_1 = __importDefault(require("./vector2d"));
11
+ const vector2d_1 = require("./vector2d");
15
12
  class Matrix2D {
16
13
  /**
17
14
  * The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
@@ -23,7 +20,7 @@ class Matrix2D {
23
20
  if (typeof value === 'undefined') {
24
21
  this._matrix = Matrix2D.identity;
25
22
  }
26
- else if (value instanceof vector2d_1.default) {
23
+ else if (value instanceof vector2d_1.Vector2D) {
27
24
  this._matrix = Matrix2D.identity;
28
25
  this._matrix[0][0] = value.x;
29
26
  this._matrix[1][0] = value.y;
@@ -196,8 +193,7 @@ class Matrix2D {
196
193
  * the first column of the matrix.
197
194
  */
198
195
  toVector() {
199
- return new vector2d_1.default(this._matrix[0][0], this._matrix[1][0]);
196
+ return new vector2d_1.Vector2D(this._matrix[0][0], this._matrix[1][0]);
200
197
  }
201
198
  }
202
199
  exports.Matrix2D = Matrix2D;
203
- exports.default = Matrix2D;
@@ -198,4 +198,3 @@ export declare class Vector2D {
198
198
  */
199
199
  zero(): void;
200
200
  }
201
- export default Vector2D;
@@ -288,4 +288,3 @@ class Vector2D {
288
288
  }
289
289
  }
290
290
  exports.Vector2D = Vector2D;
291
- exports.default = Vector2D;
@@ -8,5 +8,8 @@
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  import type { Comparator } from '../../types';
10
10
  export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(compare?: Comparator<E>);
11
+ constructor(options?: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  }
@@ -10,15 +10,17 @@ exports.MaxPriorityQueue = void 0;
10
10
  */
11
11
  const priority_queue_1 = require("./priority-queue");
12
12
  class MaxPriorityQueue extends priority_queue_1.PriorityQueue {
13
- constructor(compare = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return b - a;
13
+ constructor(options = {
14
+ comparator: (a, b) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ }
18
+ else {
19
+ return b - a;
20
+ }
19
21
  }
20
22
  }) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
24
26
  exports.MaxPriorityQueue = MaxPriorityQueue;
@@ -8,5 +8,8 @@
8
8
  import { PriorityQueue } from './priority-queue';
9
9
  import type { Comparator } from '../../types';
10
10
  export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(compare?: Comparator<E>);
11
+ constructor(options?: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  }
@@ -10,15 +10,17 @@ exports.MinPriorityQueue = void 0;
10
10
  */
11
11
  const priority_queue_1 = require("./priority-queue");
12
12
  class MinPriorityQueue extends priority_queue_1.PriorityQueue {
13
- constructor(compare = (a, b) => {
14
- if (!(typeof a === 'number' && typeof b === 'number')) {
15
- throw new Error('The a, b params of compare function must be number');
16
- }
17
- else {
18
- return a - b;
13
+ constructor(options = {
14
+ comparator: (a, b) => {
15
+ if (!(typeof a === 'number' && typeof b === 'number')) {
16
+ throw new Error('The a, b params of compare function must be number');
17
+ }
18
+ else {
19
+ return a - b;
20
+ }
19
21
  }
20
22
  }) {
21
- super(compare);
23
+ super(options);
22
24
  }
23
25
  }
24
26
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -7,6 +7,9 @@
7
7
  */
8
8
  import { Heap } from '../heap';
9
9
  import { Comparator } from '../../types';
10
- export declare class PriorityQueue<E> extends Heap<E> {
11
- constructor(comparator: Comparator<E>);
10
+ export declare class PriorityQueue<E = any> extends Heap<E> {
11
+ constructor(options: {
12
+ comparator: Comparator<E>;
13
+ nodes?: E[];
14
+ });
12
15
  }
@@ -10,8 +10,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.PriorityQueue = void 0;
11
11
  const heap_1 = require("../heap");
12
12
  class PriorityQueue extends heap_1.Heap {
13
- constructor(comparator) {
14
- super(comparator);
13
+ constructor(options) {
14
+ super(options);
15
15
  }
16
16
  }
17
17
  exports.PriorityQueue = PriorityQueue;
@@ -1,7 +1,7 @@
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
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
4
4
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
5
5
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
6
- delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
6
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
7
7
  }
@@ -19,8 +19,6 @@ export declare enum FamilyPosition {
19
19
  MAL_NODE = "MAL_NODE"
20
20
  }
21
21
  export type BinaryTreeNodeKey = number;
22
- export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
23
- export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
24
22
  export type BinaryTreeDeletedResult<N> = {
25
23
  deleted: N | null | undefined;
26
24
  needBalanced: N | null;
@@ -1,9 +1,6 @@
1
- import { BinaryTreeNodeKey } from './data-structures';
2
1
  export type Comparator<T> = (a: T, b: T) => number;
3
2
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
4
- export type MapCallback<N, D = any> = (node: N) => D;
5
- export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
6
- export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
3
+ export type OneParamCallback<N, D = any> = (node: N) => D;
7
4
  export declare enum CP {
8
5
  lt = "lt",
9
6
  eq = "eq",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.38.9",
3
+ "version": "1.39.1",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -131,6 +131,6 @@
131
131
  "typescript": "^4.9.5"
132
132
  },
133
133
  "dependencies": {
134
- "data-structure-typed": "^1.38.9"
134
+ "data-structure-typed": "^1.39.0"
135
135
  }
136
136
  }
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import {BST, BSTNode} from './bst';
9
9
  import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
10
- import {MapCallback} from '../../types';
10
+ import {OneParamCallback} 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> {
@@ -21,7 +21,8 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
21
21
 
22
22
  export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N> {
24
+ implements IBinaryTree<V, N>
25
+ {
25
26
  /**
26
27
  * This is a constructor function for an AVL tree data structure in TypeScript.
27
28
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -73,7 +74,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
73
74
  * `this._defaultCallbackByKey`
74
75
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
75
76
  */
76
- override delete<C extends MapCallback<N>>(
77
+ override delete<C extends OneParamCallback<N>>(
77
78
  identifier: ReturnType<C>,
78
79
  callback: C = this._defaultCallbackByKey as C
79
80
  ): BinaryTreeDeletedResult<N>[] {
@@ -160,7 +161,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
160
161
  // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
161
162
  switch (
162
163
  this._balanceFactor(A) // second O(1)
163
- ) {
164
+ ) {
164
165
  case -2:
165
166
  if (A && A.left) {
166
167
  if (this._balanceFactor(A.left) <= 0) {
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
17
17
  * @param - - `frequency`: The default frequency value. It is optional and has a default
18
18
  * value of 0.
19
19
  */
20
- constructor({frequency = 0, max}: { frequency?: number; max: number }) {
20
+ constructor({frequency = 0, max}: {frequency?: number; max: number}) {
21
21
  this._freq = frequency;
22
22
  this._max = max;
23
23
  this._freqMap = {0: 0};
@@ -143,6 +143,26 @@ export class BinaryIndexedTree {
143
143
  return this._binarySearch(sum, (x, y) => x <= y);
144
144
  }
145
145
 
146
+ /**
147
+ * The function calculates the prefix sum of an array using a binary indexed tree.
148
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
149
+ * array for which we want to calculate the prefix sum.
150
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
151
+ * `i`.
152
+ */
153
+ getPrefixSum(i: number): number {
154
+ this._checkIndex(i);
155
+ i++; // Convert to 1-based index
156
+
157
+ let sum = 0;
158
+ while (i > 0) {
159
+ sum += this._getFrequency(i);
160
+ i -= i & -i;
161
+ }
162
+
163
+ return sum;
164
+ }
165
+
146
166
  /**
147
167
  * The function returns the value of a specific index in a freqMap data structure, or a default value if
148
168
  * the index is not found.