data-structure-typed 1.35.0 → 1.36.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 (203) hide show
  1. package/.github/workflows/ci.yml +3 -0
  2. package/CHANGELOG.md +8 -1
  3. package/CONTRIBUTING.md +18 -0
  4. package/dist/data-structures/binary-tree/aa-tree.d.ts +2 -0
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +527 -0
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.js +323 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +94 -0
  9. package/dist/data-structures/binary-tree/avl-tree.js +90 -3
  10. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  11. package/dist/data-structures/binary-tree/b-tree.d.ts +2 -0
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +46 -0
  13. package/dist/data-structures/binary-tree/binary-indexed-tree.js +36 -0
  14. package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  15. package/dist/data-structures/binary-tree/binary-tree.d.ts +31 -0
  16. package/dist/data-structures/binary-tree/binary-tree.js +21 -0
  17. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  18. package/dist/data-structures/binary-tree/bst.d.ts +133 -0
  19. package/dist/data-structures/binary-tree/bst.js +114 -0
  20. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  21. package/dist/data-structures/binary-tree/index.d.ts +12 -0
  22. package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
  23. package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
  24. package/dist/data-structures/binary-tree/segment-tree.js +45 -0
  25. package/dist/data-structures/binary-tree/segment-tree.js.map +1 -1
  26. package/dist/data-structures/binary-tree/splay-tree.d.ts +2 -0
  27. package/dist/data-structures/binary-tree/tree-multiset.d.ts +209 -0
  28. package/dist/data-structures/binary-tree/tree-multiset.js +178 -0
  29. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  30. package/dist/data-structures/binary-tree/two-three-tree.d.ts +2 -0
  31. package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
  32. package/dist/data-structures/graph/abstract-graph.js +270 -7
  33. package/dist/data-structures/graph/abstract-graph.js.map +1 -1
  34. package/dist/data-structures/graph/directed-graph.d.ts +200 -0
  35. package/dist/data-structures/graph/directed-graph.js +167 -0
  36. package/dist/data-structures/graph/directed-graph.js.map +1 -1
  37. package/dist/data-structures/graph/index.d.ts +4 -0
  38. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  39. package/dist/data-structures/graph/map-graph.js +54 -0
  40. package/dist/data-structures/graph/map-graph.js.map +1 -1
  41. package/dist/data-structures/graph/undirected-graph.d.ts +127 -0
  42. package/dist/data-structures/graph/undirected-graph.js +105 -0
  43. package/dist/data-structures/graph/undirected-graph.js.map +1 -1
  44. package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
  45. package/dist/data-structures/hash/coordinate-map.js +35 -0
  46. package/dist/data-structures/hash/coordinate-map.js.map +1 -1
  47. package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
  48. package/dist/data-structures/hash/coordinate-set.js +28 -0
  49. package/dist/data-structures/hash/coordinate-set.js.map +1 -1
  50. package/dist/data-structures/hash/hash-map.d.ts +56 -0
  51. package/dist/data-structures/hash/hash-map.js +29 -1
  52. package/dist/data-structures/hash/hash-map.js.map +1 -1
  53. package/dist/data-structures/hash/hash-table.d.ts +106 -0
  54. package/dist/data-structures/hash/hash-table.js +88 -6
  55. package/dist/data-structures/hash/hash-table.js.map +1 -1
  56. package/dist/data-structures/hash/index.d.ts +7 -0
  57. package/dist/data-structures/hash/pair.d.ts +2 -0
  58. package/dist/data-structures/hash/tree-map.d.ts +2 -0
  59. package/dist/data-structures/hash/tree-set.d.ts +2 -0
  60. package/dist/data-structures/heap/heap.d.ts +100 -0
  61. package/dist/data-structures/heap/heap.js +215 -76
  62. package/dist/data-structures/heap/heap.js.map +1 -1
  63. package/dist/data-structures/heap/index.d.ts +3 -0
  64. package/dist/data-structures/heap/max-heap.d.ts +12 -0
  65. package/dist/data-structures/heap/max-heap.js +16 -6
  66. package/dist/data-structures/heap/max-heap.js.map +1 -1
  67. package/dist/data-structures/heap/min-heap.d.ts +12 -0
  68. package/dist/data-structures/heap/min-heap.js +16 -6
  69. package/dist/data-structures/heap/min-heap.js.map +1 -1
  70. package/dist/data-structures/index.d.ts +11 -0
  71. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
  72. package/dist/data-structures/linked-list/doubly-linked-list.js +202 -0
  73. package/dist/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  74. package/dist/data-structures/linked-list/index.d.ts +3 -0
  75. package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
  76. package/dist/data-structures/linked-list/singly-linked-list.js +135 -0
  77. package/dist/data-structures/linked-list/singly-linked-list.js.map +1 -1
  78. package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
  79. package/dist/data-structures/linked-list/skip-linked-list.js +36 -0
  80. package/dist/data-structures/linked-list/skip-linked-list.js.map +1 -1
  81. package/dist/data-structures/matrix/index.d.ts +4 -0
  82. package/dist/data-structures/matrix/matrix.d.ts +21 -0
  83. package/dist/data-structures/matrix/matrix.js +15 -0
  84. package/dist/data-structures/matrix/matrix.js.map +1 -1
  85. package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
  86. package/dist/data-structures/matrix/matrix2d.js +91 -2
  87. package/dist/data-structures/matrix/matrix2d.js.map +1 -1
  88. package/dist/data-structures/matrix/navigator.d.ts +52 -0
  89. package/dist/data-structures/matrix/navigator.js +28 -0
  90. package/dist/data-structures/matrix/navigator.js.map +1 -1
  91. package/dist/data-structures/matrix/vector2d.d.ts +201 -0
  92. package/dist/data-structures/matrix/vector2d.js +188 -1
  93. package/dist/data-structures/matrix/vector2d.js.map +1 -1
  94. package/dist/data-structures/priority-queue/index.d.ts +3 -0
  95. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
  96. package/dist/data-structures/priority-queue/max-priority-queue.js +16 -17
  97. package/dist/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  98. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
  99. package/dist/data-structures/priority-queue/min-priority-queue.js +16 -17
  100. package/dist/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  101. package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
  102. package/dist/data-structures/priority-queue/priority-queue.js +11 -174
  103. package/dist/data-structures/priority-queue/priority-queue.js.map +1 -1
  104. package/dist/data-structures/queue/deque.d.ts +165 -0
  105. package/dist/data-structures/queue/deque.js +124 -0
  106. package/dist/data-structures/queue/deque.js.map +1 -1
  107. package/dist/data-structures/queue/index.d.ts +2 -0
  108. package/dist/data-structures/queue/queue.d.ts +107 -0
  109. package/dist/data-structures/queue/queue.js +80 -0
  110. package/dist/data-structures/queue/queue.js.map +1 -1
  111. package/dist/data-structures/stack/index.d.ts +1 -0
  112. package/dist/data-structures/stack/stack.d.ts +63 -0
  113. package/dist/data-structures/stack/stack.js +50 -0
  114. package/dist/data-structures/stack/stack.js.map +1 -1
  115. package/dist/data-structures/tree/index.d.ts +1 -0
  116. package/dist/data-structures/tree/tree.d.ts +14 -0
  117. package/dist/data-structures/tree/tree.js +1 -0
  118. package/dist/data-structures/tree/tree.js.map +1 -1
  119. package/dist/data-structures/trie/index.d.ts +1 -0
  120. package/dist/data-structures/trie/trie.d.ts +61 -0
  121. package/dist/data-structures/trie/trie.js +36 -0
  122. package/dist/data-structures/trie/trie.js.map +1 -1
  123. package/dist/index.d.ts +4 -0
  124. package/dist/interfaces/abstract-binary-tree.d.ts +7 -0
  125. package/dist/interfaces/abstract-graph.d.ts +5 -0
  126. package/dist/interfaces/avl-tree.d.ts +7 -0
  127. package/dist/interfaces/binary-tree.d.ts +6 -0
  128. package/dist/interfaces/bst.d.ts +6 -0
  129. package/dist/interfaces/directed-graph.d.ts +3 -0
  130. package/dist/interfaces/doubly-linked-list.d.ts +1 -0
  131. package/dist/interfaces/heap.d.ts +1 -0
  132. package/dist/interfaces/index.d.ts +15 -0
  133. package/dist/interfaces/navigator.d.ts +1 -0
  134. package/dist/interfaces/priority-queue.d.ts +1 -0
  135. package/dist/interfaces/rb-tree.d.ts +6 -0
  136. package/dist/interfaces/segment-tree.d.ts +1 -0
  137. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  138. package/dist/interfaces/tree-multiset.d.ts +6 -0
  139. package/dist/interfaces/undirected-graph.d.ts +3 -0
  140. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  141. package/dist/types/data-structures/abstract-binary-tree.js +6 -0
  142. package/dist/types/data-structures/abstract-binary-tree.js.map +1 -1
  143. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  144. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  145. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  146. package/dist/types/data-structures/bst.d.ts +13 -0
  147. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  148. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  149. package/dist/types/data-structures/hash.d.ts +1 -0
  150. package/dist/types/data-structures/heap.d.ts +1 -0
  151. package/dist/types/data-structures/index.d.ts +16 -0
  152. package/dist/types/data-structures/map-graph.d.ts +1 -0
  153. package/dist/types/data-structures/navigator.d.ts +14 -0
  154. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  155. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  156. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  157. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  158. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  159. package/dist/types/helpers.d.ts +1 -0
  160. package/dist/types/index.d.ts +3 -0
  161. package/dist/types/utils/index.d.ts +2 -0
  162. package/dist/types/utils/utils.d.ts +7 -0
  163. package/dist/types/utils/validate-type.d.ts +19 -0
  164. package/dist/utils/index.d.ts +1 -0
  165. package/dist/utils/utils.d.ts +19 -0
  166. package/lib/data-structures/graph/abstract-graph.js +3 -5
  167. package/lib/data-structures/heap/heap.d.ts +85 -68
  168. package/lib/data-structures/heap/heap.js +186 -108
  169. package/lib/data-structures/heap/max-heap.d.ts +6 -17
  170. package/lib/data-structures/heap/max-heap.js +11 -17
  171. package/lib/data-structures/heap/min-heap.d.ts +6 -18
  172. package/lib/data-structures/heap/min-heap.js +11 -18
  173. package/lib/data-structures/priority-queue/max-priority-queue.d.ts +5 -8
  174. package/lib/data-structures/priority-queue/max-priority-queue.js +11 -30
  175. package/lib/data-structures/priority-queue/min-priority-queue.d.ts +5 -8
  176. package/lib/data-structures/priority-queue/min-priority-queue.js +11 -31
  177. package/lib/data-structures/priority-queue/priority-queue.d.ts +6 -174
  178. package/lib/data-structures/priority-queue/priority-queue.js +11 -315
  179. package/lib/types/data-structures/heap.d.ts +1 -3
  180. package/package.json +11 -7
  181. package/src/data-structures/graph/abstract-graph.ts +3 -5
  182. package/src/data-structures/heap/heap.ts +182 -140
  183. package/src/data-structures/heap/max-heap.ts +15 -22
  184. package/src/data-structures/heap/min-heap.ts +15 -23
  185. package/src/data-structures/priority-queue/max-priority-queue.ts +14 -47
  186. package/src/data-structures/priority-queue/min-priority-queue.ts +14 -48
  187. package/src/data-structures/priority-queue/priority-queue.ts +7 -350
  188. package/src/types/data-structures/heap.ts +1 -5
  189. package/test/integration/avl-tree.test.ts +4 -4
  190. package/test/integration/bst.test.ts +8 -8
  191. package/test/unit/data-structures/graph/directed-graph.test.ts +5 -5
  192. package/test/unit/data-structures/graph/overall.test.ts +2 -2
  193. package/test/unit/data-structures/heap/heap.test.ts +26 -18
  194. package/test/unit/data-structures/heap/max-heap.test.ts +50 -42
  195. package/test/unit/data-structures/heap/min-heap.test.ts +38 -68
  196. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +1 -1
  197. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +7 -9
  198. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +14 -30
  199. package/umd/bundle.min.js +1 -1
  200. package/umd/bundle.min.js.LICENSE.txt +8 -0
  201. package/umd/bundle.min.js.map +1 -1
  202. package/test/unit/data-structures/graph/index.ts +0 -2
  203. package/test/unit/data-structures/linked-list/index.ts +0 -4
@@ -1,152 +1,230 @@
1
- export class HeapItem {
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Kirk Qi
5
+ * @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ export class Heap {
9
+ constructor(comparator) {
10
+ this.nodes = [];
11
+ this.comparator = comparator;
12
+ }
2
13
  /**
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`.
14
+ * Insert an element into the heap and maintain the heap properties.
15
+ * @param value - The element to be inserted.
8
16
  */
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;
21
- }
22
- set val(value) {
23
- this._val = value;
17
+ add(value) {
18
+ this.nodes.push(value);
19
+ this.bubbleUp(this.nodes.length - 1);
20
+ return this;
24
21
  }
25
- }
26
- export class Heap {
27
22
  /**
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.
23
+ * Remove and return the top element (smallest or largest element) from the heap.
24
+ * @returns The top element or null if the heap is empty.
31
25
  */
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');
37
- }
38
- this._priorityExtractor = priorityExtractor || (el => +el);
26
+ poll() {
27
+ if (this.nodes.length === 0) {
28
+ return null;
39
29
  }
40
- else {
41
- this._priorityExtractor = el => +el;
30
+ if (this.nodes.length === 1) {
31
+ return this.nodes.pop();
42
32
  }
33
+ const topValue = this.nodes[0];
34
+ this.nodes[0] = this.nodes.pop();
35
+ this.sinkDown(0);
36
+ return topValue;
43
37
  }
44
- get pq() {
45
- return this._pq;
38
+ /**
39
+ * Float operation to maintain heap properties after adding an element.
40
+ * @param index - The index of the newly added element.
41
+ */
42
+ bubbleUp(index) {
43
+ const element = this.nodes[index];
44
+ while (index > 0) {
45
+ const parentIndex = Math.floor((index - 1) / 2);
46
+ const parent = this.nodes[parentIndex];
47
+ if (this.comparator(element, parent) < 0) {
48
+ this.nodes[index] = parent;
49
+ this.nodes[parentIndex] = element;
50
+ index = parentIndex;
51
+ }
52
+ else {
53
+ break;
54
+ }
55
+ }
46
56
  }
47
- get priorityExtractor() {
48
- return this._priorityExtractor;
57
+ /**
58
+ * Sinking operation to maintain heap properties after removing the top element.
59
+ * @param index - The index from which to start sinking.
60
+ */
61
+ sinkDown(index) {
62
+ const leftChildIndex = 2 * index + 1;
63
+ const rightChildIndex = 2 * index + 2;
64
+ const length = this.nodes.length;
65
+ let targetIndex = index;
66
+ if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
67
+ targetIndex = leftChildIndex;
68
+ }
69
+ if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
70
+ targetIndex = rightChildIndex;
71
+ }
72
+ if (targetIndex !== index) {
73
+ const temp = this.nodes[index];
74
+ this.nodes[index] = this.nodes[targetIndex];
75
+ this.nodes[targetIndex] = temp;
76
+ this.sinkDown(targetIndex);
77
+ }
49
78
  }
50
79
  /**
51
- * The function returns the size of a priority queue.
52
- * @returns The size of the priority queue.
80
+ * Fix the entire heap to maintain heap properties.
53
81
  */
54
- get size() {
55
- return this._pq.size;
82
+ fix() {
83
+ for (let i = Math.floor(this.size / 2); i >= 0; i--)
84
+ this.sinkDown(i);
56
85
  }
57
86
  /**
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.
87
+ * Peek at the top element of the heap without removing it.
88
+ * @returns The top element or null if the heap is empty.
60
89
  */
61
- isEmpty() {
62
- return this._pq.size < 1;
90
+ peek() {
91
+ if (this.nodes.length === 0) {
92
+ return null;
93
+ }
94
+ return this.nodes[0];
63
95
  }
64
96
  /**
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
97
+ * Get the size (number of elements) of the heap.
67
98
  */
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;
99
+ get size() {
100
+ return this.nodes.length;
72
101
  }
73
102
  /**
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
103
+ * Get the last element in the heap, which is not necessarily a leaf node.
104
+ * @returns The last element or null if the heap is empty.
76
105
  */
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;
106
+ leaf() {
107
+ var _a;
108
+ return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
81
109
  }
82
110
  /**
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
111
+ * Check if the heap is empty.
112
+ * @returns True if the heap is empty, otherwise false.
91
113
  */
92
- add(priority, val) {
93
- val = val === undefined ? priority : val;
94
- this._pq.add(new HeapItem(priority, val));
95
- return this;
114
+ isEmpty() {
115
+ return this.size === 0;
116
+ }
117
+ clear() {
118
+ this.nodes = [];
119
+ }
120
+ refill(nodes) {
121
+ this.nodes = nodes;
122
+ this.fix();
96
123
  }
97
124
  /**
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.
125
+ * Use a comparison function to check whether a binary heap contains a specific element.
126
+ * @param value - the element to check.
127
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
100
128
  */
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;
129
+ has(value) {
130
+ return this.nodes.includes(value);
108
131
  }
109
132
  /**
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.
133
+ * Use a comparison function to find the index of an element in the heap.
134
+ * @param value - the element to find.
135
+ * @param index - the index currently being searched.
136
+ * @returns The index of the element, or -1 if not found.
113
137
  */
114
- has(node) {
115
- if (node instanceof HeapItem) {
116
- return this.pq.getNodes().includes(node);
138
+ findIndex(value, index) {
139
+ if (index >= this.size) {
140
+ return -1;
141
+ }
142
+ const compareResult = this.comparator(value, this.nodes[index]);
143
+ if (compareResult === 0) {
144
+ return index; // Element found
145
+ }
146
+ else if (compareResult < 0) {
147
+ // The element should be in the left subtree
148
+ return this.findIndex(value, 2 * index + 1);
117
149
  }
118
150
  else {
119
- return (this.pq.getNodes().findIndex(item => {
120
- return item.val === node;
121
- }) !== -1);
151
+ // The element should be in the right subtree
152
+ return this.findIndex(value, 2 * index + 2);
122
153
  }
123
154
  }
124
155
  /**
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
156
+ * Depth-first search (DFS) method, different traversal orders can be selected。
157
+ * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
158
+ * @returns An array containing elements traversed in the specified order.
127
159
  */
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);
160
+ dfs(order) {
161
+ const result = [];
162
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
163
+ const dfsHelper = (index) => {
164
+ if (index < this.size) {
165
+ if (order === 'in') {
166
+ dfsHelper(2 * index + 1);
167
+ result.push(this.nodes[index]);
168
+ dfsHelper(2 * index + 2);
169
+ }
170
+ else if (order === 'pre') {
171
+ result.push(this.nodes[index]);
172
+ dfsHelper(2 * index + 1);
173
+ dfsHelper(2 * index + 2);
174
+ }
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
+ dfsHelper(0); // Traverse starting from the root node
183
+ return result;
132
184
  }
133
185
  /**
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.
186
+ * Convert the heap to an array.
187
+ * @returns An array containing the elements of the heap.
140
188
  */
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);
189
+ toArray() {
190
+ return [...this.nodes];
191
+ }
192
+ getNodes() {
193
+ return this.nodes;
145
194
  }
146
195
  /**
147
- * The clear function clears the priority queue.
196
+ * Clone the heap, creating a new heap with the same elements.
197
+ * @returns A new Heap instance containing the same elements.
148
198
  */
149
- clear() {
150
- this._pq.clear();
199
+ clone() {
200
+ const clonedHeap = new Heap(this.comparator);
201
+ clonedHeap.nodes = [...this.nodes];
202
+ return clonedHeap;
203
+ }
204
+ /**
205
+ * Sort the elements in the heap and return them as an array.
206
+ * @returns An array containing the elements sorted in ascending order.
207
+ */
208
+ sort() {
209
+ const visitedNode = [];
210
+ const cloned = this.clone();
211
+ while (cloned.size !== 0) {
212
+ const top = cloned.poll();
213
+ if (top)
214
+ visitedNode.push(top);
215
+ }
216
+ return visitedNode;
217
+ }
218
+ /**
219
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
220
+ * @param nodes
221
+ * @param comparator - Comparison function.
222
+ * @returns A new Heap instance.
223
+ */
224
+ static heapify(nodes, comparator) {
225
+ const binaryHeap = new Heap(comparator);
226
+ binaryHeap.nodes = [...nodes];
227
+ binaryHeap.fix(); // Fix heap properties
228
+ return binaryHeap;
151
229
  }
152
230
  }
@@ -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<T = any> extends Heap<T> {
11
+ constructor(comparator?: CompareFunction<T>);
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<T = any> extends Heap<T> {
11
+ constructor(comparator?: CompareFunction<T>);
24
12
  }
@@ -1,27 +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 MinHeap
12
- * @extends Heap
13
- */
14
9
  export class MinHeap extends Heap {
15
- /**
16
- * The constructor initializes a PriorityQueue with a comparator function that compares the priority of two HeapItem
17
- * objects.
18
- * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
19
- * type `HeapOptions<V>`, which is a generic type that represents the options for the heap.
20
- */
21
- constructor(options) {
22
- super(options);
23
- this._pq = new PriorityQueue({
24
- comparator: (a, b) => a.priority - b.priority
25
- });
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 a - b;
16
+ }
17
+ }) {
18
+ super(comparator);
26
19
  }
27
20
  }
@@ -1,15 +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
8
  import { PriorityQueue } from './priority-queue';
9
- import type { PriorityQueueOptions } from '../../types';
10
- export declare class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
- constructor(options: PriorityQueueOptions<E>);
13
- static heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MaxPriorityQueue<E>;
14
- static heapify<E>(options: PriorityQueueOptions<E>): MaxPriorityQueue<E>;
9
+ import type { CompareFunction } from '../../types';
10
+ export declare class MaxPriorityQueue<T = any> extends PriorityQueue<T> {
11
+ constructor(compare?: CompareFunction<T>);
15
12
  }
@@ -1,39 +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 { PriorityQueue } from './priority-queue';
9
9
  export class MaxPriorityQueue extends PriorityQueue {
10
- /**
11
- * The constructor initializes a priority queue with an optional comparator function.
12
- * @param [options] - The `options` parameter is an optional object that can contain various properties to configure
13
- * the priority queue.
14
- */
15
- constructor(options) {
16
- super(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator)
17
- ? options.comparator
18
- : (a, b) => {
19
- const aKey = a, bKey = b;
20
- return bKey - aKey;
21
- } }));
22
- }
23
- /**
24
- * The function `heapify` creates a max priority queue from the given options and returns it.
25
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
26
- * queue. It can have the following properties:
27
- * @returns a MaxPriorityQueue object.
28
- */
29
- static heapify(options) {
30
- const maxPQ = new MaxPriorityQueue(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator)
31
- ? options.comparator
32
- : (a, b) => {
33
- const aKey = a, bKey = b;
34
- return bKey - aKey;
35
- } }));
36
- maxPQ._fix();
37
- return maxPQ;
10
+ constructor(compare = (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(compare);
38
19
  }
39
20
  }
@@ -1,15 +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
8
  import { PriorityQueue } from './priority-queue';
9
- import type { PriorityQueueOptions } from '../../types';
10
- export declare class MinPriorityQueue<E = any> extends PriorityQueue<E> {
11
- constructor(options?: Omit<PriorityQueueOptions<number>, 'comparator'>);
12
- constructor(options: PriorityQueueOptions<E>);
13
- static heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MinPriorityQueue<E>;
14
- static heapify<E>(options: PriorityQueueOptions<E>): MinPriorityQueue<E>;
9
+ import type { CompareFunction } from '../../types';
10
+ export declare class MinPriorityQueue<T = any> extends PriorityQueue<T> {
11
+ constructor(compare?: CompareFunction<T>);
15
12
  }
@@ -1,40 +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 { PriorityQueue } from './priority-queue';
9
9
  export class MinPriorityQueue extends PriorityQueue {
10
- /**
11
- * The constructor initializes a priority queue with an optional comparator function.
12
- * @param [options] - The `options` parameter is an optional object that can contain various configuration options for
13
- * the `PriorityQueue` constructor.
14
- */
15
- constructor(options) {
16
- super(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator)
17
- ? options.comparator
18
- : (a, b) => {
19
- const aKey = a, bKey = b;
20
- return aKey - bKey;
21
- } }));
22
- }
23
- /**
24
- * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
25
- * provided, and then fixes the heap structure of the queue.
26
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
27
- * queue. It can have the following properties:
28
- * @returns a MinPriorityQueue object.
29
- */
30
- static heapify(options) {
31
- const minPQ = new MinPriorityQueue(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator)
32
- ? options.comparator
33
- : (a, b) => {
34
- const aKey = a, bKey = b;
35
- return aKey - bKey;
36
- } }));
37
- minPQ._fix();
38
- return minPQ;
10
+ constructor(compare = (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 a - b;
16
+ }
17
+ }) {
18
+ super(compare);
39
19
  }
40
20
  }