Upgrade to Pro — share decks privately, control downloads, hide ads and more …

木の上を歩こう

hama_du
September 09, 2018

 木の上を歩こう

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

hama_du

September 09, 2018
Tweet

More Decks by hama_du

Other Decks in Technology

Transcript

  1. ίϯετϥΫλ 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)) } } }
  2. ؆୯ʹ༿Λ࡞ΕΔΑ͏ʹ impl BinaryTree { fn leaf(value: i32) -> Self {

    BinaryTree { value, left: None, right: None } } }
  3. ࢼ͠ʹग़ྗ 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
  4. Iterator Λ࣮૷͢Δ…ʁ impl Iterator for BinaryTree { type Item =

    i32; fn next(&mut self) -> Option<i32> { // Ͳ͏͠Α͏ɾɾɾʁ } }
  5. BinaryTreeIterator struct BinaryTreeIterator { cur: Vec<BinaryTree> } impl Iterator for

    BinaryTreeIterator { type Item = i32; fn next(&mut self) -> Option<i32> { // ͜͜Λ࣮૷͢Δ } }
  6. 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) } }
  7. ࢼ͠ʹา͜͏ 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) // … }
  8. 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
  9. step_by fn main() { let tree = … for value

    in tree.into_iter().step_by(2) { println!("{}", value); } }
  10. map fn main() { let tree = … let dbl:

    Vec<i32> = tree .into_iter() .map(|x| x*2).collect(); println!(“{:?}”, dbl); }
  11. ίϯύΠϧ͢ΔͱΤϥʔ $ 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
  12. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ 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
  13. ΤϥʔϝοηʔδΛΑ͘ಡ΋͏ $ 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