diff --git a/Makefile b/Makefile index 712bf45..a793e04 100644 --- a/Makefile +++ b/Makefile @@ -11,9 +11,6 @@ BUILD_DIR := build # Source files SRCS := $(wildcard $(SRC_DIR)/*.c) -# Header files (used for formatting) -HEADERS := $(wildcard $(SRC_DIR)/*.h) - # Object files OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS)) @@ -36,9 +33,6 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c run: $(TARGET) ./$(TARGET) -fmt: - clang-format -i $(SRCS) $(HEADERS) - # Clean rule clean: rm -rf $(BUILD_DIR) $(TARGET) diff --git a/src/main.c b/src/main.c index 26f0092..447f528 100644 --- a/src/main.c +++ b/src/main.c @@ -4,11 +4,11 @@ #include "tree.h" int main(void) { - 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)); - 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)); + return 0; } diff --git a/src/tree.c b/src/tree.c index 31c02bc..b94880d 100644 --- a/src/tree.c +++ b/src/tree.c @@ -4,67 +4,70 @@ #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; + 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 the tree is empty + if (tree->root == NULL) { + tree->root = new_node; + return 0; // Early return } - // 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 - } + // 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_remove(Tree *tree, int data) { + return 1; +} -int tree_clear(Tree *tree) { 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; + if (tree->root == NULL) { + return 0; } - } - return size; + + 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 index 5bba666..d1d7d78 100644 --- a/src/tree.h +++ b/src/tree.h @@ -5,14 +5,14 @@ // A regular node with a left and right branch struct Node { - int data; - struct Node *left, *right; + int data; + struct Node *left, *right; }; typedef struct Node Node; // Represents a tree with a root node struct Tree { - Node *root; + Node *root; }; typedef struct Tree Tree;