Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proof by induction on binary trees

Tags:

algorithm

tree

Right now I'm currently working on a problem in my algorithm design textbook, and I've hit a bit of a brick wall.

The problem is:

A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree that the number of nodes with two children is exactly one less than the number of leaves.

I'm reasonably certain of how to do this: the base case has a single node, which means that the tree has one leaf and zero nodes with two children. However, I'm not quite certain exactly what the inductive step would entail.

like image 812
user2309750 Avatar asked Mar 06 '14 20:03

user2309750


2 Answers

A tree with a single node with no children (obviously), has/is one leaf. The number of nodes with two children (0) is exactly one less than the number of leaves (1).

Adding a node to an existing node that has no children, does not change the number of nodes with two children, nor the number of leaves.

Adding a node to an existing node that has one child, increases the number of nodes with two children by 1, and also increases the number of leaves by 1.

You cannot add a node to an existing node with any other number of children.

Since the number of nodes with two children starts as exactly one less than the number of leaves, and adding a node to the tree either changes neither number, or increases both by exactly one, then the difference between them will always be exactly one.

like image 118
Mooing Duck Avatar answered Nov 16 '22 15:11

Mooing Duck


With induction on the number of nodes with 2 children :

Base - 0 nodes with 2 children - 1 leaf (assuming the root is not counted as one). Step - Let T be a tree with n+1 > 0 nodes with 2 children

=> there is a node a with 2 children a1, a2 and in the subtree rooted in a1 or a2 there are no nodes with 2 children. we can assume it's the subtree rooted in a1.

=> remove the subtree rooted in a1, we got a tree T' with n nodes with 2 children.

=> T' has n+1 leaves.

=> add the subtree rooted in a1 to T' - we added one leaf and one node with 2 children.

There are some holes that you need to complete, but it's working. Sorry for my bad english.

like image 27
TomerPld Avatar answered Nov 16 '22 15:11

TomerPld