Exercise: A phylogenetic tree with recursion

Goal

Define a recursive enum that represents a phylogenetic tree, and write three recursive functions over it:

enum Node {
    Leaf { name: String, branch_length: f64 },
    Internal { branch_length: f64, children: Vec<Node> },
}

fn count_tips(node: &Node) -> usize;
fn max_depth(node: &Node) -> f64;
fn total_branch_length(node: &Node) -> f64;

This is your first encounter with recursive data and recursive functions in Rust — the natural shape for tree-structured biology (phylogenies, taxonomies, gene trees, lineage trees in single-cell, …).

Background

A phylogenetic tree is hierarchical: every node has a parent (except the root) and zero or more children. A node with no children is a tip (or leaf) and is labelled with a taxon name. Every edge has a numeric branch length, typically expected substitutions per site.

The most natural Rust representation uses an enum with one variant per kind of node. Children of an internal node live in a Vec, which already provides heap indirection — so we do not need Box<Node> here, even though Node is recursive. (We would need Box if a field were a bare Node rather than Vec<Node>, because a Node whose own fields include another Node would have unknown size on the stack.)

A figure with three panels showing the same 3-leaf rooted tree in three notations. Panel 1 draws the tree with horizontal edges: an internal node N1 branches to leaf C with length 0.5 and to internal node N2 with length 0.3, and N2 branches to leaf A with length 0.1 and leaf B with length 0.2. Panel 2 shows the Newick string ((A:0.1,B:0.2):0.3,C:0.5);. Panel 3 shows the equivalent Rust value as nested Node::Internal and Node::Leaf constructors. In all three panels the (A,B) subtree — the inner parenthesis pair, the inner Internal block, and the N2 subtree of the drawing — is highlighted with the same orange tint.
Figure 1: The same tree, three notations: the recursive enum value mirrors the parenthesisation of the Newick string, which mirrors the topology of the picture. The (A, B) clade is highlighted in orange across all three panels so you can see how a single subtree corresponds to a single inner parenthesis pair and a single nested Node::Internal { ... } block.

A worked example

The tree ((A:0.1, B:0.2):0.3, C:0.5); (Newick notation) has:

  • Three tips: A, B, C
  • Root has two children: an internal node (branch 0.3, parents of A and B) and the tip C (branch 0.5)
  • count_tips = 3
  • max_depth (from root to farthest tip) = 0.3 + 0.2 = 0.5 (or 0.5 via the C branch — same value here, coincidentally)
  • total_branch_length = 0.1 + 0.2 + 0.3 + 0.5 = 1.1

The exercise

cd day3/ex-phylo-tree

Implement Node, count_tips, max_depth, and total_branch_length. Make all eight tests pass.

The starter file already defines two small helpers — leaf(name, bl) and internal(bl, children) — used by the tests to build trees concisely. Do not change them.

How to run

cargo test
cargo run    # prints stats for a built-in sample tree

Hints

The enum has two variants with named fields. Derive Debug and Clone:

#[derive(Debug, Clone)]
enum Node {
    Leaf {
        name: String,
        branch_length: f64,
    },
    Internal {
        branch_length: f64,
        children: Vec<Node>,
    },
}

Why no Box? Because Vec<Node> already stores the children on the heap. The Internal variant’s size on the stack is just three pointers (the Vec header) plus an f64 plus the enum tag — fixed and known.

Inside match you destructure each variant’s fields. Use .. to ignore the fields you do not care about:

match node {
    Node::Leaf { .. } => 1,
    Node::Internal { children, .. } => {
        // children: &Vec<Node>
    }
}

A leaf is one tip; an internal node has as many tips as the sum of its children. The function is recursive:

fn count_tips(node: &Node) -> usize {
    match node {
        Node::Leaf { .. } => 1,
        Node::Internal { children, .. } => {
            let mut total = 0;
            for child in children {
                total += count_tips(child);
            }
            total
        }
    }
}

(On a later pass you can replace the inner loop with children.iter().map(count_tips).sum(), which uses Iterator::sum — same answer, more idiomatic.)

Depth from this node to the farthest descendant. For a leaf, that is just its own branch length. For an internal node, it is its own branch length plus the maximum of the children’s depths:

fn max_depth(node: &Node) -> f64 {
    match node {
        Node::Leaf { branch_length, .. } => *branch_length,
        Node::Internal { branch_length, children } => {
            let mut deepest: f64 = 0.0;
            for child in children {
                let d = max_depth(child);
                if d > deepest {
                    deepest = d;
                }
            }
            branch_length + deepest
        }
    }
}

Note *branch_length to dereference the &f64 we get from pattern-matching on &Node.

Sum every branch length in the tree:

fn total_branch_length(node: &Node) -> f64 {
    match node {
        Node::Leaf { branch_length, .. } => *branch_length,
        Node::Internal { branch_length, children } => {
            let mut total = *branch_length;
            for child in children {
                total += total_branch_length(child);
            }
            total
        }
    }
}

Solution

See day3/ex-phylo-tree-solution/src/main.rs for the complete reference, including the leaf and internal helpers used by the tests.

The three function bodies are exactly the ones in the hints above.

Where this leads

  • Day 4 (optional): parse a real Newick string into a Node tree. Newick parsing has subtleties (nested parentheses, quoting) but is a fun puzzle.
  • The most recent common ancestor of two tips — a classic recursive tree operation — would be the natural next function. We will leave it as a homework challenge for students who finish early.
  • For production phylogenetics work in Rust, the light_phylogeny crate exists; the larger ecosystem is still maturing.