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,37 +1,89 @@
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 Pablo
7
- *
6
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
8
7
  * @class
9
8
  */
10
- class Queue {
9
+ const linked_list_1 = require("../linked-list");
10
+ class Queue extends linked_list_1.SinglyLinkedList {
11
11
  /**
12
- * Creates a queue.
13
- * @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.
14
+ */
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.
14
48
  */
15
49
  constructor(elements) {
16
50
  this._nodes = elements || [];
17
51
  this._offset = 0;
18
52
  }
19
53
  /**
20
- * Adds an element at the back of the queue.
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.
21
62
  * @public
22
- * @param {any} element
63
+ * @static
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.
23
67
  */
24
- offer(element) {
68
+ static fromArray(elements) {
69
+ return new ArrayQueue(elements);
70
+ }
71
+ /**
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.
75
+ */
76
+ push(element) {
25
77
  this._nodes.push(element);
26
78
  return this;
27
79
  }
28
80
  /**
29
- * Dequeues the front element in the queue.
30
- * @public
31
- * @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.
32
84
  */
33
- poll() {
34
- if (this.size() === 0)
85
+ shift() {
86
+ if (this.size === 0)
35
87
  return null;
36
88
  const first = this.peek();
37
89
  this._offset += 1;
@@ -44,70 +96,62 @@ class Queue {
44
96
  return first;
45
97
  }
46
98
  /**
47
- * Returns the front element of the queue.
48
- * @public
49
- * @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`.
50
102
  */
51
103
  peek() {
52
- return this.size() > 0 ? this._nodes[this._offset] : null;
104
+ return this.size > 0 ? this._nodes[this._offset] : null;
53
105
  }
54
106
  /**
55
- * Returns the back element of the queue.
56
- * @public
57
- * @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`.
58
110
  */
59
111
  peekLast() {
60
- return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
112
+ return this.size > 0 ? this._nodes[this._nodes.length - 1] : null;
61
113
  }
62
114
  /**
63
- * Returns the number of elements in the queue.
64
- * @public
65
- * @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.
66
117
  */
67
- size() {
68
- return this._nodes.length - this._offset;
118
+ enqueue(value) {
119
+ this.push(value);
69
120
  }
70
121
  /**
71
- * Checks if the queue is empty.
72
- * @public
73
- * @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.
124
+ */
125
+ dequeue() {
126
+ return this.shift();
127
+ }
128
+ /**
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.
74
131
  */
75
132
  isEmpty() {
76
- return this.size() === 0;
133
+ return this.size === 0;
77
134
  }
78
135
  /**
79
- * Returns the remaining elements in the queue as an array.
80
- * @public
81
- * @returns {array}
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.
82
138
  */
83
139
  toArray() {
84
140
  return this._nodes.slice(this._offset);
85
141
  }
86
142
  /**
87
- * Clears the queue.
88
- * @public
143
+ * The clear function resets the nodes array and offset to their initial values.
89
144
  */
90
145
  clear() {
91
146
  this._nodes = [];
92
147
  this._offset = 0;
93
148
  }
94
149
  /**
95
- * Creates a shallow copy of the queue.
96
- * @public
97
- * @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.
98
152
  */
99
153
  clone() {
100
- return new Queue(this._nodes.slice(this._offset));
101
- }
102
- /**
103
- * Creates a queue from an existing array.
104
- * @public
105
- * @static
106
- * @param {array} elements
107
- * @return {Queue}
108
- */
109
- static fromArray(elements) {
110
- return new Queue(elements);
154
+ return new ArrayQueue(this._nodes.slice(this._offset));
111
155
  }
112
156
  }
113
- exports.Queue = Queue;
157
+ exports.ArrayQueue = ArrayQueue;
@@ -1,69 +1,63 @@
1
1
  /**
2
2
  * @license MIT
3
- * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
4
- *
3
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
5
4
  * @class
6
5
  */
7
- export declare class Stack<T> {
6
+ export declare class Stack<T = number> {
8
7
  protected _elements: T[];
9
8
  /**
10
- * Creates a stack.
11
- * @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
12
13
  */
13
14
  constructor(elements?: T[]);
14
15
  /**
15
- * Checks if the stack is empty.
16
- * @public
17
- * @returns {boolean}
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.
20
+ */
21
+ static fromArray<T>(elements: T[]): Stack<T>;
22
+ /**
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.
18
25
  */
19
26
  isEmpty(): boolean;
20
27
  /**
21
- * Returns the number of elements in the stack.
22
- * @public
23
- * @returns {number}
28
+ * The size() function returns the number of elements in an array.
29
+ * @returns The size of the elements array.
24
30
  */
25
31
  size(): number;
26
32
  /**
27
- * Returns the top element in the stack.
28
- * @public
29
- * @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.
30
35
  */
31
36
  peek(): T | null;
32
37
  /**
33
- * Adds an element to the top of the stack.
34
- * @public
35
- * @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.
36
41
  */
37
42
  push(element: T): Stack<T>;
38
43
  /**
39
- * Removes and returns the top element in the stack.
40
- * @public
41
- * @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`.
42
47
  */
43
48
  pop(): T | null;
44
49
  /**
45
- * Returns the remaining elements as an array.
46
- * @public
47
- * @returns {array}
50
+ * The toArray function returns a copy of the elements in an array.
51
+ * @returns An array of type T.
48
52
  */
49
53
  toArray(): T[];
50
54
  /**
51
- * Clears all elements from the stack.
52
- * @public
55
+ * The clear function clears the elements array.
53
56
  */
54
57
  clear(): void;
55
58
  /**
56
- * Creates a shallow copy from the stack.
57
- * @public
58
- * @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.
59
61
  */
60
62
  clone(): Stack<T>;
61
- /**
62
- * Creates a stack from an existing array
63
- * @public
64
- * @static
65
- * @param {array} [elements]
66
- * @return {Stack}
67
- */
68
- static fromArray<T>(elements: T[]): Stack<T>;
69
63
  }
@@ -3,38 +3,45 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Stack = void 0;
4
4
  /**
5
5
  * @license MIT
6
- * @copyright 2020 Pablo Rios <zrwusa@gmail.com>
7
- *
6
+ * @copyright Tyler Zeng <zrwusa@gmail.com>
8
7
  * @class
9
8
  */
10
9
  class Stack {
11
10
  /**
12
- * Creates a stack.
13
- * @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
14
15
  */
15
16
  constructor(elements) {
16
17
  this._elements = Array.isArray(elements) ? elements : [];
17
18
  }
18
19
  /**
19
- * Checks if the stack is empty.
20
- * @public
21
- * @returns {boolean}
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.
24
+ */
25
+ static fromArray(elements) {
26
+ return new Stack(elements);
27
+ }
28
+ /**
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.
22
31
  */
23
32
  isEmpty() {
24
33
  return this._elements.length === 0;
25
34
  }
26
35
  /**
27
- * Returns the number of elements in the stack.
28
- * @public
29
- * @returns {number}
36
+ * The size() function returns the number of elements in an array.
37
+ * @returns The size of the elements array.
30
38
  */
31
39
  size() {
32
40
  return this._elements.length;
33
41
  }
34
42
  /**
35
- * Returns the top element in the stack.
36
- * @public
37
- * @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.
38
45
  */
39
46
  peek() {
40
47
  if (this.isEmpty())
@@ -42,18 +49,18 @@ class Stack {
42
49
  return this._elements[this._elements.length - 1];
43
50
  }
44
51
  /**
45
- * Adds an element to the top of the stack.
46
- * @public
47
- * @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.
48
55
  */
49
56
  push(element) {
50
57
  this._elements.push(element);
51
58
  return this;
52
59
  }
53
60
  /**
54
- * Removes and returns the top element in the stack.
55
- * @public
56
- * @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`.
57
64
  */
58
65
  pop() {
59
66
  if (this.isEmpty())
@@ -61,37 +68,24 @@ class Stack {
61
68
  return this._elements.pop() || null;
62
69
  }
63
70
  /**
64
- * Returns the remaining elements as an array.
65
- * @public
66
- * @returns {array}
71
+ * The toArray function returns a copy of the elements in an array.
72
+ * @returns An array of type T.
67
73
  */
68
74
  toArray() {
69
75
  return this._elements.slice();
70
76
  }
71
77
  /**
72
- * Clears all elements from the stack.
73
- * @public
78
+ * The clear function clears the elements array.
74
79
  */
75
80
  clear() {
76
81
  this._elements = [];
77
82
  }
78
83
  /**
79
- * Creates a shallow copy from the stack.
80
- * @public
81
- * @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.
82
86
  */
83
87
  clone() {
84
88
  return new Stack(this._elements.slice());
85
89
  }
86
- /**
87
- * Creates a stack from an existing array
88
- * @public
89
- * @static
90
- * @param {array} [elements]
91
- * @return {Stack}
92
- */
93
- static fromArray(elements) {
94
- return new Stack(elements);
95
- }
96
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
+ }
@@ -1,32 +1,39 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TreeNode = void 0;
4
- const arr = ['1', 2, 4, 5, 6];
5
- const a = 2;
6
4
  class TreeNode {
7
- constructor(id, name, value, children) {
8
- this.id = id;
9
- this.name = name || '';
10
- this.value = value || undefined;
11
- this.children = children || [];
12
- }
13
- // TODO get set
14
- // get name (): string | undefined {
15
- // return this.name;
16
- // }
17
- //
18
- // set name (name: string | undefined) {
19
- // this.name = name;
20
- // }
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
+ }
21
28
  addChildren(children) {
22
29
  if (!this.children) {
23
30
  this.children = [];
24
31
  }
25
- if (children instanceof Array) {
26
- this.children = this.children.concat(children);
32
+ if (children instanceof TreeNode) {
33
+ this.children.push(children);
27
34
  }
28
35
  else {
29
- this.children.push(children);
36
+ this.children = this.children.concat(children);
30
37
  }
31
38
  }
32
39
  getHeight() {
@@ -1,4 +1,15 @@
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 {
9
+ constructor(v: string);
10
+ private _val;
11
+ get val(): string;
12
+ set val(v: string);
2
13
  protected _children: Map<string, TrieNode>;
3
14
  get children(): Map<string, TrieNode>;
4
15
  set children(v: Map<string, TrieNode>);
@@ -7,22 +18,44 @@ export declare class TrieNode {
7
18
  set isEnd(v: boolean);
8
19
  }
9
20
  export declare class Trie {
21
+ constructor(words?: string[]);
10
22
  protected _root: TrieNode;
11
23
  get root(): TrieNode;
12
24
  set root(v: TrieNode);
13
- constructor();
14
- put(input: string): boolean;
25
+ add(word: string): boolean;
15
26
  has(input: string): boolean;
16
27
  remove(word: string): boolean;
17
28
  /**
18
- * Only can present as a prefix, not a word
19
- * @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.
20
32
  */
21
33
  isAbsPrefix(input: string): boolean;
22
34
  /**
23
- * Can present as a prefix or word
24
- * @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.
25
38
  */
26
39
  isPrefix(input: string): boolean;
40
+ /**
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.
45
+ */
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
+ */
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
+ */
27
60
  getAll(prefix?: string): string[];
28
61
  }