SHARE
TWEET

Untitled

a guest Jun 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pub struct TreeNode<T> {
  2.     pub value: T,
  3.     pub left: Option<Box<Self>>,
  4.     pub right: Option<Box<Self>>,
  5. }
  6.  
  7. fn make_tree() -> TreeNode<u32> {
  8.     //           5
  9.     //     3            8
  10.     //   2   4       6     9
  11.     //  1              7
  12.     return TreeNode {
  13.         value: 5,
  14.         left: Some(Box::new(TreeNode {
  15.             value: 3,
  16.             left: Some(Box::new(TreeNode {
  17.                 value: 2,
  18.                 left: Some(Box::new(TreeNode {
  19.                     value: 1,
  20.                     left: None,
  21.                     right: None,
  22.                 })),
  23.                 right: None,
  24.             })),
  25.             right: Some(Box::new(TreeNode {
  26.                 value: 4,
  27.                 left: None,
  28.                 right: None,
  29.             })),
  30.         })),
  31.         right: Some(Box::new(TreeNode {
  32.             value: 8,
  33.             left: Some(Box::new(TreeNode {
  34.                 value: 6,
  35.                 left: None,
  36.                 right: Some(Box::new(TreeNode {
  37.                     value: 7,
  38.                     left: None,
  39.                     right: None,
  40.                 })),
  41.             })),
  42.             right: Some(Box::new(TreeNode {
  43.                 value: 9,
  44.                 left: None,
  45.                 right: None,
  46.             })),
  47.         })),
  48.     };
  49. }
  50.  
  51. fn traverse(node: &TreeNode<u32>) {
  52.     if let Some(ref left) = node.left {
  53.         traverse(left)
  54.     }
  55.     println!("{}", node.value);
  56.     if let Some(ref right) = node.right {
  57.         traverse(right)
  58.     }
  59. }
  60.  
  61. fn main() {
  62.     let tree = make_tree();
  63.     traverse(&tree);
  64.    
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top