Skip to content

Instantly share code, notes, and snippets.

@sachanganesh
Created June 2, 2020 17:53
Show Gist options
  • Select an option

  • Save sachanganesh/e58da21caf3925aa281d4eda784de683 to your computer and use it in GitHub Desktop.

Select an option

Save sachanganesh/e58da21caf3925aa281d4eda784de683 to your computer and use it in GitHub Desktop.
python-ported immutable preorder traversal with lifetimes in rust (tree_test)
fn main() {
let a = TreeNode::new(4, None, None);
let b = TreeNode::new(5, None, None);
let c = TreeNode::new(2, Some(Box::from(a)), Some(Box::from(b)));
let d = TreeNode::new(3, None, None);
let e = TreeNode::new(1, Some(Box::from(c)), Some(Box::from(d)));
let tree = Tree::new(Some(e));
for _node in tree.iter() {
// _node.value *= 10;
}
let mut iterator = tree.iter();
while let Some(node) = iterator.next() { // equivalent to the for loop construction
println!("{}", node.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment