btree

a balk tree implementation ;-)
Log | Files | Refs | LICENSE

commit b57e2c047c15d4265c07b44a4d731a815e03e90f
parent f5ff2b2337e0f2913f8ddaf3da7440f2e8c3a8a4
Author: Matthias Balk <mbalk@mbalk.de>
Date:   Fri,  4 Jun 2021 21:52:09 +0200

Bugfix: free data of all nodes (not just root) if requested

Diffstat:
MLICENSE | 2+-
Mbtree.c | 16+++++-----------
Mbtree.h | 5++---
Mbtreetest.c | 4++--
Mgencoupons.c | 4++--
5 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2020 Matthias Balk +Copyright 2020, 2021 Matthias Balk Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/btree.c b/btree.c @@ -1,6 +1,6 @@ /** * btree -- a balk tree implementation ;-) - * Copyright 2020 Matthias Balk + * Copyright 2020, 2021 Matthias Balk */ #include <stdio.h> @@ -35,29 +35,23 @@ node_t* bt_new() } -void bt_free(node_t *tree) +void bt_free(node_t *tree, u_int8_t free_data) { if (tree->lchild != NULL) { - bt_free(tree->lchild); + bt_free(tree->lchild, free_data); } if (tree->rchild != NULL) { - bt_free(tree->rchild); + bt_free(tree->rchild, free_data); } + if (free_data) free(tree->data); free(tree); } -void bt_free_incl_data(node_t *tree) -{ - free(tree->data); - bt_free(tree); -} - - u_int8_t bt_add(node_t *tree, char *data) { if (tree->data == NULL) diff --git a/btree.h b/btree.h @@ -1,6 +1,6 @@ /** * btree -- a balk tree implementation ;-) - * Copyright 2020 Matthias Balk + * Copyright 2020, 2021 Matthias Balk */ #ifndef BTREE_H @@ -17,8 +17,7 @@ typedef struct node node_t* bt_new(); -void bt_free(node_t *tree); -void bt_free_incl_data(node_t *tree); +void bt_free(node_t *tree, u_int8_t free_data); u_int8_t bt_add(node_t *tree, char *data); u_int8_t bt_contains(node_t *tree, const char *data); diff --git a/btreetest.c b/btreetest.c @@ -1,6 +1,6 @@ /** * btree -- a balk tree implementation ;-) - * Copyright 2020 Matthias Balk + * Copyright 2020, 2021 Matthias Balk */ #include "btree.h" @@ -32,7 +32,7 @@ int main(int argc, char **argv) bt_print_sorted(root); - bt_free(root); + bt_free(root, 0); return EXIT_SUCCESS; } diff --git a/gencoupons.c b/gencoupons.c @@ -1,6 +1,6 @@ /** * btree -- a balk tree implementation ;-) - * Copyright 2020 Matthias Balk + * Copyright 2020, 2021 Matthias Balk * * Example program that generates random codes and uses `btree` to store and * sort them. @@ -58,7 +58,7 @@ int main(int argc, char **argv) bt_print_sorted(root); printf("\nDuplicates: %d\n", duplicate_cnt); - bt_free_incl_data(root); + bt_free(root, 1); return EXIT_SUCCESS; }