binary-tree-typed 2.2.2 → 2.2.3

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 (46) hide show
  1. package/README.md +91 -7
  2. package/dist/cjs/index.cjs +22 -0
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +22 -0
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +22 -0
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +22 -0
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  11. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  12. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  14. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  15. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  16. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  18. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  19. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  20. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  21. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  22. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  23. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  24. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  25. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  26. package/dist/umd/binary-tree-typed.js +22 -0
  27. package/dist/umd/binary-tree-typed.js.map +1 -1
  28. package/dist/umd/binary-tree-typed.min.js +2 -2
  29. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  30. package/package.json +2 -2
  31. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  32. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  33. package/src/data-structures/binary-tree/bst.ts +322 -13
  34. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  35. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  36. package/src/data-structures/graph/directed-graph.ts +126 -1
  37. package/src/data-structures/graph/undirected-graph.ts +160 -1
  38. package/src/data-structures/hash/hash-map.ts +110 -27
  39. package/src/data-structures/heap/heap.ts +107 -58
  40. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  41. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  42. package/src/data-structures/queue/deque.ts +95 -67
  43. package/src/data-structures/queue/queue.ts +90 -34
  44. package/src/data-structures/stack/stack.ts +58 -40
  45. package/src/data-structures/trie/trie.ts +109 -47
  46. package/src/interfaces/binary-tree.ts +2 -0
@@ -77,53 +77,99 @@ export declare class TrieNode {
77
77
  * 10. IP Routing: Used in certain types of IP routing algorithms.
78
78
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
79
79
  * @example
80
- * // Autocomplete: Prefix validation and checking
81
- * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
80
+ * // basic Trie creation and add words
81
+ * // Create a simple Trie with initial words
82
+ * const trie = new Trie(['apple', 'app', 'apply']);
82
83
  *
83
- * // Get all completions for a prefix
84
- * const gmailCompletions = autocomplete.getWords('gmail');
85
- * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
84
+ * // Verify size
85
+ * console.log(trie.size); // 3;
86
+ *
87
+ * // Check if words exist
88
+ * console.log(trie.has('apple')); // true;
89
+ * console.log(trie.has('app')); // true;
90
+ *
91
+ * // Add a new word
92
+ * trie.add('application');
93
+ * console.log(trie.size); // 4;
86
94
  * @example
87
- * // File System Path Operations
88
- * const fileSystem = new Trie<string>([
89
- * '/home/user/documents/file1.txt',
90
- * '/home/user/documents/file2.txt',
91
- * '/home/user/pictures/photo.jpg',
92
- * '/home/user/pictures/vacation/',
93
- * '/home/user/downloads'
94
- * ]);
95
+ * // Trie getWords and prefix search
96
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
95
97
  *
96
- * // Find common directory prefix
97
- * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
98
+ * // Get all words with prefix 'app'
99
+ * const appWords = trie.getWords('app');
100
+ * console.log(appWords); // contains 'app';
101
+ * console.log(appWords); // contains 'apple';
102
+ * console.log(appWords); // contains 'apply';
103
+ * console.log(appWords); // contains 'application';
104
+ * expect(appWords).not.toContain('apricot');
105
+ * @example
106
+ * // Trie isPrefix and isAbsolutePrefix checks
107
+ * const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
98
108
  *
99
- * // List all files in a directory
100
- * const documentsFiles = fileSystem.getWords('/home/user/documents/');
101
- * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
109
+ * // Check if string is a prefix of any word
110
+ * console.log(trie.hasPrefix('tri')); // true;
111
+ * console.log(trie.hasPrefix('tr')); // true;
112
+ * console.log(trie.hasPrefix('xyz')); // false;
113
+ *
114
+ * // Check if string is an absolute prefix (not a complete word)
115
+ * console.log(trie.hasPurePrefix('tri')); // true;
116
+ * console.log(trie.hasPurePrefix('tree')); // false; // 'tree' is a complete word
117
+ *
118
+ * // Verify size
119
+ * console.log(trie.size); // 5;
102
120
  * @example
103
- * // Autocomplete: Basic word suggestions
104
- * // Create a trie for autocomplete
105
- * const autocomplete = new Trie<string>([
106
- * 'function',
107
- * 'functional',
108
- * 'functions',
109
- * 'class',
110
- * 'classes',
111
- * 'classical',
112
- * 'closure',
113
- * 'const',
114
- * 'constructor'
115
- * ]);
121
+ * // Trie delete and iteration
122
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
123
+ *
124
+ * // Delete a word
125
+ * trie.delete('card');
126
+ * console.log(trie.has('card')); // false;
127
+ *
128
+ * // Word with same prefix still exists
129
+ * console.log(trie.has('care')); // true;
116
130
  *
117
- * // Test autocomplete with different prefixes
118
- * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
119
- * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
120
- * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
131
+ * // Size decreased
132
+ * console.log(trie.size); // 5;
121
133
  *
122
- * // Test with non-matching prefix
123
- * console.log(autocomplete.getWords('xyz')); // []
134
+ * // Iterate through all words
135
+ * const allWords = [...trie];
136
+ * console.log(allWords.length); // 5;
137
+ * @example
138
+ * // Trie for autocomplete search index
139
+ * // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
140
+ * const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
141
+ *
142
+ * // User types 'j' - get all suggestions
143
+ * const jResults = searchIndex.getWords('j');
144
+ * console.log(jResults); // contains 'javascript';
145
+ * console.log(jResults); // contains 'java';
146
+ * console.log(jResults.length); // 2;
147
+ *
148
+ * // User types 'ja' - get more specific suggestions
149
+ * const jaResults = searchIndex.getWords('ja');
150
+ * console.log(jaResults); // contains 'javascript';
151
+ * console.log(jaResults); // contains 'java';
152
+ * console.log(jaResults.length); // 2;
153
+ *
154
+ * // User types 'jav' - even more specific
155
+ * const javResults = searchIndex.getWords('jav');
156
+ * console.log(javResults); // contains 'javascript';
157
+ * console.log(javResults); // contains 'java';
158
+ * console.log(javResults.length); // 2;
159
+ *
160
+ * // Check for common prefix
161
+ *
162
+ * console.log(searchIndex.hasCommonPrefix('ja')); // false; // Not all words start with 'ja'
163
+ *
164
+ * // Total words in index
165
+ * console.log(searchIndex.size); // 8;
166
+ *
167
+ * // Get height (depth of tree)
168
+ * const height = searchIndex.getHeight();
169
+ * console.log(typeof height); // 'number';
124
170
  * @example
125
171
  * // Dictionary: Case-insensitive word lookup
126
- * // Create a case-insensitive dictionary
172
+ * // Create a case-insensitive dictionary
127
173
  * const dictionary = new Trie<string>([], { caseSensitive: false });
128
174
  *
129
175
  * // Add words with mixed casing
@@ -132,14 +178,30 @@ export declare class TrieNode {
132
178
  * dictionary.add('JavaScript');
133
179
  *
134
180
  * // Test lookups with different casings
135
- * console.log(dictionary.has('hello')); // true
136
- * console.log(dictionary.has('HELLO')); // true
137
- * console.log(dictionary.has('Hello')); // true
138
- * console.log(dictionary.has('javascript')); // true
139
- * console.log(dictionary.has('JAVASCRIPT')); // true
181
+ * console.log(dictionary.has('hello')); // true;
182
+ * console.log(dictionary.has('HELLO')); // true;
183
+ * console.log(dictionary.has('Hello')); // true;
184
+ * console.log(dictionary.has('javascript')); // true;
185
+ * console.log(dictionary.has('JAVASCRIPT')); // true;
186
+ * @example
187
+ * // File System Path Operations
188
+ * const fileSystem = new Trie<string>([
189
+ * '/home/user/documents/file1.txt',
190
+ * '/home/user/documents/file2.txt',
191
+ * '/home/user/pictures/photo.jpg',
192
+ * '/home/user/pictures/vacation/',
193
+ * '/home/user/downloads'
194
+ * ]);
195
+ *
196
+ * // Find common directory prefix
197
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/';
198
+ *
199
+ * // List all files in a directory
200
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
201
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt'];
140
202
  * @example
141
203
  * // IP Address Routing Table
142
- * // Add IP address prefixes and their corresponding routes
204
+ * // Add IP address prefixes and their corresponding routes
143
205
  * const routes = {
144
206
  * '192.168.1': 'LAN_SUBNET_1',
145
207
  * '192.168.2': 'LAN_SUBNET_2',
@@ -150,13 +212,13 @@ export declare class TrieNode {
150
212
  * const ipRoutingTable = new Trie<string>(Object.keys(routes));
151
213
  *
152
214
  * // Check IP address prefix matching
153
- * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
154
- * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
215
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true;
216
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true;
155
217
  *
156
218
  * // Validate IP address belongs to subnet
157
219
  * const ip = '192.168.1.100';
158
220
  * const subnet = ip.split('.').slice(0, 3).join('.');
159
- * console.log(ipRoutingTable.hasPrefix(subnet)); // true
221
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true;
160
222
  */
161
223
  export declare class Trie<R = any> extends IterableElementBase<string, R> {
162
224
  /**
@@ -17,6 +17,7 @@ export interface IBinaryTree<K = any, V = any, R = any> {
17
17
  createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
18
18
  createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
19
19
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
+ set(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
20
21
  addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
21
22
  delete(keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
22
23
  clear(): void;
@@ -1473,6 +1473,17 @@ var binaryTreeTyped = (() => {
1473
1473
  }
1474
1474
  return false;
1475
1475
  }
1476
+ /**
1477
+ * Adds or updates a new node to the tree.
1478
+ * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
1479
+ *
1480
+ * @param keyNodeOrEntry - The key, node, or entry to add or update.
1481
+ * @param [value] - The value, if providing just a key.
1482
+ * @returns True if the addition was successful, false otherwise.
1483
+ */
1484
+ set(keyNodeOrEntry, value) {
1485
+ return this.add(keyNodeOrEntry, value);
1486
+ }
1476
1487
  /**
1477
1488
  * Adds multiple items to the tree.
1478
1489
  * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
@@ -1500,6 +1511,17 @@ var binaryTreeTyped = (() => {
1500
1511
  }
1501
1512
  return inserted;
1502
1513
  }
1514
+ /**
1515
+ * Adds or updates multiple items to the tree.
1516
+ * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
1517
+ *
1518
+ * @param keysNodesEntriesOrRaws - An iterable of items to add or update.
1519
+ * @param [values] - An optional parallel iterable of values.
1520
+ * @returns An array of booleans indicating the success of each individual `add` operation.
1521
+ */
1522
+ setMany(keysNodesEntriesOrRaws, values) {
1523
+ return this.addMany(keysNodesEntriesOrRaws, values);
1524
+ }
1503
1525
  /**
1504
1526
  * Merges another tree into this one by adding all its nodes.
1505
1527
  * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).