From 408f00bb05fbba24e6931c2363c5bdf07be2d902 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 10 May 2023 23:23:24 -0400 Subject: [PATCH] rbtree: add `rbt_destroy` This function will free all the nodes inside the red-black tree. It is still on the caller to free the struct rbtree that they passed into the function if it was dynamically allocated --- lib/rbtree.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/rbtree.c b/lib/rbtree.c index bc8d318..c6f3b66 100644 --- a/lib/rbtree.c +++ b/lib/rbtree.c @@ -272,6 +272,17 @@ void rbt_delete(struct rbtree *tree, struct rbnode *del) } } +void rbt_destroy(struct rbtree *tree, struct rbnode *node) +{ + if (node == NULL) + return; + + rbt_destroy(tree, node->left); + rbt_destroy(tree, node->right); + + rbt_delete(tree, node); +} + struct rbnode *rbt_search(struct rbtree *tree, uint64_t key) { struct rbnode *node = tree->root; @@ -288,4 +299,3 @@ struct rbnode *rbt_search(struct rbtree *tree, uint64_t key) return NULL; } -