third commit

This commit is contained in:
Valér Jakubčo 2021-11-26 02:53:02 +01:00
parent 4cfd661641
commit 0bcb1daa0b

View File

@ -8,32 +8,39 @@
struct node { struct node {
// Otázka aleo odpoveď // Otázka aleo odpoveď
char data[SIZE]; char data[SIZE];
struct node* parent;
// Odpoveď áno // Odpoveď áno
struct node* left; struct node* left;
// Odpoveď nie // Odpoveď nie
struct node* right; struct node* right;
}; };
struct node* getNewNode(char data[SIZE]){ struct node* read_tree(int* count, int* leafs){
struct node* newNode = (struct node*)calloc(1, sizeof(struct node)); char buffer[SIZE];
memcpy(newNode->data, data, SIZE); memset(buffer,0,SIZE);
newNode->left = NULL; char* r = fgets(buffer,SIZE,stdin);
newNode->right = NULL; assert(r);
return newNode; struct node* node = calloc(1,sizeof(struct node));
memcpy(node->data,buffer,SIZE);
if(r[0]!='*'){
*count = *count +1;
node->left=read_tree(count, leafs);
node->right=read_tree(count, leafs);
}else {
*leafs = *leafs+1;
}
return node;
} }
void print_tree(struct node* tree,int offset){
struct node* insert(struct node* root, char data[SIZE]){ for (int i = 0; i < offset; i++){
if(root == NULL){ printf(" ");
root = getNewNode(data);
return root;
} }
if(root->left == NULL){ printf("%s",tree->data);
root->left = insert(root->left,data); if (tree->left){
}else{ print_tree(tree->left,offset +3);
root->right = insert(root->right,data); print_tree(tree->right,offset +3);
} }
return root;
} }
void remove_tree(struct node* node){ void remove_tree(struct node* node){
@ -41,70 +48,34 @@ void remove_tree(struct node* node){
remove_tree(node->left); remove_tree(node->left);
remove_tree(node->right); remove_tree(node->right);
free(node); free(node);
} }
} }
void printt_tree(struct node* tree,int offset){ void ask(struct node* tree){
for (int i = 0; i < offset; i++){ printf("%s", tree->data);
printf(" "); if(tree->data[0] == '*'){
puts("Koniec");
} }
printf("%s",tree->data); if(tree->left != NULL){
if (tree->left){ tree = tree->left;
printt_tree(tree->left,offset+3); ask(tree);
printt_tree(tree->right,offset+3); }else if(tree->right != NULL) {
} tree = tree->right;
} ask(tree);
void printTabs(int numtabs){ }else{
for(int i=0; i<numtabs; i++){
printf("\t");
}
}
void printTree(struct node* root, int level){
if(root == NULL) {
printTabs(level);
printf("empty\n");
return; return;
} }
printTabs(level);
printf("hodnota %s\n", root->data);
printTabs(level);
printf("vlavo \n");
printTree(root->left, level+1);
printTabs(level);
printf("vpravo \n");
printTree(root->right, level+1);
printTabs(level);
printf("hotovo \n");
} }
void printtree(struct node* root){
printTree(root, 0);
}
int main(){ int main(){
struct node* tree = NULL; struct node* tree;
int count = 0;
char buff[SIZE]; int leafs = 0;
char* r; tree = read_tree(&count, &leafs);
int counter=0; //print_tree(tree, 20);
int lists=0; ask(tree);
while(1){ remove_tree(tree);
r = fgets(buff, SIZE, stdin);
if(buff[0] == '\n' || r == NULL){
break;
}
tree = insert(tree, buff);
counter++;
}
puts("Expert z bufetu to vie.");
printf("Pozna %d druhov ovocia a zeleniny.\n", lists);
puts("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.");
printf("counter %d, lists %d\n", counter, lists);
//printt_tree(tree, 1);
printtree(tree);
remove_tree(tree);
return 0; return 0;
} }