Type Definition
Generic Parameters
Hash function implementing the
Hasher traitBranching factor (compile-time constant, must be ≥ 2)
Maximum tree depth (compile-time constant, must be ≥ 1)
The branching factor
N determines how many children each internal node can have. Common values are:N=2for binary treesN=3for ternary treesN=4for quaternary trees
Constructor
new
The hash function to use for computing internal nodes
A new empty tree instance
Methods
insert
The 32-byte hash to insert as a leaf
The new Merkle root after insertion, or an error if the operation fails
TreeError::MaxDepthExceededif inserting would exceedMAX_DEPTHTreeError::CapacityExceededif the tree size would overflowu64
Example
insert_many
insert repeatedly.
Slice of 32-byte hashes to insert
The new Merkle root after batch insertion
TreeError::EmptyBatchif the slice is emptyTreeError::MaxDepthExceededif the batch would exceedMAX_DEPTHTreeError::CapacityExceededif the tree size would overflow
Example
With the
parallel feature enabled, insert_many automatically parallelizes hash computation using Rayon when the batch size exceeds the configured threshold.root
None if the tree is empty.
The 32-byte root hash, or
None for an empty treeExample
size
The leaf count
Example
depth
The tree depth (0 for a single-leaf or empty tree)
Example
For an N-ary tree with
size leaves, the depth is approximately log_N(size). A single leaf has depth 0.snapshot
An immutable snapshot that shares structure with the tree via
ArcExample
Snapshots use structural sharing via
Arc, making them very cheap to create. Multiple snapshots can coexist with minimal memory overhead.Concurrency
With theconcurrent feature enabled:
LeanIMTwraps internal state inparking_lot::RwLock- All methods take
&selfinstead of&mut self - Multiple threads can safely insert concurrently
- Readers can take lock-free snapshots
Concurrent Example
Memory Layout
The tree uses a chunked storage format with structural sharing:- Leaf level and internal levels stored in 128-hash chunks
- Chunks grouped into 256-chunk segments
- Segments frozen into immutable
Arcon completion - Snapshots share immutable segments with zero-copy
