1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! # augmented-rbtree
//!
//! An augmented red-black tree with generic, user-defined per-node statistics.
//!
//! ## What is an Augmented Red-Black Tree?
//!
//! A standard [red-black tree] is a self-balancing binary search tree that guarantees
//! O(log n) insert, delete, and lookup. An *augmented* variant extends each node with
//! extra data (called *statistics* or *augmentation*), computed from its subtree.
//! Because the tree stays balanced, the statistics at the root always reflect the entire
//! collection, and any prefix or suffix can be queried in O(log n) time.
//!
//! Common examples built on this primitive:
//!
//! | Use case | Key | Value | Augmentation |
//! |----------|-----|-------|-------------|
//! | **Order-statistics tree** | any | any | subtree size |
//! | **Interval tree** | interval start | interval end | max endpoint in subtree |
//! | **Range-sum tree** | any | numeric | subtree sum |
//! | **Range-max tree** | any | numeric | subtree max |
//!
//! [red-black tree]: https://en.wikipedia.org/wiki/Red%E2%80%93black_tree
//!
//! ## Quick Start
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! augmented-rbtree = "0.1"
//! ```
//!
//! Implement the [`Augment`] trait or use one of the built-in augmentations in [`augmentations`]. Then create a tree:
//!
//!
//! # Examples
//! ```
//! use augmented_rbtree::{Augment, AugmentedRBTreeFactory};
//!
//! /// Tracks the number of nodes in each subtree.
//! struct SubtreeCount;
//!
//! impl<K, V> Augment<K, V> for SubtreeCount {
//! type Stats = usize;
//!
//! fn compute(_k: &K, _v: &V,
//! left: Option<(&K, &V, &usize)>,
//! right: Option<(&K, &V, &usize)>) -> usize {
//! 1 + left.map(|(_, _, &c)| c).unwrap_or(0)
//! + right.map(|(_, _, &c)| c).unwrap_or(0)
//! }
//! }
//! fn main() {
//! let mut tree = AugmentedRBTreeFactory::<SubtreeCount>::new_tree();
//! tree.insert(3, "c");
//! tree.insert(1, "a");
//! tree.insert(2, "b");
//!
//! // Total count is always at the root
//! assert_eq!(tree.root_stats(), Some(&3));
//!
//! // Standard ordered-map operations
//! assert_eq!(tree.get(&2), Some(&"b"));
//! assert_eq!(tree.first_key_value_stats(), Some((&1, &"a", &1)));
//!
//! // Iterate in sorted order; each entry exposes (key, value, stats)
//! for (k, v, count) in tree.iter() {
//! println!("key={k}, value={v}, subtree_size={count}");
//! }
//! }
//! ```
//!
//! ## Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `alloc` | **Yes** | Uses the standard `alloc` crate for baseline heap allocation support. |
//! | `allocator-api` | No | Enables custom local allocator support on stable Rust via `allocator-api2`. |
//! | `nightly` | No | Opts into the upstream standard `core::alloc::Allocator` API (Requires Nightly). |
//! | `serde` | No | Implements [`serde::Serialize`] and [`serde::Deserialize`] for the tree. |
//! | `debug` | No | Makes `verify_properties` and `verify_augmentation` available in release builds. |
//! | `interval_tree` | No | Enables the [`interval_tree`] module, which implements an interval tree using this crate. |
//!
//! ## Red-Black Tree Properties
//!
//! 1. Every node is Red or Black.
//! 2. The root is Black.
//! 3. All nil leaves are Black.
//! 4. A Red node's children are both Black.
//! 5. Every path from a node to a descendant nil has the same number of Black nodes.
// enable the allocator_api feature on nightly toolchains to access the `Allocator` trait and related APIs
// If both are enabled, prioritize nightly and throw a helpful compiler warning
compile_error!;
extern crate alloc;
pub use ;
pub use Augment;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Color;
pub use ;
pub use AugmentedRBTreeSeed;