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
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, BinaryTreeNodePropertyName, BSTComparator, BSTN
9
9
  import { CP } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBST, IBSTNode } from '../../interfaces';
12
- export declare class BSTNode<V = any, NEIGHBOR extends BSTNode<V, NEIGHBOR> = BSTNodeNested<V>> extends BinaryTreeNode<V, NEIGHBOR> implements IBSTNode<V, NEIGHBOR> {
12
+ export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> implements IBSTNode<V, FAMILY> {
13
13
  constructor(key: BinaryTreeNodeKey, val?: V);
14
14
  }
15
15
  export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
@@ -1,7 +1,7 @@
1
1
  import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
2
  import { IRBTree, IRBTreeNode } from '../../interfaces';
3
3
  import { BST, BSTNode } from './bst';
4
- export declare class RBTreeNode<V = any, NEIGHBOR extends RBTreeNode<V, NEIGHBOR> = RBTreeNodeNested<V>> extends BSTNode<V, NEIGHBOR> implements IRBTreeNode<V, NEIGHBOR> {
4
+ export declare class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<V, FAMILY> implements IRBTreeNode<V, FAMILY> {
5
5
  private _color;
6
6
  constructor(key: BinaryTreeNodeKey, val?: V);
7
7
  get color(): RBColor;
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } f
9
9
  import { BinaryTreeDeletedResult, DFSOrderPattern } from '../../types';
10
10
  import { ITreeMultiset, ITreeMultisetNode } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, NEIGHBOR> implements ITreeMultisetNode<V, NEIGHBOR> {
12
+ export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> implements ITreeMultisetNode<V, FAMILY> {
13
13
  /**
14
14
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
15
15
  * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
@@ -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';
@@ -523,9 +523,7 @@ export class AbstractGraph {
523
523
  if (vertexOrKey instanceof AbstractVertex)
524
524
  distMap.set(vertexOrKey, Infinity);
525
525
  }
526
- const heap = new PriorityQueue({
527
- comparator: (a, b) => a.key - b.key
528
- });
526
+ const heap = new PriorityQueue((a, b) => a.key - b.key);
529
527
  heap.add({ key: 0, val: srcVertex });
530
528
  distMap.set(srcVertex, 0);
531
529
  preMap.set(srcVertex, null);
@@ -1,83 +1,99 @@
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
- export declare class HeapItem<V = any> {
11
- /**
12
- * The constructor function initializes an instance of a class with a priority and a value.
13
- * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
14
- * optional and has a default value of `NaN`.
15
- * @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
16
- * `V` or `null`.
17
- */
18
- constructor(priority?: number, val?: V | null);
19
- private _priority;
20
- get priority(): number;
21
- set priority(value: number);
22
- private _val;
23
- get val(): V | null;
24
- set val(value: V | null);
25
- }
26
- export declare abstract class Heap<V = any> {
7
+ import type { CompareFunction } from '../../types';
8
+ export declare class Heap<E> {
9
+ protected nodes: E[];
10
+ private readonly comparator;
11
+ constructor(comparator: CompareFunction<E>);
12
+ /**
13
+ * Insert an element into the heap and maintain the heap properties.
14
+ * @param value - The element to be inserted.
15
+ */
16
+ add(value: E): Heap<E>;
17
+ /**
18
+ * Remove and return the top element (smallest or largest element) from the heap.
19
+ * @returns The top element or null if the heap is empty.
20
+ */
21
+ poll(): E | null;
22
+ /**
23
+ * Float operation to maintain heap properties after adding an element.
24
+ * @param index - The index of the newly added element.
25
+ */
26
+ protected bubbleUp(index: number): void;
27
27
  /**
28
- * The function is a constructor for a class that initializes a priority callback function based on the
29
- * options provided.
30
- * @param [options] - An optional object that contains configuration options for the Heap.
28
+ * Sinking operation to maintain heap properties after removing the top element.
29
+ * @param index - The index from which to start sinking.
31
30
  */
32
- protected constructor(options?: HeapOptions<V>);
33
- protected abstract _pq: PriorityQueue<HeapItem<V>>;
34
- get pq(): PriorityQueue<HeapItem<V>>;
35
- protected _priorityExtractor: (val: V) => number;
36
- get priorityExtractor(): (val: V) => number;
31
+ protected sinkDown(index: number): void;
37
32
  /**
38
- * The function returns the size of a priority queue.
39
- * @returns The size of the priority queue.
33
+ * Fix the entire heap to maintain heap properties.
34
+ */
35
+ protected fix(): void;
36
+ /**
37
+ * Peek at the top element of the heap without removing it.
38
+ * @returns The top element or null if the heap is empty.
39
+ */
40
+ peek(): E | null;
41
+ /**
42
+ * Get the size (number of elements) of the heap.
40
43
  */
41
44
  get size(): number;
42
45
  /**
43
- * The function checks if a priority queue is empty.
44
- * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
46
+ * Get the last element in the heap, which is not necessarily a leaf node.
47
+ * @returns The last element or null if the heap is empty.
48
+ */
49
+ get leaf(): E | null;
50
+ /**
51
+ * Check if the heap is empty.
52
+ * @returns True if the heap is empty, otherwise false.
45
53
  */
46
54
  isEmpty(): boolean;
47
- peek(isItem?: undefined): V | undefined;
48
- peek(isItem: false): V | undefined;
49
- peek(isItem: true): HeapItem<V> | null;
50
- peekLast(isItem?: undefined): V | undefined;
51
- peekLast(isItem: false): V | undefined;
52
- peekLast(isItem: true): HeapItem<V> | null;
53
- /**
54
- * The `add` function adds an val to a priority queue with an optional priority value.
55
- * @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
56
- * type.
57
- * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
58
- * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
59
- * the value of `val`. If the `val` parameter is not a number, then the
60
- * @returns The `add` method returns the instance of the `Heap` class.
61
- * @throws {Error} if priority is not a valid number
62
- */
63
- add(priority: number, val?: V): Heap<V>;
64
- poll(isItem?: undefined): V | undefined;
65
- poll(isItem: false): V | undefined;
66
- poll(isItem: true): HeapItem<V> | null;
67
- /**
68
- * The function checks if a given node or value exists in the priority queue.
69
- * @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
70
- * @returns a boolean value.
71
- */
72
- has(node: V | HeapItem<V>): boolean;
73
- toArray(isItem?: undefined): (V | undefined)[];
74
- toArray(isItem: false): (V | undefined)[];
75
- toArray(isItem: true): (HeapItem<V> | null)[];
76
- sort(isItem?: undefined): (V | undefined)[];
77
- sort(isItem: false): (V | undefined)[];
78
- sort(isItem: true): (HeapItem<V> | null)[];
79
- /**
80
- * The clear function clears the priority queue.
55
+ /**
56
+ * Reset the nodes of the heap. Make the nodes empty.
81
57
  */
82
58
  clear(): void;
59
+ /**
60
+ * Clear and add nodes of the heap
61
+ * @param nodes
62
+ */
63
+ refill(nodes: E[]): void;
64
+ /**
65
+ * Use a comparison function to check whether a binary heap contains a specific element.
66
+ * @param value - the element to check.
67
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
68
+ */
69
+ has(value: E): boolean;
70
+ /**
71
+ * Depth-first search (DFS) method, different traversal orders can be selected。
72
+ * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
73
+ * @returns An array containing elements traversed in the specified order.
74
+ */
75
+ dfs(order: 'in' | 'pre' | 'post'): E[];
76
+ /**
77
+ * Convert the heap to an array.
78
+ * @returns An array containing the elements of the heap.
79
+ */
80
+ toArray(): E[];
81
+ getNodes(): E[];
82
+ /**
83
+ * Clone the heap, creating a new heap with the same elements.
84
+ * @returns A new Heap instance containing the same elements.
85
+ */
86
+ clone(): Heap<E>;
87
+ /**
88
+ * Sort the elements in the heap and return them as an array.
89
+ * @returns An array containing the elements sorted in ascending order.
90
+ */
91
+ sort(): E[];
92
+ /**
93
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
94
+ * @param nodes
95
+ * @param comparator - Comparison function.
96
+ * @returns A new Heap instance.
97
+ */
98
+ static heapify<E>(nodes: E[], comparator: CompareFunction<E>): Heap<E>;
83
99
  }
@@ -1,152 +1,213 @@
1
- export class HeapItem {
1
+ /**
2
+ * data-structure-typed
3
+ * @author Kirk Qi
4
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
5
+ * @license MIT License
6
+ */
7
+ export class Heap {
8
+ constructor(comparator) {
9
+ this.nodes = [];
10
+ this.comparator = comparator;
11
+ }
2
12
  /**
3
- * The constructor function initializes an instance of a class with a priority and a value.
4
- * @param {number} priority - The `priority` parameter is a number that represents the priority of the value. It is
5
- * optional and has a default value of `NaN`.
6
- * @param {V | null} [val=null] - The `val` parameter is of type `V | null`, which means it can accept a value of type
7
- * `V` or `null`.
13
+ * Insert an element into the heap and maintain the heap properties.
14
+ * @param value - The element to be inserted.
8
15
  */
9
- constructor(priority = Number.MAX_SAFE_INTEGER, val = null) {
10
- this._val = val;
11
- this._priority = priority;
12
- }
13
- get priority() {
14
- return this._priority;
15
- }
16
- set priority(value) {
17
- this._priority = value;
18
- }
19
- get val() {
20
- return this._val;
16
+ add(value) {
17
+ this.nodes.push(value);
18
+ this.bubbleUp(this.nodes.length - 1);
19
+ return this;
21
20
  }
22
- set val(value) {
23
- this._val = value;
21
+ /**
22
+ * Remove and return the top element (smallest or largest element) from the heap.
23
+ * @returns The top element or null if the heap is empty.
24
+ */
25
+ poll() {
26
+ if (this.nodes.length === 0) {
27
+ return null;
28
+ }
29
+ if (this.nodes.length === 1) {
30
+ return this.nodes.pop();
31
+ }
32
+ const topValue = this.nodes[0];
33
+ this.nodes[0] = this.nodes.pop();
34
+ this.sinkDown(0);
35
+ return topValue;
24
36
  }
25
- }
26
- export class Heap {
27
37
  /**
28
- * The function is a constructor for a class that initializes a priority callback function based on the
29
- * options provided.
30
- * @param [options] - An optional object that contains configuration options for the Heap.
38
+ * Float operation to maintain heap properties after adding an element.
39
+ * @param index - The index of the newly added element.
31
40
  */
32
- constructor(options) {
33
- if (options) {
34
- const { priorityExtractor } = options;
35
- if (priorityExtractor !== undefined && typeof priorityExtractor !== 'function') {
36
- throw new Error('.constructor expects a valid priority function');
41
+ bubbleUp(index) {
42
+ const element = this.nodes[index];
43
+ while (index > 0) {
44
+ const parentIndex = Math.floor((index - 1) / 2);
45
+ const parent = this.nodes[parentIndex];
46
+ if (this.comparator(element, parent) < 0) {
47
+ this.nodes[index] = parent;
48
+ this.nodes[parentIndex] = element;
49
+ index = parentIndex;
37
50
  }
38
- this._priorityExtractor = priorityExtractor || (el => +el);
51
+ else {
52
+ break;
53
+ }
54
+ }
55
+ }
56
+ /**
57
+ * Sinking operation to maintain heap properties after removing the top element.
58
+ * @param index - The index from which to start sinking.
59
+ */
60
+ sinkDown(index) {
61
+ const leftChildIndex = 2 * index + 1;
62
+ const rightChildIndex = 2 * index + 2;
63
+ const length = this.nodes.length;
64
+ let targetIndex = index;
65
+ if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
66
+ targetIndex = leftChildIndex;
67
+ }
68
+ if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
69
+ targetIndex = rightChildIndex;
39
70
  }
40
- else {
41
- this._priorityExtractor = el => +el;
71
+ if (targetIndex !== index) {
72
+ const temp = this.nodes[index];
73
+ this.nodes[index] = this.nodes[targetIndex];
74
+ this.nodes[targetIndex] = temp;
75
+ this.sinkDown(targetIndex);
42
76
  }
43
77
  }
44
- get pq() {
45
- return this._pq;
78
+ /**
79
+ * Fix the entire heap to maintain heap properties.
80
+ */
81
+ fix() {
82
+ for (let i = Math.floor(this.size / 2); i >= 0; i--)
83
+ this.sinkDown(i);
46
84
  }
47
- get priorityExtractor() {
48
- return this._priorityExtractor;
85
+ /**
86
+ * Peek at the top element of the heap without removing it.
87
+ * @returns The top element or null if the heap is empty.
88
+ */
89
+ peek() {
90
+ if (this.nodes.length === 0) {
91
+ return null;
92
+ }
93
+ return this.nodes[0];
49
94
  }
50
95
  /**
51
- * The function returns the size of a priority queue.
52
- * @returns The size of the priority queue.
96
+ * Get the size (number of elements) of the heap.
53
97
  */
54
98
  get size() {
55
- return this._pq.size;
99
+ return this.nodes.length;
56
100
  }
57
101
  /**
58
- * The function checks if a priority queue is empty.
59
- * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
102
+ * Get the last element in the heap, which is not necessarily a leaf node.
103
+ * @returns The last element or null if the heap is empty.
104
+ */
105
+ get leaf() {
106
+ var _a;
107
+ return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
108
+ }
109
+ /**
110
+ * Check if the heap is empty.
111
+ * @returns True if the heap is empty, otherwise false.
60
112
  */
61
113
  isEmpty() {
62
- return this._pq.size < 1;
114
+ return this.size === 0;
63
115
  }
64
116
  /**
65
- * The `peek` function returns the top item in the priority queue without removing it.
66
- * @returns The `peek()` method is returning either a `HeapItem<V>` object or `null`.Returns an val with the highest priority in the queue
117
+ * Reset the nodes of the heap. Make the nodes empty.
67
118
  */
68
- peek(isItem) {
69
- isItem = isItem !== null && isItem !== void 0 ? isItem : false;
70
- const peeked = this._pq.peek();
71
- return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
119
+ clear() {
120
+ this.nodes = [];
72
121
  }
73
122
  /**
74
- * The `peekLast` function returns the last item in the heap.
75
- * @returns The method `peekLast()` returns either a `HeapItem<V>` object or `null`.Returns an val with the lowest priority in the queue
123
+ * Clear and add nodes of the heap
124
+ * @param nodes
76
125
  */
77
- peekLast(isItem) {
78
- isItem = isItem !== null && isItem !== void 0 ? isItem : false;
79
- const leafItem = this._pq.leaf();
80
- return isItem ? leafItem : leafItem === null || leafItem === void 0 ? void 0 : leafItem.val;
126
+ refill(nodes) {
127
+ this.nodes = nodes;
128
+ this.fix();
81
129
  }
82
130
  /**
83
- * The `add` function adds an val to a priority queue with an optional priority value.
84
- * @param {V} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
85
- * type.
86
- * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
87
- * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
88
- * the value of `val`. If the `val` parameter is not a number, then the
89
- * @returns The `add` method returns the instance of the `Heap` class.
90
- * @throws {Error} if priority is not a valid number
131
+ * Use a comparison function to check whether a binary heap contains a specific element.
132
+ * @param value - the element to check.
133
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
91
134
  */
92
- add(priority, val) {
93
- val = val === undefined ? priority : val;
94
- this._pq.add(new HeapItem(priority, val));
95
- return this;
135
+ has(value) {
136
+ return this.nodes.includes(value);
96
137
  }
97
138
  /**
98
- * 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
99
- * @returns either a HeapItem<V> object or null.
139
+ * Depth-first search (DFS) method, different traversal orders can be selected。
140
+ * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
141
+ * @returns An array containing elements traversed in the specified order.
100
142
  */
101
- poll(isItem) {
102
- isItem = isItem !== null && isItem !== void 0 ? isItem : false;
103
- const top = this._pq.poll();
104
- if (!top) {
105
- return null;
106
- }
107
- return isItem ? top : top.val;
143
+ dfs(order) {
144
+ const result = [];
145
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
146
+ const dfsHelper = (index) => {
147
+ if (index < this.size) {
148
+ if (order === 'in') {
149
+ dfsHelper(2 * index + 1);
150
+ result.push(this.nodes[index]);
151
+ dfsHelper(2 * index + 2);
152
+ }
153
+ else if (order === 'pre') {
154
+ result.push(this.nodes[index]);
155
+ dfsHelper(2 * index + 1);
156
+ dfsHelper(2 * index + 2);
157
+ }
158
+ else if (order === 'post') {
159
+ dfsHelper(2 * index + 1);
160
+ dfsHelper(2 * index + 2);
161
+ result.push(this.nodes[index]);
162
+ }
163
+ }
164
+ };
165
+ dfsHelper(0); // Traverse starting from the root node
166
+ return result;
108
167
  }
109
168
  /**
110
- * The function checks if a given node or value exists in the priority queue.
111
- * @param {V | HeapItem<V>} node - The parameter `node` can be of type `V` or `HeapItem<V>`.
112
- * @returns a boolean value.
169
+ * Convert the heap to an array.
170
+ * @returns An array containing the elements of the heap.
113
171
  */
114
- has(node) {
115
- if (node instanceof HeapItem) {
116
- return this.pq.getNodes().includes(node);
117
- }
118
- else {
119
- return (this.pq.getNodes().findIndex(item => {
120
- return item.val === node;
121
- }) !== -1);
122
- }
172
+ toArray() {
173
+ return [...this.nodes];
174
+ }
175
+ getNodes() {
176
+ return this.nodes;
123
177
  }
124
178
  /**
125
- * The `toArray` function returns an array of `HeapItem<V>` objects.
126
- * @returns An array of HeapItem<V> objects.Returns a sorted list of vals
179
+ * Clone the heap, creating a new heap with the same elements.
180
+ * @returns A new Heap instance containing the same elements.
127
181
  */
128
- toArray(isItem) {
129
- isItem = isItem !== null && isItem !== void 0 ? isItem : false;
130
- const itemArray = this._pq.toArray();
131
- return isItem ? itemArray : itemArray.map(item => item.val);
182
+ clone() {
183
+ const clonedHeap = new Heap(this.comparator);
184
+ clonedHeap.nodes = [...this.nodes];
185
+ return clonedHeap;
132
186
  }
133
187
  /**
134
- * The function sorts the elements in the priority queue and returns either the sorted items or their values depending
135
- * on the value of the isItem parameter.
136
- * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the sorted result should
137
- * be an array of `HeapItem<V>` objects or an array of the values (`V`) of those objects. If `isItem` is `true`, the
138
- * sorted result will be an array of `HeapItem
139
- * @returns an array of either `HeapItem<V>`, `null`, `V`, or `undefined` values.
188
+ * Sort the elements in the heap and return them as an array.
189
+ * @returns An array containing the elements sorted in ascending order.
140
190
  */
141
- sort(isItem) {
142
- isItem = isItem !== null && isItem !== void 0 ? isItem : false;
143
- const sorted = this._pq.sort();
144
- return isItem ? sorted : sorted.map(item => item.val);
191
+ sort() {
192
+ const visitedNode = [];
193
+ const cloned = this.clone();
194
+ while (cloned.size !== 0) {
195
+ const top = cloned.poll();
196
+ if (top)
197
+ visitedNode.push(top);
198
+ }
199
+ return visitedNode;
145
200
  }
146
201
  /**
147
- * The clear function clears the priority queue.
202
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
203
+ * @param nodes
204
+ * @param comparator - Comparison function.
205
+ * @returns A new Heap instance.
148
206
  */
149
- clear() {
150
- this._pq.clear();
207
+ static heapify(nodes, comparator) {
208
+ const binaryHeap = new Heap(comparator);
209
+ binaryHeap.nodes = [...nodes];
210
+ binaryHeap.fix(); // Fix heap properties
211
+ return binaryHeap;
151
212
  }
152
213
  }
@@ -1,23 +1,12 @@
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
- import { Heap, HeapItem } from './heap';
9
- import { PriorityQueue } from '../priority-queue';
10
- import type { HeapOptions } from '../../types';
11
- /**
12
- * @class MaxHeap
13
- * @extends Heap
14
- */
15
- export declare class MaxHeap<V = any> extends Heap<V> {
16
- protected _pq: PriorityQueue<HeapItem<V>>;
17
- /**
18
- * The constructor initializes a PriorityQueue with a custom comparator function.
19
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
20
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
21
- */
22
- constructor(options?: HeapOptions<V>);
8
+ import { Heap } from './heap';
9
+ import type { CompareFunction } from '../../types';
10
+ export declare class MaxHeap<E = any> extends Heap<E> {
11
+ constructor(comparator?: CompareFunction<E>);
23
12
  }
@@ -1,26 +1,20 @@
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 { Heap } from './heap';
9
- import { PriorityQueue } from '../priority-queue';
10
- /**
11
- * @class MaxHeap
12
- * @extends Heap
13
- */
14
9
  export class MaxHeap extends Heap {
15
- /**
16
- * The constructor initializes a PriorityQueue with a custom comparator function.
17
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
18
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
19
- */
20
- constructor(options) {
21
- super(options);
22
- this._pq = new PriorityQueue({
23
- comparator: (a, b) => b.priority - a.priority
24
- });
10
+ constructor(comparator = (a, b) => {
11
+ if (!(typeof a === 'number' && typeof b === 'number')) {
12
+ throw new Error('The a, b params of compare function must be number');
13
+ }
14
+ else {
15
+ return b - a;
16
+ }
17
+ }) {
18
+ super(comparator);
25
19
  }
26
20
  }
@@ -1,24 +1,12 @@
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
- import { Heap, HeapItem } from './heap';
9
- import { PriorityQueue } from '../priority-queue';
10
- import type { HeapOptions } from '../../types';
11
- /**
12
- * @class MinHeap
13
- * @extends Heap
14
- */
15
- export declare class MinHeap<V = any> extends Heap<V> {
16
- protected _pq: PriorityQueue<HeapItem<V>>;
17
- /**
18
- * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
19
- * objects.
20
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
21
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
22
- */
23
- constructor(options?: HeapOptions<V>);
8
+ import { Heap } from './heap';
9
+ import type { CompareFunction } from '../../types';
10
+ export declare class MinHeap<E = any> extends Heap<E> {
11
+ constructor(comparator?: CompareFunction<E>);
24
12
  }