-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree2dot.cc
40 lines (37 loc) · 859 Bytes
/
tree2dot.cc
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
/** tree2dot.cc
create a graphviz input file from a tree data structure.
*/
#include <sstream>
#include <string>
#include <fstream>
#include "tree.h"
std::string tree2dot(Node * r){
ostringstream out;
// draw left edges, then left subtree's edges
if(r->left){
out<<r->data<<"->"<<r->left->data<<'\n';
out<<tree2dot(r->left);
}
// now draw right edges, ...
if(r->right){
out<<r->data<<"->"<<r->right->data<<'\n';
out<<tree2dot(r->right);
}
return out.str();
}
std::string tree2dot(Tree t){
ostringstream out;
out<<"digraph Tree {\n";
out<<tree2dot(t.root);
out<<"}\n";
}
void writeDotFile(Tree t, std::string filename){
try{
ofstream outfile;
outfile.open(filename);
outfile<<tree2dot(t);
outfile.close();
} except (std:exception){
std::cerr<<"Writing file "<<filename<<"failed.\n";
}
}