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,212 +1,254 @@
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 {PriorityQueue} from '../priority-queue';
9
- import type {HeapOptions} from '../../types';
10
8
 
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
- }
9
+ import type {CompareFunction} from '../../types';
23
10
 
24
- private _priority: number;
11
+ export class Heap<T> {
12
+ private nodes: T[] = [];
13
+ private readonly comparator: CompareFunction<T>;
25
14
 
26
- get priority(): number {
27
- return this._priority;
15
+ constructor(comparator: CompareFunction<T>) {
16
+ this.comparator = comparator;
28
17
  }
29
18
 
30
- set priority(value: number) {
31
- this._priority = value;
19
+ /**
20
+ * Insert an element into the heap and maintain the heap properties.
21
+ * @param value - The element to be inserted.
22
+ */
23
+ add(value: T): Heap<T> {
24
+ this.nodes.push(value);
25
+ this.bubbleUp(this.nodes.length - 1);
26
+ return this;
32
27
  }
33
28
 
34
- private _val: V | null;
29
+ /**
30
+ * Remove and return the top element (smallest or largest element) from the heap.
31
+ * @returns The top element or null if the heap is empty.
32
+ */
33
+ poll(): T | null {
34
+ if (this.nodes.length === 0) {
35
+ return null;
36
+ }
37
+ if (this.nodes.length === 1) {
38
+ return this.nodes.pop() as T;
39
+ }
35
40
 
36
- get val(): V | null {
37
- return this._val;
41
+ const topValue = this.nodes[0];
42
+ this.nodes[0] = this.nodes.pop() as T;
43
+ this.sinkDown(0);
44
+ return topValue;
38
45
  }
39
46
 
40
- set val(value: V | null) {
41
- this._val = value;
42
- }
43
- }
44
-
45
- export abstract class Heap<V = any> {
46
47
  /**
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.
48
+ * Float operation to maintain heap properties after adding an element.
49
+ * @param index - The index of the newly added element.
50
50
  */
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');
51
+ protected bubbleUp(index: number): void {
52
+ const element = this.nodes[index];
53
+ while (index > 0) {
54
+ const parentIndex = Math.floor((index - 1) / 2);
55
+ const parent = this.nodes[parentIndex];
56
+ if (this.comparator(element, parent) < 0) {
57
+ this.nodes[index] = parent;
58
+ this.nodes[parentIndex] = element;
59
+ index = parentIndex;
60
+ } else {
61
+ break;
56
62
  }
57
- this._priorityExtractor = priorityExtractor || (el => +el);
58
- } else {
59
- this._priorityExtractor = el => +el;
60
63
  }
61
64
  }
62
65
 
63
- protected abstract _pq: PriorityQueue<HeapItem<V>>;
66
+ /**
67
+ * Sinking operation to maintain heap properties after removing the top element.
68
+ * @param index - The index from which to start sinking.
69
+ */
70
+ protected sinkDown(index: number): void {
71
+ const leftChildIndex = 2 * index + 1;
72
+ const rightChildIndex = 2 * index + 2;
73
+ const length = this.nodes.length;
74
+ let targetIndex = index;
75
+
76
+ if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
77
+ targetIndex = leftChildIndex;
78
+ }
79
+ if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
80
+ targetIndex = rightChildIndex;
81
+ }
64
82
 
65
- get pq() {
66
- return this._pq;
83
+ if (targetIndex !== index) {
84
+ const temp = this.nodes[index];
85
+ this.nodes[index] = this.nodes[targetIndex];
86
+ this.nodes[targetIndex] = temp;
87
+ this.sinkDown(targetIndex);
88
+ }
67
89
  }
68
90
 
69
- protected _priorityExtractor: (val: V) => number;
70
- get priorityExtractor() {
71
- return this._priorityExtractor;
91
+ /**
92
+ * Fix the entire heap to maintain heap properties.
93
+ */
94
+ protected fix() {
95
+ for (let i = Math.floor(this.size / 2); i >= 0; i--) this.sinkDown(i);
72
96
  }
73
97
 
74
98
  /**
75
- * The function returns the size of a priority queue.
76
- * @returns The size of the priority queue.
99
+ * Peek at the top element of the heap without removing it.
100
+ * @returns The top element or null if the heap is empty.
77
101
  */
78
- get size(): number {
79
- return this._pq.size;
102
+ peek(): T | null {
103
+ if (this.nodes.length === 0) {
104
+ return null;
105
+ }
106
+ return this.nodes[0];
80
107
  }
81
108
 
82
109
  /**
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.
110
+ * Get the size (number of elements) of the heap.
85
111
  */
86
- isEmpty(): boolean {
87
- return this._pq.size < 1;
112
+ get size(): number {
113
+ return this.nodes.length;
88
114
  }
89
115
 
90
- peek(isItem?: undefined): V | undefined;
91
- peek(isItem: false): V | undefined;
92
- peek(isItem: true): HeapItem<V> | null;
116
+ /**
117
+ * Get the last element in the heap, which is not necessarily a leaf node.
118
+ * @returns The last element or null if the heap is empty.
119
+ */
120
+ leaf(): T | null {
121
+ return this.nodes[this.size - 1] ?? null;
122
+ }
93
123
 
94
124
  /**
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
125
+ * Check if the heap is empty.
126
+ * @returns True if the heap is empty, otherwise false.
97
127
  */
98
- peek(isItem?: boolean): HeapItem<V> | null | V | undefined {
99
- isItem = isItem ?? false;
100
- const peeked = this._pq.peek();
128
+ isEmpty() {
129
+ return this.size === 0;
130
+ }
101
131
 
102
- return isItem ? peeked : peeked?.val;
132
+ clear() {
133
+ this.nodes = [];
103
134
  }
104
135
 
105
- peekLast(isItem?: undefined): V | undefined;
106
- peekLast(isItem: false): V | undefined;
107
- peekLast(isItem: true): HeapItem<V> | null;
136
+ refill(nodes: T[]) {
137
+ this.nodes = nodes;
138
+ this.fix();
139
+ }
108
140
 
109
141
  /**
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
142
+ * Use a comparison function to check whether a binary heap contains a specific element.
143
+ * @param value - the element to check.
144
+ * @returns Returns true if the specified element is contained; otherwise, returns false.
112
145
  */
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;
146
+ has(value: T): boolean {
147
+ return this.nodes.includes(value);
118
148
  }
119
149
 
120
150
  /**
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
151
+ * Use a comparison function to find the index of an element in the heap.
152
+ * @param value - the element to find.
153
+ * @param index - the index currently being searched.
154
+ * @returns The index of the element, or -1 if not found.
129
155
  */
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));
156
+ private findIndex(value: T, index: number): number {
157
+ if (index >= this.size) {
158
+ return -1;
159
+ }
133
160
 
134
- return this;
135
- }
161
+ const compareResult = this.comparator(value, this.nodes[index]);
136
162
 
137
- poll(isItem?: undefined): V | undefined;
138
- poll(isItem: false): V | undefined;
139
- poll(isItem: true): HeapItem<V> | null;
163
+ if (compareResult === 0) {
164
+ return index; // Element found
165
+ } else if (compareResult < 0) {
166
+ // The element should be in the left subtree
167
+ return this.findIndex(value, 2 * index + 1);
168
+ } else {
169
+ // The element should be in the right subtree
170
+ return this.findIndex(value, 2 * index + 2);
171
+ }
172
+ }
140
173
 
141
174
  /**
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.
175
+ * Depth-first search (DFS) method, different traversal orders can be selected。
176
+ * @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
177
+ * @returns An array containing elements traversed in the specified order.
144
178
  */
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
- }
179
+ dfs(order: 'in' | 'pre' | 'post'): T[] {
180
+ const result: T[] = [];
181
+
182
+ // Auxiliary recursive function, traverses the binary heap according to the traversal order
183
+ const dfsHelper = (index: number) => {
184
+ if (index < this.size) {
185
+ if (order === 'in') {
186
+ dfsHelper(2 * index + 1);
187
+ result.push(this.nodes[index]);
188
+ dfsHelper(2 * index + 2);
189
+ } else if (order === 'pre') {
190
+ result.push(this.nodes[index]);
191
+ dfsHelper(2 * index + 1);
192
+ dfsHelper(2 * index + 2);
193
+ } else if (order === 'post') {
194
+ dfsHelper(2 * index + 1);
195
+ dfsHelper(2 * index + 2);
196
+ result.push(this.nodes[index]);
197
+ }
198
+ }
199
+ };
200
+
201
+ dfsHelper(0); // Traverse starting from the root node
151
202
 
152
- return isItem ? top : top.val;
203
+ return result;
153
204
  }
154
205
 
155
206
  /**
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.
207
+ * Convert the heap to an array.
208
+ * @returns An array containing the elements of the heap.
159
209
  */
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
- }
210
+ toArray(): T[] {
211
+ return [...this.nodes];
170
212
  }
171
213
 
172
- toArray(isItem?: undefined): (V | undefined)[];
173
- toArray(isItem: false): (V | undefined)[];
174
- toArray(isItem: true): (HeapItem<V> | null)[];
214
+ getNodes(): T[] {
215
+ return this.nodes;
216
+ }
175
217
 
176
218
  /**
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
219
+ * Clone the heap, creating a new heap with the same elements.
220
+ * @returns A new Heap instance containing the same elements.
179
221
  */
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);
222
+ clone(): Heap<T> {
223
+ const clonedHeap = new Heap<T>(this.comparator);
224
+ clonedHeap.nodes = [...this.nodes];
225
+ return clonedHeap;
185
226
  }
186
227
 
187
- sort(isItem?: undefined): (V | undefined)[];
188
- sort(isItem: false): (V | undefined)[];
189
- sort(isItem: true): (HeapItem<V> | null)[];
190
-
191
228
  /**
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.
229
+ * Sort the elements in the heap and return them as an array.
230
+ * @returns An array containing the elements sorted in ascending order.
198
231
  */
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);
232
+ sort(): T[] {
233
+ const visitedNode: T[] = [];
234
+ const cloned = this.clone();
235
+ while (cloned.size !== 0) {
236
+ const top = cloned.poll();
237
+ if (top) visitedNode.push(top);
238
+ }
239
+ return visitedNode;
204
240
  }
205
241
 
206
242
  /**
207
- * The clear function clears the priority queue.
243
+ * Static method that creates a binary heap from an array of nodes and a comparison function.
244
+ * @param nodes
245
+ * @param comparator - Comparison function.
246
+ * @returns A new Heap instance.
208
247
  */
209
- clear(): void {
210
- this._pq.clear();
248
+ static heapify<T>(nodes: T[], comparator: CompareFunction<T>): Heap<T> {
249
+ const binaryHeap = new Heap<T>(comparator);
250
+ binaryHeap.nodes = [...nodes];
251
+ binaryHeap.fix(); // Fix heap properties
252
+ return binaryHeap;
211
253
  }
212
254
  }
@@ -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<T = any> extends Heap<T> {
13
+ constructor(
14
+ comparator: CompareFunction<T> = (a: T, b: T) => {
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<T = any> extends Heap<T> {
13
+ constructor(
14
+ comparator: CompareFunction<T> = (a: T, b: T) => {
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
- 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;
11
+ export class MaxPriorityQueue<T = any> extends PriorityQueue<T> {
12
+ constructor(
13
+ compare: CompareFunction<T> = (a: T, b: T) => {
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
  }
@@ -1,57 +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
- export class MinPriorityQueue<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 configuration options for
18
- * the `PriorityQueue` constructor.
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 aKey - bKey;
29
- }
30
- });
31
- }
32
-
33
- static override heapify<E extends number>(options?: Omit<PriorityQueueOptions<E>, 'comparator'>): MinPriorityQueue<E>;
34
- static override heapify<E>(options: PriorityQueueOptions<E>): MinPriorityQueue<E>;
35
-
36
- /**
37
- * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
38
- * provided, and then fixes the heap structure of the queue.
39
- * @param options - The `options` parameter is an object that contains configuration options for creating a priority
40
- * queue. It can have the following properties:
41
- * @returns a MinPriorityQueue object.
42
- */
43
- static override heapify<E>(options: PriorityQueueOptions<E>): MinPriorityQueue<E> {
44
- const minPQ = new MinPriorityQueue<E>({
45
- ...options,
46
- comparator: options?.comparator
47
- ? options.comparator
48
- : (a: E, b: E) => {
49
- const aKey = a as unknown as number,
50
- bKey = b as unknown as number;
51
- return aKey - bKey;
52
- }
53
- });
54
- minPQ._fix();
55
- return minPQ;
11
+ export class MinPriorityQueue<T = any> extends PriorityQueue<T> {
12
+ constructor(
13
+ compare: CompareFunction<T> = (a: T, b: T) => {
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 a - b;
18
+ }
19
+ }
20
+ ) {
21
+ super(compare);
56
22
  }
57
23
  }