www.delorie.com/gnu/docs/avl/libavl_147.html   search  
 
Buy GNU books!


GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

8.10 Destruction

Destroying a threaded binary tree is easy. We can simply traverse the tree in inorder in the usual way. We always have a way to get to the next node without having to go back up to any of the nodes we've already destroyed. (We do, however, have to make sure to go find the next node before destroying the current one, in order to avoid reading data from freed memory.) Here's all it takes:

 
void 
tbst_destroy (struct tbst_table *tree, tbst_item_func *destroy)
{ struct tbst_node *p; /* Current node. */ struct tbst_node *n; /* Next node. */ p = tree-&#62;tbst_root; if (p != NULL) while (p-&#62;tbst_tag[0] == TBST_CHILD) p = p-&#62;tbst_link[0]; while (p != NULL)
{ n = p-&#62;tbst_link[1]; if (p-&#62;tbst_tag[1] == TBST_CHILD) while (n-&#62;tbst_tag[0] == TBST_CHILD) n = n-&#62;tbst_link[0]; if (destroy != NULL && p-&#62;tbst_data != NULL) destroy (p-&#62;tbst_data, tree-&#62;tbst_param); tree-&#62;tbst_alloc-&#62;libavl_free (tree-&#62;tbst_alloc, p); p = n; } tree-&#62;tbst_alloc-&#62;libavl_free (tree-&#62;tbst_alloc, tree); }
This code is included in @refalso{251


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003