Augmenting a small dependable data structure with logic to address a more complex use case is a pattern I've used to great effect in my career. It's a relic from a different time now that you can pull libraries with every corner of the internet via a package manager.
The code here implementes a modular C library implementing 12 advanced tree augmentations built on top of robust, production-grade base tree templates:
- Red-Black Trees (src/tree.h): Upgraded OpenBSD Red-Black tree macro library.
- Treaps (src/treap.h): BSD-style randomized binary search tree template supporting split and merge.
Every augmentation maintains
Below is a map of the base tree templates in the library and the modules derived by augmenting them:
- Interval Tree (
ITREE): Augments the tree with amaxfield representing the maximum high endpoint in the subtree, enabling optimal range overlap and containment searches. - Order-Statistic Tree (
OSTREE): Augments the tree with asizefield representing the number of nodes in the subtree, enabling rank and selection queries. - Range Sum Tree (
SUMTREE): Augments the tree with asumfield representing the sum of node values in the subtree, enabling dynamic prefix and range sum queries. - Range Minimum Tree (
MINTREE): Augments the tree with amin_valfield representing the minimum value in the subtree, enabling dynamic range minimum queries (RMQ). - Priority Search Tree (
PSTREE): Stores 2D points (x, y) ordered by x (as BST key) and augments each node with the maximum y in its subtree, enabling 3-sided range queries. - Max Subarray Sum Tree (
MAXSUB): Stores a sequence of values and augments each node with subtree-level sum, max_prefix, max_suffix, and max_sub metrics, enabling range maximum subarray sum queries. - Hash Tree (
HASHTREE): Augments the tree with a rolling hash of the sequence to support fast substring equality checks. - LCP Tree (
LCPTREE): Augments the tree with a longest common prefix of string keys, enabling optimal prefix searches. - Incremental Convex Hull (
INCHULL): Maintains the upper boundary of a 2D convex hull dynamically by adding points in left-to-right order.
- Rope (
ROPE): Augments a Treap with subtree sizes to support character sequences and fast split/merge indexing. - Euler Tour Tree (
EULER): Augments a Treap to represent forest structures via Euler tours, supporting dynamic link, cut, and connectivity queries. - Dynamic Convex Hull (
DYNHULL): Augments a Treap to maintain the upper/lower hulls of a set of points, supporting both insertion and deletion in logarithmic time.
To build the test helper and run the entire test suite under Valgrind:
make checkTo format the codebase:
make indentTo generate transparent PNG visualizations under images/ for all active modules:
make imagesThis library is licensed under the 2-clause BSD license. See LICENSE for details.