stack-typed 1.53.6 → 1.53.7
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.
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
|
@@ -92,13 +92,100 @@ export class TrieNode {
|
|
|
92
92
|
* 9. Spell Check: Checking the spelling of words.
|
|
93
93
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
94
94
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
95
|
+
* @example
|
|
96
|
+
* // Autocomplete: Prefix validation and checking
|
|
97
|
+
* const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
|
|
98
|
+
*
|
|
99
|
+
* // Get all completions for a prefix
|
|
100
|
+
* const gmailCompletions = autocomplete.getWords('gmail');
|
|
101
|
+
* console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
|
|
102
|
+
* @example
|
|
103
|
+
* // File System Path Operations
|
|
104
|
+
* const fileSystem = new Trie<string>([
|
|
105
|
+
* '/home/user/documents/file1.txt',
|
|
106
|
+
* '/home/user/documents/file2.txt',
|
|
107
|
+
* '/home/user/pictures/photo.jpg',
|
|
108
|
+
* '/home/user/pictures/vacation/',
|
|
109
|
+
* '/home/user/downloads'
|
|
110
|
+
* ]);
|
|
111
|
+
*
|
|
112
|
+
* // Find common directory prefix
|
|
113
|
+
* console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
|
|
114
|
+
*
|
|
115
|
+
* // List all files in a directory
|
|
116
|
+
* const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
117
|
+
* console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
|
|
118
|
+
* @example
|
|
119
|
+
* // Autocomplete: Basic word suggestions
|
|
120
|
+
* // Create a trie for autocomplete
|
|
121
|
+
* const autocomplete = new Trie<string>([
|
|
122
|
+
* 'function',
|
|
123
|
+
* 'functional',
|
|
124
|
+
* 'functions',
|
|
125
|
+
* 'class',
|
|
126
|
+
* 'classes',
|
|
127
|
+
* 'classical',
|
|
128
|
+
* 'closure',
|
|
129
|
+
* 'const',
|
|
130
|
+
* 'constructor'
|
|
131
|
+
* ]);
|
|
132
|
+
*
|
|
133
|
+
* // Test autocomplete with different prefixes
|
|
134
|
+
* console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
|
|
135
|
+
* console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
|
|
136
|
+
* console.log(autocomplete.getWords('con')); // ['constructor', 'const']
|
|
137
|
+
*
|
|
138
|
+
* // Test with non-matching prefix
|
|
139
|
+
* console.log(autocomplete.getWords('xyz')); // []
|
|
140
|
+
* @example
|
|
141
|
+
* // Dictionary: Case-insensitive word lookup
|
|
142
|
+
* // Create a case-insensitive dictionary
|
|
143
|
+
* const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
144
|
+
*
|
|
145
|
+
* // Add words with mixed casing
|
|
146
|
+
* dictionary.add('Hello');
|
|
147
|
+
* dictionary.add('WORLD');
|
|
148
|
+
* dictionary.add('JavaScript');
|
|
149
|
+
*
|
|
150
|
+
* // Test lookups with different casings
|
|
151
|
+
* console.log(dictionary.has('hello')); // true
|
|
152
|
+
* console.log(dictionary.has('HELLO')); // true
|
|
153
|
+
* console.log(dictionary.has('Hello')); // true
|
|
154
|
+
* console.log(dictionary.has('javascript')); // true
|
|
155
|
+
* console.log(dictionary.has('JAVASCRIPT')); // true
|
|
156
|
+
* @example
|
|
157
|
+
* // IP Address Routing Table
|
|
158
|
+
* // Add IP address prefixes and their corresponding routes
|
|
159
|
+
* const routes = {
|
|
160
|
+
* '192.168.1': 'LAN_SUBNET_1',
|
|
161
|
+
* '192.168.2': 'LAN_SUBNET_2',
|
|
162
|
+
* '10.0.0': 'PRIVATE_NETWORK_1',
|
|
163
|
+
* '10.0.1': 'PRIVATE_NETWORK_2'
|
|
164
|
+
* };
|
|
165
|
+
*
|
|
166
|
+
* const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
167
|
+
*
|
|
168
|
+
* // Check IP address prefix matching
|
|
169
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
|
|
170
|
+
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
|
|
171
|
+
*
|
|
172
|
+
* // Validate IP address belongs to subnet
|
|
173
|
+
* const ip = '192.168.1.100';
|
|
174
|
+
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
175
|
+
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
95
176
|
*/
|
|
96
177
|
export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
97
178
|
/**
|
|
98
|
-
* The constructor
|
|
99
|
-
*
|
|
100
|
-
* @param
|
|
101
|
-
*
|
|
179
|
+
* The constructor initializes a Trie data structure with optional options and words provided as
|
|
180
|
+
* input.
|
|
181
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the constructor is an
|
|
182
|
+
* iterable containing either strings or elements of type `R`. It is used to initialize the Trie with
|
|
183
|
+
* a list of words or elements. If no `words` are provided, an empty iterable is used as the default
|
|
184
|
+
* value.
|
|
185
|
+
* @param [options] - The `options` parameter in the constructor is an optional object that can
|
|
186
|
+
* contain configuration options for the Trie data structure. One of the options it can have is
|
|
187
|
+
* `caseSensitive`, which is a boolean value indicating whether the Trie should be case-sensitive or
|
|
188
|
+
* not. If `caseSensitive` is set to `
|
|
102
189
|
*/
|
|
103
190
|
constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
|
|
104
191
|
super(options);
|
|
@@ -107,13 +194,7 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
107
194
|
if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
|
|
108
195
|
}
|
|
109
196
|
if (words) {
|
|
110
|
-
|
|
111
|
-
if (this.toElementFn) {
|
|
112
|
-
this.add(this.toElementFn(word as R));
|
|
113
|
-
} else {
|
|
114
|
-
this.add(word as string);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
197
|
+
this.addMany(words);
|
|
117
198
|
}
|
|
118
199
|
}
|
|
119
200
|
|
|
@@ -175,6 +256,30 @@ export class Trie<R = any> extends IterableElementBase<string, R, Trie<R>> {
|
|
|
175
256
|
return isNewWord;
|
|
176
257
|
}
|
|
177
258
|
|
|
259
|
+
/**
|
|
260
|
+
* Time Complexity: O(n * l)
|
|
261
|
+
* Space Complexity: O(1)
|
|
262
|
+
*
|
|
263
|
+
* The `addMany` function in TypeScript takes an iterable of strings or elements of type R, converts
|
|
264
|
+
* them using a provided function if available, and adds them to a data structure while returning an
|
|
265
|
+
* array of boolean values indicating success.
|
|
266
|
+
* @param {Iterable<string> | Iterable<R>} words - The `words` parameter in the `addMany` function is
|
|
267
|
+
* an iterable that contains either strings or elements of type `R`.
|
|
268
|
+
* @returns The `addMany` method returns an array of boolean values indicating whether each word in
|
|
269
|
+
* the input iterable was successfully added to the data structure.
|
|
270
|
+
*/
|
|
271
|
+
addMany(words: Iterable<string> | Iterable<R> = []): boolean[] {
|
|
272
|
+
const ans: boolean[] = [];
|
|
273
|
+
for (const word of words) {
|
|
274
|
+
if (this.toElementFn) {
|
|
275
|
+
ans.push(this.add(this.toElementFn(word as R)));
|
|
276
|
+
} else {
|
|
277
|
+
ans.push(this.add(word as string));
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return ans;
|
|
281
|
+
}
|
|
282
|
+
|
|
178
283
|
/**
|
|
179
284
|
* Time Complexity: O(l), where l is the length of the input word.
|
|
180
285
|
* Space Complexity: O(1) - Constant space.
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
import { IterationType, OptValue } from '../../common';
|
|
3
|
-
import { DFSOperation } from '../../../
|
|
3
|
+
import { DFSOperation } from '../../../common';
|
|
4
4
|
|
|
5
5
|
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions } from './binary-tree';
|
|
3
|
-
import {
|
|
3
|
+
import { Comparable } from '../../utils';
|
|
4
4
|
|
|
5
5
|
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
6
6
|
|
|
7
7
|
export type BSTNested<K, V, R, NODE extends BSTNode<K, V, NODE>> = BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, BST<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
8
8
|
|
|
9
9
|
export type BSTOptions<K, V, R> = BinaryTreeOptions<K, V, R> & {
|
|
10
|
-
|
|
10
|
+
extractComparable?: (key: K) => Comparable
|
|
11
|
+
isReverse?: boolean;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export type BSTNOptKey<K> = K | undefined;
|
|
@@ -7,4 +7,4 @@ export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNo
|
|
|
7
7
|
|
|
8
8
|
export type RedBlackTreeNested<K, V, R, NODE extends RedBlackTreeNode<K, V, NODE>> = RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, RedBlackTree<K, V, R, NODE, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
|
9
9
|
|
|
10
|
-
export type RBTreeOptions<K, V, R> = BSTOptions<K, V, R> & {};
|
|
10
|
+
export type RBTreeOptions<K, V, R> = Omit<BSTOptions<K, V, R>, 'isReverse'> & {};
|
package/src/types/utils/utils.ts
CHANGED
|
@@ -7,17 +7,23 @@ export type SpecifyOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T,
|
|
|
7
7
|
|
|
8
8
|
export type Any = string | number | bigint | boolean | symbol | undefined | object;
|
|
9
9
|
|
|
10
|
+
export type Arithmetic = number | bigint;
|
|
11
|
+
|
|
10
12
|
export type ComparablePrimitive = number | bigint | string | boolean;
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
export interface BaseComparableObject {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ValueComparableObject extends BaseComparableObject {
|
|
19
|
+
valueOf: () => ComparablePrimitive | ValueComparableObject;
|
|
20
|
+
toString?: () => string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface StringComparableObject extends BaseComparableObject {
|
|
24
|
+
toString: () => string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ComparableObject = ValueComparableObject | StringComparableObject;
|
|
22
28
|
|
|
23
29
|
export type Comparable = ComparablePrimitive | Date | ComparableObject;
|
package/src/utils/utils.ts
CHANGED
|
@@ -226,7 +226,8 @@ export const roundFixed = (num: number, digit: number = 10) => {
|
|
|
226
226
|
*/
|
|
227
227
|
function isPrimitiveComparable(value: unknown): value is ComparablePrimitive {
|
|
228
228
|
const valueType = typeof value;
|
|
229
|
-
if (valueType === 'number') return
|
|
229
|
+
if (valueType === 'number') return true;
|
|
230
|
+
// if (valueType === 'number') return !Number.isNaN(value);
|
|
230
231
|
return valueType === 'bigint' || valueType === 'string' || valueType === 'boolean';
|
|
231
232
|
}
|
|
232
233
|
|
|
@@ -274,7 +275,8 @@ export function isComparable(value: unknown, isForceObjectComparable = false): v
|
|
|
274
275
|
if (isPrimitiveComparable(value)) return true;
|
|
275
276
|
|
|
276
277
|
if (typeof value !== 'object') return false;
|
|
277
|
-
if (value instanceof Date) return
|
|
278
|
+
if (value instanceof Date) return true;
|
|
279
|
+
// if (value instanceof Date) return !Number.isNaN(value.getTime());
|
|
278
280
|
if (isForceObjectComparable) return true;
|
|
279
281
|
const comparableValue = tryObjectToPrimitive(value);
|
|
280
282
|
if (comparableValue === null || comparableValue === undefined) return false;
|