pvjc24/a3/binary_search_tree.c
2024-04-25 21:23:56 +02:00

32 lines
742 B
C

#include "binary_search_tree.h"
#include <stdlib.h>
node_t *create_node(int data) {
node_t *node = (node_t *)malloc(sizeof(node_t));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
node_t *insert_node(node_t *tree, int data) {
if (tree == NULL) {
return create_node(data);
}
if (data < tree->data) {
tree->left = insert_node(tree->left, data);
} else if (data > tree->data) {
tree->right = insert_node(tree->right, data);
}
return tree;
}
node_t *build_tree(int *tree_data, size_t tree_data_len) {
node_t *tree = NULL;
for (size_t i = 0; i < tree_data_len; i++) {
tree = insert_node(tree, tree_data[i]);
}
return tree;
}