This commit is contained in:
Macko 2024-04-18 15:23:23 +02:00
parent 0cd0b96b00
commit 01919821be

View File

@ -1,6 +1,27 @@
#include "binary_search_tree.h"
#include <stdlib.h>
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include <stddef.h>
typedef struct node node_t;
struct node {
node_t *right;
node_t *left;
int data;
};
node_t *build_tree(int *tree_data, size_t tree_data_len);
void free_tree(node_t *tree);
int *sorted_data(node_t *tree);
#endif
// Deklarace proměnné sorted_index
static int sorted_index;
// Deklarace funkce insert
node_t* insert(node_t *node, int data) {
if (node == NULL) {
node_t *new_node = (node_t*)malloc(sizeof(node_t));
@ -16,6 +37,7 @@ node_t* insert(node_t *node, int data) {
}
}
// Deklarace funkce inorder_traversal
void inorder_traversal(node_t *node, int *sorted) {
if (node == NULL) return;
inorder_traversal(node->left, sorted);
@ -23,6 +45,7 @@ void inorder_traversal(node_t *node, int *sorted) {
inorder_traversal(node->right, sorted);
}
// Deklarace funkce build_tree
node_t* build_tree(int *tree_data, size_t tree_data_len) {
node_t *root = NULL;
for (size_t i = 0; i < tree_data_len; i++) {
@ -31,6 +54,7 @@ node_t* build_tree(int *tree_data, size_t tree_data_len) {
return root;
}
// Deklarace funkce free_tree
void free_tree(node_t *tree) {
if (tree == NULL) return;
free_tree(tree->left);
@ -38,6 +62,7 @@ void free_tree(node_t *tree) {
free(tree);
}
// Deklarace funkce sorted_data
int *sorted_data(node_t *tree) {
int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků
sorted_index = 0;