pvjc24/a3/binary_search_tree.c

50 lines
1.1 KiB
C
Raw Normal View History

2024-04-18 12:56:09 +00:00
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
2024-04-18 13:00:57 +00:00
Node* newNode(int data);
Node* insert(Node* node, int data);
int search(Node* root, int x);
2024-04-18 13:07:24 +00:00
void build_tree(); // Deklarace funkce build_tree
void free_tree(); // Deklarace funkce free_tree
2024-04-18 13:00:57 +00:00
2024-04-18 12:56:09 +00:00
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
2024-04-18 13:07:24 +00:00
return node;
2024-04-18 12:56:09 +00:00
}
Node* insert(Node* node, int data) {
if (node == NULL) {
2024-04-18 13:07:24 +00:00
return newNode(data);
2024-04-18 12:56:09 +00:00
}
else {
if (data <= node->data) node->left = insert(node->left, data);
else node->right = insert(node->right, data);
2024-04-18 13:07:24 +00:00
return node;
2024-04-18 12:56:09 +00:00
}
}
int search(Node* root, int x) {
2024-04-18 13:00:57 +00:00
if(root == NULL) return 0;
if(root->data == x) return 1;
2024-04-18 12:56:09 +00:00
else if(root->data < x) return search(root->right, x);
else return search(root->left, x);
}
2024-04-18 13:07:24 +00:00
// Implementace funkce build_tree
void build_tree() {
// Zde implementujte vytvoření stromu
}
// Implementace funkce free_tree
void free_tree() {
// Zde implementujte uvolnění paměti použité pro strom
}