123
This commit is contained in:
parent
7fa8f9b45b
commit
b955cf1824
@ -1,30 +1,19 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h> // Přidáno pro účely výstupu
|
||||
#include "binary_search_tree.h"
|
||||
|
||||
typedef struct Node {
|
||||
typedef struct node {
|
||||
struct node *right;
|
||||
struct node *left;
|
||||
int data;
|
||||
struct Node* left;
|
||||
struct Node* right;
|
||||
} Node;
|
||||
} node_t;
|
||||
|
||||
Node* newNode(int data);
|
||||
Node* insert(Node* node, int data);
|
||||
int search(Node* root, int x);
|
||||
void build_tree();
|
||||
void free_tree();
|
||||
void sorted_data(); // Deklarace funkce sorted_data
|
||||
|
||||
Node* newNode(int data) {
|
||||
Node* node = (Node*)malloc(sizeof(Node));
|
||||
node->data = data;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* insert(Node* node, int data) {
|
||||
node_t* insert(node_t *node, int data) {
|
||||
if (node == NULL) {
|
||||
return newNode(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;
|
||||
}
|
||||
else {
|
||||
if (data <= node->data) node->left = insert(node->left, data);
|
||||
@ -33,31 +22,33 @@ Node* insert(Node* node, int data) {
|
||||
}
|
||||
}
|
||||
|
||||
int search(Node* root, int x) {
|
||||
if(root == NULL) return 0;
|
||||
if(root->data == x) return 1;
|
||||
else if(root->data < x) return search(root->right, x);
|
||||
else return search(root->left, x);
|
||||
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++) {
|
||||
root = insert(root, tree_data[i]);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
// Implementace funkce build_tree
|
||||
void build_tree() {
|
||||
// Implementace funkce build_tree
|
||||
void free_tree(node_t *tree) {
|
||||
if (tree == NULL) return;
|
||||
free_tree(tree->left);
|
||||
free_tree(tree->right);
|
||||
free(tree);
|
||||
}
|
||||
|
||||
// Implementace funkce free_tree
|
||||
void free_tree() {
|
||||
// Implementace funkce free_tree
|
||||
static int sorted_index = 0;
|
||||
|
||||
void inorder_traversal(node_t *node, int *sorted) {
|
||||
if (node == NULL) return;
|
||||
inorder_traversal(node->left, sorted);
|
||||
sorted[sorted_index++] = node->data;
|
||||
inorder_traversal(node->right, sorted);
|
||||
}
|
||||
|
||||
// Implementace funkce sorted_data
|
||||
void sorted_data() {
|
||||
// Implementace funkce sorted_data
|
||||
printf("Testovací funkce pro kontrolu správnosti řazení dat.\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Volání testů a jiné části kódu
|
||||
sorted_data(); // Zavolání funkce sorted_data pro účely testování
|
||||
return 0;
|
||||
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;
|
||||
inorder_traversal(tree, sorted);
|
||||
return sorted;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user