This commit is contained in:
Bohdan Kapliuk 2024-11-14 14:25:09 +02:00
parent 491beb49ed
commit 896caa5d3e
2 changed files with 4 additions and 41 deletions

View File

@ -1,33 +0,0 @@
#ifndef BINARY_SEARCH_H
#define BINARY_SEARCH_H
#include <stddef.h>
#include <math.h>
#include <stdio.h>
const int *binary_search(int value, const int *arr, size_t length) {
if (length == 0) {
return NULL;
}
size_t left = 0;
size_t right = length - 1;
while (left <= right) {
size_t middle = left + (right - left) / 2;
if (arr[middle] == value) {
return &arr[middle];
} else if (arr[middle] > value) {
if (middle == 0) {
break;
}
right = middle - 1;
} else {
left = middle + 1;
}
}
return NULL;
}
#endif

View File

@ -14,19 +14,15 @@ struct tree* read_tree(int* counter) {
char buffer[SIZE];
memset(buffer, 0, SIZE);
char* r = fgets(buffer, SIZE, stdin);
if(r == NULL){
printf("Chybna databaza\n");
return 0;
}
if (buffer[0] == '\n') {
return NULL;
}
struct tree* tree = calloc(1, sizeof(struct tree));
if(tree == NULL){
printf("Chybna databaza\n");
exit(0);
}
tree->value = malloc(strlen(buffer) + 1);
if(tree->value == NULL){
printf("Chybna databaza\n");
exit(0);
}
strcpy(tree->value, buffer);
if (buffer[0] == '*') {
(*counter)++;