data-structure-typed 1.19.2 → 1.19.4

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +1 -1
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -0,0 +1,227 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ export class TrieNode {
9
+
10
+ constructor(v: string) {
11
+ this._val = v;
12
+ this._isEnd = false;
13
+ this._children = new Map<string, TrieNode>();
14
+ }
15
+
16
+ private _val;
17
+
18
+ get val(): string {
19
+ return this._val;
20
+ }
21
+
22
+ set val(v: string) {
23
+ this._val = v;
24
+ }
25
+
26
+ protected _children: Map<string, TrieNode>;
27
+
28
+ get children(): Map<string, TrieNode> {
29
+ return this._children;
30
+ }
31
+
32
+ set children(v: Map<string, TrieNode>) {
33
+ this._children = v;
34
+ }
35
+
36
+ protected _isEnd: boolean;
37
+
38
+ get isEnd(): boolean {
39
+ return this._isEnd;
40
+ }
41
+
42
+ set isEnd(v: boolean) {
43
+ this._isEnd = v;
44
+ }
45
+
46
+ }
47
+
48
+ export class Trie {
49
+ constructor(words?: string[]) {
50
+ this._root = new TrieNode('');
51
+ if (words) {
52
+ for (const i of words) {
53
+ this.add(i);
54
+ }
55
+ }
56
+ }
57
+
58
+ protected _root: TrieNode;
59
+
60
+ get root() {
61
+ return this._root;
62
+ }
63
+
64
+ set root(v: TrieNode) {
65
+ this._root = v;
66
+ }
67
+
68
+ add(word: string): boolean {
69
+ let cur = this._root;
70
+ for (const c of word) {
71
+ let nodeC = cur.children.get(c);
72
+ if (!nodeC) {
73
+ nodeC = new TrieNode(c);
74
+ cur.children.set(c, nodeC);
75
+ }
76
+ cur = nodeC;
77
+ }
78
+ cur.isEnd = true;
79
+ return true;
80
+ }
81
+
82
+ has(input: string): boolean {
83
+ let cur = this._root;
84
+ for (const c of input) {
85
+ const nodeC = cur.children.get(c);
86
+ if (!nodeC) return false;
87
+ cur = nodeC;
88
+ }
89
+ return cur.isEnd;
90
+ }
91
+
92
+ remove(word: string) {
93
+ let isDeleted = false;
94
+ const dfs = (cur: TrieNode, i: number): boolean => {
95
+ const char = word[i];
96
+ const child = cur.children.get(char);
97
+ if (child) {
98
+ if (i === word.length - 1) {
99
+ if (child.isEnd) {
100
+ if (child.children.size > 0) {
101
+ child.isEnd = false;
102
+ } else {
103
+ cur.children.delete(char);
104
+ }
105
+ isDeleted = true;
106
+ return true;
107
+ }
108
+ return false;
109
+ }
110
+ const res = dfs(child, i + 1);
111
+ if (res && !cur.isEnd && child.children.size === 0) {
112
+ cur.children.delete(char);
113
+ return true;
114
+ }
115
+ return false;
116
+ }
117
+ return false;
118
+ }
119
+
120
+ dfs(this.root, 0);
121
+ return isDeleted;
122
+ }
123
+
124
+ // --- start additional methods ---
125
+ /**
126
+ * 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
127
+ * @param {string} input - The input parameter is a string that represents the input value for the function.
128
+ * @returns a boolean value.
129
+ */
130
+ isAbsPrefix(input: string): boolean {
131
+ let cur = this._root;
132
+ for (const c of input) {
133
+ const nodeC = cur.children.get(c);
134
+ if (!nodeC) return false;
135
+ cur = nodeC;
136
+ }
137
+ return !cur.isEnd;
138
+ }
139
+
140
+ /**
141
+ * 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
142
+ * @param {string} input - The input parameter is a string that represents the prefix we want to check.
143
+ * @returns a boolean value.
144
+ */
145
+ isPrefix(input: string): boolean {
146
+ let cur = this._root;
147
+ for (const c of input) {
148
+ const nodeC = cur.children.get(c);
149
+ if (!nodeC) return false;
150
+ cur = nodeC;
151
+ }
152
+ return true;
153
+ }
154
+
155
+ /**
156
+ * 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
157
+ * @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
158
+ * in the Trie data structure.
159
+ * @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
160
+ */
161
+ isCommonPrefix(input: string): boolean {
162
+ let commonPre = '';
163
+ const dfs = (cur: TrieNode) => {
164
+ commonPre += cur.val;
165
+ if (commonPre === input) return;
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 === input;
172
+ }
173
+
174
+ /**
175
+ * The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
176
+ * structure.
177
+ * @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
178
+ * Trie.
179
+ */
180
+ getLongestCommonPrefix(): string {
181
+ let commonPre = '';
182
+ const dfs = (cur: TrieNode) => {
183
+ commonPre += cur.val;
184
+ if (cur.isEnd) return;
185
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
186
+ else return;
187
+ }
188
+ dfs(this._root);
189
+ return commonPre;
190
+ }
191
+
192
+ /**
193
+ * The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
194
+ * @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
195
+ * trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
196
+ * @returns an array of strings.
197
+ */
198
+ getAll(prefix = ''): string[] {
199
+ const words: string[] = [];
200
+
201
+ function dfs(node: TrieNode, word: string) {
202
+ for (const char of node.children.keys()) {
203
+ const charNode = node.children.get(char);
204
+ if (charNode !== undefined) {
205
+ dfs(charNode, word.concat(char));
206
+ }
207
+ }
208
+ if (node.isEnd) {
209
+ words.push(word);
210
+ }
211
+ }
212
+
213
+ let startNode = this._root;
214
+
215
+ if (prefix) {
216
+ for (const c of prefix) {
217
+ const nodeC = startNode.children.get(c);
218
+ if (nodeC) startNode = nodeC;
219
+ }
220
+ }
221
+
222
+ dfs(startNode, prefix);
223
+ return words;
224
+ }
225
+
226
+ // --- end additional methods ---
227
+ }
@@ -0,0 +1,42 @@
1
+ import {AbstractBinaryTreeNode} from '../binary-tree';
2
+
3
+ /**
4
+ * Enum representing different loop types.
5
+ *
6
+ * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
7
+ * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
8
+ */
9
+ export enum LoopType { ITERATIVE = 'ITERATIVE', RECURSIVE = 'RECURSIVE'}
10
+
11
+ /* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */
12
+ export enum FamilyPosition {
13
+ ROOT = 'ROOT',
14
+ LEFT = 'LEFT',
15
+ RIGHT = 'RIGHT',
16
+ ROOT_LEFT = 'ROOT_LEFT',
17
+ ROOT_RIGHT = 'ROOT_RIGHT',
18
+ ISOLATED = 'ISOLATED',
19
+ MAL_NODE = 'MAL_NODE'
20
+ }
21
+
22
+ export type BinaryTreeNodePropertyName = 'id' | 'val';
23
+ export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
24
+ export type DFSOrderPattern = 'in' | 'pre' | 'post';
25
+ export type BinaryTreeNodeId = number;
26
+ export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined, needBalanced: N | null };
27
+
28
+ export type AbstractBinaryTreeNodeProperty<N extends AbstractBinaryTreeNode<N['val'], N>> =
29
+ N['val']
30
+ | N
31
+ | number
32
+ | BinaryTreeNodeId;
33
+
34
+ export type AbstractBinaryTreeNodeProperties<N extends AbstractBinaryTreeNode<N['val'], N>> = AbstractBinaryTreeNodeProperty<N>[];
35
+
36
+ export type AbstractBinaryTreeNodeNested<T> = AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, AbstractBinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
37
+
38
+ export type AbstractBinaryTreeOptions = {
39
+ loopType?: LoopType,
40
+ autoIncrementId?: boolean,
41
+ isMergeDuplicatedVal?: boolean
42
+ }
@@ -0,0 +1,5 @@
1
+ export type VertexId = string | number;
2
+ export type EdgeId = string;
3
+ export type DijkstraResult<V> =
4
+ { distMap: Map<V, number>, preMap: Map<V, V | null>, seen: Set<V>, paths: V[][], minDist: number, minPath: V[] }
5
+ | null;
@@ -0,0 +1,5 @@
1
+ import {AVLTreeNode} from '../binary-tree';
2
+ import {BSTOptions} from './bst';
3
+
4
+ export type AVLTreeNodeNested<T> = AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5
+ export type AVLTreeOptions = BSTOptions & {};
@@ -0,0 +1,9 @@
1
+ import {BinaryTreeNode} from '../binary-tree';
2
+ import {AbstractBinaryTreeOptions} from './abstract-binary-tree';
3
+
4
+
5
+ // export type BinaryTreeDeleted<N> = { deleted: N | null | undefined, needBalanced: N | null };
6
+ // export type ResultByProperty<N extends BinaryTreeNode<N['val'], N>> = N['val'] | N | number | BinaryTreeNodeId;
7
+ // export type ResultsByProperty<N extends BinaryTreeNode<N['val'], N>> = ResultByProperty<N>[];
8
+ export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
9
+ export type BinaryTreeOptions = AbstractBinaryTreeOptions & {}
@@ -0,0 +1,12 @@
1
+ import {BSTNode} from '../binary-tree';
2
+ import type {BinaryTreeOptions} from './binary-tree';
3
+ import {BinaryTreeNodeId} from './abstract-binary-tree';
4
+
5
+ export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
6
+
7
+ export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
8
+ export type BSTOptions = BinaryTreeOptions & {
9
+ comparator?: BSTComparator,
10
+ }
11
+
12
+ export enum CP {lt = 'lt', eq = 'eq', gt = 'gt'}
@@ -0,0 +1,8 @@
1
+ // 0 means unknown, 1 means visiting, 2 means visited;
2
+ export type TopologicalStatus = 0 | 1 | 2;
3
+
4
+ export enum TopologicalProperty {
5
+ VAL = 'VAL',
6
+ NODE = 'NODE',
7
+ ID = 'ID',
8
+ }
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,5 @@
1
+ export type HeapOptions<T> = {
2
+ priorityExtractor?: (element: T) => number;
3
+ // TODO there is an idea that support chaining which is for conveniently using the data structure
4
+ // isChaining? : boolean
5
+ }
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,15 @@
1
+ export * from './binary-tree';
2
+ export * from './bst';
3
+ export * from './avl-tree';
4
+ export * from './segment-tree';
5
+ export * from './tree-multiset';
6
+ export * from './abstract-graph';
7
+ export * from './abstract-binary-tree';
8
+ export * from './rb-tree';
9
+ export * from './directed-graph';
10
+ export * from './priority-queue';
11
+ export * from './heap';
12
+ export * from './singly-linked-list';
13
+ export * from './doubly-linked-list';
14
+ export * from './navigator';
15
+ export * from './helpers';
@@ -0,0 +1,13 @@
1
+ export type Direction = 'up' | 'right' | 'down' | 'left';
2
+ export type Turning = { [key in Direction]: Direction };
3
+
4
+ export type NavigatorParams<T> = {
5
+ matrix: T[][],
6
+ turning: Turning,
7
+ onMove: (cur: [number, number]) => void
8
+ init: {
9
+ cur: [number, number],
10
+ charDir: Direction,
11
+ VISITED: T,
12
+ }
13
+ }
@@ -0,0 +1,9 @@
1
+ export type PriorityQueueComparator<T> = (a: T, b: T) => number;
2
+
3
+ export type PriorityQueueOptions<T> = {
4
+ nodes?: T[];
5
+ isFix?: boolean;
6
+ comparator: PriorityQueueComparator<T>;
7
+ }
8
+
9
+ export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post';
@@ -0,0 +1,8 @@
1
+ import {BinaryTreeOptions} from './binary-tree';
2
+ import {RBTreeNode} from '../binary-tree';
3
+
4
+ export enum RBColor { RED = 'RED', BLACK = 'BLACK'}
5
+
6
+ export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7
+
8
+ export type RBTreeOptions = BinaryTreeOptions & {}
@@ -0,0 +1 @@
1
+ export type SegmentTreeNodeVal = number;
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,8 @@
1
+ import {TreeMultisetNode} from '../binary-tree';
2
+ import {AVLTreeOptions} from './avl-tree';
3
+
4
+ export type TreeMultisetNodeNested<T> = TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, TreeMultisetNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5
+
6
+ export type TreeMultisetOptions = Omit<AVLTreeOptions, 'isMergeDuplicatedVal'> & {
7
+ isMergeDuplicatedVal: true,
8
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './data-structures';
2
+ export * from './utils';
@@ -0,0 +1,3 @@
1
+ export * from './utils';
2
+ export * from './types';
3
+ export * from './validate-type';
@@ -0,0 +1,2 @@
1
+ export * from './utils';
2
+ export * from './validate-type';
@@ -0,0 +1,6 @@
1
+ export type ToThunkFn = () => ReturnType<TrlFn>;
2
+ export type Thunk = () => ReturnType<ToThunkFn> & { __THUNK__: Symbol };
3
+ export type TrlFn = (...args: any[]) => any;
4
+ export type TrlAsyncFn = (...args: any[]) => any;
5
+
6
+ export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -0,0 +1,25 @@
1
+ export type KeyValueObject = { [key: string]: any };
2
+
3
+ export type KeyValueObjectWithId = { [key: string]: any, id: string | number | symbol };
4
+
5
+ export type NonNumberNonObjectButDefined = string | boolean | symbol | null;
6
+
7
+ export type ObjectWithoutId = Omit<KeyValueObject, 'id'>;
8
+
9
+ export type ObjectWithNonNumberId = {
10
+ [key: string]: any,
11
+ id: string | boolean | symbol | null | object | undefined;
12
+ }
13
+
14
+ export type ObjectWithNumberId = {
15
+ [key: string]: any,
16
+ id: number;
17
+ }
18
+
19
+ export type RestrictValById =
20
+ NonNumberNonObjectButDefined
21
+ | ObjectWithoutId
22
+ | ObjectWithNonNumberId
23
+ | ObjectWithNumberId;
24
+
25
+ export type DummyAny = string | number | boolean | null | undefined | object | symbol | void | Function | never;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';
9
+
10
+ export const uuidV4 = function () {
11
+ return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
12
+ const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
13
+ return v.toString(16);
14
+ });
15
+ };
16
+
17
+ export const arrayRemove = function <T>(array: T[], predicate: (item: T, index: number, array: T[]) => boolean): T[] {
18
+ let i = -1, len = array ? array.length : 0;
19
+ const result = [];
20
+
21
+ while (++i < len) {
22
+ const value = array[i];
23
+ if (predicate(value, i, array)) {
24
+ result.push(value);
25
+ Array.prototype.splice.call(array, i--, 1);
26
+ len--;
27
+ }
28
+ }
29
+
30
+ return result;
31
+ };
32
+
33
+
34
+ export const THUNK_SYMBOL = Symbol('thunk')
35
+
36
+ export const isThunk = (fnOrValue: any) => {
37
+ return typeof fnOrValue === 'function' && fnOrValue.__THUNK__ === THUNK_SYMBOL
38
+ }
39
+
40
+ export const toThunk = (fn: ToThunkFn): Thunk => {
41
+ const thunk = () => fn()
42
+ thunk.__THUNK__ = THUNK_SYMBOL
43
+ return thunk
44
+ }
45
+
46
+ export const trampoline = (fn: TrlFn) => {
47
+ const cont = (...args: [...Parameters<TrlFn>]) => toThunk(() => fn(...args))
48
+
49
+ return Object.assign(
50
+ (...args: [...Parameters<TrlFn>]) => {
51
+ let result = fn(...args)
52
+
53
+ while (isThunk(result) && typeof result === 'function') {
54
+ result = result()
55
+ }
56
+
57
+ return result
58
+ },
59
+ {cont}
60
+ )
61
+ }
62
+
63
+ export const trampolineAsync = (fn: TrlAsyncFn) => {
64
+ const cont = (...args: [...Parameters<TrlAsyncFn>]) => toThunk(() => fn(...args))
65
+
66
+ return Object.assign(
67
+ async (...args: [...Parameters<TrlAsyncFn>]) => {
68
+ let result = await fn(...args)
69
+
70
+ while (isThunk(result) && typeof result === 'function') {
71
+ result = await result()
72
+ }
73
+
74
+ return result
75
+ },
76
+ {cont}
77
+ )
78
+ }
@@ -0,0 +1,69 @@
1
+ import {z} from 'zod';
2
+ import {NonNumberNonObjectButDefined, ObjectWithNonNumberId, ObjectWithNumberId, ObjectWithoutId} from './types';
3
+
4
+
5
+ export const nonNumberNonObjectButDefinedSchema = z.union([z.string(),
6
+ z.boolean(), z.any()])
7
+ .nullable()
8
+
9
+ export const keyValueObjectSchema = z.record(z.unknown())
10
+
11
+ export const objectWithoutIdSchema = keyValueObjectSchema.refine(obj => !('id' in obj), {
12
+ message: 'Object cannot contain the \'id\' field',
13
+ });
14
+
15
+ export const keyValueObjectWithIdSchema = z.record(z.any()).and(
16
+ z.object({
17
+ id: z.union([z.string(), z.number(), z.any()])
18
+ })
19
+ )
20
+
21
+ export const objectWithNonNumberIdSchema = z.record(z.any()).and(
22
+ z.object({
23
+ id: z
24
+ .union([z.string(), z.boolean(), z.any(), z.any(), z.undefined()])
25
+ .nullable()
26
+ })
27
+ )
28
+
29
+ export const objectWithNumberIdSchema = z.record(z.any()).and(
30
+ z.object({
31
+ id: z.number()
32
+ })
33
+ )
34
+
35
+ export const binaryTreeNodeValWithId = z.union([
36
+ nonNumberNonObjectButDefinedSchema,
37
+ objectWithoutIdSchema,
38
+ objectWithNonNumberIdSchema,
39
+ objectWithNumberIdSchema
40
+ ])
41
+
42
+ export function parseBySchema(schema: z.Schema, val: any) {
43
+ try {
44
+ schema.parse(val);
45
+ return true;
46
+ } catch (error) {
47
+ return false;
48
+ }
49
+ }
50
+
51
+ export function isNonNumberNonObjectButDefined(val: any): val is NonNumberNonObjectButDefined {
52
+ return parseBySchema(nonNumberNonObjectButDefinedSchema, val);
53
+ }
54
+
55
+ export function isObjectWithoutId(val: any): val is ObjectWithoutId {
56
+ return parseBySchema(objectWithoutIdSchema, val);
57
+ }
58
+
59
+ export function isObjectWithNonNumberId(val: any): val is ObjectWithNonNumberId {
60
+ return parseBySchema(objectWithNonNumberIdSchema, val);
61
+ }
62
+
63
+ export function isObjectWithNumberId(val: any): val is ObjectWithNumberId {
64
+ return parseBySchema(objectWithNonNumberIdSchema, val);
65
+ }
66
+
67
+ export function isNumber(val: any): val is number {
68
+ return typeof val === 'number';
69
+ }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "declaration": true,
4
4
  "outDir": "./dist",
5
5
  "module": "commonjs",
6
- "target": "es5",
6
+ "target": "es6",
7
7
  "lib": [
8
8
  // "es2015",
9
9
  "esnext"