final
This commit is contained in:
parent
deee10505b
commit
6d9c7de58a
@ -12,8 +12,9 @@ struct tree {
|
|||||||
|
|
||||||
struct tree* read_tree(int* counter){
|
struct tree* read_tree(int* counter){
|
||||||
|
|
||||||
|
//read one node
|
||||||
int* c = counter;
|
int* c = counter;
|
||||||
|
|
||||||
//printf("%d\n", *c);
|
//printf("%d\n", *c);
|
||||||
char buffer[50];
|
char buffer[50];
|
||||||
memset(buffer,0,50);
|
memset(buffer,0,50);
|
||||||
@ -26,24 +27,39 @@ struct tree* read_tree(int* counter){
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(buffer[0] != '\0'){
|
if(buffer[0] != '\0'){
|
||||||
struct tree* node = calloc(1,sizeof(struct tree));
|
struct tree* node = calloc(1,sizeof(struct tree));
|
||||||
memcpy(node->value, buffer,50);
|
memcpy(node->value, buffer,50);
|
||||||
|
|
||||||
|
//count all nodes
|
||||||
*c += 1;
|
*c += 1;
|
||||||
counter = c;
|
counter = c;
|
||||||
|
|
||||||
|
//until i find "*", recursively read tree
|
||||||
if(buffer[0] != '*'){
|
if(buffer[0] != '*'){
|
||||||
|
|
||||||
|
//first check left side, if the isn't node, add it
|
||||||
if(node->left == NULL){
|
if(node->left == NULL){
|
||||||
node->left = read_tree(counter);
|
node->left = read_tree(counter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//than check right side
|
||||||
if(node->right == NULL){
|
if(node->right == NULL){
|
||||||
node->right = read_tree(counter);
|
node->right = read_tree(counter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if i found root with left and right node, decrease my counter
|
||||||
if(node->left != NULL && node->right != NULL){
|
if(node->left != NULL && node->right != NULL){
|
||||||
*c -= 2;
|
*c -= 2;
|
||||||
counter = c;
|
counter = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//if binary tree is okay, than counter is 1(there is one root)
|
||||||
if(*c == 1){
|
if(*c == 1){
|
||||||
|
|
||||||
|
//if the user still inseting, than the databaze wrong
|
||||||
r = fgets(buffer,50, stdin);
|
r = fgets(buffer,50, stdin);
|
||||||
int x = strlen(buffer);
|
int x = strlen(buffer);
|
||||||
buffer[x-1]='\0';
|
buffer[x-1]='\0';
|
||||||
@ -59,35 +75,38 @@ struct tree* read_tree(int* counter){
|
|||||||
}
|
}
|
||||||
void search(struct tree* this){
|
void search(struct tree* this){
|
||||||
|
|
||||||
|
//read answer
|
||||||
char buffer[5];
|
char answer[5];
|
||||||
memset(buffer,0,5);
|
memset(answer,0,5);
|
||||||
char* r = fgets(buffer,5,stdin);
|
char* r = fgets(answer,5,stdin);
|
||||||
|
|
||||||
if(r == NULL){
|
if(r == NULL){
|
||||||
printf("Koniec vstupu\n");
|
printf("Koniec vstupu\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
if(buffer[0] == '\n'){
|
|
||||||
|
if(answer[0] == '\n'){
|
||||||
search(this);
|
search(this);
|
||||||
}
|
}
|
||||||
/*if(buffer[0] != 'a' || buffer[0] != 'n' || buffer[0] != '\n'){
|
|
||||||
printf("Nerozumiem\n");
|
|
||||||
exit(0);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if(buffer[0] == 'a'){
|
if(answer[0] == 'a'){
|
||||||
if(this->left->value[0] == '*'){
|
|
||||||
|
//if left node is answer, print it and end the program
|
||||||
|
if(this->left->value[0] == '*'){
|
||||||
printf("%s\n", this->left->value);
|
printf("%s\n", this->left->value);
|
||||||
printf("Koniec\n");
|
printf("Koniec\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//else print it and continue searching
|
||||||
printf("%s\n", this->left->value);
|
printf("%s\n", this->left->value);
|
||||||
search(this->left);
|
search(this->left);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(buffer[0] == 'n'){
|
else if(answer[0] == 'n'){
|
||||||
|
|
||||||
|
//the same for the right side
|
||||||
if(this->right->value[0] == '*'){
|
if(this->right->value[0] == '*'){
|
||||||
printf("%s\n", this->right->value);
|
printf("%s\n", this->right->value);
|
||||||
printf("Koniec\n");
|
printf("Koniec\n");
|
||||||
@ -98,26 +117,19 @@ void search(struct tree* this){
|
|||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
||||||
|
//if input is wrong
|
||||||
printf("Nerozumiem\n");
|
printf("Nerozumiem\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_tree(struct tree* tree,int offset){
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < offset; i++){
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
printf("%s",tree->value);
|
|
||||||
if (tree->left){
|
|
||||||
print_tree(tree->left,offset +3);
|
|
||||||
print_tree(tree->right,offset +3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void destroy_tree(struct tree* root){
|
void destroy_tree(struct tree* root){
|
||||||
|
|
||||||
|
//if root is NULL,than tree is empty
|
||||||
if(root == NULL) return;
|
if(root == NULL) return;
|
||||||
|
|
||||||
|
//go to the left side and than to the right
|
||||||
destroy_tree(root->left);
|
destroy_tree(root->left);
|
||||||
destroy_tree(root->right);
|
destroy_tree(root->right);
|
||||||
free(root);
|
free(root);
|
||||||
@ -125,10 +137,14 @@ void destroy_tree(struct tree* root){
|
|||||||
}
|
}
|
||||||
int count_leaves(struct tree* node){
|
int count_leaves(struct tree* node){
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
|
//if left and right nodes are empty
|
||||||
if(node->left == NULL && node->right == NULL){
|
if(node->left == NULL && node->right == NULL){
|
||||||
count = 1;
|
count++;;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
||||||
|
//if left isn't NULL than count
|
||||||
if(node->left != NULL){
|
if(node->left != NULL){
|
||||||
count += count_leaves(node->left);
|
count += count_leaves(node->left);
|
||||||
}
|
}
|
||||||
@ -143,13 +159,14 @@ int count_all(struct tree* node){
|
|||||||
if(node == NULL){
|
if(node == NULL){
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int count = 1;
|
int count = 0;
|
||||||
if(node->left != NULL){
|
if(node->left != NULL){
|
||||||
count += count_all(node->left);
|
count += count_all(node->left);
|
||||||
}
|
}
|
||||||
if(node->right != NULL){
|
if(node->right != NULL){
|
||||||
count += count_all(node->right);
|
count += count_all(node->right);
|
||||||
}
|
}
|
||||||
|
count++;
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user