Data Structures Overview
The shared package contains reusable lists, queues, stacks, heaps, trees, and math/comparator utilities. Two recent additions are highlighted below:
- BinaryHeap – array-backed heap optimized for fast insert/extract and used by
PriorityQueue. - BinarySearchTree – pointer-based search tree that keeps data ordered while enabling in-order traversal and range queries.
Complexity: BinaryHeap vs BinarySearchTree
| Structure | Insert | Lookup | Extract min/max | Traversal | Notes |
|---|---|---|---|---|---|
| BinaryHeap | O(log n) | O(n) (needs scan) | O(log n) | O(n) (via snapshot) | Great for schedulers/priority queues. Comparator defines min/max behavior. |
| BinarySearchTree | O(log n) average (O(n) worst) | O(log n) average | O(log n) | O(n) with chosen order | Maintains sorted order and exposes traversals + min/max. |
ts
const heap = new BinaryHeap(createComparator('priority'))
heap.insert({ priority: 10 })
const top = heap.extractMin()
const tree = new BinarySearchTree(createComparator('id'))
;['b', 'a', 'c'].forEach(id => tree.insert({ id }))
const ordered = tree.traverse() // in-orderOther structures (quick recap)
- Lists: flexible sequences (
List,LinkedList, etc.) with slicing/splicing helpers. - Queues: FIFO options (
Queue,LinkedQueue,PriorityQueue,Dequeue). - Stacks: LIFO via
StackandLinkedStack. - Heaps:
BinaryHeapfor general use,FibonacciHeapfor algorithms requiring amortizeddecreaseKey. - Sorting: QuickSort/HeapSort utilities operating on
ICollectionimplementations. - Math & utils:
Point,Ordering,createComparator,printHeap, and core interfaces.
Notes
- Read more in Heaps and Trees.
- Lists, queues, stacks, and sorting helpers each have dedicated pages linked from the sidebar.
- All structures rely on the shared Comparator helpers for deterministic ordering.