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,139 +0,0 @@
1
- import {DoublyLinkedList} from '../linked-list';
2
-
3
- // O(n) time complexity of obtaining the value
4
- // O(1) time complexity of adding at the beginning and the end
5
- export class Deque<T> extends DoublyLinkedList<T> {
6
-
7
- }
8
-
9
- // O(1) time complexity of obtaining the value
10
- // O(n) time complexity of adding at the beginning and the end
11
- // todo tested slowest one
12
- export class ObjectDeque<T> {
13
- protected _nodes: { [key: number]: T } = {};
14
- protected _capacity = Number.MAX_SAFE_INTEGER;
15
- protected _first: number = -1;
16
- protected _last: number = -1;
17
- protected _size: number = 0;
18
-
19
- constructor(capacity?: number) {
20
- if (capacity !== undefined) this._capacity = capacity;
21
- }
22
-
23
- size() {
24
- return this._size;
25
- }
26
-
27
- offerFirst(value: T) {
28
- if (this._size === 0) {
29
- const mid = Math.floor(this._capacity / 2);
30
- this._first = mid;
31
- this._last = mid;
32
- } else {
33
- this._first--;
34
- }
35
- this._nodes[this._first] = value;
36
- this._size++;
37
- }
38
-
39
- offerLast(value: T) {
40
- if (this._size === 0) {
41
- const mid = Math.floor(this._capacity / 2);
42
- this._first = mid;
43
- this._last = mid;
44
- } else {
45
- this._last++;
46
- }
47
- this._nodes[this._last] = value;
48
- this._size++;
49
- }
50
-
51
- pollFirst() {
52
- if (!this._size) return;
53
- const value = this.peekFirst();
54
- delete this._nodes[this._first];
55
- this._first++;
56
- this._size--;
57
- return value;
58
- }
59
-
60
- peekFirst() {
61
- if (this._size) return this._nodes[this._first];
62
- }
63
-
64
- pollLast() {
65
- if (!this._size) return;
66
- const value = this.peekLast();
67
- delete this._nodes[this._last];
68
- this._last--;
69
- this._size--;
70
-
71
- return value;
72
- }
73
-
74
- peekLast() {
75
- if (this._size) return this._nodes[this._last];
76
- }
77
-
78
- get(index: number) {
79
- return this._nodes[this._first + index] || null;
80
- }
81
-
82
- isEmpty() {
83
- return this._size <= 0;
84
- }
85
- }
86
-
87
- // O(1) time complexity of obtaining the value
88
- // O(n) time complexity of adding at the beginning and the end
89
- export class ArrayDeque<T> {
90
- protected _nodes: T[] = [];
91
-
92
- get size() {
93
- return this._nodes.length;
94
- }
95
-
96
- offerLast(value: T) {
97
- return this._nodes.push(value);
98
- }
99
-
100
- pollLast(): T | null {
101
- return this._nodes.pop() ?? null;
102
- }
103
-
104
- pollFirst(): T | null {
105
- return this._nodes.shift() ?? null;
106
- }
107
-
108
- offerFirst(value: T) {
109
- return this._nodes.unshift(value);
110
- }
111
-
112
- peekFirst(): T | null {
113
- return this._nodes[0] ?? null;
114
- }
115
-
116
- peekLast(): T | null {
117
- return this._nodes[this._nodes.length - 1] ?? null;
118
- }
119
-
120
- get(index: number): T | null {
121
- return this._nodes[index] ?? null;
122
- }
123
-
124
- set(index: number, value: T) {
125
- return this._nodes[index] = value;
126
- }
127
-
128
- insert(index: number, value: T) {
129
- return this._nodes.splice(index, 0, value);
130
- }
131
-
132
- remove(index: number) {
133
- return this._nodes.splice(index, 1);
134
- }
135
-
136
- isEmpty() {
137
- return this._nodes.length === 0;
138
- }
139
- }
@@ -1,2 +0,0 @@
1
- export * from './queue';
2
- export * from './deque';
@@ -1,122 +0,0 @@
1
- /**
2
- * @license MIT
3
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
4
- * @class
5
- */
6
- export class Queue<T> {
7
- protected _nodes: T[];
8
- protected _offset: number;
9
-
10
- /**
11
- * Creates a queue.
12
- * @param {array} [elements]
13
- */
14
- constructor(elements?: T[]) {
15
- this._nodes = elements || [];
16
- this._offset = 0;
17
- }
18
-
19
- /**
20
- * Creates a queue from an existing array.
21
- * @public
22
- * @static
23
- * @param {array} elements
24
- * @return {Queue}
25
- */
26
- static fromArray<T>(elements: T[]): Queue<T> {
27
- return new Queue(elements);
28
- }
29
-
30
- /**
31
- * Adds an element at the back of the queue.
32
- * @public
33
- * @param {any} element
34
- */
35
- offer(element: T): Queue<T> {
36
- this._nodes.push(element);
37
- return this;
38
- }
39
-
40
- /**
41
- * Dequeues the front element in the queue.
42
- * @public
43
- * @returns {any}
44
- */
45
- poll(): T | null {
46
- if (this.size() === 0) return null;
47
-
48
- const first = this.peek();
49
- this._offset += 1;
50
-
51
- if (this._offset * 2 < this._nodes.length) return first;
52
-
53
- // only remove dequeued elements when reaching half size
54
- // to decrease latency of shifting elements.
55
- this._nodes = this._nodes.slice(this._offset);
56
- this._offset = 0;
57
- return first;
58
- }
59
-
60
- /**
61
- * Returns the front element of the queue.
62
- * @public
63
- * @returns {any}
64
- */
65
- peek(): T | null {
66
- return this.size() > 0 ? this._nodes[this._offset] : null;
67
- }
68
-
69
- /**
70
- * Returns the back element of the queue.
71
- * @public
72
- * @returns {any}
73
- */
74
- peekLast(): T | null {
75
- return this.size() > 0 ? this._nodes[this._nodes.length - 1] : null;
76
- }
77
-
78
- /**
79
- * Returns the number of elements in the queue.
80
- * @public
81
- * @returns {number}
82
- */
83
- size(): number {
84
- return this._nodes.length - this._offset;
85
- }
86
-
87
- /**
88
- * Checks if the queue is empty.
89
- * @public
90
- * @returns {boolean}
91
- */
92
- isEmpty(): boolean {
93
- return this.size() === 0;
94
- }
95
-
96
- /**
97
- * Returns the remaining elements in the queue as an array.
98
- * @public
99
- * @returns {array}
100
- */
101
- toArray(): T[] {
102
- return this._nodes.slice(this._offset);
103
- }
104
-
105
- /**
106
- * Clears the queue.
107
- * @public
108
- */
109
- clear(): void {
110
- this._nodes = [];
111
- this._offset = 0;
112
- }
113
-
114
- /**
115
- * Creates a shallow copy of the queue.
116
- * @public
117
- * @return {Queue}
118
- */
119
- clone(): Queue<T> {
120
- return new Queue(this._nodes.slice(this._offset));
121
- }
122
- }
@@ -1 +0,0 @@
1
- export * from './stack';
@@ -1,103 +0,0 @@
1
- /**
2
- * @license MIT
3
- * @copyright 2020 Tyler Zeng <zrwusa@gmail.com>
4
- * @class
5
- */
6
- export class Stack<T> {
7
- protected _elements: T[];
8
-
9
- /**
10
- * Creates a stack.
11
- * @param {array} [elements]
12
- */
13
- constructor(elements?: T[]) {
14
- this._elements = Array.isArray(elements) ? elements : [];
15
- }
16
-
17
- /**
18
- * Creates a stack from an existing array
19
- * @public
20
- * @static
21
- * @param {array} [elements]
22
- * @return {Stack}
23
- */
24
- static fromArray<T>(elements: T[]): Stack<T> {
25
- return new Stack(elements);
26
- }
27
-
28
- /**
29
- * Checks if the stack is empty.
30
- * @public
31
- * @returns {boolean}
32
- */
33
- isEmpty(): boolean {
34
- return this._elements.length === 0;
35
- }
36
-
37
- /**
38
- * Returns the number of elements in the stack.
39
- * @public
40
- * @returns {number}
41
- */
42
- size(): number {
43
- return this._elements.length;
44
- }
45
-
46
- /**
47
- * Returns the top element in the stack.
48
- * @public
49
- * @returns {object}
50
- */
51
- peek(): T | null {
52
- if (this.isEmpty()) return null;
53
-
54
- return this._elements[this._elements.length - 1];
55
- }
56
-
57
- /**
58
- * Adds an element to the top of the stack.
59
- * @public
60
- * @param {object} element
61
- */
62
- push(element: T): Stack<T> {
63
- this._elements.push(element);
64
- return this;
65
- }
66
-
67
- /**
68
- * Removes and returns the top element in the stack.
69
- * @public
70
- * @returns {object}
71
- */
72
- pop(): T | null {
73
- if (this.isEmpty()) return null;
74
-
75
- return this._elements.pop() || null;
76
- }
77
-
78
- /**
79
- * Returns the remaining elements as an array.
80
- * @public
81
- * @returns {array}
82
- */
83
- toArray(): T[] {
84
- return this._elements.slice();
85
- }
86
-
87
- /**
88
- * Clears all elements from the stack.
89
- * @public
90
- */
91
- clear(): void {
92
- this._elements = [];
93
- }
94
-
95
- /**
96
- * Creates a shallow copy from the stack.
97
- * @public
98
- * @return {Stack}
99
- */
100
- clone(): Stack<T> {
101
- return new Stack(this._elements.slice());
102
- }
103
- }
@@ -1,51 +0,0 @@
1
- export const THUNK_SYMBOL = Symbol('thunk')
2
-
3
- export const isThunk = (fnOrValue: any) => {
4
- return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
5
- }
6
-
7
- type ToThunkFn = () => ReturnType<TrlFn>;
8
-
9
- type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: typeof THUNK_SYMBOL };
10
-
11
- export const toThunk = (fn: ToThunkFn): Thunk => {
12
- const thunk = () => fn()
13
- thunk.__THUNK__ = THUNK_SYMBOL
14
- return thunk
15
- }
16
-
17
- type TrlFn = (...args: any[]) => any;
18
- export const trampoline = (fn: TrlFn) => {
19
- const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
20
-
21
- return Object.assign(
22
- (...args: [...Parameters<TrlFn>]) => {
23
- let result = fn(...args)
24
-
25
- while (isThunk(result) && typeof result === 'function') {
26
- result = result()
27
- }
28
-
29
- return result
30
- },
31
- {cont}
32
- )
33
- }
34
-
35
- type TrlAsyncFn = (...args: any[]) => any;
36
- export const trampolineAsync = (fn: TrlAsyncFn) => {
37
- const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
38
-
39
- return Object.assign(
40
- async (...args: [...Parameters<TrlAsyncFn>]) => {
41
- let result = await fn(...args)
42
-
43
- while (isThunk(result) && typeof result === 'function') {
44
- result = await result()
45
- }
46
-
47
- return result
48
- },
49
- {cont}
50
- )
51
- }
@@ -1 +0,0 @@
1
- export * from './trie';
@@ -1,203 +0,0 @@
1
- export class TrieNode {
2
- protected _value;
3
-
4
- constructor(v: string) {
5
- this._value = v;
6
- this._isEnd = false;
7
- this._children = new Map<string, TrieNode>();
8
- }
9
-
10
- protected _children: Map<string, TrieNode>;
11
-
12
- get children(): Map<string, TrieNode> {
13
- return this._children;
14
- }
15
-
16
- set children(v: Map<string, TrieNode>) {
17
- this._children = v;
18
- }
19
-
20
- protected _isEnd: boolean;
21
-
22
- get isEnd(): boolean {
23
- return this._isEnd;
24
- }
25
-
26
- set isEnd(v: boolean) {
27
- this._isEnd = v;
28
- }
29
-
30
- get val(): string {
31
- return this._value;
32
- }
33
-
34
- set val(v: string) {
35
- this._value = v;
36
- }
37
- }
38
-
39
- export class Trie {
40
- constructor(words?: string[]) {
41
- this._root = new TrieNode('');
42
- if (words) {
43
- for (const i of words) {
44
- this.put(i);
45
- }
46
- }
47
- }
48
-
49
- protected _root: TrieNode;
50
-
51
- get root() {
52
- return this._root;
53
- }
54
-
55
- set root(v: TrieNode) {
56
- this._root = v;
57
- }
58
-
59
- put(word: string): boolean {
60
- let cur = this._root;
61
- for (const c of word) {
62
- let nodeC = cur.children.get(c);
63
- if (!nodeC) {
64
- nodeC = new TrieNode(c);
65
- cur.children.set(c, nodeC);
66
- }
67
- cur = nodeC;
68
- }
69
- cur.isEnd = true;
70
- return true;
71
- }
72
-
73
- has(input: string): boolean {
74
- let cur = this._root;
75
- for (const c of input) {
76
- const nodeC = cur.children.get(c);
77
- if (!nodeC) return false;
78
- cur = nodeC;
79
- }
80
- return cur.isEnd;
81
- }
82
-
83
- remove(word: string) {
84
- let isDeleted = false;
85
- const dfs = (cur: TrieNode, i: number): boolean => {
86
- const char = word[i];
87
- const child = cur.children.get(char);
88
- if (child) {
89
- if (i === word.length - 1) {
90
- if (child.isEnd) {
91
- if (child.children.size > 0) {
92
- child.isEnd = false;
93
- } else {
94
- cur.children.delete(char);
95
- }
96
- isDeleted = true;
97
- return true;
98
- }
99
- return false;
100
- }
101
- const res = dfs(child, i + 1);
102
- if (res && !cur.isEnd && child.children.size === 0) {
103
- cur.children.delete(char);
104
- return true;
105
- }
106
- return false;
107
- }
108
- return false;
109
- }
110
-
111
- dfs(this.root, 0);
112
- return isDeleted;
113
- }
114
-
115
- // --- start additional methods ---
116
- /**
117
- * Only can present as a prefix, not a word
118
- * @param input
119
- */
120
- isAbsPrefix(input: string): boolean {
121
- let cur = this._root;
122
- for (const c of input) {
123
- const nodeC = cur.children.get(c);
124
- if (!nodeC) return false;
125
- cur = nodeC;
126
- }
127
- return !cur.isEnd;
128
- }
129
-
130
- /**
131
- * Can present as a abs prefix or word
132
- * @param input
133
- */
134
- isPrefix(input: string): boolean {
135
- let cur = this._root;
136
- for (const c of input) {
137
- const nodeC = cur.children.get(c);
138
- if (!nodeC) return false;
139
- cur = nodeC;
140
- }
141
- return true;
142
- }
143
-
144
- /**
145
- * Check if the input string is the common prefix of all the words
146
- * @param input
147
- */
148
- isCommonPrefix(input: string): boolean {
149
- let commonPre = '';
150
- const dfs = (cur: TrieNode) => {
151
- commonPre += cur.val;
152
- if (commonPre === input) return;
153
- if (cur.isEnd) return;
154
- if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
155
- else return;
156
- }
157
- dfs(this._root);
158
- return commonPre === input;
159
- }
160
-
161
- // Retrieve the longest common prefix of all the words
162
- getLongestCommonPrefix(): string {
163
- let commonPre = '';
164
- const dfs = (cur: TrieNode) => {
165
- commonPre += cur.val;
166
- if (cur.isEnd) return;
167
- if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
168
- else return;
169
- }
170
- dfs(this._root);
171
- return commonPre;
172
- }
173
-
174
- getAll(prefix = ''): string[] {
175
- const words: string[] = [];
176
-
177
- function dfs(node: TrieNode, word: string) {
178
- for (const char of node.children.keys()) {
179
- const charNode = node.children.get(char);
180
- if (charNode !== undefined) {
181
- dfs(charNode, word.concat(char));
182
- }
183
- }
184
- if (node.isEnd) {
185
- words.push(word);
186
- }
187
- }
188
-
189
- let startNode = this._root;
190
-
191
- if (prefix) {
192
- for (const c of prefix) {
193
- const nodeC = startNode.children.get(c);
194
- if (nodeC) startNode = nodeC;
195
- }
196
- }
197
-
198
- dfs(startNode, prefix);
199
- return words;
200
- }
201
-
202
- // --- end additional methods ---
203
- }
@@ -1,51 +0,0 @@
1
- export type VertexId = string | number;
2
- export type DijkstraResult<V> =
3
- { distMap: Map<V, number>, preMap: Map<V, V | null>, seen: Set<V>, paths: V[][], minDist: number, minPath: V[] }
4
- | null;
5
-
6
- export interface IGraph<V, E> {
7
-
8
- containsVertex(vertexOrId: V | VertexId): boolean;
9
-
10
- getVertex(vertexOrId: VertexId | V): V | null;
11
-
12
- getVertexId(vertexOrId: V | VertexId): VertexId;
13
-
14
- vertexSet(): Map<VertexId, V>;
15
-
16
- addVertex(v: V): boolean;
17
-
18
- removeVertex(vertexOrId: V | VertexId): boolean;
19
-
20
- removeAllVertices(vertices: V[] | VertexId[]): boolean;
21
-
22
- degreeOf(vertexOrId: V | VertexId): number;
23
-
24
- edgesOf(vertexOrId: V | VertexId): E[];
25
-
26
- containsEdge(src: V | VertexId, dest: V | VertexId): boolean;
27
-
28
- // containsEdge(e: E): boolean;
29
-
30
- getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
31
-
32
- // getAllEdges(src: V, dest: V): E[];
33
-
34
- edgeSet(): E[];
35
-
36
- addEdge(edge: E): boolean;
37
-
38
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
39
-
40
- removeEdge(edge: E): E | null;
41
-
42
- // removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[];
43
-
44
- // removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean;
45
-
46
- setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean;
47
-
48
- getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null;
49
-
50
- getNeighbors(vertexOrId: V | VertexId): V[];
51
- }
@@ -1,6 +0,0 @@
1
- import { AVLTreeNode } from "../binary-tree";
2
-
3
- export interface AVLTreeDeleted<T> {
4
- deleted: AVLTreeNode<T> | null;
5
- needBalanced: AVLTreeNode<T> | null;
6
- }
@@ -1,15 +0,0 @@
1
- import { BinaryTreeNode } from "../binary-tree";
2
-
3
- export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
4
- export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
5
- export type DFSOrderPattern = 'in' | 'pre' | 'post';
6
- export type BinaryTreeNodeId = number;
7
- export type BinaryTreeDeleted<T> = { deleted: BinaryTreeNode<T> | null | undefined, needBalanced: BinaryTreeNode<T> | null };
8
- export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
9
- export type ResultsByProperty<T> = ResultByProperty<T>[];
10
-
11
- export interface BinaryTreeNodeObj<T> {
12
- id: BinaryTreeNodeId;
13
- val: T;
14
- count?: number;
15
- }
@@ -1,5 +0,0 @@
1
- import { BSTNode } from '../binary-tree';
2
- import type {BinaryTreeNodeId} from './binary-tree';
3
-
4
- export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
5
- export type BSTDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };