# Thread: deleting all nodes of a binary search tree...

1. ## deleting all nodes of a binary search tree...

to delete all the nodes of a binary search tree (using the 'post order traversal") i have used the following coding...
even though it does delete all the nodes of the binary search tree, it gives an error at the end...
so i want to know if my coding is correct..

void tree::deleteAll(Node* localRoot)
{
if(localRoot !=NULL)
{
deleteAll(localRoot->leftChild);
deleteAll(localRoot->rightChild);
delete localRoot;
}
} 2. what is the error that you are getting?

edit:: you should also check if localRoot->(left or right)child is a null, before deleting it. 3. this is a pretty standard implementation of a recursive freeing of a BST:
Code:
```freeBST( Node* t ) //get root
{
if( t == NULL )
return;
if( t->leftChild != NULL )
freeBST( t->leftChild );
if( t->rightChild != NULL )
freeBST( t->rightChild );

delete t; 		/* free(t) if c */

return;
}``` 4. >edit:: you should also check if localRoot->(left or right)child is a null, before deleting it.
He does, that is the base case for the recursion.

>this is a pretty standard implementation of a recursive freeing of a BST
Umm...not really.

>freeBST( Node* t ) //get root
You forgot the return value, and the comment is just obscure.

>if( t->leftChild != NULL )
>freeBST( t->leftChild );
There's no need for this test if you return when t is a null pointer.

>if( t->rightChild != NULL )
>freeBST( t->rightChild );
Ditto

>delete t; /* free(t) if c */
Free t if c? What is that supposed to mean?

Recursive post order traversal usually takes one of these two forms:
Code:
```void delete_all ( struct node *tree )
{
if ( tree == NULL )
return;

delete_all ( tree->left );
delete_all ( tree->right );
delete tree;
}```
Code:
```void delete_all ( struct node *tree )
{
if ( tree != NULL ) {
delete_all ( tree->left );
delete_all ( tree->right );
delete tree;
}
}```
The OP's code conforms to the second, so the function is correct and we can conclude that the problem is elsewhere.  Popular pages Recent additions 