木の上を歩こう

Eaa974d2082a6bad3672eabfd6445d02?s=47 hama_du
September 09, 2018

 木の上を歩こう

グラフ理論における木の紹介。例として 二分木を Rust でstep by step で実装。

Eaa974d2082a6bad3672eabfd6445d02?s=128

hama_du

September 09, 2018
Tweet

Transcript

  1. 3.
  2. 4.
  3. 37.

    ίϯετϥΫλ impl BinaryTree { fn new(value: i32, left: Option<BinaryTree>, right:

    Option<BinaryTree>) -> Self { BinaryTree { value, left: left.map(|l| Box::new(l)), right: right.map(|r| Box::new(r)) } } }
  4. 38.

    ؆୯ʹ༿Λ࡞ΕΔΑ͏ʹ impl BinaryTree { fn leaf(value: i32) -> Self {

    BinaryTree { value, left: None, right: None } } }
  5. 39.

    ࢼ͠ʹग़ྗ fn main() { let tree = BinaryTree::new( 3, Some(BinaryTree::new(

    7, (ུ) )), Some(BinaryTree::new( 2, (ུ) )) ); println!(“{:?}”, tree); } ͜͜·Ͱͷ಺༰ͷRust Playground: https://play.rust-lang.org/? gist=64cde6d75f21c94c785116cb86c8d97c&version=stable&mode=debug&edition=2015
  6. 46.

    Iterator Λ࣮૷͢Δ…ʁ impl Iterator for BinaryTree { type Item =

    i32; fn next(&mut self) -> Option<i32> { // Ͳ͏͠Α͏ɾɾɾʁ } }
  7. 49.

    BinaryTreeIterator struct BinaryTreeIterator { cur: Vec<BinaryTree> } impl Iterator for

    BinaryTreeIterator { type Item = i32; fn next(&mut self) -> Option<i32> { // ͜͜Λ࣮૷͢Δ } }
  8. 50.

    BinaryTreeIterator - next fn next(&mut self) -> Option<i32> { if

    self.cur.len() == 0 { None } else { let node = self.cur.pop().unwrap(); if let Some(right) = node.right { self.cur.push(*right); } if let Some(left) = node.left { self.cur.push(*left); } Some(node.value) } }
  9. 52.

    ࢼ͠ʹา͜͏ fn main() { let tree = … let mut

    iter = tree.into_iter(); println!(“{:?}”, iter.next()); // Some(3) println!(“{:?}”, iter.next()); // Some(7) println!(“{:?}”, iter.next()); // Some(1) println!(“{:?}”, iter.next()); // Some(5) // … }
  10. 54.

    forจͱ࢖͏ fn main() { let tree = … for value

    in tree.into_iter() { println!("{}", value); } } ͜͜·Ͱͷ಺༰ͷRust Playground: https://play.rust-lang.org/? gist=f0ddf0692e9cf0217899c0354305cc5d&version=stable&mode=debug&edition=2015
  11. 55.

    step_by fn main() { let tree = … for value

    in tree.into_iter().step_by(2) { println!("{}", value); } }
  12. 56.

    map fn main() { let tree = … let dbl:

    Vec<i32> = tree .into_iter() .map(|x| x*2).collect(); println!(“{:?}”, dbl); }
  13. 59.
  14. 62.

    ίϯύΠϧ͢ΔͱΤϥʔ $ rustc main.rs error[E0072]: recursive type `BinaryTree` has infinite

    size --> src/main.rs:1:1 | 1 | struct BinaryTree { | ^^^^^^^^^^^^^^^^^ recursive type has infinite size 2 | left: Option<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable
  15. 63.
  16. 64.

    ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ rustc main.rs error[E0072]: recursive type `BinaryTree` has infinite

    size --> src/main.rs:1:1 | 1 | struct BinaryTree { | ^^^^^^^^^^^^^^^^^ recursive type has infinite size 2 | left: Option<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable
  17. 67.

    ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ rustc main.rs error[E0072]: recursive type `BinaryTree` has infinite

    size --> src/main.rs:1:1 | 1 | struct BinaryTree { | ^^^^^^^^^^^^^^^^^ recursive type has infinite size 2 | left: Option<BinaryTree>, | ------------------------ recursive without indirection 3 | right: Option<BinaryTree> | ------------------------- recursive without indirection | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `BinaryTree` representable