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"
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;
}

View file

@ -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;
}

View file

@ -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;