From 5b7efc9ef38a06aeb2aa8c6c8668b32d556f9bac Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Wed, 27 Mar 2024 06:40:47 +0100 Subject: [PATCH] Formatting --- src/main.c | 14 +++---- src/tree.c | 107 ++++++++++++++++++++++++++--------------------------- src/tree.h | 6 +-- 3 files changed, 62 insertions(+), 65 deletions(-) diff --git a/src/main.c b/src/main.c index 447f528..26f0092 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 b94880d..31c02bc 100644 --- a/src/tree.c +++ b/src/tree.c @@ -4,70 +4,67 @@ #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 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 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 - } - } + // 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; - } + 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; - } + 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 size; } diff --git a/src/tree.h b/src/tree.h index d1d7d78..5bba666 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;