Update cv7/program.c
This commit is contained in:
parent
36c5519813
commit
9acb88877a
@ -5,17 +5,14 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define SIZE 200
|
#define SIZE 200
|
||||||
|
|
||||||
struct tree
|
struct tree
|
||||||
{
|
{
|
||||||
int id;
|
|
||||||
char value[SIZE];
|
char value[SIZE];
|
||||||
char question[SIZE];
|
|
||||||
|
|
||||||
struct tree *left, *right;
|
struct tree *left, *right;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -25,12 +22,18 @@ struct tree* read_tree(struct tree* p)
|
|||||||
memset(buffer,0,SIZE);
|
memset(buffer,0,SIZE);
|
||||||
char* r = fgets(buffer,SIZE,stdin);
|
char* r = fgets(buffer,SIZE,stdin);
|
||||||
assert(r);
|
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);
|
memcpy(node->value,buffer,SIZE);
|
||||||
|
//printf("\n%s", node->value);
|
||||||
|
node->left=NULL;
|
||||||
|
node->right=NULL;
|
||||||
|
|
||||||
if(node->value[0]!='*')
|
if(node->value[0]!='*')
|
||||||
{
|
{
|
||||||
read_tree(p->left);
|
|
||||||
read_tree(p->right);
|
read_tree(node->left);
|
||||||
|
|
||||||
|
read_tree(node->right);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -38,46 +41,26 @@ struct tree* read_tree(struct tree* p)
|
|||||||
|
|
||||||
void print_tree(struct tree* tree,int offset){
|
void print_tree(struct tree* tree,int offset){
|
||||||
for (int i = 0; i < offset; i++){
|
for (int i = 0; i < offset; i++){
|
||||||
printf(" ");
|
printf(".");
|
||||||
}
|
}
|
||||||
printf("%s",tree->question);
|
|
||||||
|
printf("(%s)",tree->value);
|
||||||
if (tree->left){
|
if (tree->left){
|
||||||
print_tree(tree->left,offset +3);
|
print_tree(tree->left,offset +3);
|
||||||
|
}
|
||||||
|
if (tree->right){
|
||||||
print_tree(tree->right,offset +3);
|
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)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct tree *tr;
|
||||||
|
|
||||||
|
read_tree(tr);
|
||||||
|
printf("-------");
|
||||||
|
print_tree(tr,3);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user