Compare commits
2 commits
c88d6cefa3
...
5b7efc9ef3
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5b7efc9ef3 | ||
![]() |
c49f02d8d7 |
4 changed files with 68 additions and 65 deletions
6
Makefile
6
Makefile
|
@ -11,6 +11,9 @@ BUILD_DIR := build
|
||||||
# Source files
|
# Source files
|
||||||
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
||||||
|
|
||||||
|
# Header files (used for formatting)
|
||||||
|
HEADERS := $(wildcard $(SRC_DIR)/*.h)
|
||||||
|
|
||||||
# Object files
|
# Object files
|
||||||
OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS))
|
OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(SRCS))
|
||||||
|
|
||||||
|
@ -33,6 +36,9 @@ $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
|
||||||
run: $(TARGET)
|
run: $(TARGET)
|
||||||
./$(TARGET)
|
./$(TARGET)
|
||||||
|
|
||||||
|
fmt:
|
||||||
|
clang-format -i $(SRCS) $(HEADERS)
|
||||||
|
|
||||||
# Clean rule
|
# Clean rule
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILD_DIR) $(TARGET)
|
rm -rf $(BUILD_DIR) $(TARGET)
|
||||||
|
|
14
src/main.c
14
src/main.c
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
107
src/tree.c
107
src/tree.c
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue