data-structure-typed 0.9.16 → 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 (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 +96 -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,48 +1,91 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Queue = void 0;
3
+ exports.ArrayQueue = exports.Queue = void 0;
4
4
  /**
5
5
  * @license MIT
6
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
6
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
7
7
  * @class
8
8
  */
9
- var Queue = /** @class */ (function () {
9
+ const linked_list_1 = require("../linked-list");
10
+ class Queue extends linked_list_1.SinglyLinkedList {
10
11
  /**
11
- * Creates a queue.
12
- * @param {array} [elements]
12
+ * The enqueue function adds a value to the end of an array.
13
+ * @param {T} value - The value parameter represents the value that you want to add to the queue.
13
14
  */
14
- function Queue(elements) {
15
+ enqueue(value) {
16
+ this.push(value);
17
+ }
18
+ /**
19
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
20
+ * @returns The method is returning the element at the front of the queue, or null if the queue is empty.
21
+ */
22
+ dequeue() {
23
+ return this.shift();
24
+ }
25
+ /**
26
+ * The `peek` function returns the value of the head node in a linked list, or `undefined` if the list is empty.
27
+ * @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
28
+ */
29
+ peek() {
30
+ var _a;
31
+ return (_a = this.head) === null || _a === void 0 ? void 0 : _a.val;
32
+ }
33
+ *[Symbol.iterator]() {
34
+ let current = this.head;
35
+ while (current) {
36
+ yield current.val;
37
+ current = current.next;
38
+ }
39
+ }
40
+ }
41
+ exports.Queue = Queue;
42
+ class ArrayQueue {
43
+ /**
44
+ * The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
45
+ * @param {T[]} [elements] - The `elements` parameter is an optional array of elements of type `T`. If provided, it
46
+ * will be used to initialize the `_nodes` property of the class. If not provided, the `_nodes` property will be
47
+ * initialized as an empty array.
48
+ */
49
+ constructor(elements) {
15
50
  this._nodes = elements || [];
16
51
  this._offset = 0;
17
52
  }
18
53
  /**
19
- * Creates a queue from an existing array.
54
+ * The size function returns the number of elements in an array.
55
+ * @returns {number} The size of the array, which is the difference between the length of the array and the offset.
56
+ */
57
+ get size() {
58
+ return this._nodes.length - this._offset;
59
+ }
60
+ /**
61
+ * The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.
20
62
  * @public
21
63
  * @static
22
- * @param {array} elements
23
- * @return {Queue}
64
+ * @param {T[]} elements - The "elements" parameter is an array of elements of type T.
65
+ * @returns The method is returning a new instance of the Queue class, initialized with the elements from the input
66
+ * array.
24
67
  */
25
- Queue.fromArray = function (elements) {
26
- return new Queue(elements);
27
- };
68
+ static fromArray(elements) {
69
+ return new ArrayQueue(elements);
70
+ }
28
71
  /**
29
- * Adds an element at the back of the queue.
30
- * @public
31
- * @param {any} element
72
+ * The push function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
73
+ * @param {T} element - The `element` parameter represents the element that you want to add to the queue.
74
+ * @returns The `add` method is returning a `Queue<T>` object.
32
75
  */
33
- Queue.prototype.offer = function (element) {
76
+ push(element) {
34
77
  this._nodes.push(element);
35
78
  return this;
36
- };
79
+ }
37
80
  /**
38
- * Dequeues the front element in the queue.
39
- * @public
40
- * @returns {any}
81
+ * The `shift` function removes and returns the first element in the queue, and adjusts the internal data structure if
82
+ * necessary to optimize performance.
83
+ * @returns The function `shift()` returns either the first element in the queue or `null` if the queue is empty.
41
84
  */
42
- Queue.prototype.poll = function () {
43
- if (this.size() === 0)
85
+ shift() {
86
+ if (this.size === 0)
44
87
  return null;
45
- var first = this.peek();
88
+ const first = this.peek();
46
89
  this._offset += 1;
47
90
  if (this._offset * 2 < this._nodes.length)
48
91
  return first;
@@ -51,63 +94,64 @@ var Queue = /** @class */ (function () {
51
94
  this._nodes = this._nodes.slice(this._offset);
52
95
  this._offset = 0;
53
96
  return first;
54
- };
97
+ }
55
98
  /**
56
- * Returns the front element of the queue.
57
- * @public
58
- * @returns {any}
99
+ * The `peek` function returns the first element of the array `_nodes` if it exists, otherwise it returns `null`.
100
+ * @returns The `peek()` method returns the first element of the data structure, represented by the `_nodes` array at
101
+ * the `_offset` index. If the data structure is empty (size is 0), it returns `null`.
59
102
  */
60
- Queue.prototype.peek = function () {
61
- return this.size() > 0 ? this._nodes[this._offset] : null;
62
- };
103
+ peek() {
104
+ return this.size > 0 ? this._nodes[this._offset] : null;
105
+ }
63
106
  /**
64
- * Returns the back element of the queue.
65
- * @public
66
- * @returns {any}
107
+ * The `peekLast` function returns the last element in an array-like data structure, or null if the structure is empty.
108
+ * @returns The method `peekLast()` returns the last element of the `_nodes` array if the array is not empty. If the
109
+ * array is empty, it returns `null`.
67
110
  */
68
- Queue.prototype.peekLast = function () {
69
- return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
70
- };
111
+ peekLast() {
112
+ return this.size > 0 ? this._nodes[this._nodes.length - 1] : null;
113
+ }
71
114
  /**
72
- * Returns the number of elements in the queue.
73
- * @public
74
- * @returns {number}
115
+ * The enqueue function adds a value to the end of a queue.
116
+ * @param {T} value - The value parameter represents the value that you want to add to the queue.
75
117
  */
76
- Queue.prototype.size = function () {
77
- return this._nodes.length - this._offset;
78
- };
118
+ enqueue(value) {
119
+ this.push(value);
120
+ }
79
121
  /**
80
- * Checks if the queue is empty.
81
- * @public
82
- * @returns {boolean}
122
+ * The `dequeue` function removes and returns the first element from a queue, or returns null if the queue is empty.
123
+ * @returns The method is returning a value of type T or null.
83
124
  */
84
- Queue.prototype.isEmpty = function () {
85
- return this.size() === 0;
86
- };
125
+ dequeue() {
126
+ return this.shift();
127
+ }
87
128
  /**
88
- * Returns the remaining elements in the queue as an array.
89
- * @public
90
- * @returns {array}
129
+ * The function checks if a data structure is empty by comparing its size to zero.
130
+ * @returns {boolean} A boolean value indicating whether the size of the object is 0 or not.
91
131
  */
92
- Queue.prototype.toArray = function () {
132
+ isEmpty() {
133
+ return this.size === 0;
134
+ }
135
+ /**
136
+ * The toArray() function returns an array of elements from the current offset to the end of the _nodes array.
137
+ * @returns An array of type T is being returned.
138
+ */
139
+ toArray() {
93
140
  return this._nodes.slice(this._offset);
94
- };
141
+ }
95
142
  /**
96
- * Clears the queue.
97
- * @public
143
+ * The clear function resets the nodes array and offset to their initial values.
98
144
  */
99
- Queue.prototype.clear = function () {
145
+ clear() {
100
146
  this._nodes = [];
101
147
  this._offset = 0;
102
- };
148
+ }
103
149
  /**
104
- * Creates a shallow copy of the queue.
105
- * @public
106
- * @return {Queue}
150
+ * The `clone()` function returns a new Queue object with the same elements as the original Queue.
151
+ * @returns The `clone()` method is returning a new instance of the `Queue` class.
107
152
  */
108
- Queue.prototype.clone = function () {
109
- return new Queue(this._nodes.slice(this._offset));
110
- };
111
- return Queue;
112
- }());
113
- exports.Queue = Queue;
153
+ clone() {
154
+ return new ArrayQueue(this._nodes.slice(this._offset));
155
+ }
156
+ }
157
+ exports.ArrayQueue = ArrayQueue;
@@ -1,68 +1,63 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
4
4
  * @class
5
5
  */
6
- export declare class Stack<T> {
6
+ export declare class Stack<T = number> {
7
7
  protected _elements: T[];
8
8
  /**
9
- * Creates a stack.
10
- * @param {array} [elements]
9
+ * The constructor initializes an array of elements, which can be provided as an optional parameter.
10
+ * @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
11
+ * of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
12
+ * is provided and is an array, it is assigned to the `_elements
11
13
  */
12
14
  constructor(elements?: T[]);
13
15
  /**
14
- * Creates a stack from an existing array
15
- * @public
16
- * @static
17
- * @param {array} [elements]
18
- * @return {Stack}
16
+ * The function "fromArray" creates a new Stack object from an array of elements.
17
+ * @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
18
+ * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
19
+ * array.
19
20
  */
20
21
  static fromArray<T>(elements: T[]): Stack<T>;
21
22
  /**
22
- * Checks if the stack is empty.
23
- * @public
24
- * @returns {boolean}
23
+ * The function checks if an array is empty and returns a boolean value.
24
+ * @returns A boolean value indicating whether the `_elements` array is empty or not.
25
25
  */
26
26
  isEmpty(): boolean;
27
27
  /**
28
- * Returns the number of elements in the stack.
29
- * @public
30
- * @returns {number}
28
+ * The size() function returns the number of elements in an array.
29
+ * @returns The size of the elements array.
31
30
  */
32
31
  size(): number;
33
32
  /**
34
- * Returns the top element in the stack.
35
- * @public
36
- * @returns {object}
33
+ * The `peek` function returns the last element of an array, or null if the array is empty.
34
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
37
35
  */
38
36
  peek(): T | null;
39
37
  /**
40
- * Adds an element to the top of the stack.
41
- * @public
42
- * @param {object} element
38
+ * The push function adds an element to the stack and returns the updated stack.
39
+ * @param {T} element - The parameter "element" is of type T, which means it can be any data type.
40
+ * @returns The `push` method is returning the updated `Stack<T>` object.
43
41
  */
44
42
  push(element: T): Stack<T>;
45
43
  /**
46
- * Removes and returns the top element in the stack.
47
- * @public
48
- * @returns {object}
44
+ * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
45
+ * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
46
+ * array is empty, it returns `null`.
49
47
  */
50
48
  pop(): T | null;
51
49
  /**
52
- * Returns the remaining elements as an array.
53
- * @public
54
- * @returns {array}
50
+ * The toArray function returns a copy of the elements in an array.
51
+ * @returns An array of type T.
55
52
  */
56
53
  toArray(): T[];
57
54
  /**
58
- * Clears all elements from the stack.
59
- * @public
55
+ * The clear function clears the elements array.
60
56
  */
61
57
  clear(): void;
62
58
  /**
63
- * Creates a shallow copy from the stack.
64
- * @public
65
- * @return {Stack}
59
+ * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
60
+ * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
66
61
  */
67
62
  clone(): Stack<T>;
68
63
  }
@@ -3,95 +3,89 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stack = void 0;
4
4
  /**
5
5
  * @license MIT
6
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
6
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
7
7
  * @class
8
8
  */
9
- var Stack = /** @class */ (function () {
9
+ class Stack {
10
10
  /**
11
- * Creates a stack.
12
- * @param {array} [elements]
11
+ * The constructor initializes an array of elements, which can be provided as an optional parameter.
12
+ * @param {T[]} [elements] - The `elements` parameter is an optional parameter of type `T[]`, which represents an array
13
+ * of elements of type `T`. It is used to initialize the `_elements` property of the class. If the `elements` parameter
14
+ * is provided and is an array, it is assigned to the `_elements
13
15
  */
14
- function Stack(elements) {
16
+ constructor(elements) {
15
17
  this._elements = Array.isArray(elements) ? elements : [];
16
18
  }
17
19
  /**
18
- * Creates a stack from an existing array
19
- * @public
20
- * @static
21
- * @param {array} [elements]
22
- * @return {Stack}
20
+ * The function "fromArray" creates a new Stack object from an array of elements.
21
+ * @param {T[]} elements - The `elements` parameter is an array of elements of type `T`.
22
+ * @returns {Stack} The method is returning a new instance of the Stack class, initialized with the elements from the input
23
+ * array.
23
24
  */
24
- Stack.fromArray = function (elements) {
25
+ static fromArray(elements) {
25
26
  return new Stack(elements);
26
- };
27
+ }
27
28
  /**
28
- * Checks if the stack is empty.
29
- * @public
30
- * @returns {boolean}
29
+ * The function checks if an array is empty and returns a boolean value.
30
+ * @returns A boolean value indicating whether the `_elements` array is empty or not.
31
31
  */
32
- Stack.prototype.isEmpty = function () {
32
+ isEmpty() {
33
33
  return this._elements.length === 0;
34
- };
34
+ }
35
35
  /**
36
- * Returns the number of elements in the stack.
37
- * @public
38
- * @returns {number}
36
+ * The size() function returns the number of elements in an array.
37
+ * @returns The size of the elements array.
39
38
  */
40
- Stack.prototype.size = function () {
39
+ size() {
41
40
  return this._elements.length;
42
- };
41
+ }
43
42
  /**
44
- * Returns the top element in the stack.
45
- * @public
46
- * @returns {object}
43
+ * The `peek` function returns the last element of an array, or null if the array is empty.
44
+ * @returns The `peek()` function returns the last element of the `_elements` array, or `null` if the array is empty.
47
45
  */
48
- Stack.prototype.peek = function () {
46
+ peek() {
49
47
  if (this.isEmpty())
50
48
  return null;
51
49
  return this._elements[this._elements.length - 1];
52
- };
50
+ }
53
51
  /**
54
- * Adds an element to the top of the stack.
55
- * @public
56
- * @param {object} element
52
+ * The push function adds an element to the stack and returns the updated stack.
53
+ * @param {T} element - The parameter "element" is of type T, which means it can be any data type.
54
+ * @returns The `push` method is returning the updated `Stack<T>` object.
57
55
  */
58
- Stack.prototype.push = function (element) {
56
+ push(element) {
59
57
  this._elements.push(element);
60
58
  return this;
61
- };
59
+ }
62
60
  /**
63
- * Removes and returns the top element in the stack.
64
- * @public
65
- * @returns {object}
61
+ * The `pop` function removes and returns the last element from an array, or returns null if the array is empty.
62
+ * @returns The `pop()` method is returning the last element of the array `_elements` if the array is not empty. If the
63
+ * array is empty, it returns `null`.
66
64
  */
67
- Stack.prototype.pop = function () {
65
+ pop() {
68
66
  if (this.isEmpty())
69
67
  return null;
70
68
  return this._elements.pop() || null;
71
- };
69
+ }
72
70
  /**
73
- * Returns the remaining elements as an array.
74
- * @public
75
- * @returns {array}
71
+ * The toArray function returns a copy of the elements in an array.
72
+ * @returns An array of type T.
76
73
  */
77
- Stack.prototype.toArray = function () {
74
+ toArray() {
78
75
  return this._elements.slice();
79
- };
76
+ }
80
77
  /**
81
- * Clears all elements from the stack.
82
- * @public
78
+ * The clear function clears the elements array.
83
79
  */
84
- Stack.prototype.clear = function () {
80
+ clear() {
85
81
  this._elements = [];
86
- };
82
+ }
87
83
  /**
88
- * Creates a shallow copy from the stack.
89
- * @public
90
- * @return {Stack}
84
+ * The `clone()` function returns a new `Stack` object with the same elements as the original stack.
85
+ * @returns The `clone()` method is returning a new `Stack` object with a copy of the `_elements` array.
91
86
  */
92
- Stack.prototype.clone = function () {
87
+ clone() {
93
88
  return new Stack(this._elements.slice());
94
- };
95
- return Stack;
96
- }());
89
+ }
90
+ }
97
91
  exports.Stack = Stack;
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./tree"), exports);
@@ -0,0 +1,14 @@
1
+ export declare class TreeNode<T = any> {
2
+ constructor(id: string, value?: T, children?: TreeNode<T>[]);
3
+ private _id;
4
+ get id(): string;
5
+ set id(value: string);
6
+ private _value?;
7
+ get value(): T | undefined;
8
+ set value(value: T | undefined);
9
+ private _children?;
10
+ get children(): TreeNode<T>[] | undefined;
11
+ set children(value: TreeNode<T>[] | undefined);
12
+ addChildren(children: TreeNode<T> | TreeNode<T>[]): void;
13
+ getHeight(): number;
14
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TreeNode = void 0;
4
+ class TreeNode {
5
+ constructor(id, value, children) {
6
+ this._id = id;
7
+ this._value = value || undefined;
8
+ this._children = children || [];
9
+ }
10
+ get id() {
11
+ return this._id;
12
+ }
13
+ set id(value) {
14
+ this._id = value;
15
+ }
16
+ get value() {
17
+ return this._value;
18
+ }
19
+ set value(value) {
20
+ this._value = value;
21
+ }
22
+ get children() {
23
+ return this._children;
24
+ }
25
+ set children(value) {
26
+ this._children = value;
27
+ }
28
+ addChildren(children) {
29
+ if (!this.children) {
30
+ this.children = [];
31
+ }
32
+ if (children instanceof TreeNode) {
33
+ this.children.push(children);
34
+ }
35
+ else {
36
+ this.children = this.children.concat(children);
37
+ }
38
+ }
39
+ getHeight() {
40
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
41
+ const beginRoot = this;
42
+ let maxDepth = 1;
43
+ if (beginRoot) {
44
+ const bfs = (node, level) => {
45
+ if (level > maxDepth) {
46
+ maxDepth = level;
47
+ }
48
+ const { children } = node;
49
+ if (children) {
50
+ for (let i = 0, len = children.length; i < len; i++) {
51
+ bfs(children[i], level + 1);
52
+ }
53
+ }
54
+ };
55
+ bfs(beginRoot, 1);
56
+ }
57
+ return maxDepth;
58
+ }
59
+ }
60
+ exports.TreeNode = TreeNode;
@@ -1,38 +1,61 @@
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
  export declare class TrieNode {
2
- protected _value: string;
3
9
  constructor(v: string);
10
+ private _val;
11
+ get val(): string;
12
+ set val(v: string);
4
13
  protected _children: Map<string, TrieNode>;
5
14
  get children(): Map<string, TrieNode>;
6
15
  set children(v: Map<string, TrieNode>);
7
16
  protected _isEnd: boolean;
8
17
  get isEnd(): boolean;
9
18
  set isEnd(v: boolean);
10
- get val(): string;
11
- set val(v: string);
12
19
  }
13
20
  export declare class Trie {
14
21
  constructor(words?: string[]);
15
22
  protected _root: TrieNode;
16
23
  get root(): TrieNode;
17
24
  set root(v: TrieNode);
18
- put(word: string): boolean;
25
+ add(word: string): boolean;
19
26
  has(input: string): boolean;
20
27
  remove(word: string): boolean;
21
28
  /**
22
- * Only can present as a prefix, not a word
23
- * @param input
29
+ * The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
30
+ * @param {string} input - The input parameter is a string that represents the input value for the function.
31
+ * @returns a boolean value.
24
32
  */
25
33
  isAbsPrefix(input: string): boolean;
26
34
  /**
27
- * Can present as a abs prefix or word
28
- * @param input
35
+ * The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
36
+ * @param {string} input - The input parameter is a string that represents the prefix we want to check.
37
+ * @returns a boolean value.
29
38
  */
30
39
  isPrefix(input: string): boolean;
31
40
  /**
32
- * Check if the input string is the common prefix of all the words
33
- * @param input
41
+ * The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
42
+ * @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
43
+ * in the Trie data structure.
44
+ * @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
34
45
  */
35
46
  isCommonPrefix(input: string): boolean;
47
+ /**
48
+ * The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
49
+ * structure.
50
+ * @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
51
+ * Trie.
52
+ */
36
53
  getLongestCommonPrefix(): string;
54
+ /**
55
+ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
56
+ * @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
57
+ * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
58
+ * @returns an array of strings.
59
+ */
37
60
  getAll(prefix?: string): string[];
38
61
  }