diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-28 23:56:44 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-03-29 00:22:55 +1300 |
commit | 244b02c76bce50a87de5540486df5535416c6b4d (patch) | |
tree | cdee7dd751f5d9e3a76d00770e3fcaa3f992ccd9 | |
parent | Tinkering with simple binary trees (diff) | |
download | btree-244b02c76bce50a87de5540486df5535416c6b4d.tar.gz btree-244b02c76bce50a87de5540486df5535416c6b4d.zip |
Add free() func
-rw-r--r-- | btree-int.c | 14 | ||||
-rw-r--r-- | btree-str.c | 15 |
2 files changed, 29 insertions, 0 deletions
diff --git a/btree-int.c b/btree-int.c index 8935760..7dbdf90 100644 --- a/btree-int.c +++ b/btree-int.c @@ -9,6 +9,7 @@ typedef struct Node { void an(Node **, Node *); void pn(Node *); +void fn(Node *); void an(Node **r, Node *n) { if (!*r) { @@ -37,6 +38,17 @@ void pn(Node *n) { return; } +void fn(Node *n) { + if (!n) + return; + if (n->l) + fn(n->l); + if (n->r) + fn(n->r); + free(n); + return; +} + int main(int argc, char **argv) { Node *r = NULL; @@ -49,5 +61,7 @@ int main(int argc, char **argv) { pn(r); + fn(r); + exit(EXIT_SUCCESS); } diff --git a/btree-str.c b/btree-str.c index 3f080a4..539f03a 100644 --- a/btree-str.c +++ b/btree-str.c @@ -10,6 +10,7 @@ typedef struct Node { void an(Node **, Node *); void pn(Node *); +void fn(Node *); void an(Node **r, Node *n) { if (!*r) { @@ -38,6 +39,18 @@ void pn(Node *n) { return; } +void fn(Node *n) { + if (!n) { + return; + } + if (n->l) + fn(n->l); + if (n->r) + fn(n->r); + free(n); + return; +} + int main(int argc, char **argv) { Node *r = NULL; @@ -50,5 +63,7 @@ int main(int argc, char **argv) { pn(r); + fn(r); + exit(EXIT_SUCCESS); } |