83 lines
1.6 KiB
C
83 lines
1.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <malloc.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define SIZE 200
|
|
|
|
struct tree
|
|
{
|
|
int id;
|
|
char value[SIZE];
|
|
char question[SIZE];
|
|
|
|
struct tree *left, *right;
|
|
};
|
|
|
|
struct tree* read_tree(struct tree* p)
|
|
{
|
|
char buffer[SIZE];
|
|
memset(buffer,0,SIZE);
|
|
char* r = fgets(buffer,SIZE,stdin);
|
|
assert(r);
|
|
struct tree* node = calloc(1,sizeof(struct tree));
|
|
memcpy(node->value,buffer,SIZE);
|
|
if(node->value[0]!='*')
|
|
{
|
|
read_tree(p->left);
|
|
read_tree(p->right);
|
|
}
|
|
return node;
|
|
}
|
|
|
|
|
|
void print_tree(struct tree* tree,int offset){
|
|
for (int i = 0; i < offset; i++){
|
|
printf(" ");
|
|
}
|
|
printf("%s",tree->question);
|
|
if (tree->left){
|
|
print_tree(tree->left,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)
|
|
{
|
|
|
|
return 0;
|
|
} |