Update 'a3/binary_search_tree.c'

This commit is contained in:
Tokarčík 2024-04-28 19:58:27 +00:00
parent 52e154db28
commit 1d07f03672

View File

@ -1,64 +1,66 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "binary_search_tree.h"
#include <stdlib.h>
node_t *create_node(int data) ;
node_t *create_node(int data) {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
node_t *build_tree(int *tree_data, size_t tree_data_len) {
node_t *root = create_node(tree_data[0]);
size_t i = 1;
while (i < tree_data_len) {
node_t *new_node = create_node(tree_data[i]);
node_t *current = root;
while (1) {
if (new_node->data <= current->data) { //porovnanie novy uzol a aktualny
if (current->left == NULL) {
current->left = new_node;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = new_node;
break;
}
current = current->right;
}
}
i++;
}
return root;
}
// Function to free the memory allocated for the binary search tree
void free_tree(node_t *tree) {
free(tree);
}
// Function to return sorted data from the binary search tree
int *sorted_data(node_t *tree) {
return NULL;
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "binary_search_tree.h"
#include <stdlib.h>
node_t *create_node(int data) ;
node_t *create_node(int data) {
node_t *new_node = (node_t *)malloc(sizeof(node_t));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
node_t *build_tree(int *tree_data, size_t tree_data_len) {
node_t *root = create_node(tree_data[0]);
size_t i = 1;
while (i < tree_data_len) {
node_t *new_node = create_node(tree_data[i]);
node_t *current = root;
while (1) {
if (new_node->data <= current->data) { //porovnanie novy uzol a aktualny
if (current->left == NULL) {
current->left = new_node;
break;
}
current = current->left;
} else {
if (current->right == NULL) {
current->right = new_node;
break;
}
current = current->right;
}
}
i++;
}
return root;
}
// Function to free the memory allocated for the binary search tree
void free_tree(node_t *tree) {
free(tree);
}
// Function to return sorted data from the binary search tree
int *sorted_data(node_t *tree) {
if(tree==NULL){
return NULL;
}
return NULL;
}