Worked fine (without crashing that is) for me after correcting one item that prevented me from compiling:
Code:
node* newNode(int data) { //create a new node
node* node = malloc(sizeof(node));
node->data = data;
node->right = NULL;
node->left = NULL;
return node;
}
error C2061: syntax error : identifier 'node'
That was just because the name of the variable and the type were the same and it was confusing the compiler. I just renamed the variable in that case.
Some nits:
#1. Uninitialized variable "choice"
Code:
int data,choice;
char ans;
node* n = NULL;
printf("Binary Trees-Main menu");
printf("\n1.Insert data\n2.Print tree\n3.Get size of tree\n4.Get max depth of tree\n5.Quit\n");
while(choice != 5){
#2 Error message when selecting option 5 "quit"
#3 MaxDepth function does not return proper result due to some missing bits of code:
Code:
else
{
ldepth = maxdepth(n->left);
rdepth = maxdepth(n->right);
Should probably be:
Code:
else
{
ldepth = 1 + maxdepth(n->left);
rdepth = 1 + maxdepth(n->right);
#4 At some point before the program ends you should think about taking care of the memory you've allocated for the nodes.