data-structure-typed 1.35.1 → 1.36.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 (214) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +21 -0
  4. package/dist/data-structures/binary-tree/abstract-binary-tree.js +13 -864
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
  7. package/dist/data-structures/binary-tree/avl-tree.js +90 -3
  8. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  9. package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +536 -0
  14. package/dist/data-structures/binary-tree/binary-tree.js +1194 -2
  15. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  16. package/dist/data-structures/binary-tree/bst.d.ts +133 -0
  17. package/dist/data-structures/binary-tree/bst.js +114 -0
  18. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  19. package/dist/data-structures/binary-tree/index.d.ts +12 -0
  20. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  21. package/dist/data-structures/binary-tree/rb-tree.js.map +1 -1
  22. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  23. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  24. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  25. package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
  26. package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
  28. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  29. package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  30. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  31. package/dist/data-structures/graph/abstract-graph.js +270 -7
  32. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  33. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  34. package/dist/data-structures/graph/directed-graph.js +167 -0
  35. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  36. package/dist/data-structures/graph/index.d.ts +4 -0
  37. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  38. package/dist/data-structures/graph/map-graph.js +54 -0
  39. package/dist/data-structures/graph/map-graph.js.map +1 -1
  40. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  41. package/dist/data-structures/graph/undirected-graph.js +105 -0
  42. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  43. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  44. package/dist/data-structures/hash/coordinate-map.js +35 -0
  45. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  46. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  47. package/dist/data-structures/hash/coordinate-set.js +28 -0
  48. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  49. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  50. package/dist/data-structures/hash/hash-map.js +29 -1
  51. package/dist/data-structures/hash/hash-map.js.map +1 -1
  52. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  53. package/dist/data-structures/hash/hash-table.js +88 -6
  54. package/dist/data-structures/hash/hash-table.js.map +1 -1
  55. package/dist/data-structures/hash/index.d.ts +7 -0
  56. package/dist/data-structures/hash/pair.d.ts +2 -0
  57. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  58. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  59. package/dist/data-structures/heap/heap.d.ts +99 -0
  60. package/dist/data-structures/heap/heap.js +200 -78
  61. package/dist/data-structures/heap/heap.js.map +1 -1
  62. package/dist/data-structures/heap/index.d.ts +3 -0
  63. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  64. package/dist/data-structures/heap/max-heap.js +16 -6
  65. package/dist/data-structures/heap/max-heap.js.map +1 -1
  66. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  67. package/dist/data-structures/heap/min-heap.js +16 -6
  68. package/dist/data-structures/heap/min-heap.js.map +1 -1
  69. package/dist/data-structures/index.d.ts +11 -0
  70. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  71. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
  72. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  73. package/dist/data-structures/linked-list/index.d.ts +3 -0
  74. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  75. package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
  76. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  77. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  78. package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
  79. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  80. package/dist/data-structures/matrix/index.d.ts +4 -0
  81. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  82. package/dist/data-structures/matrix/matrix.js +15 -0
  83. package/dist/data-structures/matrix/matrix.js.map +1 -1
  84. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  85. package/dist/data-structures/matrix/matrix2d.js +91 -2
  86. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  87. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  88. package/dist/data-structures/matrix/navigator.js +28 -0
  89. package/dist/data-structures/matrix/navigator.js.map +1 -1
  90. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  91. package/dist/data-structures/matrix/vector2d.js +188 -1
  92. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  93. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  94. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  95. package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
  96. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  97. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  98. package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
  99. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  100. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  101. package/dist/data-structures/priority-queue/priority-queue.js +11 -174
  102. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  103. package/dist/data-structures/queue/deque.d.ts +165 -0
  104. package/dist/data-structures/queue/deque.js +124 -0
  105. package/dist/data-structures/queue/deque.js.map +1 -1
  106. package/dist/data-structures/queue/index.d.ts +2 -0
  107. package/dist/data-structures/queue/queue.d.ts +107 -0
  108. package/dist/data-structures/queue/queue.js +80 -0
  109. package/dist/data-structures/queue/queue.js.map +1 -1
  110. package/dist/data-structures/stack/index.d.ts +1 -0
  111. package/dist/data-structures/stack/stack.d.ts +63 -0
  112. package/dist/data-structures/stack/stack.js +50 -0
  113. package/dist/data-structures/stack/stack.js.map +1 -1
  114. package/dist/data-structures/tree/index.d.ts +1 -0
  115. package/dist/data-structures/tree/tree.d.ts +14 -0
  116. package/dist/data-structures/tree/tree.js +1 -0
  117. package/dist/data-structures/tree/tree.js.map +1 -1
  118. package/dist/data-structures/trie/index.d.ts +1 -0
  119. package/dist/data-structures/trie/trie.d.ts +61 -0
  120. package/dist/data-structures/trie/trie.js +36 -0
  121. package/dist/data-structures/trie/trie.js.map +1 -1
  122. package/dist/index.d.ts +4 -0
  123. package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
  124. package/dist/interfaces/abstract-graph.d.ts +5 -0
  125. package/dist/interfaces/avl-tree.d.ts +7 -0
  126. package/dist/interfaces/binary-tree.d.ts +6 -0
  127. package/dist/interfaces/bst.d.ts +6 -0
  128. package/dist/interfaces/directed-graph.d.ts +3 -0
  129. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  130. package/dist/interfaces/heap.d.ts +1 -0
  131. package/dist/interfaces/index.d.ts +15 -0
  132. package/dist/interfaces/navigator.d.ts +1 -0
  133. package/dist/interfaces/priority-queue.d.ts +1 -0
  134. package/dist/interfaces/rb-tree.d.ts +6 -0
  135. package/dist/interfaces/segment-tree.d.ts +1 -0
  136. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  137. package/dist/interfaces/tree-multiset.d.ts +6 -0
  138. package/dist/interfaces/undirected-graph.d.ts +3 -0
  139. package/dist/types/data-structures/abstract-binary-tree.d.ts +32 -0
  140. package/dist/types/data-structures/abstract-binary-tree.js +6 -0
  141. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  142. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  143. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  144. package/dist/types/data-structures/binary-tree.d.ts +6 -0
  145. package/dist/types/data-structures/bst.d.ts +13 -0
  146. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  147. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  148. package/dist/types/data-structures/hash.d.ts +1 -0
  149. package/dist/types/data-structures/heap.d.ts +1 -0
  150. package/dist/types/data-structures/index.d.ts +16 -0
  151. package/dist/types/data-structures/map-graph.d.ts +1 -0
  152. package/dist/types/data-structures/navigator.d.ts +14 -0
  153. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  154. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  155. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  156. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  157. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  158. package/dist/types/helpers.d.ts +1 -0
  159. package/dist/types/index.d.ts +3 -0
  160. package/dist/types/utils/index.d.ts +2 -0
  161. package/dist/types/utils/utils.d.ts +7 -0
  162. package/dist/types/utils/validate-type.d.ts +19 -0
  163. package/dist/utils/index.d.ts +1 -0
  164. package/dist/utils/utils.d.ts +19 -0
  165. package/lib/data-structures/binary-tree/abstract-binary-tree.d.ts +3 -509
  166. package/lib/data-structures/binary-tree/abstract-binary-tree.js +1 -1175
  167. package/lib/data-structures/binary-tree/avl-tree.d.ts +1 -1
  168. package/lib/data-structures/binary-tree/binary-tree.d.ts +506 -1
  169. package/lib/data-structures/binary-tree/binary-tree.js +1173 -2
  170. package/lib/data-structures/binary-tree/bst.d.ts +1 -1
  171. package/lib/data-structures/binary-tree/rb-tree.d.ts +1 -1
  172. package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -1
  173. package/lib/data-structures/graph/abstract-graph.js +3 -5
  174. package/lib/data-structures/heap/heap.d.ts +84 -68
  175. package/lib/data-structures/heap/heap.js +168 -107
  176. package/lib/data-structures/heap/max-heap.d.ts +6 -17
  177. package/lib/data-structures/heap/max-heap.js +11 -17
  178. package/lib/data-structures/heap/min-heap.d.ts +6 -18
  179. package/lib/data-structures/heap/min-heap.js +11 -18
  180. package/lib/data-structures/priority-queue/max-priority-queue.d.ts +4 -7
  181. package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
  182. package/lib/data-structures/priority-queue/min-priority-queue.d.ts +4 -7
  183. package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
  184. package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
  185. package/lib/data-structures/priority-queue/priority-queue.js +11 -315
  186. package/lib/types/data-structures/abstract-binary-tree.d.ts +1 -3
  187. package/lib/types/data-structures/binary-tree.d.ts +4 -2
  188. package/lib/types/data-structures/heap.d.ts +1 -3
  189. package/package.json +10 -6
  190. package/src/data-structures/binary-tree/abstract-binary-tree.ts +4 -1527
  191. package/src/data-structures/binary-tree/avl-tree.ts +3 -3
  192. package/src/data-structures/binary-tree/binary-tree.ts +1524 -5
  193. package/src/data-structures/binary-tree/bst.ts +3 -3
  194. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  195. package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
  196. package/src/data-structures/graph/abstract-graph.ts +3 -5
  197. package/src/data-structures/heap/heap.ts +167 -143
  198. package/src/data-structures/heap/max-heap.ts +15 -22
  199. package/src/data-structures/heap/min-heap.ts +15 -23
  200. package/src/data-structures/priority-queue/max-priority-queue.ts +13 -46
  201. package/src/data-structures/priority-queue/min-priority-queue.ts +13 -47
  202. package/src/data-structures/priority-queue/priority-queue.ts +7 -350
  203. package/src/types/data-structures/abstract-binary-tree.ts +1 -1
  204. package/src/types/data-structures/binary-tree.ts +2 -2
  205. package/src/types/data-structures/heap.ts +1 -5
  206. package/test/unit/data-structures/heap/heap.test.ts +26 -18
  207. package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
  208. package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
  209. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +9 -10
  210. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
  211. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +14 -30
  212. package/umd/bundle.min.js +1 -1
  213. package/umd/bundle.min.js.LICENSE.txt +15 -0
  214. package/umd/bundle.min.js.map +1 -1
@@ -16,9 +16,9 @@ import {CP, LoopType} from '../../types';
16
16
  import {BinaryTree, BinaryTreeNode} from './binary-tree';
17
17
  import {IBST, IBSTNode} from '../../interfaces';
18
18
 
19
- export class BSTNode<V = any, NEIGHBOR extends BSTNode<V, NEIGHBOR> = BSTNodeNested<V>>
20
- extends BinaryTreeNode<V, NEIGHBOR>
21
- implements IBSTNode<V, NEIGHBOR>
19
+ export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>>
20
+ extends BinaryTreeNode<V, FAMILY>
21
+ implements IBSTNode<V, FAMILY>
22
22
  {
23
23
  constructor(key: BinaryTreeNodeKey, val?: V) {
24
24
  super(key, val);
@@ -2,9 +2,9 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
2
2
  import {IRBTree, IRBTreeNode} from '../../interfaces';
3
3
  import {BST, BSTNode} from './bst';
4
4
 
5
- export class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTreeNodeNested<V>>
6
- extends BSTNode<V, NEIGHBOR>
7
- implements IRBTreeNode<V, NEIGHBOR>
5
+ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>>
6
+ extends BSTNode<V, FAMILY>
7
+ implements IRBTreeNode<V, FAMILY>
8
8
  {
9
9
  private _color: RBColor;
10
10
 
@@ -10,9 +10,9 @@ import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType}
10
10
  import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
11
11
  import {AVLTree, AVLTreeNode} from './avl-tree';
12
12
 
13
- export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>>
14
- extends AVLTreeNode<V, NEIGHBOR>
15
- implements ITreeMultisetNode<V, NEIGHBOR>
13
+ export class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>>
14
+ extends AVLTreeNode<V, FAMILY>
15
+ implements ITreeMultisetNode<V, FAMILY>
16
16
  {
17
17
  /**
18
18
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
  import {arrayRemove, uuidV4} from '../../utils';
@@ -622,9 +622,7 @@ export abstract class AbstractGraph<
622
622
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
623
623
  }
624
624
 
625
- const heap = new PriorityQueue<{key: number; val: V}>({
626
- comparator: (a, b) => a.key - b.key
627
- });
625
+ const heap = new PriorityQueue<{key: number; val: V}>((a, b) => a.key - b.key);
628
626
  heap.add({key: 0, val: srcVertex});
629
627
 
630
628
  distMap.set(srcVertex, 0);
@@ -1,212 +1,236 @@
1
1
  /**
2
2
  * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
3
+ * @author Kirk Qi
4
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
5
  * @license MIT License
7
6
  */
8
- import {PriorityQueue} from '../priority-queue';
9
- import type {HeapOptions} from '../../types';
10
7
 
11
- export class HeapItem<V = any> {
12
- /**
13
- * The constructor function initializes an instance of a class with a priority and a value.
14
- * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
15
- * optional and has a default value of `NaN`.
16
- * @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
17
- * `V` or `null`.
18
- */
19
- constructor(priority: number = Number.MAX_SAFE_INTEGER, val: V | null = null) {
20
- this._val = val;
21
- this._priority = priority;
22
- }
8
+ import type {CompareFunction} from '../../types';
23
9
 
24
- private _priority: number;
10
+ export class Heap<E> {
11
+ protected nodes: E[] = [];
12
+ private readonly comparator: CompareFunction<E>;
25
13
 
26
- get priority(): number {
27
- return this._priority;
14
+ constructor(comparator: CompareFunction<E>) {
15
+ this.comparator = comparator;
28
16
  }
29
17
 
30
- set priority(value: number) {
31
- this._priority = value;
18
+ /**
19
+ * Insert an element into the heap and maintain the heap properties.
20
+ * @param value - The element to be inserted.
21
+ */
22
+ add(value: E): Heap<E> {
23
+ this.nodes.push(value);
24
+ this.bubbleUp(this.nodes.length - 1);
25
+ return this;
32
26
  }
33
27
 
34
- private _val: V | null;
35
-
36
- get val(): V | null {
37
- return this._val;
38
- }
28
+ /**
29
+ * Remove and return the top element (smallest or largest element) from the heap.
30
+ * @returns The top element or null if the heap is empty.
31
+ */
32
+ poll(): E | null {
33
+ if (this.nodes.length === 0) {
34
+ return null;
35
+ }
36
+ if (this.nodes.length === 1) {
37
+ return this.nodes.pop() as E;
38
+ }
39
39
 
40
- set val(value: V | null) {
41
- this._val = value;
40
+ const topValue = this.nodes[0];
41
+ this.nodes[0] = this.nodes.pop() as E;
42
+ this.sinkDown(0);
43
+ return topValue;
42
44
  }
43
- }
44
45
 
45
- export abstract class Heap<V = any> {
46
46
  /**
47
- * The function is a constructor for a class that initializes a priority callback function based on the
48
- * options provided.
49
- * @param [options] - An optional object that contains configuration options for the Heap.
47
+ * Float operation to maintain heap properties after adding an element.
48
+ * @param index - The index of the newly added element.
50
49
  */
51
- protected constructor(options?: HeapOptions<V>) {
52
- if (options) {
53
- const {priorityExtractor} = options;
54
- if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
55
- throw new Error('.constructor expects a valid priority function');
50
+ protected bubbleUp(index: number): void {
51
+ const element = this.nodes[index];
52
+ while (index > 0) {
53
+ const parentIndex = Math.floor((index - 1) / 2);
54
+ const parent = this.nodes[parentIndex];
55
+ if (this.comparator(element, parent) < 0) {
56
+ this.nodes[index] = parent;
57
+ this.nodes[parentIndex] = element;
58
+ index = parentIndex;
59
+ } else {
60
+ break;
56
61
  }
57
- this._priorityExtractor = priorityExtractor || (el => +el);
58
- } else {
59
- this._priorityExtractor = el => +el;
60
62
  }
61
63
  }
62
64
 
63
- protected abstract _pq: PriorityQueue<HeapItem<V>>;
65
+ /**
66
+ * Sinking operation to maintain heap properties after removing the top element.
67
+ * @param index - The index from which to start sinking.
68
+ */
69
+ protected sinkDown(index: number): void {
70
+ const leftChildIndex = 2 * index + 1;
71
+ const rightChildIndex = 2 * index + 2;
72
+ const length = this.nodes.length;
73
+ let targetIndex = index;
74
+
75
+ if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
76
+ targetIndex = leftChildIndex;
77
+ }
78
+ if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
79
+ targetIndex = rightChildIndex;
80
+ }
64
81
 
65
- get pq() {
66
- return this._pq;
82
+ if (targetIndex !== index) {
83
+ const temp = this.nodes[index];
84
+ this.nodes[index] = this.nodes[targetIndex];
85
+ this.nodes[targetIndex] = temp;
86
+ this.sinkDown(targetIndex);
87
+ }
67
88
  }
68
89
 
69
- protected _priorityExtractor: (val: V) => number;
70
- get priorityExtractor() {
71
- return this._priorityExtractor;
90
+ /**
91
+ * Fix the entire heap to maintain heap properties.
92
+ */
93
+ protected fix() {
94
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
72
95
  }
73
96
 
74
97
  /**
75
- * The function returns the size of a priority queue.
76
- * @returns The size of the priority queue.
98
+ * Peek at the top element of the heap without removing it.
99
+ * @returns The top element or null if the heap is empty.
77
100
  */
78
- get size(): number {
79
- return this._pq.size;
101
+ peek(): E | null {
102
+ if (this.nodes.length === 0) {
103
+ return null;
104
+ }
105
+ return this.nodes[0];
80
106
  }
81
107
 
82
108
  /**
83
- * The function checks if a priority queue is empty.
84
- * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
109
+ * Get the size (number of elements) of the heap.
85
110
  */
86
- isEmpty(): boolean {
87
- return this._pq.size < 1;
111
+ get size(): number {
112
+ return this.nodes.length;
88
113
  }
89
114
 
90
- peek(isItem?: undefined): V | undefined;
91
- peek(isItem: false): V | undefined;
92
- peek(isItem: true): HeapItem<V> | null;
93
-
94
115
  /**
95
- * The `peek` function returns the top item in the priority queue without removing it.
96
- * @returns The `peek()` method is returning either a `HeapItem<V>` object or `null`.Returns an val with the highest priority in the queue
116
+ * Get the last element in the heap, which is not necessarily a leaf node.
117
+ * @returns The last element or null if the heap is empty.
97
118
  */
98
- peek(isItem?: boolean): HeapItem<V> | null | V | undefined {
99
- isItem = isItem ?? false;
100
- const peeked = this._pq.peek();
101
-
102
- return isItem ? peeked : peeked?.val;
119
+ get leaf(): E | null {
120
+ return this.nodes[this.size - 1] ?? null;
103
121
  }
104
122
 
105
- peekLast(isItem?: undefined): V | undefined;
106
- peekLast(isItem: false): V | undefined;
107
- peekLast(isItem: true): HeapItem<V> | null;
108
-
109
123
  /**
110
- * The `peekLast` function returns the last item in the heap.
111
- * @returns The method `peekLast()` returns either a `HeapItem<V>` object or `null`.Returns an val with the lowest priority in the queue
124
+ * Check if the heap is empty.
125
+ * @returns True if the heap is empty, otherwise false.
112
126
  */
113
- peekLast(isItem?: boolean): HeapItem<V> | null | V | undefined {
114
- isItem = isItem ?? false;
115
- const leafItem = this._pq.leaf();
116
-
117
- return isItem ? leafItem : leafItem?.val;
127
+ isEmpty() {
128
+ return this.size === 0;
118
129
  }
119
130
 
120
131
  /**
121
- * The `add` function adds an val to a priority queue with an optional priority value.
122
- * @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
123
- * type.
124
- * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
125
- * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
126
- * the value of `val`. If the `val` parameter is not a number, then the
127
- * @returns The `add` method returns the instance of the `Heap` class.
128
- * @throws {Error} if priority is not a valid number
132
+ * Reset the nodes of the heap. Make the nodes empty.
129
133
  */
130
- add(priority: number, val?: V): Heap<V> {
131
- val = val === undefined ? (priority as unknown as V) : val;
132
- this._pq.add(new HeapItem<V>(priority, val));
134
+ clear() {
135
+ this.nodes = [];
136
+ }
133
137
 
134
- return this;
138
+ /**
139
+ * Clear and add nodes of the heap
140
+ * @param nodes
141
+ */
142
+ refill(nodes: E[]) {
143
+ this.nodes = nodes;
144
+ this.fix();
135
145
  }
136
146
 
137
- poll(isItem?: undefined): V | undefined;
138
- poll(isItem: false): V | undefined;
139
- poll(isItem: true): HeapItem<V> | null;
147
+ /**
148
+ * Use a comparison function to check whether a binary heap contains a specific element.
149
+ * @param value - the element to check.
150
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
151
+ */
152
+ has(value: E): boolean {
153
+ return this.nodes.includes(value);
154
+ }
140
155
 
141
156
  /**
142
- * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
143
- * @returns either a HeapItem<V> object or null.
157
+ * Depth-first search (DFS) method, different traversal orders can be selected。
158
+ * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
159
+ * @returns An array containing elements traversed in the specified order.
144
160
  */
145
- poll(isItem?: boolean): HeapItem<V> | null | V | undefined {
146
- isItem = isItem ?? false;
147
- const top = this._pq.poll();
148
- if (!top) {
149
- return null;
150
- }
161
+ dfs(order: 'in' | 'pre' | 'post'): E[] {
162
+ const result: E[] = [];
163
+
164
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
165
+ const dfsHelper = (index: number) => {
166
+ if (index < this.size) {
167
+ if (order === 'in') {
168
+ dfsHelper(2 * index + 1);
169
+ result.push(this.nodes[index]);
170
+ dfsHelper(2 * index + 2);
171
+ } else if (order === 'pre') {
172
+ result.push(this.nodes[index]);
173
+ dfsHelper(2 * index + 1);
174
+ dfsHelper(2 * index + 2);
175
+ } else if (order === 'post') {
176
+ dfsHelper(2 * index + 1);
177
+ dfsHelper(2 * index + 2);
178
+ result.push(this.nodes[index]);
179
+ }
180
+ }
181
+ };
182
+
183
+ dfsHelper(0); // Traverse starting from the root node
151
184
 
152
- return isItem ? top : top.val;
185
+ return result;
153
186
  }
154
187
 
155
188
  /**
156
- * The function checks if a given node or value exists in the priority queue.
157
- * @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
158
- * @returns a boolean value.
189
+ * Convert the heap to an array.
190
+ * @returns An array containing the elements of the heap.
159
191
  */
160
- has(node: V | HeapItem<V>): boolean {
161
- if (node instanceof HeapItem) {
162
- return this.pq.getNodes().includes(node);
163
- } else {
164
- return (
165
- this.pq.getNodes().findIndex(item => {
166
- return item.val === node;
167
- }) !== -1
168
- );
169
- }
192
+ toArray(): E[] {
193
+ return [...this.nodes];
170
194
  }
171
195
 
172
- toArray(isItem?: undefined): (V | undefined)[];
173
- toArray(isItem: false): (V | undefined)[];
174
- toArray(isItem: true): (HeapItem<V> | null)[];
196
+ getNodes(): E[] {
197
+ return this.nodes;
198
+ }
175
199
 
176
200
  /**
177
- * The `toArray` function returns an array of `HeapItem<V>` objects.
178
- * @returns An array of HeapItem<V> objects.Returns a sorted list of vals
201
+ * Clone the heap, creating a new heap with the same elements.
202
+ * @returns A new Heap instance containing the same elements.
179
203
  */
180
- toArray(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
181
- isItem = isItem ?? false;
182
- const itemArray = this._pq.toArray();
183
-
184
- return isItem ? itemArray : itemArray.map(item => item.val);
204
+ clone(): Heap<E> {
205
+ const clonedHeap = new Heap<E>(this.comparator);
206
+ clonedHeap.nodes = [...this.nodes];
207
+ return clonedHeap;
185
208
  }
186
209
 
187
- sort(isItem?: undefined): (V | undefined)[];
188
- sort(isItem: false): (V | undefined)[];
189
- sort(isItem: true): (HeapItem<V> | null)[];
190
-
191
210
  /**
192
- * The function sorts the elements in the priority queue and returns either the sorted items or their values depending
193
- * on the value of the isItem parameter.
194
- * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
195
- * be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
196
- * sorted result will be an array of `HeapItem
197
- * @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
211
+ * Sort the elements in the heap and return them as an array.
212
+ * @returns An array containing the elements sorted in ascending order.
198
213
  */
199
- sort(isItem?: boolean): (HeapItem<V> | null | V | undefined)[] {
200
- isItem = isItem ?? false;
201
- const sorted = this._pq.sort();
202
-
203
- return isItem ? sorted : sorted.map(item => item.val);
214
+ sort(): E[] {
215
+ const visitedNode: E[] = [];
216
+ const cloned = this.clone();
217
+ while (cloned.size !== 0) {
218
+ const top = cloned.poll();
219
+ if (top) visitedNode.push(top);
220
+ }
221
+ return visitedNode;
204
222
  }
205
223
 
206
224
  /**
207
- * The clear function clears the priority queue.
225
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
226
+ * @param nodes
227
+ * @param comparator - Comparison function.
228
+ * @returns A new Heap instance.
208
229
  */
209
- clear(): void {
210
- this._pq.clear();
230
+ static heapify<E>(nodes: E[], comparator: CompareFunction<E>): Heap<E> {
231
+ const binaryHeap = new Heap<E>(comparator);
232
+ binaryHeap.nodes = [...nodes];
233
+ binaryHeap.fix(); // Fix heap properties
234
+ return binaryHeap;
211
235
  }
212
236
  }
@@ -1,31 +1,24 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import {Heap, HeapItem} from './heap';
10
- import {PriorityQueue} from '../priority-queue';
11
- import type {HeapOptions} from '../../types';
9
+ import {Heap} from './heap';
10
+ import type {CompareFunction} from '../../types';
12
11
 
13
- /**
14
- * @class MaxHeap
15
- * @extends Heap
16
- */
17
- export class MaxHeap<V = any> extends Heap<V> {
18
- protected _pq: PriorityQueue<HeapItem<V>>;
19
-
20
- /**
21
- * The constructor initializes a PriorityQueue with a custom comparator function.
22
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
23
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
24
- */
25
- constructor(options?: HeapOptions<V>) {
26
- super(options);
27
- this._pq = new PriorityQueue<HeapItem<V>>({
28
- comparator: (a, b) => b.priority - a.priority
29
- });
12
+ export class MaxHeap<E = any> extends Heap<E> {
13
+ constructor(
14
+ comparator: CompareFunction<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;
19
+ }
20
+ }
21
+ ) {
22
+ super(comparator);
30
23
  }
31
24
  }
@@ -1,32 +1,24 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import {Heap, HeapItem} from './heap';
10
- import {PriorityQueue} from '../priority-queue';
11
- import type {HeapOptions} from '../../types';
9
+ import {Heap} from './heap';
10
+ import type {CompareFunction} from '../../types';
12
11
 
13
- /**
14
- * @class MinHeap
15
- * @extends Heap
16
- */
17
- export class MinHeap<V = any> extends Heap<V> {
18
- protected _pq: PriorityQueue<HeapItem<V>>;
19
-
20
- /**
21
- * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
22
- * objects.
23
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
24
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
25
- */
26
- constructor(options?: HeapOptions<V>) {
27
- super(options);
28
- this._pq = new PriorityQueue<HeapItem<V>>({
29
- comparator: (a, b) => a.priority - b.priority
30
- });
12
+ export class MinHeap<E = any> extends Heap<E> {
13
+ constructor(
14
+ comparator: CompareFunction<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;
19
+ }
20
+ }
21
+ ) {
22
+ super(comparator);
31
23
  }
32
24
  }
@@ -1,56 +1,23 @@
1
1
  /**
2
2
  * data-structure-typed
3
3
  *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
8
  import {PriorityQueue} from './priority-queue';
9
- import type {PriorityQueueOptions, SpecifyOptional} from '../../types';
9
+ import type {CompareFunction} from '../../types';
10
10
 
11
11
  export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
12
- constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
13
- constructor(options: PriorityQueueOptions<E>);
14
-
15
- /**
16
- * The constructor initializes a priority queue with an optional comparator function.
17
- * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
18
- * the priority queue.
19
- */
20
- constructor(options?: SpecifyOptional<PriorityQueueOptions<E>, 'comparator'>) {
21
- super({
22
- ...options,
23
- comparator: options?.comparator
24
- ? options.comparator
25
- : (a: E, b: E) => {
26
- const aKey = a as unknown as number,
27
- bKey = b as unknown as number;
28
- return bKey - aKey;
29
- }
30
- });
31
- }
32
-
33
- static override heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MaxPriorityQueue<E>;
34
- static override heapify<E>(options: PriorityQueueOptions<E>): MaxPriorityQueue<E>;
35
-
36
- /**
37
- * The function `heapify` creates a max priority queue from the given options and returns it.
38
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
39
- * queue. It can have the following properties:
40
- * @returns a MaxPriorityQueue object.
41
- */
42
- static override heapify<E>(options: PriorityQueueOptions<E>): MaxPriorityQueue<E> {
43
- const maxPQ = new MaxPriorityQueue<E>({
44
- ...options,
45
- comparator: options?.comparator
46
- ? options.comparator
47
- : (a: E, b: E) => {
48
- const aKey = a as unknown as number,
49
- bKey = b as unknown as number;
50
- return bKey - aKey;
51
- }
52
- });
53
- maxPQ._fix();
54
- return maxPQ;
12
+ constructor(
13
+ compare: CompareFunction<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;
18
+ }
19
+ }
20
+ ) {
21
+ super(compare);
55
22
  }
56
23
  }