Can't use rbapply() for rbdestroy since the destructor is passed a
data pointer, not a node pointer.
This commit is contained in:
29
redblack.c
29
redblack.c
@@ -35,6 +35,8 @@
|
|||||||
static void rbrepair __P((struct rbtree *, struct rbnode *));
|
static void rbrepair __P((struct rbtree *, struct rbnode *));
|
||||||
static void rotate_left __P((struct rbtree *, struct rbnode *));
|
static void rotate_left __P((struct rbtree *, struct rbnode *));
|
||||||
static void rotate_right __P((struct rbtree *, struct rbnode *));
|
static void rotate_right __P((struct rbtree *, struct rbnode *));
|
||||||
|
static void _rbdestroy __P((struct rbtree *, struct rbnode *,
|
||||||
|
void (*)(VOID *)));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Red-Black tree, see http://en.wikipedia.org/wiki/Red-black_tree
|
* Red-Black tree, see http://en.wikipedia.org/wiki/Red-black_tree
|
||||||
@@ -307,30 +309,33 @@ rbsuccessor(tree, node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Helper function for rbdestroy()
|
* Recursive portion of rbdestroy().
|
||||||
*/
|
*/
|
||||||
static int
|
static void
|
||||||
_rbdestroy(v1, v2)
|
_rbdestroy(tree, node, destroy)
|
||||||
VOID *v1, *v2;
|
struct rbtree *tree;
|
||||||
|
struct rbnode *node;
|
||||||
|
void (*destroy)__P((VOID *));
|
||||||
{
|
{
|
||||||
struct rbnode *node = (struct rbnode *) v1;
|
if (node != rbnil(tree)) {
|
||||||
void (*destroy)__P((VOID *)) = (void (*)__P((VOID *))) v2;
|
_rbdestroy(tree, node->left, destroy);
|
||||||
|
_rbdestroy(tree, node->right, destroy);
|
||||||
destroy(node);
|
if (destroy != NULL)
|
||||||
free(node);
|
destroy(node->data);
|
||||||
return(0);
|
free(node);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Destroy the specified tree, calling the destructor destroy
|
* Destroy the specified tree, calling the destructor destroy
|
||||||
* for each node and then freeing it.
|
* for each node and then freeing the tree itself.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
rbdestroy(tree, destroy)
|
rbdestroy(tree, destroy)
|
||||||
struct rbtree *tree;
|
struct rbtree *tree;
|
||||||
void (*destroy)__P((VOID *));
|
void (*destroy)__P((VOID *));
|
||||||
{
|
{
|
||||||
rbapply(tree, _rbdestroy, (VOID *)destroy, postorder);
|
_rbdestroy(tree, rbfirst(tree), destroy);
|
||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user