620

# 木の上を歩こう

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

September 09, 2018

## Transcript

4. None

35. ### ೋ෼໦ͷදݱ struct BinaryTree { left: Option<Box<BinaryTree>>, right: Option<Box<BinaryTree>> } ώʔϓ΁ͷϙΠϯλ

pub enum Option<T> { None, Some(T), }

}
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)) } } }
38. ### ؆୯ʹ༿Λ࡞ΕΔΑ͏ʹ impl BinaryTree { fn leaf(value: i32) -> Self {

BinaryTree { value, left: None, right: None } } }
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

41. ### Iterator τϨΠτ trait Iterator { type Item; fn next(&mut self)

-> Option<Self::Item>; }

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

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

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

BinaryTreeIterator { type Item = i32; fn next(&mut self) -> Option<i32> { // ͜͜Λ࣮૷͢Δ } }
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) } }
51. ### BinaryTreeIteratorΛ࡞Δ impl BinaryTree { fn into_iter(self) -> BinaryTreeIterator { BinaryTreeIterator

{ cur: vec![self] } } }
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) // … }

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
55. ### step_by fn main() { let tree = … for value

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

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

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

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

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