red-black-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 +92 -37
  2. package/dist/cjs/index.cjs +163 -0
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +164 -0
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +163 -0
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +164 -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/red-black-tree-typed.js +164 -0
  27. package/dist/umd/red-black-tree-typed.js.map +1 -1
  28. package/dist/umd/red-black-tree-typed.min.js +3 -3
  29. package/dist/umd/red-black-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
@@ -188,48 +188,97 @@ export class RedBlackTreeNode<K = any, V = any> {
188
188
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
189
189
  *
190
190
  * @example
191
- * // using Red-Black Tree as a price-based index for stock data
192
- * // Define the structure of individual stock records
193
- * interface StockRecord {
194
- * price: number; // Stock price (key for indexing)
195
- * symbol: string; // Stock ticker symbol
196
- * volume: number; // Trade volume
191
+ * // basic Red-Black Tree with simple number keys
192
+ * // Create a simple Red-Black Tree with numeric keys
193
+ * const tree = new RedBlackTree([5, 2, 8, 1, 9]);
194
+ *
195
+ * tree.print();
196
+ * // _2___
197
+ * // / \
198
+ * // 1 _8_
199
+ * // / \
200
+ * // 5 9
201
+ *
202
+ * // Verify the tree maintains sorted order
203
+ * console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
204
+ *
205
+ * // Check size
206
+ * console.log(tree.size); // 5;
207
+ * @example
208
+ * // Red-Black Tree with key-value pairs for lookups
209
+ * interface Employee {
210
+ * id: number;
211
+ * name: string;
197
212
  * }
198
213
  *
199
- * // Simulate stock market data as it might come from an external feed
200
- * const marketStockData: StockRecord[] = [
201
- * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
202
- * { price: 335.2, symbol: 'MSFT', volume: 800000 },
203
- * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
204
- * { price: 267.98, symbol: 'META', volume: 750000 },
205
- * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
206
- * ];
214
+ * // Create tree with employee data
215
+ * const employees = new RedBlackTree<number, Employee>([
216
+ * [1, { id: 1, name: 'Alice' }],
217
+ * [3, { id: 3, name: 'Charlie' }],
218
+ * [2, { id: 2, name: 'Bob' }]
219
+ * ]);
220
+ *
221
+ * // Retrieve employee by ID
222
+ * const alice = employees.get(1);
223
+ * console.log(alice?.name); // 'Alice';
224
+ *
225
+ * // Verify sorted order by ID
226
+ * console.log([...employees.keys()]); // [1, 2, 3];
227
+ * @example
228
+ * // Red-Black Tree range search for filtering
229
+ * interface Product {
230
+ * name: string;
231
+ * price: number;
232
+ * }
207
233
  *
208
- * // Extend the stock record type to include metadata for database usage
209
- * type StockTableRecord = StockRecord & { lastUpdated: Date };
234
+ * const products = new RedBlackTree<number, Product>([
235
+ * [10, { name: 'Item A', price: 10 }],
236
+ * [25, { name: 'Item B', price: 25 }],
237
+ * [40, { name: 'Item C', price: 40 }],
238
+ * [50, { name: 'Item D', price: 50 }]
239
+ * ]);
210
240
  *
211
- * // Create a Red-Black Tree to index stock records by price
212
- * // Simulates a database index with stock price as the key for quick lookups
213
- * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
214
- * toEntryFn: stockRecord => [
215
- * stockRecord.price, // Use stock price as the key
216
- * {
217
- * ...stockRecord,
218
- * lastUpdated: new Date() // Add a timestamp for when the record was indexed
219
- * }
220
- * ]
241
+ * // Find products in price range [20, 45]
242
+ * const pricesInRange = products.rangeSearch([20, 45], node => {
243
+ * return products.get(node)?.name;
221
244
  * });
222
245
  *
223
- * // Query the stock with the highest price
224
- * const highestPricedStock = priceIndex.getRightMost();
225
- * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
246
+ * console.log(pricesInRange); // ['Item B', 'Item C'];
247
+ * @example
248
+ * // Red-Black Tree as database index for stock market data
249
+ * interface StockPrice {
250
+ * symbol: string;
251
+ * volume: number;
252
+ * timestamp: Date;
253
+ * }
254
+ *
255
+ * // Simulate real-time stock price index
256
+ * const priceIndex = new RedBlackTree<number, StockPrice>([
257
+ * [142.5, { symbol: 'AAPL', volume: 1000000, timestamp: new Date() }],
258
+ * [335.2, { symbol: 'MSFT', volume: 800000, timestamp: new Date() }],
259
+ * [3285.04, { symbol: 'AMZN', volume: 500000, timestamp: new Date() }],
260
+ * [267.98, { symbol: 'META', volume: 750000, timestamp: new Date() }],
261
+ * [234.57, { symbol: 'GOOGL', volume: 900000, timestamp: new Date() }]
262
+ * ]);
263
+ *
264
+ * // Find highest-priced stock
265
+ * const maxPrice = priceIndex.getRightMost();
266
+ * console.log(priceIndex.get(maxPrice)?.symbol); // 'AMZN';
267
+ *
268
+ * // Find stocks in price range [200, 400] for portfolio balancing
269
+ * const stocksInRange = priceIndex.rangeSearch([200, 400], node => {
270
+ * const stock = priceIndex.get(node);
271
+ * return {
272
+ * symbol: stock?.symbol,
273
+ * price: node,
274
+ * volume: stock?.volume
275
+ * };
276
+ * });
226
277
  *
227
- * // Query stocks within a specific price range (200 to 400)
228
- * const stocksInRange = priceIndex.rangeSearch(
229
- * [200, 400], // Price range
230
- * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
231
- * );
232
- * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
278
+ * console.log(stocksInRange.length); // 3;
279
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'GOOGL')); // true;
280
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'META')); // true;
281
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'MSFT')); // true;
233
282
  */
234
283
 
235
284
  export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
@@ -186,7 +186,7 @@ export class TreeMultiMapNode<K = any, V = any> {
186
186
  *
187
187
  * @example
188
188
  * // players ranked by score with their equipment
189
- * type Equipment = {
189
+ * type Equipment = {
190
190
  * name: string; // Equipment name
191
191
  * quality: 'legendary' | 'epic' | 'rare' | 'common';
192
192
  * level: number;
@@ -347,7 +347,7 @@ export class TreeMultiMapNode<K = any, V = any> {
347
347
  * // },
348
348
  * // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
349
349
  * // ]
350
- * // ]
350
+ * // ];
351
351
  */
352
352
  export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
353
353
  /**
@@ -35,7 +35,132 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
35
35
  * @template VO - Concrete vertex class (extends AbstractVertex<V>).
36
36
  * @template EO - Concrete edge class (extends AbstractEdge<E>).
37
37
  * @remarks Time O(1), Space O(1)
38
- * @example examples will be generated by unit test
38
+ * @example
39
+ * // basic DirectedGraph vertex and edge creation
40
+ * // Create a simple directed graph
41
+ * const graph = new DirectedGraph<string>();
42
+ *
43
+ * // Add vertices
44
+ * graph.addVertex('A');
45
+ * graph.addVertex('B');
46
+ * graph.addVertex('C');
47
+ *
48
+ * // Verify vertices exist
49
+ * console.log(graph.hasVertex('A')); // true;
50
+ * console.log(graph.hasVertex('B')); // true;
51
+ * console.log(graph.hasVertex('C')); // true;
52
+ * console.log(graph.hasVertex('D')); // false;
53
+ *
54
+ * // Check vertex count
55
+ * console.log(graph.size); // 3;
56
+ * @example
57
+ * // DirectedGraph edge operations
58
+ * const graph = new DirectedGraph<string>();
59
+ *
60
+ * // Add vertices
61
+ * graph.addVertex('A');
62
+ * graph.addVertex('B');
63
+ * graph.addVertex('C');
64
+ *
65
+ * // Add directed edges
66
+ * graph.addEdge('A', 'B', 1);
67
+ * graph.addEdge('B', 'C', 2);
68
+ * graph.addEdge('A', 'C', 3);
69
+ *
70
+ * // Verify edges exist
71
+ * console.log(graph.hasEdge('A', 'B')); // true;
72
+ * console.log(graph.hasEdge('B', 'C')); // true;
73
+ * console.log(graph.hasEdge('C', 'B')); // false; // Graph is directed
74
+ *
75
+ * // Get neighbors of A
76
+ * const neighborsA = graph.getNeighbors('A');
77
+ * console.log(neighborsA[0].key); // 'B';
78
+ * console.log(neighborsA[1].key); // 'C';
79
+ * @example
80
+ * // DirectedGraph deleteEdge and vertex operations
81
+ * const graph = new DirectedGraph<string>();
82
+ *
83
+ * // Build a small graph
84
+ * graph.addVertex('X');
85
+ * graph.addVertex('Y');
86
+ * graph.addVertex('Z');
87
+ * graph.addEdge('X', 'Y', 1);
88
+ * graph.addEdge('Y', 'Z', 2);
89
+ *
90
+ * // Delete an edge
91
+ * graph.deleteEdgeSrcToDest('X', 'Y');
92
+ * console.log(graph.hasEdge('X', 'Y')); // false;
93
+ *
94
+ * // Edge in other direction should not exist
95
+ * console.log(graph.hasEdge('Y', 'X')); // false;
96
+ *
97
+ * // Other edges should remain
98
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
99
+ *
100
+ * // Delete a vertex
101
+ * graph.deleteVertex('Y');
102
+ * console.log(graph.hasVertex('Y')); // false;
103
+ * console.log(graph.size); // 2;
104
+ * @example
105
+ * // DirectedGraph topologicalSort for task scheduling
106
+ * const graph = new DirectedGraph<string>();
107
+ *
108
+ * // Build a DAG (Directed Acyclic Graph) for task dependencies
109
+ * graph.addVertex('Design');
110
+ * graph.addVertex('Implement');
111
+ * graph.addVertex('Test');
112
+ * graph.addVertex('Deploy');
113
+ *
114
+ * // Add dependency edges
115
+ * graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
116
+ * graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
117
+ * graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
118
+ *
119
+ * // Topological sort gives valid execution order
120
+ * const executionOrder = graph.topologicalSort();
121
+ * console.log(executionOrder); // defined;
122
+ * console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
123
+ *
124
+ * // All vertices should be included
125
+ * console.log(executionOrder?.length); // 4;
126
+ * @example
127
+ * // DirectedGraph dijkstra shortest path for network routing
128
+ * // Build a weighted directed graph representing network nodes and costs
129
+ * const network = new DirectedGraph<string>();
130
+ *
131
+ * // Add network nodes
132
+ * network.addVertex('Router-A');
133
+ * network.addVertex('Router-B');
134
+ * network.addVertex('Router-C');
135
+ * network.addVertex('Router-D');
136
+ * network.addVertex('Router-E');
137
+ *
138
+ * // Add weighted edges (network latency costs)
139
+ * network.addEdge('Router-A', 'Router-B', 5);
140
+ * network.addEdge('Router-A', 'Router-C', 10);
141
+ * network.addEdge('Router-B', 'Router-D', 3);
142
+ * network.addEdge('Router-C', 'Router-D', 2);
143
+ * network.addEdge('Router-D', 'Router-E', 4);
144
+ * network.addEdge('Router-B', 'Router-E', 12);
145
+ *
146
+ * // Find shortest path from Router-A to Router-E
147
+ * const { minDist, minPath } = network.dijkstra('Router-A', 'Router-E', true, true) || {
148
+ * minDist: undefined,
149
+ * minPath: undefined
150
+ * };
151
+ *
152
+ * // Verify shortest path is found
153
+ * console.log(minDist); // defined;
154
+ * console.log(minPath); // defined;
155
+ *
156
+ * // Shortest path should be A -> B -> D -> E with cost 5+3+4=12
157
+ * // Or A -> C -> D -> E with cost 10+2+4=16
158
+ * // So the minimum is 12
159
+ * console.log(minDist); // <= 16;
160
+ *
161
+ * // Verify path is valid (includes start and end)
162
+ * console.log(minPath?.[0].key); // 'Router-A';
163
+ * console.log(minPath?.[minPath.length - 1].key); // 'Router-E';
39
164
  */
40
165
  export class DirectedGraph<
41
166
  V = any,
@@ -33,7 +33,166 @@ export class UndirectedEdge<E = number> extends AbstractEdge<E> {
33
33
  * @template VO - Concrete vertex class (extends AbstractVertex<V>).
34
34
  * @template EO - Concrete edge class (extends AbstractEdge<E>).
35
35
  * @remarks Time O(1), Space O(1)
36
- * @example examples will be generated by unit test
36
+ * @example
37
+ * // basic UndirectedGraph vertex and edge creation
38
+ * // Create a simple undirected graph
39
+ * const graph = new UndirectedGraph<string>();
40
+ *
41
+ * // Add vertices
42
+ * graph.addVertex('A');
43
+ * graph.addVertex('B');
44
+ * graph.addVertex('C');
45
+ * graph.addVertex('D');
46
+ *
47
+ * // Verify vertices exist
48
+ * console.log(graph.hasVertex('A')); // true;
49
+ * console.log(graph.hasVertex('B')); // true;
50
+ * console.log(graph.hasVertex('E')); // false;
51
+ *
52
+ * // Check vertex count
53
+ * console.log(graph.size); // 4;
54
+ * @example
55
+ * // UndirectedGraph edge operations (bidirectional)
56
+ * const graph = new UndirectedGraph<string>();
57
+ *
58
+ * // Add vertices
59
+ * graph.addVertex('A');
60
+ * graph.addVertex('B');
61
+ * graph.addVertex('C');
62
+ *
63
+ * // Add undirected edges (both directions automatically)
64
+ * graph.addEdge('A', 'B', 1);
65
+ * graph.addEdge('B', 'C', 2);
66
+ * graph.addEdge('A', 'C', 3);
67
+ *
68
+ * // Verify edges exist in both directions
69
+ * console.log(graph.hasEdge('A', 'B')); // true;
70
+ * console.log(graph.hasEdge('B', 'A')); // true; // Bidirectional!
71
+ *
72
+ * console.log(graph.hasEdge('C', 'B')); // true;
73
+ * console.log(graph.hasEdge('B', 'C')); // true; // Bidirectional!
74
+ *
75
+ * // Get neighbors of A
76
+ * const neighborsA = graph.getNeighbors('A');
77
+ * console.log(neighborsA[0].key); // 'B';
78
+ * console.log(neighborsA[1].key); // 'C';
79
+ * @example
80
+ * // UndirectedGraph deleteEdge and vertex operations
81
+ * const graph = new UndirectedGraph<string>();
82
+ *
83
+ * // Build a simple undirected graph
84
+ * graph.addVertex('X');
85
+ * graph.addVertex('Y');
86
+ * graph.addVertex('Z');
87
+ * graph.addEdge('X', 'Y', 1);
88
+ * graph.addEdge('Y', 'Z', 2);
89
+ * graph.addEdge('X', 'Z', 3);
90
+ *
91
+ * // Delete an edge
92
+ * graph.deleteEdge('X', 'Y');
93
+ * console.log(graph.hasEdge('X', 'Y')); // false;
94
+ *
95
+ * // Bidirectional deletion confirmed
96
+ * console.log(graph.hasEdge('Y', 'X')); // false;
97
+ *
98
+ * // Other edges should remain
99
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
100
+ * console.log(graph.hasEdge('Z', 'Y')); // true;
101
+ *
102
+ * // Delete a vertex
103
+ * graph.deleteVertex('Y');
104
+ * console.log(graph.hasVertex('Y')); // false;
105
+ * console.log(graph.size); // 2;
106
+ * @example
107
+ * // UndirectedGraph connectivity and neighbors
108
+ * const graph = new UndirectedGraph<string>();
109
+ *
110
+ * // Build a friendship network
111
+ * const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
112
+ * for (const person of people) {
113
+ * graph.addVertex(person);
114
+ * }
115
+ *
116
+ * // Add friendships (undirected edges)
117
+ * graph.addEdge('Alice', 'Bob', 1);
118
+ * graph.addEdge('Alice', 'Charlie', 1);
119
+ * graph.addEdge('Bob', 'Diana', 1);
120
+ * graph.addEdge('Charlie', 'Eve', 1);
121
+ * graph.addEdge('Diana', 'Eve', 1);
122
+ *
123
+ * // Get friends of each person
124
+ * const aliceFriends = graph.getNeighbors('Alice');
125
+ * console.log(aliceFriends[0].key); // 'Bob';
126
+ * console.log(aliceFriends[1].key); // 'Charlie';
127
+ * console.log(aliceFriends.length); // 2;
128
+ *
129
+ * const dianaFriends = graph.getNeighbors('Diana');
130
+ * console.log(dianaFriends[0].key); // 'Bob';
131
+ * console.log(dianaFriends[1].key); // 'Eve';
132
+ * console.log(dianaFriends.length); // 2;
133
+ *
134
+ * // Verify bidirectional friendship
135
+ * const bobFriends = graph.getNeighbors('Bob');
136
+ * console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
137
+ * console.log(bobFriends[1].key); // 'Diana';
138
+ * @example
139
+ * // UndirectedGraph for social network connectivity analysis
140
+ * interface Person {
141
+ * id: number;
142
+ * name: string;
143
+ * location: string;
144
+ * }
145
+ *
146
+ * // UndirectedGraph is perfect for modeling symmetric relationships
147
+ * // (friendships, collaborations, partnerships)
148
+ * const socialNetwork = new UndirectedGraph<number, Person>();
149
+ *
150
+ * // Add people as vertices
151
+ * const people: [number, Person][] = [
152
+ * [1, { id: 1, name: 'Alice', location: 'New York' }],
153
+ * [2, { id: 2, name: 'Bob', location: 'San Francisco' }],
154
+ * [3, { id: 3, name: 'Charlie', location: 'Boston' }],
155
+ * [4, { id: 4, name: 'Diana', location: 'New York' }],
156
+ * [5, { id: 5, name: 'Eve', location: 'Seattle' }]
157
+ * ];
158
+ *
159
+ * for (const [id] of people) {
160
+ * socialNetwork.addVertex(id);
161
+ * }
162
+ *
163
+ * // Add friendships (automatically bidirectional)
164
+ * socialNetwork.addEdge(1, 2, 1); // Alice <-> Bob
165
+ * socialNetwork.addEdge(1, 3, 1); // Alice <-> Charlie
166
+ * socialNetwork.addEdge(2, 4, 1); // Bob <-> Diana
167
+ * socialNetwork.addEdge(3, 5, 1); // Charlie <-> Eve
168
+ * socialNetwork.addEdge(4, 5, 1); // Diana <-> Eve
169
+ *
170
+ * console.log(socialNetwork.size); // 5;
171
+ *
172
+ * // Find direct connections for Alice
173
+ * const aliceConnections = socialNetwork.getNeighbors(1);
174
+ * console.log(aliceConnections[0].key); // 2;
175
+ * console.log(aliceConnections[1].key); // 3;
176
+ * console.log(aliceConnections.length); // 2;
177
+ *
178
+ * // Verify bidirectional connections
179
+ * console.log(socialNetwork.hasEdge(1, 2)); // true;
180
+ * console.log(socialNetwork.hasEdge(2, 1)); // true; // Friendship works both ways!
181
+ *
182
+ * // Remove a person from network
183
+ * socialNetwork.deleteVertex(2); // Bob leaves
184
+ * console.log(socialNetwork.hasVertex(2)); // false;
185
+ * console.log(socialNetwork.size); // 4;
186
+ *
187
+ * // Alice loses Bob as a friend
188
+ * const updatedAliceConnections = socialNetwork.getNeighbors(1);
189
+ * console.log(updatedAliceConnections[0].key); // 3;
190
+ * console.log(updatedAliceConnections[1]); // undefined;
191
+ *
192
+ * // Diana loses Bob as a friend
193
+ * const dianaConnections = socialNetwork.getNeighbors(4);
194
+ * console.log(dianaConnections[0].key); // 5;
195
+ * console.log(dianaConnections[1]); // undefined;
37
196
  */
38
197
  export class UndirectedGraph<
39
198
  V = any,
@@ -29,7 +29,7 @@ import { isWeakKey, rangeCheck } from '../../utils';
29
29
  * 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
30
30
  * @example
31
31
  * // should maintain insertion order
32
- * const linkedHashMap = new LinkedHashMap<number, string>();
32
+ * const linkedHashMap = new LinkedHashMap<number, string>();
33
33
  * linkedHashMap.set(1, 'A');
34
34
  * linkedHashMap.set(2, 'B');
35
35
  * linkedHashMap.set(3, 'C');
@@ -39,40 +39,123 @@ import { isWeakKey, rangeCheck } from '../../utils';
39
39
  * // [1, 'A'],
40
40
  * // [2, 'B'],
41
41
  * // [3, 'C']
42
- * // ]
42
+ * // ];
43
43
  * @example
44
- * // fast lookup of values by key
45
- * const hashMap = new HashMap<number, string>();
46
- * hashMap.set(1, 'A');
47
- * hashMap.set(2, 'B');
48
- * hashMap.set(3, 'C');
44
+ * // basic HashMap creation and set operation
45
+ * // Create a simple HashMap with key-value pairs
46
+ * const map = new HashMap<number, string>([
47
+ * [1, 'one'],
48
+ * [2, 'two'],
49
+ * [3, 'three']
50
+ * ]);
49
51
  *
50
- * console.log(hashMap.get(1)); // 'A'
51
- * console.log(hashMap.get(2)); // 'B'
52
- * console.log(hashMap.get(3)); // 'C'
53
- * console.log(hashMap.get(99)); // undefined
52
+ * // Verify size
53
+ * console.log(map.size); // 3;
54
+ *
55
+ * // Set a new key-value pair
56
+ * map.set(4, 'four');
57
+ * console.log(map.size); // 4;
58
+ *
59
+ * // Verify entries
60
+ * console.log([...map.entries()]); // length: 4;
54
61
  * @example
55
- * // remove duplicates when adding multiple entries
56
- * const hashMap = new HashMap<number, string>();
57
- * hashMap.set(1, 'A');
58
- * hashMap.set(2, 'B');
59
- * hashMap.set(1, 'C'); // Update value for key 1
62
+ * // HashMap get and has operations
63
+ * const map = new HashMap<string, number>([
64
+ * ['apple', 1],
65
+ * ['banana', 2],
66
+ * ['cherry', 3]
67
+ * ]);
68
+ *
69
+ * // Check if key exists
70
+ * console.log(map.has('apple')); // true;
71
+ * console.log(map.has('date')); // false;
60
72
  *
61
- * console.log(hashMap.size); // 2
62
- * console.log(hashMap.get(1)); // 'C'
63
- * console.log(hashMap.get(2)); // 'B'
73
+ * // Get value by key
74
+ * console.log(map.get('banana')); // 2;
75
+ * console.log(map.get('grape')); // undefined;
76
+ *
77
+ * // Get all keys and values
78
+ * const keys = [...map.keys()];
79
+ * const values = [...map.values()];
80
+ * console.log(keys); // contains 'apple';
81
+ * console.log(values); // contains 3;
64
82
  * @example
65
- * // count occurrences of keys
66
- * const data = [1, 2, 1, 3, 2, 1];
83
+ * // HashMap iteration and filter operations
84
+ * const map = new HashMap<number, string>([
85
+ * [1, 'Alice'],
86
+ * [2, 'Bob'],
87
+ * [3, 'Charlie'],
88
+ * [4, 'Diana'],
89
+ * [5, 'Eve']
90
+ * ]);
91
+ *
92
+ * // Iterate through entries
93
+ * const entries: [number, string][] = [];
94
+ * for (const [key, value] of map) {
95
+ * entries.push([key, value]);
96
+ * }
97
+ * console.log(entries); // length: 5;
98
+ *
99
+ * // Filter operation (for iteration with collection methods)
100
+ * const filtered = [...map].filter(([key]) => key > 2);
101
+ * console.log(filtered.length); // 3;
67
102
  *
68
- * const countMap = new HashMap<number, number>();
69
- * for (const key of data) {
70
- * countMap.set(key, (countMap.get(key) || 0) + 1);
103
+ * // Map operation
104
+ * const values = [...map.values()].map(v => v.length);
105
+ * console.log(values); // contains 3; // 'Bob', 'Eve'
106
+ * console.log(values); // contains 7;
107
+ * @example
108
+ * // HashMap for user session caching O(1) performance
109
+ * interface UserSession {
110
+ * userId: number;
111
+ * username: string;
112
+ * loginTime: number;
113
+ * lastActivity: number;
71
114
  * }
72
115
  *
73
- * console.log(countMap.get(1)); // 3
74
- * console.log(countMap.get(2)); // 2
75
- * console.log(countMap.get(3)); // 1
116
+ * // HashMap provides O(1) average-case performance for set/get/delete
117
+ * // Perfect for session management with fast lookups
118
+ * const sessionCache = new HashMap<string, UserSession>();
119
+ *
120
+ * // Simulate user sessions
121
+ * const sessions: [string, UserSession][] = [
122
+ * ['session_001', { userId: 1, username: 'alice', loginTime: 1000, lastActivity: 1050 }],
123
+ * ['session_002', { userId: 2, username: 'bob', loginTime: 1100, lastActivity: 1150 }],
124
+ * ['session_003', { userId: 3, username: 'charlie', loginTime: 1200, lastActivity: 1250 }]
125
+ * ];
126
+ *
127
+ * // Store sessions with O(1) insertion
128
+ * for (const [token, session] of sessions) {
129
+ * sessionCache.set(token, session);
130
+ * }
131
+ *
132
+ * console.log(sessionCache.size); // 3;
133
+ *
134
+ * // Retrieve session with O(1) lookup
135
+ * const userSession = sessionCache.get('session_001');
136
+ * console.log(userSession?.username); // 'alice';
137
+ * console.log(userSession?.userId); // 1;
138
+ *
139
+ * // Update session with O(1) operation
140
+ * if (userSession) {
141
+ * userSession.lastActivity = 2000;
142
+ * sessionCache.set('session_001', userSession);
143
+ * }
144
+ *
145
+ * // Check updated value
146
+ * const updated = sessionCache.get('session_001');
147
+ * console.log(updated?.lastActivity); // 2000;
148
+ *
149
+ * // Cleanup: delete expired sessions
150
+ * sessionCache.delete('session_002');
151
+ * console.log(sessionCache.has('session_002')); // false;
152
+ *
153
+ * // Verify remaining sessions
154
+ * console.log(sessionCache.size); // 2;
155
+ *
156
+ * // Get all active sessions
157
+ * const activeCount = [...sessionCache.values()].length;
158
+ * console.log(activeCount); // 2;
76
159
  */
77
160
  export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K, V> {
78
161
  /**