Sunday, September 25, 2011

Function to count the number of terminal(leaf) nodes in a binary tree.

The function "terminal_count()" counts the number of leaf nodes of a binary tree.

int terminal_count(struct node *t)
{
  static int count = 0;
  if((t -> Llink == NULL) && (t -> Rlink == NULL))
    return 1;
  else
    count = count + terminal_count(t -> Llink) + terminal_count(t -> Rlink);
  return count;
}

Written by

Height of a Binary Tree

Function to find the height of a binary tree


int height(struct node *t)
{
  if(t == NULL)
    return 0;
  int left = height(t -> Llink);
  int right = height(t -> Rlink);
  if(left > right)
    return left + 1;
  else 
    return right + 1; 
  }
}

Written by