Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 160 additions & 47 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ description = """
Trees methods for linksplatform
"""
readme = "README.md"
exclude = ["docs/case-studies/issue-32/evidence/**"]
keywords = ["trees", "linksplatform", "data-structures"]
categories = ["data-structures"]

Expand Down
125 changes: 115 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ Crates.io package: [platform-trees](https://crates.io/crates/platform-trees)
This crate provides generic traits for size-balanced binary trees and
circular linked lists used throughout the LinksPlatform ecosystem:

- **`RecursiveSizeBalancedTree<T>`** — Base size-balanced binary tree
trait with node navigation, tree rotations, size management, and
tree queries (`contains`, `get_leftest`, `get_rightest`).
- **`IterativeSizeBalancedTree<T>`** — Extends
`RecursiveSizeBalancedTree` with iterative `attach` and `detach`
operations that avoid stack overflow on deep trees.
- **`RecursiveSizeBalancedTree<T>`** — Size-balanced binary search tree
with recursive `attach_recursive` and `detach_recursive`, an in-place
`rebuild_complete`, rotations, and navigation.
- **`IterativeSizeBalancedTree<T>`** — Extends the base trait with iterative
`attach` and `detach` for the same node layout and tree shape.
- **`LinkedList<T>`** — Base doubly-linked list trait with
`get_previous`, `get_next`, `set_previous`, `set_next`.
- **`AbsoluteLinkedList<T>`** — Linked list with direct access to
Expand All @@ -36,11 +35,10 @@ circular linked lists used throughout the LinksPlatform ecosystem:

## Installation

Add to your `Cargo.toml`:
Add the crate to your project with Cargo.

```toml
[dependencies]
platform-trees = "0.2.0"
```sh
cargo add platform-trees
```

## Usage
Expand Down Expand Up @@ -113,6 +111,32 @@ impl RecursiveSizeBalancedTree<usize> for MyTreeStorage {
impl IterativeSizeBalancedTree<usize> for MyTreeStorage {}
```

Use `attach` and `detach` with a storage implementation of
`IterativeSizeBalancedTree`, or call `attach_recursive` and
`detach_recursive` on the base trait. Pass a readable and writable root
slot to each method. Use a nonzero ID with both child links clear for a
new node and a consistent strict order for the comparisons. Inserting
an existing or equivalent key, or removing a node whose key or ID does
not match, leaves the tree unchanged. A successful removal clears that
node's two child links and size.

### Balance and persisted trees

Each subtree is at least as large as both of its sibling's subtrees.
Both implementations of Chen's maintenance perform the same rotations in
the same order. They support `O(log n)` lookups and amortized `O(log n)`
updates on balanced trees. `maximum_balanced_height(n)` gives the
worst-case AVL height bound in levels, including 16 levels for 4,096
nodes. A perfectly full tree can be shorter.

When opening a tree built by an older version, call `rebuild_complete`
once per root before queries or mutations. It builds a complete tree
from an acyclic, ordered tree with correct subtree sizes. It uses
`O(n)` time and `O(1)` additional space, preserving node IDs, keys, and
the storage layout. Both mutation APIs also rebuild and retry a
successful edit when its search or replacement path exceeds the
height bound.

### Implementing `LinkedList`

```rust
Expand Down Expand Up @@ -161,6 +185,87 @@ impl AbsoluteLinkedList<usize> for MyListStorage {
impl AbsoluteCircularLinkedList<usize> for MyListStorage {}
```

## Performance and capacity

The tree algorithms use three index-sized fields per node (two child
IDs and a subtree size) and one root ID per tree. The recursive variant
uses call-stack space. The iterative variant uses two scratch stacks
with 32 inline entries each (roughly 1 KiB total on 64-bit targets before
stack bookkeeping and compiler overhead) and allocates only when one
exceeds its inline capacity. Neither variant adds instrumentation to
the node layout. Updates use `O(log n)` scratch space on a balanced tree.

The circular lists use two index-sized fields per element and three
per list head. Lists support constant-time edits given the element ID
and, for a relative list, the head ID. Finding an unknown element by
value requires scanning the list. The absolute variant stores one
head record. The relative variant supports multiple heads and lets the
caller choose their storage.

Zero is the empty ID for the trees and list elements. A `b`-bit
unsigned ID and size type can represent at most `2^b − 1` elements
(255 with `u8`, 65,535 with `u16`), reduced by IDs reserved by the
storage implementation and the addressable space of that storage.
The tree height helper supports all primitive unsigned widths through
`u128` (at most 184 levels). The sample relative list reserves a head
record for every possible element in its backing range.

### Reference measurements

The 64-bit Linux reference run used an AMD EPYC CPU, Rust 1.99.0 and release optimizations.
Update time is the median of nine rounds of inserting all keys then
removing them in the same order. Tree search time is measured in a
separate pass and list edits receive a known ID. The update ratio uses
the recursive tree for the same size and order, with smaller values
indicating less time. Storage includes the preallocated fixture, empty
slot, and heads. The final column counts total heap bytes allocated
during the measured operation passes. Stack use is described above.

`cargo run --locked --release --example benchmark_collections` prints
the full comparison. Choose **benchmarks** when dispatching the
**CI/CD Pipeline** on GitHub to publish the table in the job summary
and as an artifact.

<!-- benchmark-results-start -->
| Nodes | Insertion order | Structure | Update ns/op | Relative | Search ns/op | Storage B | Update/search heap B |
| ---: | --- | --- | ---: | ---: | ---: | ---: | ---: |
| 1000 | Ascending | Recursive SBT | 287 | 1.000× | 41 | 24056 | 0 |
| 1000 | Ascending | Iterative SBT | 222 | 0.771× | 37 | 24056 | 0 |
| 1000 | Ascending | Absolute circular list | 2 | 0.009× | — | 16064 | 0 |
| 1000 | Ascending | Relative circular list | 3 | 0.013× | — | 40088 | 0 |
| 1000 | Descending | Recursive SBT | 227 | 1.000× | 42 | 24056 | 0 |
| 1000 | Descending | Iterative SBT | 215 | 0.950× | 41 | 24056 | 0 |
| 1000 | Descending | Absolute circular list | 2 | 0.012× | — | 16064 | 0 |
| 1000 | Descending | Relative circular list | 3 | 0.017× | — | 40088 | 0 |
| 1000 | Shuffled | Recursive SBT | 277 | 1.000× | 59 | 24056 | 0 |
| 1000 | Shuffled | Iterative SBT | 281 | 1.012× | 58 | 24056 | 0 |
| 1000 | Shuffled | Absolute circular list | 2 | 0.010× | — | 16064 | 0 |
| 1000 | Shuffled | Relative circular list | 4 | 0.014× | — | 40088 | 0 |
| 1000 | Hot/cold | Recursive SBT | 225 | 1.000× | 47 | 24056 | 0 |
| 1000 | Hot/cold | Iterative SBT | 222 | 0.988× | 45 | 24056 | 0 |
| 1000 | Hot/cold | Absolute circular list | 2 | 0.012× | — | 16064 | 0 |
| 1000 | Hot/cold | Relative circular list | 4 | 0.018× | — | 40088 | 0 |
| 10000 | Ascending | Recursive SBT | 302 | 1.000× | 57 | 240056 | 0 |
| 10000 | Ascending | Iterative SBT | 282 | 0.935× | 56 | 240056 | 0 |
| 10000 | Ascending | Absolute circular list | 2 | 0.008× | — | 160064 | 0 |
| 10000 | Ascending | Relative circular list | 3 | 0.012× | — | 400088 | 0 |
| 10000 | Descending | Recursive SBT | 293 | 1.000× | 57 | 240056 | 0 |
| 10000 | Descending | Iterative SBT | 281 | 0.959× | 57 | 240056 | 0 |
| 10000 | Descending | Absolute circular list | 2 | 0.008× | — | 160064 | 0 |
| 10000 | Descending | Relative circular list | 3 | 0.012× | — | 400088 | 0 |
| 10000 | Shuffled | Recursive SBT | 397 | 1.000× | 95 | 240056 | 0 |
| 10000 | Shuffled | Iterative SBT | 391 | 0.986× | 100 | 240056 | 0 |
| 10000 | Shuffled | Absolute circular list | 2 | 0.007× | — | 160064 | 0 |
| 10000 | Shuffled | Relative circular list | 4 | 0.011× | — | 400088 | 0 |
| 10000 | Hot/cold | Recursive SBT | 293 | 1.000× | 66 | 240056 | 0 |
| 10000 | Hot/cold | Iterative SBT | 254 | 0.869× | 60 | 240056 | 0 |
| 10000 | Hot/cold | Absolute circular list | 2 | 0.009× | — | 160064 | 0 |
| 10000 | Hot/cold | Relative circular list | 3 | 0.013× | — | 400088 | 0 |
<!-- benchmark-results-end -->

The before/after reproductions, analysis, and upstream evidence are in
the [issue #32 case study](docs/case-studies/issue-32/README.md).

## Depend on

- [platform-num](https://crates.io/crates/platform-num)
Expand Down
13 changes: 13 additions & 0 deletions changelog.d/20261130_050000_chen_tree_balance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
bump: minor
---

### Fixed

- Run Chen's size-bound maintenance after every iterative insertion and removal, including removal of a node with two children.
- Leave storage intact for missing, duplicate, and equivalent-key edits.

### Added

- Recursive insertion and removal, a height-bound query, and in-place complete rebuilding for legacy indexes.
- A reproducible balance regression and benchmarks for the tree and circular-list variants.
Loading
Loading