Загрузка данных
#ifndef BST_H
#define BST_H
typedef struct s_btree {
struct s_btree *left;
struct s_btree *right;
int item;
} t_btree;
t_btree *bstree_create_node(int item);
void bstree_insert(t_btree *root, int item, int (*cmpf)(int, int));
int compare_int(int first, int second);
void bstree_destroy(t_btree *root);
#endif
#include "bst.h"
#include <stdlib.h>
t_btree *bstree_create_node(int item) {
t_btree *node = malloc(sizeof(t_btree));
if (node != NULL) {
node->left = NULL;
node->right = NULL;
node->item = item;
}
return node;
}
void bstree_insert(t_btree *root, int item, int (*cmpf)(int, int)) {
if (root != NULL) {
if (cmpf(item, root->item) < 0) {
if (root->left == NULL) {
root->left = bstree_create_node(item);
} else {
bstree_insert(root->left, item, cmpf);
}
} else {
if (root->right == NULL) {
root->right = bstree_create_node(item);
} else {
bstree_insert(root->right, item, cmpf);
}
}
}
}
int compare_int(int first, int second) { return first - second; }
void bstree_destroy(t_btree *root) {
if (root != NULL) {
bstree_destroy(root->left);
bstree_destroy(root->right);
free(root);
}
}
#include <stdio.h>
#include "bst.h"
#define SUCCESS "SUCCESS"
#define FAIL "FAIL"
void bst_insert_left_test(void);
void bst_insert_right_test(void);
void bst_insert_tree_test(void);
int main(void) {
bst_insert_left_test();
bst_insert_right_test();
bst_insert_tree_test();
return 0;
}
void bst_insert_left_test(void) {
t_btree *root = bstree_create_node(4);
bstree_insert(root, 2, compare_int);
printf("bst_insert_left_test result: ");
if (root != NULL && root->left != NULL && root->left->item == 2 && root->right == NULL) {
printf("%s", SUCCESS);
} else {
printf("%s", FAIL);
}
printf("\n");
bstree_destroy(root);
}
void bst_insert_right_test(void) {
t_btree *root = bstree_create_node(4);
bstree_insert(root, 8, compare_int);
printf("bst_insert_right_test result: ");
if (root != NULL && root->right != NULL && root->right->item == 8 && root->left == NULL) {
printf("%s", SUCCESS);
} else {
printf("%s", FAIL);
}
printf("\n");
bstree_destroy(root);
}
void bst_insert_tree_test(void) {
t_btree *root = bstree_create_node(4);
bstree_insert(root, 2, compare_int);
bstree_insert(root, 8, compare_int);
bstree_insert(root, 1, compare_int);
bstree_insert(root, 3, compare_int);
printf("bst_insert_tree_test result: ");
if (root != NULL && root->left != NULL && root->right != NULL && root->left->left != NULL &&
root->left->right != NULL && root->left->left->item == 1 && root->left->right->item == 3 &&
root->right->item == 8) {
printf("%s", SUCCESS);
} else {
printf("%s", FAIL);
}
printf("\n");
bstree_destroy(root);
}
CC = gcc
CFLAGS = -Wall -Wextra -Werror -std=c11
BUILD_DIR = ../build
.PHONY: all clean rebuild door_struct list_test stack_test print_module documentation_module bst_create_test bst_insert_test
all: door_struct list_test stack_test print_module documentation_module bst_create_test bst_insert_test
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
door_struct: $(BUILD_DIR)
$(CC) $(CFLAGS) dmanager_module.c -o $(BUILD_DIR)/Quest_1
list_test: $(BUILD_DIR)
$(CC) $(CFLAGS) list.c list_test.c -o $(BUILD_DIR)/Quest_2
stack_test: $(BUILD_DIR)
$(CC) $(CFLAGS) stack.c stack_test.c -o $(BUILD_DIR)/Quest_3
print_module: $(BUILD_DIR)
$(CC) $(CFLAGS) print_module.c main_module_entry_point.c -o $(BUILD_DIR)/Quest_4
documentation_module: $(BUILD_DIR)
$(CC) $(CFLAGS) print_module.c documentation_module.c main_module_entry_point.c -o $(BUILD_DIR)/Quest_5
bst_create_test: $(BUILD_DIR)
$(CC) $(CFLAGS) bst.c bst_create_test.c -o $(BUILD_DIR)/Quest_6
bst_insert_test: $(BUILD_DIR)
$(CC) $(CFLAGS) bst.c bst_insert_test.c -o $(BUILD_DIR)/Quest_7
clean:
rm -rf $(BUILD_DIR)
rebuild: clean all