123
This commit is contained in:
parent
0cd0b96b00
commit
01919821be
@ -1,6 +1,27 @@
|
|||||||
#include "binary_search_tree.h"
|
#include "binary_search_tree.h"
|
||||||
#include <stdlib.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) {
|
node_t* insert(node_t *node, int data) {
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
node_t *new_node = (node_t*)malloc(sizeof(node_t));
|
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) {
|
void inorder_traversal(node_t *node, int *sorted) {
|
||||||
if (node == NULL) return;
|
if (node == NULL) return;
|
||||||
inorder_traversal(node->left, sorted);
|
inorder_traversal(node->left, sorted);
|
||||||
@ -23,6 +45,7 @@ void inorder_traversal(node_t *node, int *sorted) {
|
|||||||
inorder_traversal(node->right, sorted);
|
inorder_traversal(node->right, sorted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deklarace funkce build_tree
|
||||||
node_t* build_tree(int *tree_data, size_t tree_data_len) {
|
node_t* build_tree(int *tree_data, size_t tree_data_len) {
|
||||||
node_t *root = NULL;
|
node_t *root = NULL;
|
||||||
for (size_t i = 0; i < tree_data_len; i++) {
|
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;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deklarace funkce free_tree
|
||||||
void free_tree(node_t *tree) {
|
void free_tree(node_t *tree) {
|
||||||
if (tree == NULL) return;
|
if (tree == NULL) return;
|
||||||
free_tree(tree->left);
|
free_tree(tree->left);
|
||||||
@ -38,6 +62,7 @@ void free_tree(node_t *tree) {
|
|||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deklarace funkce sorted_data
|
||||||
int *sorted_data(node_t *tree) {
|
int *sorted_data(node_t *tree) {
|
||||||
int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků
|
int *sorted = (int*)malloc(sizeof(int) * 1000); // Předpokládáme, že strom má maximálně 1000 prvků
|
||||||
sorted_index = 0;
|
sorted_index = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user