Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::max;
- trait TreeLike {
- fn height(&self) -> i32;
- }
- struct Tree {
- left: Option<Box<Tree>>,
- right: Option<Box<Tree>>,
- value: i32
- }
- impl TreeLike for Tree {
- fn height(&self) -> i32 {
- 1 + max(self.left.height(), self.right.height())
- }
- }
- impl TreeLike for Option<Box<Tree>> {
- fn height(&self) -> i32 {
- match self {
- Some(ref tree) => tree.height(),
- None => 0,
- }
- }
- }
- fn main() {
- let child = Tree {
- left: None,
- right: None,
- value: 3,
- };
- let parent = Tree {
- left: Some(Box::new(child)),
- right: None,
- value: 2,
- };
- let a = parent.height().to_string();
- println!("{0}", a);
- }
Add Comment
Please, Sign In to add comment