Jump to content

bt.h

From Hugo's wiki

bt.h is a single-header red-black tree for C with string keys. Adding, finding and deleting are O(log n), and deleting keeps the tree balanced.

Walks in key order (bt_first/bt_next, or for_bt_each) keep no state of their own, so they can nest. The older bt_iter still works, but has a single state for the whole program.

Usage

  1. Copy bt.h into your project.
  2. #define BT_IMPLEMENTATION before including it, in exactly one file.
  3. #include "bt.h" wherever you need it.

The public API is documented in bt.h; test.c is an example.

Note If you get implicit declaration of function strdup, include bt.h before string.h, or define _XOPEN_SOURCE >= 500 or _POSIX_C_SOURCE >= 200809L.

The tree is not type-agnostic yet.

See also