I need to write a routine that computes the maximum depth of the tree and run it after each insert and print out the depth. I've tried writing the code but it doesn't print out the depth! Can anyone help??
Code:
node *insert(int value, node *n)
{
if (n == NULL)
{
return mkleaf(value) ;
}
if (value <= n -> contents )
{
n -> left = insert(value, n -> left) ;
}
else
{
n -> right = insert(value, n -> right) ;
}
return n ;
}
int printnode(node *n)
{
printf("node at %p value=%d left=%p right=%p\n", n, n -> contents, n -> left, n -> right) ;
}
node *mknode(int value, node *left, node *right)
{
node *res = (node *)malloc(sizeof (node)) ;
res -> contents = value ;
res -> left = left ;
res -> right = right ;
return res ;
}
node *mkleaf(int value)
{
return mknode (value, NULL, NULL) ;
}
int preorder(node *n)
{
if (n == NULL) return ;
preorder (n -> left) ;
printf ("%d ", n -> contents) ;
preorder (n -> right) ;
}