-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput-dot.txt
More file actions
44 lines (41 loc) · 1.45 KB
/
output-dot.txt
File metadata and controls
44 lines (41 loc) · 1.45 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
/* -*- mode:c -*- */
/* These functions should be added to your tree.c file */
/**
* Output a DOT description of this tree to the given output stream.
* DOT is a plain text graph description language (see www.graphviz.org).
* You can create a viewable graph with the command
*
* dot -Tpdf < graphfile.dot > graphfile.pdf
*
* You can also use png, ps, jpg, svg... instead of pdf
*
* @param t the tree to output the DOT description of.
* @param out the stream to write the DOT description to.
*/
void tree_output_dot(tree t, FILE *out) {
fprintf(out, "digraph tree {\nnode [shape = Mrecord, penwidth = 2];\n");
tree_output_dot_aux(t, out);
fprintf(out, "}\n");
}
/**
* Traverses the tree writing a DOT description about connections, and
* possibly colours, to the given output stream.
*
* @param t the tree to output a DOT description of.
* @param out the stream to write the DOT output to.
*/
static void tree_output_dot_aux(tree t, FILE *out) {
if(t->key != NULL) {
fprintf(out, "\"%s\"[label=\"{<f0>%s:%d|{<f1>|<f2>}}\"color=%s];\n",
t->key, t->key, t->frequency,
(RBT == tree_type && RED == t->colour) ? "red":"black");
}
if(t->left != NULL) {
tree_output_dot_aux(t->left, out);
fprintf(out, "\"%s\":f1 -> \"%s\":f0;\n", t->key, t->left->key);
}
if(t->right != NULL) {
tree_output_dot_aux(t->right, out);
fprintf(out, "\"%s\":f2 -> \"%s\":f0;\n", t->key, t->right->key);
}
}