This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn main() { | |
| let mut tree = Tree::new(); | |
| let a = tree.add_node(TreeNode::new(4, None, None)); | |
| let b = tree.add_node(TreeNode::new(5, None, None)); | |
| let c = tree.add_node(TreeNode::new(2, Some(a), Some(b))); | |
| let d = tree.add_node(TreeNode::new(3, None, None)); | |
| let e = tree.add_node(TreeNode::new(1, Some(c), Some(d))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct PreorderIter { | |
| stack: Vec<TreeIndex> | |
| } | |
| impl PreorderIter { | |
| pub fn new(root: Option<TreeIndex>) -> Self { | |
| if let Some(index) = root { | |
| PreorderIter { | |
| stack: vec![index] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct Tree { | |
| arena: Vec<Option<TreeNode>>, | |
| root: Option<TreeIndex> | |
| } | |
| impl Tree { | |
| pub fn new() -> Self { | |
| Tree { | |
| arena: Vec::new(), | |
| root: None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub type TreeIndex = usize; | |
| pub struct TreeNode { | |
| pub value: usize, | |
| pub left: Option<TreeIndex>, | |
| pub right: Option<TreeIndex> | |
| } | |
| impl TreeNode { | |
| pub fn new( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct PreorderIter<'a> { | |
| stack: Vec<&'a mut TreeNode> | |
| } | |
| impl<'a> PreorderIter<'a> { | |
| pub fn new(root: Option<&'a mut TreeNode>) -> Self { | |
| if let Some(node) = root { | |
| PreorderIter { | |
| stack: vec![node] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct Tree { | |
| root: Option<TreeNode> | |
| } | |
| impl Tree { | |
| pub fn new(root: Option<TreeNode>) -> Self { | |
| Tree { | |
| root | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct PreorderIter<'a> { // explicitly annotate the lifetime of the struct | |
| stack: Vec<&'a TreeNode> // specify that the borrow lives for as long as the struct lives | |
| } | |
| impl<'a> PreorderIter<'a> { // declare the lifetime and apply as lifetime of the struct | |
| pub fn new(root: Option<&'a TreeNode>) -> Self { // annotate lifetime of the borrow | |
| if let Some(node) = root { | |
| PreorderIter { | |
| stack: vec![node] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct PreorderIter { | |
| stack: Vec<&TreeNode> | |
| } | |
| impl PreorderIter { | |
| pub fn new(root: Option<&TreeNode>) -> Self { | |
| if let Some(node) = root { | |
| PreorderIter { | |
| stack: vec![node] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct Tree { | |
| root: Option<TreeNode> | |
| } | |
| impl Tree { | |
| pub fn new(root: Option<TreeNode>) -> Self { | |
| Tree { | |
| root | |
| } | |
| } |
NewerOlder