In this homework, the task is to develop code for balanced search trees (BST) and AVL trees in the Rust language. Use "cargo new --lib avl" to start a Rust library.
You must use the following data structure to represent trees.
/// An internal node of an `Tree`
#[derive(Debug)]
struct Node<T: Ord> {
value: T, // value in the node
height: usize, // height of the tree
left: Option<Box<Node<T>>>, // left subtree; None is the empty tree
right: Option<Box<Node<T>>>, // right subtree
}
#[derive(Debug)]
pub struct Tree<T: Ord> {
root: Option<Box<Node<T>>>, // root of the tree
length: usize, // the total number of nodes in the tree
}
In this part, we write code for balanced search trees (BST). You can read this webpage to refresh your memory about BST.
Implement the following BST operations:
impl<T: Ord> Tree<T> {
/// Creates an empty `Tree`.
pub fn new() -> Tree<T> { ... }
/// Returns the number of nodes in the tree.
pub fn len(&self) -> usize { ... }
/// Returns `true` if the tree contains no nodes.
pub fn is_empty(&self) -> bool { ... }
/// Adds a value to the tree
/// If the tree does not yet contain the value, return true;
/// otherwise, return false and no need to duplicate the value in the tree
pub fn insert(&mut self, value: T) -> bool { ... }
/// Returns `true` if the tree contains the value.
pub fn contains(&self, value: &T) -> bool { ... }
/// Returns the smallest value in the tree, if one exists.
pub fn min(&self) -> Option<&T> { ... }
/// Returns the biggest value in the tree, if one exists.
pub fn max(&self) -> Option<&T> { ... }
}
Tips:
The most difficult operation to implement is the insert operation; consider implementing other operations first.
For this part, the height field in Node is not useful. Your code can ignore it and set it to some constant value (e.g., 0).
You should not change the type signatures of the above operations. E.g., you should not require T implements the Clone trait.
Write a few unit tests to test the above BST operations; make sure every operation is covered in at least one unit test. Also wirte a couple of property-based tests. Before each test, write comments documenting the test.
Submit your main Rust file (lib.rs) through Canvas. The deadline is Apr 10th by midnight. Your code should be well-documented with comments before each function and other places you deem important.
Author: G. Gary Tan and Jialun Zhang, Penn State University, 2026. All rights reserved.