# Red-black tree

Implement an in-memory **red-black tree**: an ordered map from 64-bit signed
integer keys to 64-bit signed integer values.

This is the classic red-black binary search tree (Guibas & Sedgewick; CLRS
chapter 13), **not** an AVL tree, a treap, a splay tree, a skip list, a
B-tree, or any structure that keeps more than one entry per node.

## Structure

- **A binary search tree.** Every node has at most two children. For every
  node, all keys in its left subtree are less than its key and all keys in
  its right subtree are greater.
- **One entry per node.** Each node holds exactly one key and its value. Each
  key is stored exactly once, in the node that represents it; a tree of `n`
  entries has exactly `n` nodes.
- **Every node is red or black**, and the tree satisfies the red-black
  properties:
  1. The root is black.
  2. A red node has no red child.
  3. Every path from the root down to a missing child (a nil link) passes
     through the same number of black nodes.
- The tree is kept balanced **only** by recolouring nodes and by rotations,
  performed as part of `insert` and `remove`. It is never rebuilt or
  rebalanced in bulk.
- **Removal really removes.** `remove` unlinks the node from the tree and
  restores the red-black properties before it returns. There are no deleted
  markers or lazily removed entries.
- A node holds its key, its value, its colour, and its links to its children
  (and to its parent, if you want one). It holds no other links: no threads to
  the next or previous node and no linked list through the entries.
- The red-black tree is the only structure that stores entries. No side
  index, hash table, sorted array, or cache of entries or lookups.
- Keys are unique. Keys are compared as signed 64-bit integers; any `i64`
  value is a valid key and any `i64` value is a valid value.

These invariants must hold after every operation returns.

## Operations

| Operation | Behaviour |
| --- | --- |
| `new()` | Create an empty tree. |
| `insert(key, value) -> bool` | If `key` is absent, add the entry and return `true`. If `key` is present, replace its value and return `false`. |
| `get(key) -> value or absent` | Return the value stored for `key`, or report that the key is absent. |
| `remove(key) -> bool` | If `key` is present, delete its entry and return `true`. Otherwise change nothing and return `false`. |
| `range(lo, hi, limit) -> entries` | Produce the entries with `lo <= key <= hi` (both bounds inclusive) in ascending key order, stopping after `limit` entries. If `lo > hi` or `limit` is 0 the result is empty. `lo` and `hi` need not be keys in the tree. |
| `len() -> count` | Number of entries in the tree. |
| `height() -> levels` | Number of nodes on the longest path from the root down to a node with no children: 0 for an empty tree, 1 for a tree of one node, and so on. |

Complexity requirements, for a tree of `n` entries:

- `insert`, `get`, `remove`: `O(log n)`.
- `range`: `O(log n + m)` where `m` is the number of entries produced. It
  must walk the tree directly; it must not be built from repeated `get`s or
  from a scan that starts at the smallest key.
- `len`: `O(1)`.
- `height`: may take `O(n)`. It is used only to check the tree and is never
  timed.

A tree that has been emptied by `remove` must be fully usable again
(`len() == 0`, `height() == 0`, and later inserts work).

## Rules

- Write the red-black tree yourself. Do not use or wrap an ordered container,
  tree, or sorted-map from a library (`std::map`, `BTreeMap`, `TreeMap`,
  `sortedcontainers`, ...).
- Use only the language's standard library. No third-party packages and no
  code in another language (no C extensions, FFI, JNI, cgo, or inline assembly).
- No architecture-specific SIMD intrinsics (`<arm_neon.h>`, `std::arch`,
  `System.Runtime.Intrinsics`, and the like). Portable features of the
  language and its compiler are fine: branch hints, prefetch hints, and
  portable vector types.
- Single-threaded: do not start threads or processes.
- Do not change process-wide runtime settings (garbage collector tuning,
  JIT flags, recursion limits, allocator replacement, and so on).
- Low-level utilities from the standard library are fine: arrays, block
  copies, bit operations, and similar.
- Memory use must stay proportional to the largest number of entries the tree
  has held, and everything is released when the tree is destroyed in
  languages with manual memory management.
- Language features that trade safety for speed (for example `unsafe` in
  Rust) are permitted.

The exact names, signatures, and file names for your language are given in
the language binding that accompanies this specification. The benchmark
driver calls exactly that interface.

## How it is judged

1. **Correctness.** A fixed test suite drives the tree through ten workloads
   at several sizes up to 100,000 entries, and compares every result
   against a reference. `height()` is checked against the bounds a red-black
   tree guarantees: at most `2 * floor(log2(n + 1))` levels for `n` entries.
2. **Speed.** The same ten workloads are then timed on a tree of 1,500,000
   entries, which is far larger than the CPU caches. Only the time spent
   inside your operations is measured, so write for performance: the goal is
   the fastest correct red-black tree you can write in this language.
