data-structure-typed 0.9.16 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,82 +1,93 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __read = (this && this.__read) || function (o, n) {
14
- var m = typeof Symbol === "function" && o[Symbol.iterator];
15
- if (!m) return o;
16
- var i = m.call(o), r, ar = [], e;
17
- try {
18
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
19
- }
20
- catch (error) { e = { error: error }; }
21
- finally {
22
- try {
23
- if (r && !r.done && (m = i["return"])) m.call(i);
24
- }
25
- finally { if (e) throw e.error; }
26
- }
27
- return ar;
28
- };
29
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
30
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
31
- if (ar || !(i in from)) {
32
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
33
- ar[i] = from[i];
34
- }
35
- }
36
- return to.concat(ar || Array.prototype.slice.call(from));
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.PriorityQueue = void 0;
40
- var PriorityQueue = /** @class */ (function () {
41
- function PriorityQueue(options) {
42
- this.nodes = [];
43
- this._comparator = function (a, b) {
44
- var aKey = a, bKey = b;
4
+ class PriorityQueue {
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
+ */
10
+ constructor(options) {
11
+ this._nodes = [];
12
+ this._comparator = (a, b) => {
13
+ const aKey = a, bKey = b;
45
14
  return aKey - bKey;
46
15
  };
47
- var nodes = options.nodes, comparator = options.comparator, _a = options.isFix, isFix = _a === void 0 ? true : _a;
16
+ const { nodes, comparator, isFix = true } = options;
48
17
  this._comparator = comparator;
49
- if (nodes && nodes instanceof Array && nodes.length > 0) {
18
+ if (nodes && Array.isArray(nodes) && nodes.length > 0) {
50
19
  // TODO support distinct
51
- this.nodes = Array.isArray(nodes) ? __spreadArray([], __read(nodes), false) : [];
20
+ this._nodes = [...nodes];
52
21
  isFix && this._fix();
53
22
  }
54
23
  }
55
- Object.defineProperty(PriorityQueue.prototype, "size", {
56
- get: function () {
57
- return this.nodes.length;
58
- },
59
- enumerable: false,
60
- configurable: true
61
- });
62
- PriorityQueue.heapify = function (options) {
63
- var heap = new PriorityQueue(options);
24
+ get nodes() {
25
+ return this._nodes;
26
+ }
27
+ get size() {
28
+ return this.nodes.length;
29
+ }
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);
64
39
  heap._fix();
65
40
  return heap;
66
- };
67
- PriorityQueue.isPriorityQueueified = function (options) {
68
- return new PriorityQueue(__assign(__assign({}, options), { isFix: true })).isValid();
69
- };
70
- PriorityQueue.prototype.offer = function (node) {
41
+ }
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();
51
+ }
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;
57
+ }
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) {
71
64
  this.nodes.push(node);
72
65
  this._heapifyUp(this.size - 1);
73
- };
74
- PriorityQueue.prototype.peek = function () {
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
+ */
81
+ peek() {
75
82
  return this.size ? this.nodes[0] : null;
76
- };
77
- PriorityQueue.prototype.poll = function () {
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
+ */
88
+ poll() {
78
89
  var _a, _b;
79
- var res = null;
90
+ let res = null;
80
91
  if (this.size > 1) {
81
92
  this._swap(0, this.nodes.length - 1);
82
93
  res = (_a = this.nodes.pop()) !== null && _a !== void 0 ? _a : null;
@@ -86,105 +97,182 @@ var PriorityQueue = /** @class */ (function () {
86
97
  res = (_b = this.nodes.pop()) !== null && _b !== void 0 ? _b : null;
87
98
  }
88
99
  return res;
89
- };
90
- PriorityQueue.prototype.leaf = function () {
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
+ */
106
+ leaf() {
91
107
  var _a;
92
108
  return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : null;
93
- };
94
- PriorityQueue.prototype.isEmpty = function () {
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
+ */
116
+ isEmpty() {
95
117
  return this.size === 0;
96
- };
97
- PriorityQueue.prototype.clear = function () {
98
- this.nodes = [];
99
- };
100
- PriorityQueue.prototype.toArray = function () {
101
- return __spreadArray([], __read(this.nodes), false);
102
- };
103
- PriorityQueue.prototype.clone = function () {
118
+ }
119
+ /**
120
+ * The clear function clears the nodes array.
121
+ */
122
+ clear() {
123
+ this._setNodes([]);
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
+ */
129
+ toArray() {
130
+ return [...this.nodes];
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
+ */
138
+ clone() {
104
139
  return new PriorityQueue({ nodes: this.nodes, comparator: this._comparator });
105
- };
140
+ }
106
141
  // --- start additional methods ---
107
- PriorityQueue.prototype.isValid = function () {
108
- var _this = this;
109
- var isValidRecursive = function (parentIndex) {
110
- var isValidLeft = true;
111
- var isValidRight = true;
112
- if (_this._getLeft(parentIndex) !== -1) {
113
- var leftChildIndex = (parentIndex * 2) + 1;
114
- if (!_this._compare(parentIndex, leftChildIndex))
115
- return false;
116
- isValidLeft = isValidRecursive(leftChildIndex);
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
+ */
146
+ isValid() {
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;
117
152
  }
118
- if (_this._getRight(parentIndex) !== -1) {
119
- var rightChildIndex = (parentIndex * 2) + 2;
120
- if (!_this._compare(parentIndex, rightChildIndex))
121
- return false;
122
- isValidRight = isValidRecursive(rightChildIndex);
153
+ if (this._isValidIndex(rightChildIndex) && !this._compare(rightChildIndex, i)) {
154
+ return false;
123
155
  }
124
- return isValidLeft && isValidRight;
125
- };
126
- return isValidRecursive(0);
127
- };
128
- PriorityQueue.prototype.sort = function () {
129
- var visitedNode = [];
156
+ }
157
+ return true;
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
+ */
167
+ sort() {
168
+ const visitedNode = [];
130
169
  while (this.size !== 0) {
131
- var top = this.poll();
170
+ const top = this.poll();
132
171
  if (top)
133
172
  visitedNode.push(top);
134
173
  }
135
174
  return visitedNode;
136
- };
137
- PriorityQueue.prototype.DFS = function (dfsMode) {
138
- var _this = this;
139
- var visitedNode = [];
140
- var traverse = function (cur) {
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
+ */
183
+ DFS(dfsMode) {
184
+ const visitedNode = [];
185
+ const traverse = (cur) => {
141
186
  var _a, _b, _c;
142
- var leftChildIndex = _this._getLeft(cur);
143
- var rightChildIndex = _this._getRight(cur);
187
+ const leftChildIndex = this._getLeft(cur);
188
+ const rightChildIndex = this._getRight(cur);
144
189
  switch (dfsMode) {
145
190
  case 'in':
146
- _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
147
- visitedNode.push((_a = _this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
148
- _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
191
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
192
+ visitedNode.push((_a = this.nodes[cur]) !== null && _a !== void 0 ? _a : null);
193
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
149
194
  break;
150
195
  case 'pre':
151
- visitedNode.push((_b = _this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
152
- _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
153
- _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
196
+ visitedNode.push((_b = this.nodes[cur]) !== null && _b !== void 0 ? _b : null);
197
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
198
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
154
199
  break;
155
200
  case 'post':
156
- _this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
157
- _this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
158
- visitedNode.push((_c = _this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
201
+ this._isValidIndex(leftChildIndex) && traverse(leftChildIndex);
202
+ this._isValidIndex(rightChildIndex) && traverse(rightChildIndex);
203
+ visitedNode.push((_c = this.nodes[cur]) !== null && _c !== void 0 ? _c : null);
159
204
  break;
160
205
  }
161
206
  };
162
207
  this._isValidIndex(0) && traverse(0);
163
208
  return visitedNode;
164
- };
165
- PriorityQueue.prototype._compare = function (a, b) {
209
+ }
210
+ _setNodes(value) {
211
+ this._nodes = value;
212
+ }
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) {
166
222
  return this._comparator(this.nodes[a], this.nodes[b]) > 0;
167
- };
168
- PriorityQueue.prototype._swap = function (a, b) {
169
- var temp = this.nodes[a];
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];
170
231
  this.nodes[a] = this.nodes[b];
171
232
  this.nodes[b] = temp;
172
- };
173
- PriorityQueue.prototype._isValidIndex = function (index) {
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) {
174
241
  return index > -1 && index < this.nodes.length;
175
- };
176
- PriorityQueue.prototype._getParent = function (child) {
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) {
177
249
  return Math.floor((child - 1) / 2);
178
- };
179
- PriorityQueue.prototype._getLeft = function (parent) {
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) {
180
257
  return (2 * parent) + 1;
181
- };
182
- PriorityQueue.prototype._getRight = function (parent) {
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) {
183
265
  return (2 * parent) + 2;
184
- };
185
- PriorityQueue.prototype._getComparedChild = function (parent) {
186
- var min = parent;
187
- var left = this._getLeft(parent), right = this._getRight(parent);
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);
188
276
  if (left < this.size && this._compare(min, left)) {
189
277
  min = left;
190
278
  }
@@ -192,26 +280,39 @@ var PriorityQueue = /** @class */ (function () {
192
280
  min = right;
193
281
  }
194
282
  return min;
195
- };
196
- PriorityQueue.prototype._heapifyUp = function (start) {
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) {
197
290
  while (start > 0 && this._compare(this._getParent(start), start)) {
198
- var parent = this._getParent(start);
291
+ const parent = this._getParent(start);
199
292
  this._swap(start, parent);
200
293
  start = parent;
201
294
  }
202
- };
203
- PriorityQueue.prototype._heapifyDown = function (start) {
204
- var min = this._getComparedChild(start);
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);
205
303
  while (this._compare(start, min)) {
206
304
  this._swap(min, start);
207
305
  start = min;
208
306
  min = this._getComparedChild(start);
209
307
  }
210
- };
211
- PriorityQueue.prototype._fix = function () {
212
- for (var i = Math.floor(this.size / 2); i > -1; i--)
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--)
213
315
  this._heapifyDown(i);
214
- };
215
- return PriorityQueue;
216
- }());
316
+ }
317
+ }
217
318
  exports.PriorityQueue = PriorityQueue;
@@ -1,37 +1,165 @@
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
+ */
1
8
  import { DoublyLinkedList } from '../linked-list';
2
9
  export declare class Deque<T> extends DoublyLinkedList<T> {
3
10
  }
4
- export declare class ObjectDeque<T> {
5
- protected _nodes: {
6
- [key: number]: T;
7
- };
8
- protected _capacity: number;
9
- protected _first: number;
10
- protected _last: number;
11
- protected _size: number;
11
+ export declare class ObjectDeque<T = number> {
12
12
  constructor(capacity?: number);
13
- size(): number;
14
- offerFirst(value: T): void;
15
- offerLast(value: T): void;
13
+ private _nodes;
14
+ get nodes(): {
15
+ [p: number]: T;
16
+ };
17
+ private _capacity;
18
+ get capacity(): number;
19
+ set capacity(value: number);
20
+ private _first;
21
+ get first(): number;
22
+ set first(value: number);
23
+ private _last;
24
+ get last(): number;
25
+ set last(value: number);
26
+ private _size;
27
+ get size(): number;
28
+ /**
29
+ * The "addFirst" function adds a value to the beginning of an array-like data structure.
30
+ * @param {T} value - The `value` parameter represents the value that you want to add to the beginning of the data
31
+ * structure.
32
+ */
33
+ addFirst(value: T): void;
34
+ /**
35
+ * The addLast function adds a value to the end of an array-like data structure.
36
+ * @param {T} value - The `value` parameter represents the value that you want to add to the end of the data structure.
37
+ */
38
+ addLast(value: T): void;
39
+ /**
40
+ * The function `pollFirst()` removes and returns the first element in a data structure.
41
+ * @returns The value of the first element in the data structure.
42
+ */
16
43
  pollFirst(): T | undefined;
44
+ /**
45
+ * The `peekFirst` function returns the first element in an array-like data structure if it exists.
46
+ * @returns The element at the first position of the `_nodes` array.
47
+ */
17
48
  peekFirst(): T | undefined;
49
+ /**
50
+ * The `pollLast()` function removes and returns the last element in a data structure.
51
+ * @returns The value that was removed from the data structure.
52
+ */
18
53
  pollLast(): T | undefined;
54
+ /**
55
+ * The `peekLast()` function returns the last element in an array-like data structure.
56
+ * @returns The last element in the array "_nodes" is being returned.
57
+ */
19
58
  peekLast(): T | undefined;
59
+ /**
60
+ * The get function returns the element at the specified index in an array-like data structure.
61
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
62
+ * retrieve from the array.
63
+ * @returns The element at the specified index in the `_nodes` array is being returned. If there is no element at that
64
+ * index, `null` is returned.
65
+ */
20
66
  get(index: number): NonNullable<T> | null;
67
+ /**
68
+ * The function checks if the size of a data structure is less than or equal to zero.
69
+ * @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
70
+ */
21
71
  isEmpty(): boolean;
72
+ protected _seNodes(value: {
73
+ [p: number]: T;
74
+ }): void;
75
+ protected _setSize(value: number): void;
22
76
  }
23
77
  export declare class ArrayDeque<T> {
24
78
  protected _nodes: T[];
25
79
  get size(): number;
26
- offerLast(value: T): number;
80
+ /**
81
+ * O(n) time complexity of adding at the beginning and the end
82
+ */
83
+ /**
84
+ * The function "addLast" adds a value to the end of an array.
85
+ * @param {T} value - The value parameter represents the value that you want to add to the end of the array.
86
+ * @returns The return value is the new length of the array after the value has been added.
87
+ */
88
+ addLast(value: T): number;
89
+ /**
90
+ * The function "pollLast" returns and removes the last element from an array, or returns null if the array is empty.
91
+ * @returns The method `pollLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
92
+ */
27
93
  pollLast(): T | null;
94
+ /**
95
+ * The `pollFirst` function removes and returns the first element from an array, or returns null if the array is empty.
96
+ * @returns The `pollFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
97
+ * empty.
98
+ */
28
99
  pollFirst(): T | null;
29
- offerFirst(value: T): number;
100
+ /**
101
+ * O(n) time complexity of adding at the beginning and the end
102
+ */
103
+ /**
104
+ * The function "addFirst" adds a value to the beginning of an array.
105
+ * @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
106
+ * @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
107
+ * `value` at the beginning.
108
+ */
109
+ addFirst(value: T): number;
110
+ /**
111
+ * The `peekFirst` function returns the first element of an array or null if the array is empty.
112
+ * @returns The function `peekFirst()` is returning the first element (`T`) of the `_nodes` array. If the array is
113
+ * empty, it will return `null`.
114
+ */
30
115
  peekFirst(): T | null;
116
+ /**
117
+ * The `peekLast` function returns the last element of an array or null if the array is empty.
118
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
119
+ */
31
120
  peekLast(): T | null;
121
+ /**
122
+ * O(1) time complexity of obtaining the value
123
+ */
124
+ /**
125
+ * The get function returns the element at the specified index in an array, or null if the index is out of bounds.
126
+ * @param {number} index - The index parameter is a number that represents the position of the element you want to
127
+ * retrieve from the array.
128
+ * @returns The method is returning the element at the specified index in the `_nodes` array. If the element exists, it
129
+ * will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
130
+ */
32
131
  get(index: number): T | null;
132
+ /**
133
+ * The set function assigns a value to a specific index in an array.
134
+ * @param {number} index - The index parameter is a number that represents the position of the element in the array
135
+ * that you want to set a new value for.
136
+ * @param {T} value - The value parameter represents the new value that you want to set at the specified index in the
137
+ * _nodes array.
138
+ * @returns The value that is being set at the specified index in the `_nodes` array.
139
+ */
33
140
  set(index: number, value: T): T;
141
+ /**
142
+ * The insert function adds a value at a specified index in an array.
143
+ * @param {number} index - The index parameter specifies the position at which the value should be inserted in the
144
+ * array. It is a number that represents the index of the array where the value should be inserted. The index starts
145
+ * from 0, so the first element of the array has an index of 0, the second element has
146
+ * @param {T} value - The value parameter represents the value that you want to insert into the array at the specified
147
+ * index.
148
+ * @returns The splice method returns an array containing the removed elements, if any. In this case, since no elements
149
+ * are being removed, an empty array will be returned.
150
+ */
34
151
  insert(index: number, value: T): T[];
152
+ /**
153
+ * The remove function removes an element from an array at a specified index.
154
+ * @param {number} index - The index parameter specifies the position of the element to be removed from the array. It
155
+ * is a number that represents the index of the element to be removed.
156
+ * @returns The method is returning an array containing the removed element.
157
+ */
35
158
  remove(index: number): T[];
159
+ /**
160
+ * The function checks if an array called "_nodes" is empty.
161
+ * @returns The method `isEmpty()` is returning a boolean value. It returns `true` if the length of the `_nodes` array
162
+ * is 0, indicating that the array is empty. Otherwise, it returns `false`.
163
+ */
36
164
  isEmpty(): boolean;
37
165
  }