How to count the nodes on a binary tree?

585 Views Asked by At

I need to give an inductive definition of the function $nodecount(t)$, which will determine the number of internal nodes in a binary tree $t$.

I understand the concept of both inductive definitions, and binary trees, but don't understand how that can be combined to give a definition of a function that will traverse the tree while counting the nodes - it just makes me think of a programming related solution (i.e. if current is a leaf then stop searching that branch, else continue searching and increment the count value).

How should I approach such a problem using purely inductive definitions?

1

There are 1 best solutions below

6
On

EDIT: Count internal nodes instead of nodes.

The internal node count is 1 + the internal node count of the left subtree + the internal node count of the right subtree. The basis is that the node count of a node with no child is $0$.