data-structure-typed 1.38.8 → 1.39.0

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 (88) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +9 -9
  3. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  4. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  5. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +3 -3
  10. package/dist/cjs/data-structures/graph/abstract-graph.js +4 -4
  11. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/directed-graph.d.ts +4 -4
  13. package/dist/cjs/data-structures/graph/directed-graph.js +6 -6
  14. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +3 -3
  15. package/dist/cjs/data-structures/graph/undirected-graph.js +4 -4
  16. package/dist/cjs/data-structures/heap/heap.d.ts +10 -5
  17. package/dist/cjs/data-structures/heap/heap.js +10 -10
  18. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  19. package/dist/cjs/data-structures/heap/max-heap.d.ts +4 -1
  20. package/dist/cjs/data-structures/heap/max-heap.js +9 -7
  21. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/min-heap.d.ts +4 -1
  23. package/dist/cjs/data-structures/heap/min-heap.js +9 -7
  24. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  25. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  26. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  27. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  28. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  29. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  30. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  31. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  32. package/dist/cjs/data-structures/priority-queue/priority-queue.js +2 -2
  33. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  34. package/dist/cjs/data-structures/trie/trie.d.ts +2 -1
  35. package/dist/cjs/data-structures/trie/trie.js +3 -2
  36. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  37. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
  38. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +17 -0
  39. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +3 -3
  40. package/dist/mjs/data-structures/graph/abstract-graph.js +4 -4
  41. package/dist/mjs/data-structures/graph/directed-graph.d.ts +4 -4
  42. package/dist/mjs/data-structures/graph/directed-graph.js +6 -6
  43. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +3 -3
  44. package/dist/mjs/data-structures/graph/undirected-graph.js +4 -4
  45. package/dist/mjs/data-structures/heap/heap.d.ts +10 -5
  46. package/dist/mjs/data-structures/heap/heap.js +10 -10
  47. package/dist/mjs/data-structures/heap/max-heap.d.ts +4 -1
  48. package/dist/mjs/data-structures/heap/max-heap.js +9 -7
  49. package/dist/mjs/data-structures/heap/min-heap.d.ts +4 -1
  50. package/dist/mjs/data-structures/heap/min-heap.js +9 -7
  51. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +4 -1
  52. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +9 -7
  53. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +4 -1
  54. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +9 -7
  55. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +5 -2
  56. package/dist/mjs/data-structures/priority-queue/priority-queue.js +2 -2
  57. package/dist/mjs/data-structures/trie/trie.d.ts +2 -1
  58. package/dist/mjs/data-structures/trie/trie.js +3 -2
  59. package/dist/umd/data-structure-typed.min.js +1 -1
  60. package/dist/umd/data-structure-typed.min.js.map +1 -1
  61. package/package.json +5 -5
  62. package/src/data-structures/binary-tree/binary-indexed-tree.ts +20 -0
  63. package/src/data-structures/binary-tree/binary-tree.ts +2 -2
  64. package/src/data-structures/binary-tree/bst.ts +3 -1
  65. package/src/data-structures/binary-tree/rb-tree.ts +3 -1
  66. package/src/data-structures/graph/abstract-graph.ts +5 -5
  67. package/src/data-structures/graph/directed-graph.ts +6 -6
  68. package/src/data-structures/graph/undirected-graph.ts +4 -4
  69. package/src/data-structures/heap/heap.ts +12 -12
  70. package/src/data-structures/heap/max-heap.ts +8 -6
  71. package/src/data-structures/heap/min-heap.ts +8 -6
  72. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -6
  73. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -6
  74. package/src/data-structures/priority-queue/priority-queue.ts +3 -3
  75. package/src/data-structures/trie/trie.ts +4 -2
  76. package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +35 -0
  77. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -5
  78. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  79. package/test/unit/data-structures/binary-tree/overall.test.ts +18 -18
  80. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +1 -1
  81. package/test/unit/data-structures/graph/directed-graph.test.ts +3 -3
  82. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  83. package/test/unit/data-structures/graph/undirected-graph.test.ts +2 -2
  84. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  85. package/test/unit/data-structures/heap/max-heap.test.ts +1 -1
  86. package/test/unit/data-structures/heap/min-heap.test.ts +1 -1
  87. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +3 -3
  88. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +10 -10
@@ -110,7 +110,7 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
110
110
  * (VertexKey). It represents the second vertex of the edge that needs to be removed.
111
111
  * @returns the removed edge (E) if it exists, or null if either of the vertices (V) does not exist.
112
112
  */
113
- removeEdgeBetween(v1, v2) {
113
+ deleteEdgeBetween(v1, v2) {
114
114
  const vertex1 = this._getVertex(v1);
115
115
  const vertex2 = this._getVertex(v2);
116
116
  if (!vertex1 || !vertex2) {
@@ -128,12 +128,12 @@ class UndirectedGraph extends abstract_graph_1.AbstractGraph {
128
128
  return removed;
129
129
  }
130
130
  /**
131
- * The removeEdge function removes an edge between two vertices in a graph.
131
+ * The deleteEdge function removes an edge between two vertices in a graph.
132
132
  * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
133
133
  * @returns The method is returning either the removed edge (of type E) or null if the edge was not found.
134
134
  */
135
- removeEdge(edge) {
136
- return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
135
+ deleteEdge(edge) {
136
+ return this.deleteEdgeBetween(edge.vertices[0], edge.vertices[1]);
137
137
  }
138
138
  /**
139
139
  * The function `degreeOf` returns the degree of a vertex in a graph, which is the number of edges connected to that
@@ -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.
@@ -10,8 +10,12 @@ exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
10
10
  class Heap {
11
11
  nodes = [];
12
12
  comparator;
13
- constructor(comparator) {
14
- this.comparator = comparator;
13
+ constructor(options) {
14
+ this.comparator = options.comparator;
15
+ if (options.nodes && options.nodes.length > 0) {
16
+ this.nodes = options.nodes;
17
+ this.fix();
18
+ }
15
19
  }
16
20
  /**
17
21
  * 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;
@@ -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;
@@ -77,8 +77,9 @@ export declare class Trie {
77
77
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
78
78
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
79
79
  * @param {number} max - The max count of words will be found
80
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
80
81
  * @returns {string[]} an array of strings.
81
82
  */
82
- getWords(prefix?: string, max?: number): string[];
83
+ getWords(prefix?: string, max?: number, isAllWhenEmptyPrefix?: boolean): string[];
83
84
  private _caseProcess;
84
85
  }
@@ -231,9 +231,10 @@ class Trie {
231
231
  * @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
232
232
  * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
233
233
  * @param {number} max - The max count of words will be found
234
+ * @param isAllWhenEmptyPrefix - If true, when the prefix provided as '', returns all the words in the trie.
234
235
  * @returns {string[]} an array of strings.
235
236
  */
236
- getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
237
+ getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
237
238
  prefix = this._caseProcess(prefix);
238
239
  const words = [];
239
240
  let found = 0;
@@ -259,7 +260,7 @@ class Trie {
259
260
  startNode = nodeC;
260
261
  }
261
262
  }
262
- if (startNode !== this.root)
263
+ if (isAllWhenEmptyPrefix || startNode !== this.root)
263
264
  dfs(startNode, prefix);
264
265
  return words;
265
266
  }