Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "easy-tree"
version = "0.2.0"
version = "0.3.0"
authors = ["Anton Suprunchuk <anton.suprunchuk@gmail.com>"]
edition = "2021"
description = "A simple and efficient tree structure library for Rust with recursive traversal"
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,30 @@ impl<T> Tree<T> {
mut after_processing_the_subtree: impl FnMut(usize, &mut T, &mut S),
s: &mut S,
) {
if self.is_empty() {
self.traverse_subtree_mut(
0,
&mut before_processing_children,
&mut after_processing_the_subtree,
s,
)
}

/// Walks the tree recursively starting from a specific node, applying the given functions
/// before and after processing the children of each node. This version allows for mutable
/// access to the nodes. Skips
pub fn traverse_subtree_mut<S>(
&mut self,
start: usize,
mut before_processing_children: impl FnMut(usize, &mut T, &mut S),
mut after_processing_the_subtree: impl FnMut(usize, &mut T, &mut S),
s: &mut S,
) {
if self.is_empty() || self.nodes.get(start).is_none() {
return;
}

self.stack.clear();
self.stack.push((0, false));
self.stack.push((start, false));

while let Some((index, children_visited)) = self.stack.pop() {
if children_visited {
Expand Down
Loading