I coded a binary tree with basic functions: insert, display, search and clear. The clear() function isn't working properly. I want to delete all the nodes. So I modified the display function:
I remove the nodes in sort order (small -> big numbers). After I cleared the tree, I display() the tree again. It prints 1 rubbish number. Though the n->right and n->left should be NULL. I know the clear() function is wrong, but I don't know how to clear the tree properly.Code:// header files struct tree { int dat; struct tree *left, *right; } *root, *temp; void display(struct tree *n) { if(n->left) display(n->left); printf("%d\n", n->dat); if(n->right) display(n->right); } // Other functions void clear(struct tree *n) { if(n->left) clear(n->left); free(n); if(n->right) clear(n->right); n->left = NULL; n->right = NULL; }