stack-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 (40) hide show
  1. package/README.md +66 -46
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs.map +1 -1
  4. package/dist/esm/index.mjs.map +1 -1
  5. package/dist/esm-legacy/index.mjs.map +1 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +96 -2
  7. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
  8. package/dist/types/data-structures/binary-tree/bst.d.ts +156 -13
  9. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +84 -35
  10. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
  11. package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
  12. package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
  13. package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
  14. package/dist/types/data-structures/heap/heap.d.ts +107 -58
  15. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
  16. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
  17. package/dist/types/data-structures/queue/deque.d.ts +95 -67
  18. package/dist/types/data-structures/queue/queue.d.ts +90 -34
  19. package/dist/types/data-structures/stack/stack.d.ts +58 -40
  20. package/dist/types/data-structures/trie/trie.d.ts +109 -47
  21. package/dist/types/interfaces/binary-tree.d.ts +1 -0
  22. package/dist/umd/stack-typed.js.map +1 -1
  23. package/dist/umd/stack-typed.min.js.map +1 -1
  24. package/package.json +2 -2
  25. package/src/data-structures/binary-tree/avl-tree.ts +96 -2
  26. package/src/data-structures/binary-tree/binary-tree.ts +117 -7
  27. package/src/data-structures/binary-tree/bst.ts +322 -13
  28. package/src/data-structures/binary-tree/red-black-tree.ts +84 -35
  29. package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
  30. package/src/data-structures/graph/directed-graph.ts +126 -1
  31. package/src/data-structures/graph/undirected-graph.ts +160 -1
  32. package/src/data-structures/hash/hash-map.ts +110 -27
  33. package/src/data-structures/heap/heap.ts +107 -58
  34. package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
  35. package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
  36. package/src/data-structures/queue/deque.ts +95 -67
  37. package/src/data-structures/queue/queue.ts +90 -34
  38. package/src/data-structures/stack/stack.ts +58 -40
  39. package/src/data-structures/trie/trie.ts +109 -47
  40. package/src/interfaces/binary-tree.ts +2 -0
@@ -126,8 +126,57 @@ export declare class BSTNode<K = any, V = any> {
126
126
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
127
127
  *
128
128
  * @example
129
+ * // basic BST creation and add operation
130
+ * // Create a simple BST with numeric keys
131
+ * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
132
+ *
133
+ * bst.print();
134
+ * // _______8__________
135
+ * // / \
136
+ * // ___4___ ____12_____
137
+ * // / \ / \
138
+ * // _2_ _6_ _10__ _14__
139
+ * // / \ / \ / \ / \
140
+ * // 1 3 5 7 9 11 13 15__
141
+ * // \
142
+ * // 16
143
+ *
144
+ * // Verify size
145
+ * console.log(bst.size); // 16;
146
+ *
147
+ * // Add new elements
148
+ * bst.add(17);
149
+ * bst.add(0);
150
+ * console.log(bst.size); // 18;
151
+ *
152
+ * // Verify keys are searchable
153
+ * console.log(bst.has(11)); // true;
154
+ * console.log(bst.has(100)); // false;
155
+ * @example
156
+ * // BST delete and search after deletion
157
+ * const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
158
+ *
159
+ * // Delete a leaf node
160
+ * bst.delete(1);
161
+ * console.log(bst.has(1)); // false;
162
+ *
163
+ * // Delete a node with one child
164
+ * bst.delete(2);
165
+ * console.log(bst.has(2)); // false;
166
+ *
167
+ * // Delete a node with two children
168
+ * bst.delete(3);
169
+ * console.log(bst.has(3)); // false;
170
+ *
171
+ * // Size decreases with each deletion
172
+ * console.log(bst.size); // 13;
173
+ *
174
+ * // Other nodes remain searchable
175
+ * console.log(bst.has(11)); // true;
176
+ * console.log(bst.has(15)); // true;
177
+ * @example
129
178
  * // Merge 3 sorted datasets
130
- * const dataset1 = new BST<number, string>([
179
+ * const dataset1 = new BST<number, string>([
131
180
  * [1, 'A'],
132
181
  * [7, 'G']
133
182
  * ]);
@@ -147,18 +196,58 @@ export declare class BSTNode<K = any, V = any> {
147
196
  * merged.merge(dataset3);
148
197
  *
149
198
  * // Verify merged dataset is in sorted order
150
- * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
199
+ * console.log([...merged.values()]); // ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
151
200
  * @example
152
- * // Find elements in a range
153
- * const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
154
- * console.log(bst.search(new Range(5, 10))); // [5, 7, 10]
155
- * console.log(bst.rangeSearch([4, 12], node => node.key.toString())); // ['5', '7', '10', '12']
156
- * console.log(bst.search(new Range(4, 12, true, false))); // [5, 7, 10]
157
- * console.log(bst.rangeSearch([15, 20])); // [15, 18]
158
- * console.log(bst.search(new Range(15, 20, false))); // [18]
201
+ * // BST with custom objects for expression evaluation
202
+ * interface Expression {
203
+ * id: number;
204
+ * operator: string;
205
+ * precedence: number;
206
+ * }
207
+ *
208
+ * // BST efficiently stores and retrieves operators by precedence
209
+ * const operatorTree = new BST<number, Expression>(
210
+ * [
211
+ * [1, { id: 1, operator: '+', precedence: 1 }],
212
+ * [2, { id: 2, operator: '*', precedence: 2 }],
213
+ * [3, { id: 3, operator: '/', precedence: 2 }],
214
+ * [4, { id: 4, operator: '-', precedence: 1 }],
215
+ * [5, { id: 5, operator: '^', precedence: 3 }]
216
+ * ],
217
+ * { isMapMode: false }
218
+ * );
219
+ *
220
+ * console.log(operatorTree.size); // 5;
221
+ *
222
+ * // Quick lookup of operators
223
+ * const mult = operatorTree.get(2);
224
+ * console.log(mult?.operator); // '*';
225
+ * console.log(mult?.precedence); // 2;
226
+ *
227
+ * // Check if operator exists
228
+ * console.log(operatorTree.has(5)); // true;
229
+ * console.log(operatorTree.has(99)); // false;
230
+ *
231
+ * // Retrieve operator by precedence level
232
+ * const expNode = operatorTree.getNode(3);
233
+ * console.log(expNode?.key); // 3;
234
+ * console.log(expNode?.value?.precedence); // 2;
235
+ *
236
+ * // Delete operator and verify
237
+ * operatorTree.delete(1);
238
+ * console.log(operatorTree.has(1)); // false;
239
+ * console.log(operatorTree.size); // 4;
240
+ *
241
+ * // Get tree height for optimization analysis
242
+ * const treeHeight = operatorTree.getHeight();
243
+ * console.log(treeHeight); // > 0;
244
+ *
245
+ * // Remaining operators are still accessible
246
+ * const remaining = operatorTree.get(2);
247
+ * console.log(remaining); // defined;
159
248
  * @example
160
249
  * // Find lowest common ancestor
161
- * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
250
+ * const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
162
251
  *
163
252
  * // LCA helper function
164
253
  * const findLCA = (num1: number, num2: number): number | undefined => {
@@ -178,9 +267,9 @@ export declare class BSTNode<K = any, V = any> {
178
267
  * }
179
268
  *
180
269
  * // Assertions
181
- * console.log(findLCA(3, 10)); // 7
182
- * console.log(findLCA(5, 35)); // 15
183
- * console.log(findLCA(20, 30)); // 25
270
+ * console.log(findLCA(3, 10)); // 7;
271
+ * console.log(findLCA(5, 35)); // 15;
272
+ * console.log(findLCA(20, 30)); // 25;
184
273
  */
185
274
  export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R> implements IBinaryTree<K, V, R> {
186
275
  /**
@@ -355,6 +444,28 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
355
444
  * @returns An array of booleans indicating the success of each individual `add` operation.
356
445
  */
357
446
  addMany(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, BSTNode<K, V>>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): boolean[];
447
+ /**
448
+ * Returns the first node with a key greater than or equal to the given key.
449
+ * This is equivalent to C++ std::lower_bound on a BST.
450
+ * Supports RECURSIVE and ITERATIVE implementations.
451
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
452
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
453
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
454
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
455
+ * @returns The first node with key >= given key, or undefined if no such node exists.
456
+ */
457
+ lowerBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
458
+ /**
459
+ * Returns the first node with a key strictly greater than the given key.
460
+ * This is equivalent to C++ std::upper_bound on a BST.
461
+ * Supports RECURSIVE and ITERATIVE implementations.
462
+ * Time Complexity: O(log n) on average, O(h) where h is tree height.
463
+ * Space Complexity: O(h) for recursion, O(1) for iteration.
464
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
465
+ * @param iterationType The iteration type (RECURSIVE or ITERATIVE). Defaults to this.iterationType.
466
+ * @returns The first node with key > given key, or undefined if no such node exists.
467
+ */
468
+ upperBound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType?: IterationType): BSTNode<K, V> | undefined;
358
469
  /**
359
470
  * Traverses the tree and returns nodes that are lesser or greater than a target node.
360
471
  * @remarks Time O(N), as it performs a full traversal. Space O(log N) or O(N).
@@ -405,6 +516,38 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
405
516
  * @returns True if a node was deleted, false otherwise.
406
517
  */
407
518
  deleteWhere(predicate: (key: K, value: V | undefined, index: number, tree: this) => boolean): boolean;
519
+ /**
520
+ * (Protected) Core bound search implementation supporting all parameter types.
521
+ * Unified logic for both lowerBound and upperBound.
522
+ * Resolves various input types (Key, Node, Entry, Predicate) using parent class utilities.
523
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
524
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
525
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
526
+ * @returns The first matching node, or undefined if no such node exists.
527
+ */
528
+ protected _bound(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
529
+ /**
530
+ * (Protected) Binary search for bound by key with pruning optimization.
531
+ * Performs standard BST binary search, choosing left or right subtree based on comparator result.
532
+ * For lowerBound: finds first node where key >= target.
533
+ * For upperBound: finds first node where key > target.
534
+ * @param key - The target key to search for.
535
+ * @param isLower - True for lowerBound (>=), false for upperBound (>).
536
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
537
+ * @returns The first node matching the bound condition, or undefined if none exists.
538
+ */
539
+ protected _boundByKey(key: K, isLower: boolean, iterationType: IterationType): BSTNode<K, V> | undefined;
540
+ /**
541
+ * (Protected) In-order traversal search by predicate.
542
+ * Falls back to linear in-order traversal when predicate-based search is required.
543
+ * Returns the first node that satisfies the predicate function.
544
+ * Note: Predicate-based search cannot leverage BST's binary search optimization.
545
+ * Time Complexity: O(n) since it may visit every node.
546
+ * @param predicate - The predicate function to test nodes.
547
+ * @param iterationType - The iteration type (RECURSIVE or ITERATIVE).
548
+ * @returns The first node satisfying predicate, or undefined if none found.
549
+ */
550
+ protected _boundByPredicate(predicate: NodePredicate<BSTNode<K, V>>, iterationType: IterationType): BSTNode<K, V> | undefined;
408
551
  /**
409
552
  * (Protected) Creates a new, empty instance of the same BST constructor.
410
553
  * @remarks Time O(1)
@@ -112,48 +112,97 @@ export declare class RedBlackTreeNode<K = any, V = any> {
112
112
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
113
113
  *
114
114
  * @example
115
- * // using Red-Black Tree as a price-based index for stock data
116
- * // Define the structure of individual stock records
117
- * interface StockRecord {
118
- * price: number; // Stock price (key for indexing)
119
- * symbol: string; // Stock ticker symbol
120
- * volume: number; // Trade volume
115
+ * // basic Red-Black Tree with simple number keys
116
+ * // Create a simple Red-Black Tree with numeric keys
117
+ * const tree = new RedBlackTree([5, 2, 8, 1, 9]);
118
+ *
119
+ * tree.print();
120
+ * // _2___
121
+ * // / \
122
+ * // 1 _8_
123
+ * // / \
124
+ * // 5 9
125
+ *
126
+ * // Verify the tree maintains sorted order
127
+ * console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
128
+ *
129
+ * // Check size
130
+ * console.log(tree.size); // 5;
131
+ * @example
132
+ * // Red-Black Tree with key-value pairs for lookups
133
+ * interface Employee {
134
+ * id: number;
135
+ * name: string;
121
136
  * }
122
137
  *
123
- * // Simulate stock market data as it might come from an external feed
124
- * const marketStockData: StockRecord[] = [
125
- * { price: 142.5, symbol: 'AAPL', volume: 1000000 },
126
- * { price: 335.2, symbol: 'MSFT', volume: 800000 },
127
- * { price: 3285.04, symbol: 'AMZN', volume: 500000 },
128
- * { price: 267.98, symbol: 'META', volume: 750000 },
129
- * { price: 234.57, symbol: 'GOOGL', volume: 900000 }
130
- * ];
138
+ * // Create tree with employee data
139
+ * const employees = new RedBlackTree<number, Employee>([
140
+ * [1, { id: 1, name: 'Alice' }],
141
+ * [3, { id: 3, name: 'Charlie' }],
142
+ * [2, { id: 2, name: 'Bob' }]
143
+ * ]);
144
+ *
145
+ * // Retrieve employee by ID
146
+ * const alice = employees.get(1);
147
+ * console.log(alice?.name); // 'Alice';
148
+ *
149
+ * // Verify sorted order by ID
150
+ * console.log([...employees.keys()]); // [1, 2, 3];
151
+ * @example
152
+ * // Red-Black Tree range search for filtering
153
+ * interface Product {
154
+ * name: string;
155
+ * price: number;
156
+ * }
131
157
  *
132
- * // Extend the stock record type to include metadata for database usage
133
- * type StockTableRecord = StockRecord & { lastUpdated: Date };
158
+ * const products = new RedBlackTree<number, Product>([
159
+ * [10, { name: 'Item A', price: 10 }],
160
+ * [25, { name: 'Item B', price: 25 }],
161
+ * [40, { name: 'Item C', price: 40 }],
162
+ * [50, { name: 'Item D', price: 50 }]
163
+ * ]);
134
164
  *
135
- * // Create a Red-Black Tree to index stock records by price
136
- * // Simulates a database index with stock price as the key for quick lookups
137
- * const priceIndex = new RedBlackTree<number, StockTableRecord, StockRecord>(marketStockData, {
138
- * toEntryFn: stockRecord => [
139
- * stockRecord.price, // Use stock price as the key
140
- * {
141
- * ...stockRecord,
142
- * lastUpdated: new Date() // Add a timestamp for when the record was indexed
143
- * }
144
- * ]
165
+ * // Find products in price range [20, 45]
166
+ * const pricesInRange = products.rangeSearch([20, 45], node => {
167
+ * return products.get(node)?.name;
145
168
  * });
146
169
  *
147
- * // Query the stock with the highest price
148
- * const highestPricedStock = priceIndex.getRightMost();
149
- * console.log(priceIndex.get(highestPricedStock)?.symbol); // 'AMZN' // Amazon has the highest price
170
+ * console.log(pricesInRange); // ['Item B', 'Item C'];
171
+ * @example
172
+ * // Red-Black Tree as database index for stock market data
173
+ * interface StockPrice {
174
+ * symbol: string;
175
+ * volume: number;
176
+ * timestamp: Date;
177
+ * }
178
+ *
179
+ * // Simulate real-time stock price index
180
+ * const priceIndex = new RedBlackTree<number, StockPrice>([
181
+ * [142.5, { symbol: 'AAPL', volume: 1000000, timestamp: new Date() }],
182
+ * [335.2, { symbol: 'MSFT', volume: 800000, timestamp: new Date() }],
183
+ * [3285.04, { symbol: 'AMZN', volume: 500000, timestamp: new Date() }],
184
+ * [267.98, { symbol: 'META', volume: 750000, timestamp: new Date() }],
185
+ * [234.57, { symbol: 'GOOGL', volume: 900000, timestamp: new Date() }]
186
+ * ]);
187
+ *
188
+ * // Find highest-priced stock
189
+ * const maxPrice = priceIndex.getRightMost();
190
+ * console.log(priceIndex.get(maxPrice)?.symbol); // 'AMZN';
191
+ *
192
+ * // Find stocks in price range [200, 400] for portfolio balancing
193
+ * const stocksInRange = priceIndex.rangeSearch([200, 400], node => {
194
+ * const stock = priceIndex.get(node);
195
+ * return {
196
+ * symbol: stock?.symbol,
197
+ * price: node,
198
+ * volume: stock?.volume
199
+ * };
200
+ * });
150
201
  *
151
- * // Query stocks within a specific price range (200 to 400)
152
- * const stocksInRange = priceIndex.rangeSearch(
153
- * [200, 400], // Price range
154
- * node => priceIndex.get(node)?.symbol // Extract stock symbols for the result
155
- * );
156
- * console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
202
+ * console.log(stocksInRange.length); // 3;
203
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'GOOGL')); // true;
204
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'META')); // true;
205
+ * console.log(stocksInRange.some((s: any) => s.symbol === 'MSFT')); // true;
157
206
  */
158
207
  export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
159
208
  constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
@@ -116,7 +116,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
116
116
  *
117
117
  * @example
118
118
  * // players ranked by score with their equipment
119
- * type Equipment = {
119
+ * type Equipment = {
120
120
  * name: string; // Equipment name
121
121
  * quality: 'legendary' | 'epic' | 'rare' | 'common';
122
122
  * level: number;
@@ -277,7 +277,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
277
277
  * // },
278
278
  * // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
279
279
  * // ]
280
- * // ]
280
+ * // ];
281
281
  */
282
282
  export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
283
283
  /**
@@ -23,7 +23,132 @@ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
23
23
  * @template VO - Concrete vertex class (extends AbstractVertex<V>).
24
24
  * @template EO - Concrete edge class (extends AbstractEdge<E>).
25
25
  * @remarks Time O(1), Space O(1)
26
- * @example examples will be generated by unit test
26
+ * @example
27
+ * // basic DirectedGraph vertex and edge creation
28
+ * // Create a simple directed graph
29
+ * const graph = new DirectedGraph<string>();
30
+ *
31
+ * // Add vertices
32
+ * graph.addVertex('A');
33
+ * graph.addVertex('B');
34
+ * graph.addVertex('C');
35
+ *
36
+ * // Verify vertices exist
37
+ * console.log(graph.hasVertex('A')); // true;
38
+ * console.log(graph.hasVertex('B')); // true;
39
+ * console.log(graph.hasVertex('C')); // true;
40
+ * console.log(graph.hasVertex('D')); // false;
41
+ *
42
+ * // Check vertex count
43
+ * console.log(graph.size); // 3;
44
+ * @example
45
+ * // DirectedGraph edge operations
46
+ * const graph = new DirectedGraph<string>();
47
+ *
48
+ * // Add vertices
49
+ * graph.addVertex('A');
50
+ * graph.addVertex('B');
51
+ * graph.addVertex('C');
52
+ *
53
+ * // Add directed edges
54
+ * graph.addEdge('A', 'B', 1);
55
+ * graph.addEdge('B', 'C', 2);
56
+ * graph.addEdge('A', 'C', 3);
57
+ *
58
+ * // Verify edges exist
59
+ * console.log(graph.hasEdge('A', 'B')); // true;
60
+ * console.log(graph.hasEdge('B', 'C')); // true;
61
+ * console.log(graph.hasEdge('C', 'B')); // false; // Graph is directed
62
+ *
63
+ * // Get neighbors of A
64
+ * const neighborsA = graph.getNeighbors('A');
65
+ * console.log(neighborsA[0].key); // 'B';
66
+ * console.log(neighborsA[1].key); // 'C';
67
+ * @example
68
+ * // DirectedGraph deleteEdge and vertex operations
69
+ * const graph = new DirectedGraph<string>();
70
+ *
71
+ * // Build a small graph
72
+ * graph.addVertex('X');
73
+ * graph.addVertex('Y');
74
+ * graph.addVertex('Z');
75
+ * graph.addEdge('X', 'Y', 1);
76
+ * graph.addEdge('Y', 'Z', 2);
77
+ *
78
+ * // Delete an edge
79
+ * graph.deleteEdgeSrcToDest('X', 'Y');
80
+ * console.log(graph.hasEdge('X', 'Y')); // false;
81
+ *
82
+ * // Edge in other direction should not exist
83
+ * console.log(graph.hasEdge('Y', 'X')); // false;
84
+ *
85
+ * // Other edges should remain
86
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
87
+ *
88
+ * // Delete a vertex
89
+ * graph.deleteVertex('Y');
90
+ * console.log(graph.hasVertex('Y')); // false;
91
+ * console.log(graph.size); // 2;
92
+ * @example
93
+ * // DirectedGraph topologicalSort for task scheduling
94
+ * const graph = new DirectedGraph<string>();
95
+ *
96
+ * // Build a DAG (Directed Acyclic Graph) for task dependencies
97
+ * graph.addVertex('Design');
98
+ * graph.addVertex('Implement');
99
+ * graph.addVertex('Test');
100
+ * graph.addVertex('Deploy');
101
+ *
102
+ * // Add dependency edges
103
+ * graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
104
+ * graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
105
+ * graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
106
+ *
107
+ * // Topological sort gives valid execution order
108
+ * const executionOrder = graph.topologicalSort();
109
+ * console.log(executionOrder); // defined;
110
+ * console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
111
+ *
112
+ * // All vertices should be included
113
+ * console.log(executionOrder?.length); // 4;
114
+ * @example
115
+ * // DirectedGraph dijkstra shortest path for network routing
116
+ * // Build a weighted directed graph representing network nodes and costs
117
+ * const network = new DirectedGraph<string>();
118
+ *
119
+ * // Add network nodes
120
+ * network.addVertex('Router-A');
121
+ * network.addVertex('Router-B');
122
+ * network.addVertex('Router-C');
123
+ * network.addVertex('Router-D');
124
+ * network.addVertex('Router-E');
125
+ *
126
+ * // Add weighted edges (network latency costs)
127
+ * network.addEdge('Router-A', 'Router-B', 5);
128
+ * network.addEdge('Router-A', 'Router-C', 10);
129
+ * network.addEdge('Router-B', 'Router-D', 3);
130
+ * network.addEdge('Router-C', 'Router-D', 2);
131
+ * network.addEdge('Router-D', 'Router-E', 4);
132
+ * network.addEdge('Router-B', 'Router-E', 12);
133
+ *
134
+ * // Find shortest path from Router-A to Router-E
135
+ * const { minDist, minPath } = network.dijkstra('Router-A', 'Router-E', true, true) || {
136
+ * minDist: undefined,
137
+ * minPath: undefined
138
+ * };
139
+ *
140
+ * // Verify shortest path is found
141
+ * console.log(minDist); // defined;
142
+ * console.log(minPath); // defined;
143
+ *
144
+ * // Shortest path should be A -> B -> D -> E with cost 5+3+4=12
145
+ * // Or A -> C -> D -> E with cost 10+2+4=16
146
+ * // So the minimum is 12
147
+ * console.log(minDist); // <= 16;
148
+ *
149
+ * // Verify path is valid (includes start and end)
150
+ * console.log(minPath?.[0].key); // 'Router-A';
151
+ * console.log(minPath?.[minPath.length - 1].key); // 'Router-E';
27
152
  */
28
153
  export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
29
154
  /**
@@ -22,7 +22,166 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
22
22
  * @template VO - Concrete vertex class (extends AbstractVertex<V>).
23
23
  * @template EO - Concrete edge class (extends AbstractEdge<E>).
24
24
  * @remarks Time O(1), Space O(1)
25
- * @example examples will be generated by unit test
25
+ * @example
26
+ * // basic UndirectedGraph vertex and edge creation
27
+ * // Create a simple undirected graph
28
+ * const graph = new UndirectedGraph<string>();
29
+ *
30
+ * // Add vertices
31
+ * graph.addVertex('A');
32
+ * graph.addVertex('B');
33
+ * graph.addVertex('C');
34
+ * graph.addVertex('D');
35
+ *
36
+ * // Verify vertices exist
37
+ * console.log(graph.hasVertex('A')); // true;
38
+ * console.log(graph.hasVertex('B')); // true;
39
+ * console.log(graph.hasVertex('E')); // false;
40
+ *
41
+ * // Check vertex count
42
+ * console.log(graph.size); // 4;
43
+ * @example
44
+ * // UndirectedGraph edge operations (bidirectional)
45
+ * const graph = new UndirectedGraph<string>();
46
+ *
47
+ * // Add vertices
48
+ * graph.addVertex('A');
49
+ * graph.addVertex('B');
50
+ * graph.addVertex('C');
51
+ *
52
+ * // Add undirected edges (both directions automatically)
53
+ * graph.addEdge('A', 'B', 1);
54
+ * graph.addEdge('B', 'C', 2);
55
+ * graph.addEdge('A', 'C', 3);
56
+ *
57
+ * // Verify edges exist in both directions
58
+ * console.log(graph.hasEdge('A', 'B')); // true;
59
+ * console.log(graph.hasEdge('B', 'A')); // true; // Bidirectional!
60
+ *
61
+ * console.log(graph.hasEdge('C', 'B')); // true;
62
+ * console.log(graph.hasEdge('B', 'C')); // true; // Bidirectional!
63
+ *
64
+ * // Get neighbors of A
65
+ * const neighborsA = graph.getNeighbors('A');
66
+ * console.log(neighborsA[0].key); // 'B';
67
+ * console.log(neighborsA[1].key); // 'C';
68
+ * @example
69
+ * // UndirectedGraph deleteEdge and vertex operations
70
+ * const graph = new UndirectedGraph<string>();
71
+ *
72
+ * // Build a simple undirected graph
73
+ * graph.addVertex('X');
74
+ * graph.addVertex('Y');
75
+ * graph.addVertex('Z');
76
+ * graph.addEdge('X', 'Y', 1);
77
+ * graph.addEdge('Y', 'Z', 2);
78
+ * graph.addEdge('X', 'Z', 3);
79
+ *
80
+ * // Delete an edge
81
+ * graph.deleteEdge('X', 'Y');
82
+ * console.log(graph.hasEdge('X', 'Y')); // false;
83
+ *
84
+ * // Bidirectional deletion confirmed
85
+ * console.log(graph.hasEdge('Y', 'X')); // false;
86
+ *
87
+ * // Other edges should remain
88
+ * console.log(graph.hasEdge('Y', 'Z')); // true;
89
+ * console.log(graph.hasEdge('Z', 'Y')); // true;
90
+ *
91
+ * // Delete a vertex
92
+ * graph.deleteVertex('Y');
93
+ * console.log(graph.hasVertex('Y')); // false;
94
+ * console.log(graph.size); // 2;
95
+ * @example
96
+ * // UndirectedGraph connectivity and neighbors
97
+ * const graph = new UndirectedGraph<string>();
98
+ *
99
+ * // Build a friendship network
100
+ * const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
101
+ * for (const person of people) {
102
+ * graph.addVertex(person);
103
+ * }
104
+ *
105
+ * // Add friendships (undirected edges)
106
+ * graph.addEdge('Alice', 'Bob', 1);
107
+ * graph.addEdge('Alice', 'Charlie', 1);
108
+ * graph.addEdge('Bob', 'Diana', 1);
109
+ * graph.addEdge('Charlie', 'Eve', 1);
110
+ * graph.addEdge('Diana', 'Eve', 1);
111
+ *
112
+ * // Get friends of each person
113
+ * const aliceFriends = graph.getNeighbors('Alice');
114
+ * console.log(aliceFriends[0].key); // 'Bob';
115
+ * console.log(aliceFriends[1].key); // 'Charlie';
116
+ * console.log(aliceFriends.length); // 2;
117
+ *
118
+ * const dianaFriends = graph.getNeighbors('Diana');
119
+ * console.log(dianaFriends[0].key); // 'Bob';
120
+ * console.log(dianaFriends[1].key); // 'Eve';
121
+ * console.log(dianaFriends.length); // 2;
122
+ *
123
+ * // Verify bidirectional friendship
124
+ * const bobFriends = graph.getNeighbors('Bob');
125
+ * console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
126
+ * console.log(bobFriends[1].key); // 'Diana';
127
+ * @example
128
+ * // UndirectedGraph for social network connectivity analysis
129
+ * interface Person {
130
+ * id: number;
131
+ * name: string;
132
+ * location: string;
133
+ * }
134
+ *
135
+ * // UndirectedGraph is perfect for modeling symmetric relationships
136
+ * // (friendships, collaborations, partnerships)
137
+ * const socialNetwork = new UndirectedGraph<number, Person>();
138
+ *
139
+ * // Add people as vertices
140
+ * const people: [number, Person][] = [
141
+ * [1, { id: 1, name: 'Alice', location: 'New York' }],
142
+ * [2, { id: 2, name: 'Bob', location: 'San Francisco' }],
143
+ * [3, { id: 3, name: 'Charlie', location: 'Boston' }],
144
+ * [4, { id: 4, name: 'Diana', location: 'New York' }],
145
+ * [5, { id: 5, name: 'Eve', location: 'Seattle' }]
146
+ * ];
147
+ *
148
+ * for (const [id] of people) {
149
+ * socialNetwork.addVertex(id);
150
+ * }
151
+ *
152
+ * // Add friendships (automatically bidirectional)
153
+ * socialNetwork.addEdge(1, 2, 1); // Alice <-> Bob
154
+ * socialNetwork.addEdge(1, 3, 1); // Alice <-> Charlie
155
+ * socialNetwork.addEdge(2, 4, 1); // Bob <-> Diana
156
+ * socialNetwork.addEdge(3, 5, 1); // Charlie <-> Eve
157
+ * socialNetwork.addEdge(4, 5, 1); // Diana <-> Eve
158
+ *
159
+ * console.log(socialNetwork.size); // 5;
160
+ *
161
+ * // Find direct connections for Alice
162
+ * const aliceConnections = socialNetwork.getNeighbors(1);
163
+ * console.log(aliceConnections[0].key); // 2;
164
+ * console.log(aliceConnections[1].key); // 3;
165
+ * console.log(aliceConnections.length); // 2;
166
+ *
167
+ * // Verify bidirectional connections
168
+ * console.log(socialNetwork.hasEdge(1, 2)); // true;
169
+ * console.log(socialNetwork.hasEdge(2, 1)); // true; // Friendship works both ways!
170
+ *
171
+ * // Remove a person from network
172
+ * socialNetwork.deleteVertex(2); // Bob leaves
173
+ * console.log(socialNetwork.hasVertex(2)); // false;
174
+ * console.log(socialNetwork.size); // 4;
175
+ *
176
+ * // Alice loses Bob as a friend
177
+ * const updatedAliceConnections = socialNetwork.getNeighbors(1);
178
+ * console.log(updatedAliceConnections[0].key); // 3;
179
+ * console.log(updatedAliceConnections[1]); // undefined;
180
+ *
181
+ * // Diana loses Bob as a friend
182
+ * const dianaConnections = socialNetwork.getNeighbors(4);
183
+ * console.log(dianaConnections[0].key); // 5;
184
+ * console.log(dianaConnections[1]); // undefined;
26
185
  */
27
186
  export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
28
187
  /**