Update cv7/program.c

This commit is contained in:
Yurii Yakovenko 2024-11-11 21:10:54 +00:00
parent 36c5519813
commit 9acb88877a

View File

@ -5,17 +5,14 @@
#include <string.h>
#define SIZE 200
struct tree
{
int id;
char value[SIZE];
char question[SIZE];
struct tree *left, *right;
};
@ -25,12 +22,18 @@ struct tree* read_tree(struct tree* p)
memset(buffer,0,SIZE);
char* r = fgets(buffer,SIZE,stdin);
assert(r);
struct tree* node = calloc(1,sizeof(struct tree));
struct tree* node = (struct tree*)calloc(1,sizeof(struct tree));
memcpy(node->value,buffer,SIZE);
//printf("\n%s", node->value);
node->left=NULL;
node->right=NULL;
if(node->value[0]!='*')
{
read_tree(p->left);
read_tree(p->right);
read_tree(node->left);
read_tree(node->right);
}
return node;
}
@ -38,46 +41,26 @@ struct tree* read_tree(struct tree* p)
void print_tree(struct tree* tree,int offset){
for (int i = 0; i < offset; i++){
printf(" ");
printf(".");
}
printf("%s",tree->question);
printf("(%s)",tree->value);
if (tree->left){
print_tree(tree->left,offset +3);
}
if (tree->right){
print_tree(tree->right,offset +3);
}
}
// Výpis stromu vo formáte Graphviz bez hlavičky
void print_dot_node(struct tree* node){
if (node == NULL){
return;
}
print_dot_node(node->left);
// Parameter ID je cislo uzla. Cislo uzla urcite pri nacitani pomocou pocitadla.
printf("%d [label=\"%s\"];\n",node->id, node->question);
if (node->left){
printf("%d -> %d;\n",node->id,node->left->id);
}
if (node->right){
printf("%d -> %d;\n",node->id,node->right->id);
}
print_dot_node(node->right);
}
// Výpis stromu vo formáte Graphviz
// obrázok stromu vytvoríte:
// program | dot -T png -o obrazok.png
void print_dot(struct tree* node){
printf("digraph G {\n");
print_dot_node(node);
printf("}\n");
}
int main(void)
{
struct tree *tr;
read_tree(tr);
printf("-------");
print_tree(tr,3);
return 0;
}