data-structure-typed 0.8.18 → 1.3.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 (272) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +690 -2
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  7. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -18
  8. package/dist/data-structures/binary-tree/avl-tree.js +110 -37
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +40 -2
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +44 -2
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -138
  12. package/dist/data-structures/binary-tree/binary-tree.js +27 -979
  13. package/dist/data-structures/binary-tree/bst.d.ts +118 -28
  14. package/dist/data-structures/binary-tree/bst.js +162 -124
  15. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  16. package/dist/data-structures/binary-tree/index.js +1 -0
  17. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  18. package/dist/data-structures/binary-tree/rb-tree.js +40 -2
  19. package/dist/data-structures/binary-tree/segment-tree.d.ts +63 -13
  20. package/dist/data-structures/binary-tree/segment-tree.js +80 -17
  21. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -10
  22. package/dist/data-structures/binary-tree/tree-multiset.js +682 -9
  23. package/dist/data-structures/graph/abstract-graph.d.ts +270 -64
  24. package/dist/data-structures/graph/abstract-graph.js +365 -92
  25. package/dist/data-structures/graph/directed-graph.d.ts +175 -26
  26. package/dist/data-structures/graph/directed-graph.js +249 -95
  27. package/dist/data-structures/graph/index.d.ts +1 -0
  28. package/dist/data-structures/graph/index.js +1 -0
  29. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  30. package/dist/data-structures/graph/map-graph.js +111 -0
  31. package/dist/data-structures/graph/undirected-graph.d.ts +111 -8
  32. package/dist/data-structures/graph/undirected-graph.js +154 -44
  33. package/dist/data-structures/hash/coordinate-map.d.ts +39 -2
  34. package/dist/data-structures/hash/coordinate-map.js +44 -3
  35. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  36. package/dist/data-structures/hash/coordinate-set.js +34 -0
  37. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  38. package/dist/data-structures/hash/hash-table.js +4 -0
  39. package/dist/data-structures/hash/index.d.ts +5 -0
  40. package/dist/data-structures/hash/index.js +5 -0
  41. package/dist/data-structures/hash/pair.d.ts +2 -1
  42. package/dist/data-structures/hash/pair.js +4 -0
  43. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  44. package/dist/data-structures/hash/tree-map.js +4 -0
  45. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-set.js +4 -0
  47. package/dist/data-structures/heap/heap.d.ts +62 -51
  48. package/dist/data-structures/heap/heap.js +106 -63
  49. package/dist/data-structures/heap/max-heap.d.ts +13 -4
  50. package/dist/data-structures/heap/max-heap.js +10 -2
  51. package/dist/data-structures/heap/min-heap.d.ts +14 -4
  52. package/dist/data-structures/heap/min-heap.js +11 -2
  53. package/dist/data-structures/index.d.ts +1 -0
  54. package/dist/data-structures/index.js +1 -0
  55. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -57
  56. package/dist/data-structures/linked-list/doubly-linked-list.js +461 -194
  57. package/dist/data-structures/linked-list/index.d.ts +1 -0
  58. package/dist/data-structures/linked-list/index.js +1 -0
  59. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -319
  60. package/dist/data-structures/linked-list/singly-linked-list.js +338 -557
  61. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  62. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  63. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  64. package/dist/data-structures/matrix/matrix.js +14 -0
  65. package/dist/data-structures/matrix/matrix2d.d.ts +87 -4
  66. package/dist/data-structures/matrix/matrix2d.js +91 -8
  67. package/dist/data-structures/matrix/navigator.d.ts +37 -16
  68. package/dist/data-structures/matrix/navigator.js +28 -0
  69. package/dist/data-structures/matrix/vector2d.d.ts +156 -29
  70. package/dist/data-structures/matrix/vector2d.js +184 -55
  71. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -1
  72. package/dist/data-structures/priority-queue/max-priority-queue.js +28 -4
  73. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -1
  74. package/dist/data-structures/priority-queue/min-priority-queue.js +29 -4
  75. package/dist/data-structures/priority-queue/priority-queue.d.ts +166 -22
  76. package/dist/data-structures/priority-queue/priority-queue.js +219 -75
  77. package/dist/data-structures/queue/deque.d.ts +141 -13
  78. package/dist/data-structures/queue/deque.js +151 -7
  79. package/dist/data-structures/queue/queue.d.ts +68 -42
  80. package/dist/data-structures/queue/queue.js +95 -51
  81. package/dist/data-structures/stack/stack.d.ts +30 -36
  82. package/dist/data-structures/stack/stack.js +31 -37
  83. package/dist/data-structures/tree/index.d.ts +1 -0
  84. package/dist/data-structures/tree/index.js +17 -0
  85. package/dist/data-structures/tree/tree.d.ts +14 -0
  86. package/dist/{types/utils.js → data-structures/tree/tree.js} +26 -19
  87. package/dist/data-structures/trie/trie.d.ts +39 -6
  88. package/dist/data-structures/trie/trie.js +81 -12
  89. package/dist/index.d.ts +3 -0
  90. package/dist/index.js +3 -0
  91. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  92. package/dist/interfaces/abstract-binary-tree.js +2 -0
  93. package/dist/interfaces/abstract-graph.d.ts +17 -0
  94. package/dist/interfaces/abstract-graph.js +2 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/avl-tree.js +2 -0
  97. package/dist/interfaces/binary-tree.d.ts +6 -0
  98. package/dist/interfaces/binary-tree.js +2 -0
  99. package/dist/interfaces/bst.d.ts +17 -0
  100. package/dist/interfaces/bst.js +2 -0
  101. package/dist/interfaces/directed-graph.d.ts +12 -0
  102. package/dist/interfaces/directed-graph.js +2 -0
  103. package/dist/interfaces/doubly-linked-list.js +2 -0
  104. package/dist/interfaces/heap.js +2 -0
  105. package/dist/interfaces/index.d.ts +15 -0
  106. package/dist/interfaces/index.js +31 -0
  107. package/dist/interfaces/navigator.js +2 -0
  108. package/dist/interfaces/priority-queue.js +2 -0
  109. package/dist/interfaces/rb-tree.d.ts +8 -0
  110. package/dist/interfaces/rb-tree.js +2 -0
  111. package/dist/interfaces/segment-tree.js +2 -0
  112. package/dist/interfaces/singly-linked-list.js +2 -0
  113. package/dist/interfaces/tree-multiset.d.ts +7 -0
  114. package/dist/interfaces/tree-multiset.js +2 -0
  115. package/dist/interfaces/undirected-graph.d.ts +5 -0
  116. package/dist/interfaces/undirected-graph.js +2 -0
  117. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  118. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  119. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  120. package/dist/types/data-structures/abstract-graph.js +2 -0
  121. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  122. package/dist/types/data-structures/avl-tree.js +2 -0
  123. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  124. package/dist/types/data-structures/binary-tree.js +2 -0
  125. package/dist/types/data-structures/bst.d.ts +13 -0
  126. package/dist/types/data-structures/bst.js +9 -0
  127. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  128. package/dist/types/data-structures/directed-graph.js +9 -0
  129. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  130. package/dist/types/data-structures/heap.d.ts +3 -0
  131. package/dist/types/data-structures/heap.js +2 -0
  132. package/dist/types/data-structures/index.d.ts +13 -7
  133. package/dist/types/data-structures/index.js +31 -0
  134. package/dist/types/data-structures/map-graph.d.ts +1 -0
  135. package/dist/types/data-structures/map-graph.js +2 -0
  136. package/dist/types/data-structures/navigator.d.ts +14 -0
  137. package/dist/types/data-structures/navigator.js +2 -0
  138. package/dist/types/data-structures/priority-queue.d.ts +7 -0
  139. package/dist/types/data-structures/priority-queue.js +2 -0
  140. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  141. package/dist/types/data-structures/rb-tree.js +8 -0
  142. package/dist/types/data-structures/segment-tree.d.ts +1 -0
  143. package/dist/types/data-structures/segment-tree.js +2 -0
  144. package/dist/types/data-structures/singly-linked-list.js +2 -0
  145. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  146. package/dist/types/data-structures/tree-multiset.js +2 -0
  147. package/dist/types/helpers.d.ts +1 -0
  148. package/dist/types/helpers.js +2 -0
  149. package/dist/types/index.d.ts +2 -0
  150. package/dist/types/index.js +2 -0
  151. package/dist/types/utils/index.d.ts +2 -0
  152. package/dist/types/utils/index.js +18 -0
  153. package/dist/types/utils/utils.d.ts +7 -0
  154. package/dist/types/utils/utils.js +2 -0
  155. package/dist/types/utils/validate-type.d.ts +19 -0
  156. package/dist/types/utils/validate-type.js +2 -0
  157. package/dist/utils/index.js +17 -0
  158. package/dist/utils/utils.d.ts +19 -0
  159. package/dist/{data-structures/trampoline.js → utils/utils.js} +26 -12
  160. package/package.json +106 -55
  161. package/.idea/data-structure-typed.iml +0 -12
  162. package/.idea/modules.xml +0 -8
  163. package/.idea/vcs.xml +0 -6
  164. package/dist/data-structures/trampoline.d.ts +0 -25
  165. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  166. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  167. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  168. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  169. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  170. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  171. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  172. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  173. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  174. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  175. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  176. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  177. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  178. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  179. package/dist/types/data-structures/graph/index.d.ts +0 -3
  180. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  181. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  182. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  183. package/dist/types/data-structures/hash/index.d.ts +0 -1
  184. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  185. package/dist/types/data-structures/heap/index.d.ts +0 -3
  186. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  187. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  188. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  189. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  190. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  191. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  192. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  193. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  194. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  195. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  196. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  197. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  198. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  199. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  200. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  201. package/dist/types/data-structures/queue/index.d.ts +0 -1
  202. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  203. package/dist/types/data-structures/stack/index.d.ts +0 -1
  204. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  205. package/dist/types/data-structures/trampoline.d.ts +0 -25
  206. package/dist/types/data-structures/trie/index.d.ts +0 -1
  207. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  208. package/dist/types/types/utils.d.ts +0 -46
  209. package/dist/types/utils.d.ts +0 -46
  210. package/dist/utils.d.ts +0 -122
  211. package/dist/utils.js +0 -569
  212. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  213. package/src/data-structures/binary-tree/avl-tree.ts +0 -232
  214. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  215. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  216. package/src/data-structures/binary-tree/binary-tree.ts +0 -1088
  217. package/src/data-structures/binary-tree/bst.ts +0 -404
  218. package/src/data-structures/binary-tree/index.ts +0 -11
  219. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  220. package/src/data-structures/binary-tree/segment-tree.ts +0 -164
  221. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  222. package/src/data-structures/binary-tree/tree-multiset.ts +0 -21
  223. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  224. package/src/data-structures/graph/abstract-graph.ts +0 -789
  225. package/src/data-structures/graph/directed-graph.ts +0 -322
  226. package/src/data-structures/graph/index.ts +0 -3
  227. package/src/data-structures/graph/undirected-graph.ts +0 -154
  228. package/src/data-structures/hash/coordinate-map.ts +0 -24
  229. package/src/data-structures/hash/coordinate-set.ts +0 -20
  230. package/src/data-structures/hash/hash-table.ts +0 -1
  231. package/src/data-structures/hash/index.ts +0 -1
  232. package/src/data-structures/heap/heap.ts +0 -136
  233. package/src/data-structures/heap/index.ts +0 -3
  234. package/src/data-structures/heap/max-heap.ts +0 -22
  235. package/src/data-structures/heap/min-heap.ts +0 -24
  236. package/src/data-structures/index.ts +0 -11
  237. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -258
  238. package/src/data-structures/linked-list/index.ts +0 -2
  239. package/src/data-structures/linked-list/singly-linked-list.ts +0 -750
  240. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  241. package/src/data-structures/matrix/index.ts +0 -4
  242. package/src/data-structures/matrix/matrix.ts +0 -13
  243. package/src/data-structures/matrix/matrix2d.ts +0 -125
  244. package/src/data-structures/matrix/navigator.ts +0 -99
  245. package/src/data-structures/matrix/vector2d.ts +0 -189
  246. package/src/data-structures/priority-queue/index.ts +0 -3
  247. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -12
  248. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -12
  249. package/src/data-structures/priority-queue/priority-queue.ts +0 -208
  250. package/src/data-structures/queue/deque.ts +0 -139
  251. package/src/data-structures/queue/index.ts +0 -2
  252. package/src/data-structures/queue/queue.ts +0 -123
  253. package/src/data-structures/stack/index.ts +0 -1
  254. package/src/data-structures/stack/stack.ts +0 -104
  255. package/src/data-structures/trampoline.ts +0 -91
  256. package/src/data-structures/trie/index.ts +0 -1
  257. package/src/data-structures/trie/trie.ts +0 -153
  258. package/src/index.ts +0 -1
  259. package/src/types/index.ts +0 -1
  260. package/src/types/patches/index.d.ts +0 -0
  261. package/src/types/utils.ts +0 -158
  262. package/src/utils.ts +0 -605
  263. package/tsconfig.json +0 -53
  264. /package/dist/{types/data-structures/hash/hash-table.d.ts → interfaces/doubly-linked-list.d.ts} +0 -0
  265. /package/dist/{types/data-structures/hash/pair.d.ts → interfaces/heap.d.ts} +0 -0
  266. /package/dist/{types/data-structures/hash/tree-map.d.ts → interfaces/navigator.d.ts} +0 -0
  267. /package/dist/{types/data-structures/hash/tree-set.d.ts → interfaces/priority-queue.d.ts} +0 -0
  268. /package/dist/{types/data-structures/linked-list/skip-linked-list.d.ts → interfaces/segment-tree.d.ts} +0 -0
  269. /package/{src/data-structures/hash/pair.ts → dist/interfaces/singly-linked-list.d.ts} +0 -0
  270. /package/{src/data-structures/hash/tree-map.ts → dist/types/data-structures/doubly-linked-list.d.ts} +0 -0
  271. /package/{src/data-structures/hash/tree-set.ts → dist/types/data-structures/singly-linked-list.d.ts} +0 -0
  272. /package/dist/{types/types → utils}/index.d.ts +0 -0
@@ -1,15 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MinPriorityQueue = void 0;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
4
11
  const priority_queue_1 = require("./priority-queue");
5
12
  class MinPriorityQueue extends priority_queue_1.PriorityQueue {
13
+ /**
14
+ * The constructor initializes a priority queue with an optional comparator function.
15
+ * @param [options] - The `options` parameter is an optional object that can contain various configuration options for
16
+ * the `PriorityQueue` constructor.
17
+ */
6
18
  constructor(options) {
7
- super({
8
- nodes: options.nodes, comparator: (a, b) => {
19
+ super(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : (a, b) => {
9
20
  const aKey = a, bKey = b;
10
21
  return aKey - bKey;
11
- }
12
- });
22
+ } }));
23
+ }
24
+ /**
25
+ * The function `heapify` creates a new MinPriorityQueue instance and sets the comparator function based on the options
26
+ * provided, and then fixes the heap structure of the queue.
27
+ * @param options - The `options` parameter is an object that contains configuration options for creating a priority
28
+ * queue. It can have the following properties:
29
+ * @returns a MinPriorityQueue object.
30
+ */
31
+ static heapify(options) {
32
+ const minPQ = new MinPriorityQueue(Object.assign(Object.assign({}, options), { comparator: (options === null || options === void 0 ? void 0 : options.comparator) ? options.comparator : (a, b) => {
33
+ const aKey = a, bKey = b;
34
+ return aKey - bKey;
35
+ } }));
36
+ minPQ._fix();
37
+ return minPQ;
13
38
  }
14
39
  }
15
40
  exports.MinPriorityQueue = MinPriorityQueue;
@@ -1,36 +1,180 @@
1
- export type PriorityQueueComparator<T> = (a: T, b: T) => number;
2
- export interface PriorityQueueOptions<T> {
3
- nodes?: T[];
4
- isFix?: boolean;
5
- comparator: PriorityQueueComparator<T>;
6
- }
7
- export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post';
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type { PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions } from '../../types';
8
9
  export declare class PriorityQueue<T = number> {
9
- protected nodes: T[];
10
+ /**
11
+ * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
12
+ * function.
13
+ * @param options - The `options` parameter is an object that contains the following properties:
14
+ */
15
+ constructor(options: PriorityQueueOptions<T>);
16
+ protected _nodes: T[];
17
+ get nodes(): T[];
10
18
  get size(): number;
19
+ /**
20
+ * The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
21
+ * @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
22
+ * It can include properties such as "comparator" which specifies the comparison function used to order the elements in
23
+ * the priority queue, and "initialValues" which is an array of initial values to be added to the priority
24
+ * @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
25
+ */
26
+ static heapify<T>(options: PriorityQueueOptions<T>): PriorityQueue<T>;
27
+ /**
28
+ * The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
29
+ * the isValid method.
30
+ * @param options - An object containing options for creating a priority queue. The options object should have the
31
+ * following properties:
32
+ * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
33
+ */
34
+ static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>): boolean;
35
+ /**
36
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
37
+ */
38
+ getNodes(): T[];
39
+ /**
40
+ * The "add" function adds a node to the heap and ensures that the heap property is maintained.
41
+ * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
42
+ * that needs to be added to the heap.
43
+ */
44
+ add(node: T): void;
45
+ /**
46
+ * The "has" function checks if a given node is present in the list of nodes.
47
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
48
+ * we want to check if it exists in the `nodes` array.
49
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
50
+ */
51
+ has(node: T): boolean;
52
+ /**
53
+ * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
54
+ * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
55
+ * Otherwise, it returns `null`.
56
+ */
57
+ peek(): T | null;
58
+ /**
59
+ * The `poll` function removes and returns the top element from a heap data structure.
60
+ * @returns The `poll()` method returns a value of type `T` or `null`.
61
+ */
62
+ poll(): T | null;
63
+ /**
64
+ * The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
65
+ * @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
66
+ * empty or the last element is `null`, then it returns `null`.
67
+ */
68
+ leaf(): T | null;
69
+ /**
70
+ * The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
71
+ * object is empty or not.
72
+ * @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
73
+ * 0.
74
+ */
75
+ isEmpty(): boolean;
76
+ /**
77
+ * The clear function clears the nodes array.
78
+ */
79
+ clear(): void;
80
+ /**
81
+ * The toArray function returns an array containing all the elements in the nodes property.
82
+ * @returns An array of type T, which is the elements of the nodes property.
83
+ */
84
+ toArray(): T[];
85
+ /**
86
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
87
+ * original instance.
88
+ * @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
89
+ * `comparator` properties as the original instance.
90
+ */
91
+ clone(): PriorityQueue<T>;
92
+ /**
93
+ * The `isValid` function recursively checks if a binary tree satisfies a certain condition.
94
+ * @returns The function `isValid()` returns a boolean value.
95
+ */
96
+ isValid(): boolean;
97
+ /**
98
+ * O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
99
+ */
100
+ /**
101
+ * The function sorts the elements in a data structure and returns them in an array.
102
+ * Plan to support sorting of duplicate elements.
103
+ * @returns The `sort()` method is returning an array of type `T[]`.
104
+ */
105
+ sort(): T[];
106
+ /**
107
+ * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
108
+ * based on the specified traversal order.
109
+ * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
110
+ * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
111
+ * @returns an array of type `(T | null)[]`.
112
+ */
113
+ DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[];
114
+ protected _setNodes(value: T[]): void;
11
115
  protected readonly _comparator: PriorityQueueComparator<T>;
12
- constructor(options: PriorityQueueOptions<T>);
116
+ /**
117
+ * The function compares two numbers using a custom comparator function.
118
+ * @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
119
+ * @param {number} b - The parameter "b" is a number.
120
+ * @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
121
+ * comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
122
+ * indicating that the element at index `a` is greater than the element at index `b`.
123
+ */
13
124
  protected _compare(a: number, b: number): boolean;
125
+ /**
126
+ * The function swaps two elements in an array.
127
+ * @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
128
+ * @param {number} b - The parameter "b" is a number.
129
+ */
14
130
  protected _swap(a: number, b: number): void;
131
+ /**
132
+ * The function checks if a given index is valid within an array.
133
+ * @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
134
+ * checked for validity.
135
+ * @returns A boolean value indicating whether the given index is valid or not.
136
+ */
15
137
  protected _isValidIndex(index: number): boolean;
138
+ /**
139
+ * The function returns the index of the parent node given the index of a child node in a binary tree.
140
+ * @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
141
+ * @returns the parent of the given child node.
142
+ */
16
143
  protected _getParent(child: number): number;
144
+ /**
145
+ * The function returns the index of the left child node in a binary tree given the index of its parent node.
146
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
147
+ * @returns the left child of a given parent node in a binary tree.
148
+ */
17
149
  protected _getLeft(parent: number): number;
150
+ /**
151
+ * The function returns the index of the right child node in a binary tree given the index of its parent node.
152
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
153
+ * @returns the right child of a given parent node in a binary tree.
154
+ */
18
155
  protected _getRight(parent: number): number;
156
+ /**
157
+ * The function returns the index of the smallest child node of a given parent node.
158
+ * @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
159
+ * tree.
160
+ * @returns the minimum value between the parent node and its left and right child nodes.
161
+ */
19
162
  protected _getComparedChild(parent: number): number;
163
+ /**
164
+ * The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
165
+ * correct position.
166
+ * @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
167
+ */
20
168
  protected _heapifyUp(start: number): void;
169
+ /**
170
+ * The function performs a heapify operation by comparing and swapping elements in a binary heap.
171
+ * @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
172
+ * operation should start.
173
+ */
21
174
  protected _heapifyDown(start: number): void;
175
+ /**
176
+ * The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
177
+ * towards the root.
178
+ */
22
179
  protected _fix(): void;
23
- offer(node: T): void;
24
- peek(): T | null;
25
- poll(): T | null;
26
- leaf(): T | null;
27
- isEmpty(): boolean;
28
- clear(): void;
29
- toArray(): T[];
30
- clone(): PriorityQueue<T>;
31
- isValid(): boolean;
32
- sort(): T[];
33
- DFS(dfsMode: PriorityQueueDFSOrderPattern): (T | null)[];
34
- static heapify<T>(options: PriorityQueueOptions<T>): PriorityQueue<T>;
35
- static isPriorityQueueified<T>(options: Omit<PriorityQueueOptions<T>, 'isFix'>): boolean;
36
180
  }
@@ -2,80 +2,89 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PriorityQueue = void 0;
4
4
  class PriorityQueue {
5
- get size() {
6
- return this.nodes.length;
7
- }
5
+ /**
6
+ * The constructor initializes a priority queue with the given options, including an array of nodes and a comparator
7
+ * function.
8
+ * @param options - The `options` parameter is an object that contains the following properties:
9
+ */
8
10
  constructor(options) {
9
- this.nodes = [];
11
+ this._nodes = [];
10
12
  this._comparator = (a, b) => {
11
13
  const aKey = a, bKey = b;
12
14
  return aKey - bKey;
13
15
  };
14
16
  const { nodes, comparator, isFix = true } = options;
15
17
  this._comparator = comparator;
16
- if (nodes && nodes instanceof Array && nodes.length > 0) {
18
+ if (nodes && Array.isArray(nodes) && nodes.length > 0) {
17
19
  // TODO support distinct
18
- this.nodes = Array.isArray(nodes) ? [...nodes] : [];
20
+ this._nodes = [...nodes];
19
21
  isFix && this._fix();
20
22
  }
21
23
  }
22
- _compare(a, b) {
23
- return this._comparator(this.nodes[a], this.nodes[b]) > 0;
24
- }
25
- _swap(a, b) {
26
- const temp = this.nodes[a];
27
- this.nodes[a] = this.nodes[b];
28
- this.nodes[b] = temp;
29
- }
30
- _isValidIndex(index) {
31
- return index > -1 && index < this.nodes.length;
32
- }
33
- _getParent(child) {
34
- return Math.floor((child - 1) / 2);
35
- }
36
- _getLeft(parent) {
37
- return (2 * parent) + 1;
38
- }
39
- _getRight(parent) {
40
- return (2 * parent) + 2;
24
+ get nodes() {
25
+ return this._nodes;
41
26
  }
42
- _getComparedChild(parent) {
43
- let min = parent;
44
- const left = this._getLeft(parent), right = this._getRight(parent);
45
- if (left < this.size && this._compare(min, left)) {
46
- min = left;
47
- }
48
- if (right < this.size && this._compare(min, right)) {
49
- min = right;
50
- }
51
- return min;
27
+ get size() {
28
+ return this.nodes.length;
52
29
  }
53
- _heapifyUp(start) {
54
- while (start > 0 && this._compare(this._getParent(start), start)) {
55
- const parent = this._getParent(start);
56
- this._swap(start, parent);
57
- start = parent;
58
- }
30
+ /**
31
+ * The `heapify` function creates a new PriorityQueue instance and fixes the heap property.
32
+ * @param options - The "options" parameter is an object that contains the configuration options for the PriorityQueue.
33
+ * It can include properties such as "comparator" which specifies the comparison function used to order the elements in
34
+ * the priority queue, and "initialValues" which is an array of initial values to be added to the priority
35
+ * @returns a new instance of the PriorityQueue class after performing the heapify operation on it.
36
+ */
37
+ static heapify(options) {
38
+ const heap = new PriorityQueue(options);
39
+ heap._fix();
40
+ return heap;
59
41
  }
60
- _heapifyDown(start) {
61
- let min = this._getComparedChild(start);
62
- while (this._compare(start, min)) {
63
- this._swap(min, start);
64
- start = min;
65
- min = this._getComparedChild(start);
66
- }
42
+ /**
43
+ * The function checks if a priority queue is valid by creating a new priority queue with a fix option and then calling
44
+ * the isValid method.
45
+ * @param options - An object containing options for creating a priority queue. The options object should have the
46
+ * following properties:
47
+ * @returns the result of calling the `isValid()` method on a new instance of the `PriorityQueue` class.
48
+ */
49
+ static isPriorityQueueified(options) {
50
+ return new PriorityQueue(Object.assign(Object.assign({}, options), { isFix: false })).isValid();
67
51
  }
68
- _fix() {
69
- for (let i = Math.floor(this.size / 2); i > -1; i--)
70
- this._heapifyDown(i);
52
+ /**
53
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
54
+ */
55
+ getNodes() {
56
+ return this._nodes;
71
57
  }
72
- offer(node) {
58
+ /**
59
+ * The "add" function adds a node to the heap and ensures that the heap property is maintained.
60
+ * @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
61
+ * that needs to be added to the heap.
62
+ */
63
+ add(node) {
73
64
  this.nodes.push(node);
74
65
  this._heapifyUp(this.size - 1);
75
66
  }
67
+ /**
68
+ * The "has" function checks if a given node is present in the list of nodes.
69
+ * @param {T} node - The parameter `node` is of type `T`, which means it can be any type. It represents the node that
70
+ * we want to check if it exists in the `nodes` array.
71
+ * @returns a boolean value indicating whether the given node is included in the array of nodes.
72
+ */
73
+ has(node) {
74
+ return this.nodes.includes(node);
75
+ }
76
+ /**
77
+ * The `peek` function returns the first element of the `nodes` array if it exists, otherwise it returns `null`.
78
+ * @returns The `peek()` function is returning the first element (`T`) of the `nodes` array if the `size` is not zero.
79
+ * Otherwise, it returns `null`.
80
+ */
76
81
  peek() {
77
82
  return this.size ? this.nodes[0] : null;
78
83
  }
84
+ /**
85
+ * The `poll` function removes and returns the top element from a heap data structure.
86
+ * @returns The `poll()` method returns a value of type `T` or `null`.
87
+ */
79
88
  poll() {
80
89
  var _a, _b;
81
90
  let res = null;
@@ -89,43 +98,72 @@ class PriorityQueue {
89
98
  }
90
99
  return res;
91
100
  }
101
+ /**
102
+ * The `leaf` function returns the last element in the `nodes` array or `null` if the array is empty.
103
+ * @returns The method `leaf()` is returning the last element (`T`) in the `nodes` array if it exists. If the array is
104
+ * empty or the last element is `null`, then it returns `null`.
105
+ */
92
106
  leaf() {
93
107
  var _a;
94
108
  return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
95
109
  }
110
+ /**
111
+ * The function checks if the size of an object is equal to zero and returns a boolean value indicating whether the
112
+ * object is empty or not.
113
+ * @returns The method `isEmpty()` is returning a boolean value indicating whether the size of the object is equal to
114
+ * 0.
115
+ */
96
116
  isEmpty() {
97
117
  return this.size === 0;
98
118
  }
119
+ /**
120
+ * The clear function clears the nodes array.
121
+ */
99
122
  clear() {
100
- this.nodes = [];
123
+ this._setNodes([]);
101
124
  }
125
+ /**
126
+ * The toArray function returns an array containing all the elements in the nodes property.
127
+ * @returns An array of type T, which is the elements of the nodes property.
128
+ */
102
129
  toArray() {
103
130
  return [...this.nodes];
104
131
  }
132
+ /**
133
+ * The `clone` function returns a new instance of the `PriorityQueue` class with the same nodes and comparator as the
134
+ * original instance.
135
+ * @returns The `clone()` method is returning a new instance of the `PriorityQueue` class with the same `nodes` and
136
+ * `comparator` properties as the original instance.
137
+ */
105
138
  clone() {
106
139
  return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
107
140
  }
108
141
  // --- start additional methods ---
142
+ /**
143
+ * The `isValid` function recursively checks if a binary tree satisfies a certain condition.
144
+ * @returns The function `isValid()` returns a boolean value.
145
+ */
109
146
  isValid() {
110
- const isValidRecursive = (parentIndex) => {
111
- let isValidLeft = true;
112
- let isValidRight = true;
113
- if (this._getLeft(parentIndex) !== -1) {
114
- const leftChildIndex = (parentIndex * 2) + 1;
115
- if (!this._compare(parentIndex, leftChildIndex))
116
- return false;
117
- isValidLeft = isValidRecursive(leftChildIndex);
147
+ for (let i = 0; i < this.nodes.length; i++) {
148
+ const leftChildIndex = this._getLeft(i);
149
+ const rightChildIndex = this._getRight(i);
150
+ if (this._isValidIndex(leftChildIndex) && !this._compare(leftChildIndex, i)) {
151
+ return false;
118
152
  }
119
- if (this._getRight(parentIndex) !== -1) {
120
- const rightChildIndex = (parentIndex * 2) + 2;
121
- if (!this._compare(parentIndex, rightChildIndex))
122
- return false;
123
- isValidRight = isValidRecursive(rightChildIndex);
153
+ if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
154
+ return false;
124
155
  }
125
- return isValidLeft && isValidRight;
126
- };
127
- return isValidRecursive(0);
156
+ }
157
+ return true;
128
158
  }
159
+ /**
160
+ * O(n log n), In scenarios with smaller data sizes, heap sort is generally expected to be slower than QuickSort or MergeSort.
161
+ */
162
+ /**
163
+ * The function sorts the elements in a data structure and returns them in an array.
164
+ * Plan to support sorting of duplicate elements.
165
+ * @returns The `sort()` method is returning an array of type `T[]`.
166
+ */
129
167
  sort() {
130
168
  const visitedNode = [];
131
169
  while (this.size !== 0) {
@@ -135,6 +173,13 @@ class PriorityQueue {
135
173
  }
136
174
  return visitedNode;
137
175
  }
176
+ /**
177
+ * The DFS function performs a depth-first search traversal on a binary tree and returns an array of visited nodes
178
+ * based on the specified traversal order.
179
+ * @param {PriorityQueueDFSOrderPattern} dfsMode - The dfsMode parameter is a string that specifies the order in which
180
+ * the nodes should be visited during the Depth-First Search (DFS) traversal. It can have one of the following values:
181
+ * @returns an array of type `(T | null)[]`.
182
+ */
138
183
  DFS(dfsMode) {
139
184
  const visitedNode = [];
140
185
  const traverse = (cur) => {
@@ -162,13 +207,112 @@ class PriorityQueue {
162
207
  this._isValidIndex(0) && traverse(0);
163
208
  return visitedNode;
164
209
  }
165
- static heapify(options) {
166
- const heap = new PriorityQueue(options);
167
- heap._fix();
168
- return heap;
210
+ _setNodes(value) {
211
+ this._nodes = value;
169
212
  }
170
- static isPriorityQueueified(options) {
171
- return new PriorityQueue(Object.assign(Object.assign({}, options), { isFix: true })).isValid();
213
+ /**
214
+ * The function compares two numbers using a custom comparator function.
215
+ * @param {number} a - The parameter "a" is a number that represents the index of a node in an array.
216
+ * @param {number} b - The parameter "b" is a number.
217
+ * @returns the result of the comparison between the elements at indices `a` and `b` in the `nodes` array. The
218
+ * comparison is done using the `_comparator` function, and if the result is greater than 0, `true` is returned,
219
+ * indicating that the element at index `a` is greater than the element at index `b`.
220
+ */
221
+ _compare(a, b) {
222
+ return this._comparator(this.nodes[a], this.nodes[b]) > 0;
223
+ }
224
+ /**
225
+ * The function swaps two elements in an array.
226
+ * @param {number} a - The parameter "a" is a number that represents the index of an element in an array.
227
+ * @param {number} b - The parameter "b" is a number.
228
+ */
229
+ _swap(a, b) {
230
+ const temp = this.nodes[a];
231
+ this.nodes[a] = this.nodes[b];
232
+ this.nodes[b] = temp;
233
+ }
234
+ /**
235
+ * The function checks if a given index is valid within an array.
236
+ * @param {number} index - The parameter "index" is of type number and represents the index value that needs to be
237
+ * checked for validity.
238
+ * @returns A boolean value indicating whether the given index is valid or not.
239
+ */
240
+ _isValidIndex(index) {
241
+ return index > -1 && index < this.nodes.length;
242
+ }
243
+ /**
244
+ * The function returns the index of the parent node given the index of a child node in a binary tree.
245
+ * @param {number} child - The "child" parameter is a number representing the index of a child node in a binary tree.
246
+ * @returns the parent of the given child node.
247
+ */
248
+ _getParent(child) {
249
+ return Math.floor((child - 1) / 2);
250
+ }
251
+ /**
252
+ * The function returns the index of the left child node in a binary tree given the index of its parent node.
253
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
254
+ * @returns the left child of a given parent node in a binary tree.
255
+ */
256
+ _getLeft(parent) {
257
+ return (2 * parent) + 1;
258
+ }
259
+ /**
260
+ * The function returns the index of the right child node in a binary tree given the index of its parent node.
261
+ * @param {number} parent - The parameter "parent" is a number that represents the index of a node in a binary tree.
262
+ * @returns the right child of a given parent node in a binary tree.
263
+ */
264
+ _getRight(parent) {
265
+ return (2 * parent) + 2;
266
+ }
267
+ /**
268
+ * The function returns the index of the smallest child node of a given parent node.
269
+ * @param {number} parent - The parent parameter is a number that represents the index of the parent node in a binary
270
+ * tree.
271
+ * @returns the minimum value between the parent node and its left and right child nodes.
272
+ */
273
+ _getComparedChild(parent) {
274
+ let min = parent;
275
+ const left = this._getLeft(parent), right = this._getRight(parent);
276
+ if (left < this.size && this._compare(min, left)) {
277
+ min = left;
278
+ }
279
+ if (right < this.size && this._compare(min, right)) {
280
+ min = right;
281
+ }
282
+ return min;
283
+ }
284
+ /**
285
+ * The function `_heapifyUp` is used to maintain the heap property by moving an element up the heap until it is in the
286
+ * correct position.
287
+ * @param {number} start - The start parameter is the index of the element that needs to be moved up in the heap.
288
+ */
289
+ _heapifyUp(start) {
290
+ while (start > 0 && this._compare(this._getParent(start), start)) {
291
+ const parent = this._getParent(start);
292
+ this._swap(start, parent);
293
+ start = parent;
294
+ }
295
+ }
296
+ /**
297
+ * The function performs a heapify operation by comparing and swapping elements in a binary heap.
298
+ * @param {number} start - The start parameter is the index of the element in the heap from where the heapifyDown
299
+ * operation should start.
300
+ */
301
+ _heapifyDown(start) {
302
+ let min = this._getComparedChild(start);
303
+ while (this._compare(start, min)) {
304
+ this._swap(min, start);
305
+ start = min;
306
+ min = this._getComparedChild(start);
307
+ }
308
+ }
309
+ /**
310
+ * The _fix function performs a heapify operation on the elements of the heap starting from the middle and moving
311
+ * towards the root.
312
+ */
313
+ _fix() {
314
+ for (let i = Math.floor(this.size / 2); i > -1; i--)
315
+ this._heapifyDown(i);
172
316
  }
173
317
  }
174
318
  exports.PriorityQueue = PriorityQueue;