- Notifications
You must be signed in to change notification settings - Fork0
calcit-lang/ternary-tree.rs
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This library implements a variant of a 2-3 tree with ternary branching, also known as a ternary search tree. It is further enhanced with finger-tree-inspired optimizations.
Thepop_left() andpush_right() operations are optimized to be amortizedO(1) in the best cases andO(log n) when restructuring is involved.
For a visual explanation of the tree's layout (from 0 to 159), you can watch thisvideo or try thelive demo.
API documentation is available ondocs.rs.
use im_ternary_tree::TernaryTreeList;println!("{}",TernaryTreeList::<usize>::from(&[]));// Create a new list with an updated value at a specific indexlet origin5 =[1,2,3,4,5];let data5 =TernaryTreeList::from(&origin5);let updated = data5.assoc(3,10);println!("{}", data5.format_inline());println!("{}", updated.format_inline());assert_eq!(updated.unsafe_get(3),10);
A more detailed, Chinese-language explanation of the design is available in thisvideo.
This library features special optimizations forpush_right andpop_left, inspired by the design offinger trees.
As the size of the tree grows, operations are focused on a shallow branch at the right end. This minimizes the number of nodes that need to be indexed for new elements. A random demonstration of this can be seen below:
The left branches are also intentionally kept shallow, which reduces the cost ofpop_left operations.
Benchmarks comparingTernaryTreeList withstd::vec::Vec andstd::collections::VecDeque show a clear performance profile. As an immutable data structure,TernaryTreeList has some overhead compared to its mutable counterparts but offers significant advantages in specific scenarios.
push_right/drop_right(Appending/Popping from the tail):VecandVecDequeare significantly faster, as they are mutable and optimized for O(1) amortized operations at the tail.TernaryTreeListis slower due to the nature of immutable structures, which require creating new tree nodes.
push_left/drop_left(Prepending/Popping from the head):TernaryTreeListisdramatically faster thanVec.Vec::insert(0, ...)is an O(n) operation, whileTernaryTreeList's finger-tree-inspired optimizations make head operations much more efficient.VecDequeis still the fastest, as it is a mutable data structure specifically designed for O(1) head and tail operations.
Conclusion:
- Use
TernaryTreeListwhen:- You need animmutable (persistent) list.
- You requireefficient push and pop operations on both ends of the list, and the performance of a mutable deque is not required.
- Use
VecorVecDequewhen:- Mutability is acceptable.
- You need the absolute best performance for purely mutable operations.
pop_rightlacks some optimizations.- Elements in the middle of the tree may be deeply nested, resulting in slower performance for accessing or modifying them.
This project is licensed under the MIT License.
About
Structural sharing unbalanced 2-3 tree with tricks like finger-tree.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.

