daggy
daggy copied to clipboard
Iterator over tree ends
Is there a walker method that efficiently returns the elements at the end of the tree (leaves)?
I took a look at the raw nodes and it looks like all of the leaf nodes have a next
value that looks like next: [EdgeIndex(End), EdgeIndex(6)]
. So I could call filter on a recursive walk over all of the nodes in the tree or on .raw_nodes()
and check if next[0] == EdgeIndex(End)
, but that seems like a waste. Is there a better way to do this using the currently available API?
For reference, this is an example of what I'm going with for now:
let end_index = EdgeIndex::<u32>::end();
let leaves: Vec<Node> = tree.raw_nodes().iter().filter(|&node| {
node.next_edge(EdgeDirection::Outgoing) == end_index
}).collect();
Related: https://github.com/mitchmindtree/daggy/issues/24