-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreechart.h
More file actions
58 lines (53 loc) · 1.39 KB
/
treechart.h
File metadata and controls
58 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/** BY @OscarScrooge*/
typedef struct NODE{
int id;
struct NODE *leftChild;
struct NODE *rightChild;
}NODE;
void chart(NODE *root){
//system("cls");
FILE* file;
file = fopen("tree.dot", "wt");
fputs("digraph Tree{\n", file);
if(root != 0){
graf(root, file);
}else{
fputs("Empty", file);
}
fputs("}", file);
fclose(file);
system("dot -Tpng tree.dot -o tree.png");
printf("\nBINARY TREE CREATED IN YOUR DIRECTORY \n");
return;
}
void graf(NODE *root, FILE* file){
if(root != 0){
if(root->leftChild != 0){
chartTreeS(root, root->leftChild, file);
graf(root->leftChild, file);
}
if(root->rightChild != 0){
chartTreeS(root, root->rightChild, file);
graf(root->rightChild, file);
}
if(root->leftChild == 0 && root->rightChild == 0){
int n = root->id;
char * cad = malloc(12 * sizeof(char));
sprintf(cad, "%i", n);
fputs(cad, file);
fputs("\n", file);
}
}
}
void chartTreeS(NODE *root, NODE *hijo, FILE* file){
int n = root->id;
int n2 = hijo->id;
char * cad = malloc(12 * sizeof(char));
char * cad2 = malloc(12 * sizeof(char));
sprintf(cad, "%i", n);
sprintf(cad2, "%i", n2);
fputs(cad, file);
fputs("->", file);
fputs(cad2, file);
fputs("\n", file);
}