Formatting

This commit is contained in:
Imbus 2024-03-27 06:40:47 +01:00
parent c49f02d8d7
commit 5b7efc9ef3
3 changed files with 62 additions and 65 deletions

View file

@ -4,11 +4,11 @@
#include "tree.h" #include "tree.h"
int main(void) { int main(void) {
Tree tree = {NULL}; Tree tree = {NULL};
printf("Tree size: %d\n", tree_size(&tree)); printf("Tree size: %d\n", tree_size(&tree));
tree_insert(&tree, 1); tree_insert(&tree, 1);
tree_insert(&tree, 2); tree_insert(&tree, 2);
tree_insert(&tree, 3); tree_insert(&tree, 3);
printf("Tree size: %d\n", tree_size(&tree)); printf("Tree size: %d\n", tree_size(&tree));
return 0; return 0;
} }

View file

@ -4,70 +4,67 @@
#include "tree.h" #include "tree.h"
int tree_insert(Tree *tree, int data) { int tree_insert(Tree *tree, int data) {
Node *new_node = malloc(sizeof(Node)); Node *new_node = malloc(sizeof(Node));
new_node->data = data; new_node->data = data;
new_node->left = NULL; new_node->left = NULL;
new_node->right = NULL; new_node->right = NULL;
// If the tree is empty // If the tree is empty
if (tree->root == NULL) { if (tree->root == NULL) {
tree->root = new_node; tree->root = new_node;
return 0; // Early return 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 // If less-or-equal than our data
Node *cursor = tree->root; if (new_node->data <= cursor->data) {
// If there is a child to the left
// An iterative (non-recursive approach) to tree insertion if (cursor->left != NULL)
// While we look for a place to put our new node cursor = cursor->left;
while(1) { else {
// If larger than data cursor->left = new_node; // Put our node here
if(new_node->data > cursor->data) { break; // Break the outer while loop
// 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) { int tree_remove(Tree *tree, int data) { return 1; }
return 1;
}
int tree_clear(Tree *tree) { int tree_clear(Tree *tree) { return 1; }
return 1;
}
int tree_size(Tree *tree) { int tree_size(Tree *tree) {
if (tree->root == NULL) { if (tree->root == NULL) {
return 0; return 0;
} }
int size = 0; int size = 0;
Node *current = tree->root; Node *current = tree->root;
while (current != NULL) { while (current != NULL) {
size++; size++;
if (current->left != NULL) { if (current->left != NULL) {
current = current->left; current = current->left;
} else if (current->right != NULL) { } else if (current->right != NULL) {
current = current->right; current = current->right;
} else { } else {
current = NULL; current = NULL;
}
} }
return size; }
return size;
} }

View file

@ -5,14 +5,14 @@
// A regular node with a left and right branch // A regular node with a left and right branch
struct Node { struct Node {
int data; int data;
struct Node *left, *right; struct Node *left, *right;
}; };
typedef struct Node Node; typedef struct Node Node;
// Represents a tree with a root node // Represents a tree with a root node
struct Tree { struct Tree {
Node *root; Node *root;
}; };
typedef struct Tree Tree; typedef struct Tree Tree;