Write the following functions and add them to the genBSTh fi
Write the following functions and add them to the genBST.h file supplied in the download link below.
http://s000.tinyupload.com/index.php?file_id=86544375185714297666
a) A function to count the number of nodes in a binary tree.
b) A function to count the number of leaves.
c) A function to find the height of the tree.
Solution
a)
int count(struct tnode *p)
{
if( p == NULL)
return(0);
else
if( p->lchild == NULL && p->rchild == NULL)
return(1);
else
return(1 + (count(p->lchild) + count(p->rchild)));
}
b)
void count_leaf(bt * list)
{
if (list == NULL)
{
return;
}
if (list->l == NULL && list->r == NULL)
{
count++;
}
count_leaf(list->l);
count_leaf(list->r);
}
c)
int maxDepth(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}

