Consider the following binary tree structure and code that calculates its height. What will be the printed height?
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn height(&self) -> i32 {
let left_height = match &self.left {
Some(left_node) => left_node.height(),
None => 0,
};
let right_height = match &self.right {
Some(right_node) => right_node.height(),
None => 0,
};
1 + std::cmp::max(left_height, right_height)
}
}
fn main() {
let tree = Node {
value: 1,
left: Some(Box::new(Node {
value: 2,
left: Some(Box::new(Node {
value: 4,
left: None,
right: None,
})),
right: None,
})),
right: Some(Box::new(Node {
value: 3,
left: None,
right: Some(Box::new(Node {
value: 5,
left: None,
right: None,
})),
})),
};
println!("{}", tree.height());
}Height is the number of nodes on the longest path from root to leaf.
The longest path is from root (1) to leaf (4) or (5), both paths have length 3 nodes.
Given a binary tree with only one node, what will the height function print?
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn height(&self) -> i32 {
let left_height = match &self.left {
Some(left_node) => left_node.height(),
None => 0,
};
let right_height = match &self.right {
Some(right_node) => right_node.height(),
None => 0,
};
1 + std::cmp::max(left_height, right_height)
}
}
fn main() {
let tree = Node {
value: 10,
left: None,
right: None,
};
println!("{}", tree.height());
}Think about the height of a tree with only one node.
A single node tree has height 1 because it counts itself.
Examine the following Rust code for height calculation. Why does it cause a stack overflow error?
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn height(&self) -> i32 {
let left_height = match &self.left {
Some(left_node) => self.height(),
None => 0,
};
let right_height = match &self.right {
Some(right_node) => self.height(),
None => 0,
};
1 + std::cmp::max(left_height, right_height)
}
}Look carefully at the recursive calls inside the match arms.
Inside the match, it should call height on left_node and right_node, but it calls self.height() repeatedly causing infinite recursion.
Given a binary tree with n nodes, what is the time complexity of the recursive height function?
Consider how many nodes the function visits.
The function visits every node once to calculate height, so it is linear time O(n).
Consider inserting nodes in this order into an empty binary tree: 10, 5, 1, 7, 40, 50. What is the height of the resulting tree?
struct Node {
value: i32,
left: Option<Box<Node>>,
right: Option<Box<Node>>,
}
impl Node {
fn insert(&mut self, val: i32) {
if val < self.value {
match &mut self.left {
Some(left_node) => left_node.insert(val),
None => self.left = Some(Box::new(Node { value: val, left: None, right: None })),
}
} else {
match &mut self.right {
Some(right_node) => right_node.insert(val),
None => self.right = Some(Box::new(Node { value: val, left: None, right: None })),
}
}
}
fn height(&self) -> i32 {
let left_height = match &self.left {
Some(left_node) => left_node.height(),
None => 0,
};
let right_height = match &self.right {
Some(right_node) => right_node.height(),
None => 0,
};
1 + std::cmp::max(left_height, right_height)
}
}
fn main() {
let mut root = Node { value: 10, left: None, right: None };
root.insert(5);
root.insert(1);
root.insert(7);
root.insert(40);
root.insert(50);
println!("{}", root.height());
}Draw the tree after all insertions and count the longest path.
The tree is unbalanced with longest path 10 -> 5 -> 1 or 10 -> 40 -> 50, both paths have length 3 nodes.
