pvjc24/a3/binary_search_tree.c

34 lines
644 B
C
Raw Normal View History

2024-04-23 17:33:27 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "binary_search_tree.h"
2024-04-23 17:43:45 +00:00
void free_tree(node_t *tree) {
if (tree == NULL) {
2024-04-23 17:44:34 +00:00
return;
2024-04-23 17:43:45 +00:00
}
free_tree(tree->left);
free_tree(tree->right);
free(tree);
}
2024-04-23 17:33:27 +00:00
node_t *build_tree(int *tree_data, size_t tree_data_len){
node_t *koren = (node_t *)malloc(sizeof(node_t));
2024-04-23 17:34:49 +00:00
if(tree_data == NULL || tree_data_len == 0){
2024-04-23 17:33:27 +00:00
return 0;
}
2024-04-23 17:34:49 +00:00
if(tree_data != NULL && tree_data_len != 0){
2024-04-23 17:33:27 +00:00
koren->data = tree_data[0];
2024-04-23 17:40:18 +00:00
koren->left = (node_t *)malloc(sizeof(node_t));
koren->left->data = tree_data[0];
2024-04-23 17:33:27 +00:00
}
return koren;
}