neco-tree
Generic ID-bearing multi-way tree with cursor-based navigation.
Each node carries a unique u64 id and a user-defined value of type T. An internal HashMap index makes id-based lookup O(1). CursoredTree<T> adds a movable cursor that tracks the current position in the tree.
Features
Tree<T>: rooted multi-way tree with auto-incrementing node ids- O(1) id lookup via internal index (
find,find_path_to) CursoredTree<T>: cursor navigation (parent, child, sibling, jump)- DFS iterator with depth information
- Policy-based pruning (
KeepLastN,KeepDepthUnder) - Subtree removal with automatic index rebuild
Usage
Build a tree and navigate with a cursor:
use CursoredTree;
let mut tree = new;
// push adds a child to the current node and moves the cursor there
let a = tree.push;
let a1 = tree.push;
assert_eq!;
assert!;
// go_parent moves up
tree.go_parent;
assert_eq!;
// go_root jumps to root
tree.go_root;
assert_eq!;
Use Tree<T> directly when no cursor is needed:
use Tree;
let mut tree = new;
let a = tree.push_child.unwrap;
let b = tree.push_child.unwrap;
tree.push_child.unwrap;
// DFS traversal with depth
for in tree.dfs
// O(1) lookup
assert_eq!;
assert_eq!;
Prune old branches:
use ;
let mut tree = new;
tree.push_child.unwrap;
tree.push_child.unwrap;
tree.push_child.unwrap;
// Keep only the 2 newest children per node
tree.prune;
assert_eq!;
API
| Item | Description |
|---|---|
Node<T> |
Single tree node with id, depth, value, and children |
Tree<T> |
Rooted multi-way tree with O(1) id index |
CursoredTree<T> |
Tree<T> with a movable cursor tracking the current position |
DfsIter |
Depth-first iterator yielding (depth, &Node<T>) |
PrunePolicy |
Pruning strategy: KeepLastN(n) or KeepDepthUnder(depth) |
ParentNotFound |
Error returned when push_child targets a nonexistent parent |
Complexity
| Operation | Time |
|---|---|
push_child |
O(1) amortised |
find / find_path_to |
O(1) amortised |
dfs / flatten |
O(n) |
remove_subtree |
O(n) (index rebuild) |
prune |
O(n) (index rebuild) |
go_parent / go_child / go_to |
O(1) |
License
MIT