diff --git a/src/main.c b/src/main.c index b55d6c9..a86ee0f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,174 @@ -#include "greet.h" #include +#include + +#include "greet.h" + +struct Node { + int data; + struct Node *left, *right; +}; +typedef struct Node Node; + +struct Tree { + Node *root; +}; +typedef struct Tree Tree; + +int tree_insert(Tree *tree, int data) { + Node *node = malloc(sizeof(Node)); + node->data = data; + node->left = NULL; + node->right = NULL; + + if (tree->root == NULL) { + tree->root = node; + return 0; + } + + Node *current = tree->root; + while (current != NULL) { + if (data < current->data) { + if (current->left == NULL) { + current->left = node; + return 0; + } + current = current->left; + } else { + if (current->right == NULL) { + current->right = node; + return 0; + } + current = current->right; + } + } + return 1; +} + +int tree_remove(Tree *tree, int data) { + if (tree->root == NULL) { + return 1; + } + + Node *current = tree->root; + Node *parent = NULL; + while (current != NULL) { + if (data < current->data) { + parent = current; + current = current->left; + } else if (data > current->data) { + parent = current; + current = current->right; + } else { + if (current->left == NULL && current->right == NULL) { + if (parent == NULL) { + tree->root = NULL; + } else if (parent->left == current) { + parent->left = NULL; + } else { + parent->right = NULL; + } + free(current); + return 0; + } else if (current->left == NULL) { + if (parent == NULL) { + tree->root = current->right; + } else if (parent->left == current) { + parent->left = current->right; + } else { + parent->right = current->right; + } + free(current); + return 0; + } else if (current->right == NULL) { + if (parent == NULL) { + tree->root = current->left; + } else if (parent->left == current) { + parent->left = current->left; + } else { + parent->right = current->left; + } + free(current); + return 0; + } else { + Node *successor = current->right; + Node *successor_parent = current; + while (successor->left != NULL) { + successor_parent = successor; + successor = successor->left; + } + current->data = successor->data; + if (successor_parent->left == successor) { + successor_parent->left = successor->right; + } else { + successor_parent->right = successor->right; + } + free(successor); + return 0; + } + } + } + return 1; +} + +int tree_clear(Tree *tree) { + if (tree->root == NULL) { + return 0; + } + + Node *current = tree->root; + while (current != NULL) { + if (current->left != NULL) { + current = current->left; + } else if (current->right != NULL) { + current = current->right; + } else { + Node *parent = current; + current = NULL; + if (parent == tree->root) { + tree->root = NULL; + } else if (parent->left != NULL) { + parent->left = NULL; + } else { + parent->right = NULL; + } + free(parent); + } + } + return 0; +} + +int tree_size(Tree *tree) { + if (tree->root == 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; +} int main(void) { - greet("Template"); - return 0; + Tree tree = {NULL}; + printf("Tree size: %d\n", tree_size(&tree)); + tree_insert(&tree, 1); + tree_insert(&tree, 2); + tree_insert(&tree, 3); + printf("Tree size: %d\n", tree_size(&tree)); + tree_remove(&tree, 2); + printf("Tree size: %d\n", tree_size(&tree)); + tree_clear(&tree); + printf("Tree size: %d\n", tree_size(&tree)); + + greet("Template"); + return 0; } \ No newline at end of file