diff --git a/src/main.c b/src/main.c index 26f0092..50db989 100644 --- a/src/main.c +++ b/src/main.c @@ -5,10 +5,10 @@ int main(void) { Tree tree = {NULL}; - printf("Tree size: %d\n", tree_size(&tree)); + printf("Tree size: %d\n", tree_size(tree.root)); tree_insert(&tree, 1); tree_insert(&tree, 2); tree_insert(&tree, 3); - printf("Tree size: %d\n", tree_size(&tree)); + printf("Tree size: %d\n", tree_size(tree.root)); return 0; } diff --git a/src/tree.c b/src/tree.c index 31c02bc..69c6ea2 100644 --- a/src/tree.c +++ b/src/tree.c @@ -49,22 +49,10 @@ int tree_remove(Tree *tree, int data) { return 1; } int tree_clear(Tree *tree) { return 1; } -int tree_size(Tree *tree) { - if (tree->root == NULL) { +int tree_size(Node *node) { + if (node == NULL) { return 0; } - int size = 0; - Node *current = tree->root; - while (current != NULL) { - size++; - if (current->left != NULL) { - current = current->left; - } else if (current->right != NULL) { - current = current->right; - } else { - current = NULL; - } - } - return size; + return 1 + tree_size(node->left) + tree_size(node->right); } diff --git a/src/tree.h b/src/tree.h index 5bba666..9352dac 100644 --- a/src/tree.h +++ b/src/tree.h @@ -19,4 +19,4 @@ 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_size(Tree *tree); +int tree_size(Node *node);