From cabe3c579a5d92bc0cd2e4b6c38aa3a6d4874150 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 27 Mar 2024 06:33:46 +0100 Subject: [PATCH] Removed unused junk --- src/fib.c | 7 --- src/fib.h | 3 - src/greet.c | 8 --- src/greet.h | 3 - src/main.c | 164 +--------------------------------------------------- src/tree.c | 73 +++++++++++++++++++++++ src/tree.h | 22 +++++++ 7 files changed, 97 insertions(+), 183 deletions(-) delete mode 100644 src/fib.c delete mode 100644 src/fib.h delete mode 100644 src/greet.c delete mode 100644 src/greet.h create mode 100644 src/tree.c create mode 100644 src/tree.h diff --git a/src/fib.c b/src/fib.c deleted file mode 100644 index 8170a9b..0000000 --- a/src/fib.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "fib.h" - -int fib(int n) { - if (n < 2) - return n; - return fib(n - 1) + fib(n - 2); -} \ No newline at end of file diff --git a/src/fib.h b/src/fib.h deleted file mode 100644 index 54c0ae4..0000000 --- a/src/fib.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -int fib(int n); \ No newline at end of file diff --git a/src/greet.c b/src/greet.c deleted file mode 100644 index e87af9f..0000000 --- a/src/greet.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "greet.h" -#include "fib.h" -#include - -void greet(const char *name) { - printf("Hello %s!\n", name); - printf("Fibonacci of 10 is %d\n", fib(10)); -} \ No newline at end of file diff --git a/src/greet.h b/src/greet.h deleted file mode 100644 index 4101e80..0000000 --- a/src/greet.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void greet(const char *name); \ No newline at end of file diff --git a/src/main.c b/src/main.c index a86ee0f..447f528 100644 --- a/src/main.c +++ b/src/main.c @@ -1,161 +1,7 @@ #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; -} +#include "tree.h" int main(void) { Tree tree = {NULL}; @@ -164,11 +10,5 @@ int main(void) { 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 +} diff --git a/src/tree.c b/src/tree.c new file mode 100644 index 0000000..b94880d --- /dev/null +++ b/src/tree.c @@ -0,0 +1,73 @@ +#include +#include + +#include "tree.h" + +int tree_insert(Tree *tree, int data) { + Node *new_node = malloc(sizeof(Node)); + new_node->data = data; + new_node->left = NULL; + new_node->right = NULL; + + // If the tree is empty + if (tree->root == NULL) { + tree->root = new_node; + return 0; // Early return + } + + // If the tree is non-empty + Node *cursor = tree->root; + + // An iterative (non-recursive approach) to tree insertion + // While we look for a place to put our new node + while(1) { + // If larger than data + if(new_node->data > cursor->data) { + // If there is a child to the right + if(cursor->right != NULL) cursor = cursor->right; + else { + cursor->right = new_node; // Put our node here + break; // Break the outer while loop + } + } + + // If less-or-equal than our data + if(new_node->data <= cursor->data) { + // If there is a child to the left + if(cursor->left != NULL) cursor = cursor->left; + else { + cursor->left = new_node; // Put our node here + break; // Break the outer while loop + } + } + } + +} + +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) { + 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; +} diff --git a/src/tree.h b/src/tree.h new file mode 100644 index 0000000..d1d7d78 --- /dev/null +++ b/src/tree.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +// A regular node with a left and right branch +struct Node { + int data; + struct Node *left, *right; +}; +typedef struct Node Node; + +// Represents a tree with a root node +struct Tree { + Node *root; +}; +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);