-1

I have been given this induction problem as practice, and I haven't worked with induction in a few months so I'm lost on how to approach the problem. Here's the example:

"Prove by induction: In a non-empty binary tree, the number of nodes is equal to the number of links between the nodes plus one".

I have a basic idea of how to start it, like how the base case is 1 node with 0 links. However, I'm not sure how to represent "links" in my solution. Any help is greatly appreciated.

Evan
  • 33
  • 1
  • 4

1 Answers1

0

I'd start by inductively defining a nonempty binary tree.

Inductive nonemptyTree :=
    | Endpoint 
    | Single (l : nonemptyTree)
    | Double (l : nonemptyTree) (r : nonemptyTree).

This looks a bit more complicated than a normal binary tree, but clearly distinguishing the different formations and removing the typical "Nil" constructor help with the proof.

From there, you'll want to define what "links" and "nodes" are and how to calculate them given a tree. This is pretty easy given the nonemptyTree definition.

Fixpoint links (t : nonemptyTree) : nat :=
    match t with
    | Endpoint => 0
    | Single l => 1 + links l
    | Double l r => 2 + links l + links r
    end.

Fixpoint nodes (t : nonemptyTree ) : nat :=
    match t with
    | Endpoint => 1
    | Single l => 1 + nodes l
    | Double l r => 1 + nodes l + nodes r
    end.

At that point, we just need to prove that for all trees t, nodes t = links t + 1. There are only three cases to consider, and you've already gotten the first one (base case).

Jackson
  • 559
  • 7
  • 20