diff --git a/src/main.c b/src/main.c index 50db989..2f57fdb 100644 --- a/src/main.c +++ b/src/main.c @@ -10,5 +10,7 @@ int main(void) { tree_insert(&tree, 2); tree_insert(&tree, 3); printf("Tree size: %d\n", tree_size(tree.root)); + printf("Tree contains 2: %d\n", tree_contains(tree.root, 2)); + // tree_clear(&tree.root); // Something is fishy here return 0; } diff --git a/src/tree.c b/src/tree.c index 69c6ea2..b9e683d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -45,9 +45,34 @@ int tree_insert(Tree *tree, int data) { } } -int tree_remove(Tree *tree, int data) { return 1; } +bool tree_contains(Node *node, int data) { + if (node == NULL) + return false; -int tree_clear(Tree *tree) { return 1; } + if (node->data == data) + return true; + + return tree_contains(node->left, data) || tree_contains(node->right, data); +} + +int tree_remove(Node **node, int data) { + if (node == NULL) + return false; + // TODO +} + +int tree_clear(Node *node) { + if (node == NULL) + return 0; + + if (node->left != NULL) + tree_clear(node->left); + if (node->right != NULL) + tree_clear(node->right); + + free(node); + return 0; +} int tree_size(Node *node) { if (node == NULL) { diff --git a/src/tree.h b/src/tree.h index 9352dac..54d3cfa 100644 --- a/src/tree.h +++ b/src/tree.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -17,6 +18,7 @@ struct Tree { typedef struct Tree Tree; int tree_insert(Tree *tree, int data); -int tree_remove(Tree *tree, int data); -int tree_clear(Tree *tree); +int tree_remove(Node **node, int data); +int tree_clear(Node *node); int tree_size(Node *node); +bool tree_contains(Node *node, int data);