4.11.2 Aside: Recursive Destruction [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

The algorithm used in the previous section is easy and fast, but it is not the most common method for destroying a tree. The usual way is to perform a traversal of the tree, in much the same way we did for tree traversal and copying. Once again, we'll start from a recursive implementation, because these are so easy to write. The only tricky part is that subtrees have to be freed before the root. This code is hard-wired to use free() for simplicity:

85. <Destroy a BST recursively 85> =
static void 
bst_destroy_recursive (struct bst_node *node)
{ if (node == NULL) return; bst_destroy_recursive (node->bst_link[0]); bst_destroy_recursive (node->bst_link[1]); free (node); }