# Algorithms

Stateless functions exported as namespaces. Each namespace groups related algorithms that operate on raw data (arrays, callbacks, iterables) rather than requiring specific collection classes.

## Namespaces

| Namespace      | Module            | Types                                    | Functions                                                                                                                               |
| :------------- | :---------------- | :--------------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------- |
| `heap`         | `heap.ts`         |                                          | `bubbleUp`, `sinkDown`, `heapify`, `find`, `push`, `pop`, `pushPop`, `popPush`, `pushAll`, `remove`, `update`, `getTopK`, `mergeSorted` |
| `bfs`          | `bfs.ts`          | `GetNeighbors`                           | `bfsNeighborhood`, `bfsPath`, `bfsBidirectionalPath`                                                                                    |
| `graph`        | `graph.ts`        | `GetWeightedNeighbors`, `DijkstraResult` | `dijkstra`, `topologicalSort`                                                                                                           |
| `text`         | `text.ts`         |                                          | `getLevenshteinDistance`, `getOsaDistance`, `indicesOf` (KMP)                                                                           |
| `linked`       | `linked.ts`       | `LinkedNode`                             | `entries`, `keys`, `values`, `iterable`, `reverse`                                                                                      |
| `doublyLinked` | `doublyLinked.ts` | `DoublyLinkedNode`                       | `reverse`                                                                                                                               |
| `tree`         | `binaryTree.ts`   | `BinaryTreeNode`                         | `clone`, `inOrderTraverse`, `preOrderTraverse`, `postOrderTraverse`                                                                     |
| `trie`         | `trie.ts`         | `TrieNode`                               | `add`, `remove`, `walk`, `genTrieNode`                                                                                                  |

## Internal modules

These modules support collection implementations and are not exported from the package's public API:

| Module                 | Purpose                                         | Used by                                       |
| :--------------------- | :---------------------------------------------- | :-------------------------------------------- |
| `skewHeap.ts`          | `find`, `findParent`, `skewMerge`, `skewMerge2` | `SkewHeap`                                    |
| `graph/pathBuilder.ts` | `buildPath`, `buildBiPath`                      | `bfsPath`, `bfsBidirectionalPath`, `dijkstra` |

## Design principles

- **Callback-based, not class-based.** Graph algorithms accept `GetNeighbors<K>` or `GetWeightedNeighbors<K>` callbacks, not `Graph` instances. Any data source that can enumerate neighbors works.
- **Array-level heap operations.** The `heap` namespace operates on raw `T[]` arrays with `CompareFn<T>`. `BinaryHeap` delegates to these; `dijkstra` uses them directly on a raw array. This enables in-place heapsort (`getTopK`) without class overhead.
- **Node primitives.** The `linked`, `doublyLinked`, and `tree` namespaces export node types and operations for building custom linked structures. Collection classes (`LinkedQueue`, `LinkedDeque`, `SkewHeap`) delegate to these internally, but they are also available for direct use.
- **`pop()` does not resize.** The algorithm-level `pop` moves the last element to the root but does not call `array.pop()`. Callers must truncate manually or use `BinaryHeap` which handles it. See `pop()` JSDoc for details.
