-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttfunc.cpp
65 lines (60 loc) · 1.85 KB
/
ttfunc.cpp
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
59
60
61
62
63
64
65
extern int yylineno;
ttnode *make_node(string sval,string identifier, ttnode *n1, ttnode *n2, ttnode *n3)
{
ttnode *n = new ttnode();
n->item = sval;
n->identifier = identifier;
n->first = n1;
n->second = n2;
n->third = n3;
n->line_num = yylineno;
return n;
}
vector<bool> line;
ofstream fout("tree.txt");
void printtreeold(ttnode *t, string term)
{
if(t==NULL) return;
if(!line.empty()) fout << " \t";
for(int i = 0; i < (int)line.size() - 1; i++) if(line[i]) fout << "¦ \t"; else fout << " \t";
fout << term;
fout << t->identifier;
if((t->item).size())
fout << "( " << t->item << " )";
fout << endl;
if(t->second) {line.push_back(1); printtreeold(t->first,"+-- "); line.pop_back();}
else {line.push_back(0); printtreeold(t->first,"+-- "); line.pop_back();}
if(t->third) {line.push_back(1); printtreeold(t->second, "+-- "); line.pop_back();}
else {line.push_back(0); printtreeold(t->second, "+-- "); line.pop_back();}
{line.push_back(0); printtreeold(t->third, "+-- "); line.pop_back();}
}
int print(string par, string child){
if(par == "begin")
{
cout << "digraph G {\n";
cout << "begin [shape=box,style=filled,color=white]\n";
cout << "\"" << par << "\" " << "[label = \"\"];\n";
}
else
cout << "\"" << par << "\" " << "[label = \"" << par.substr(par.find_first_of('.') + 1) << "\"];\n";
cout << "\"" << child << "\" " << "[label = \"" << child.substr(child.find_first_of('.') + 1) << "\"];\n";
cout<<"\""<<par<<"\""<<" -> "<<"\""<<child<<"\""<<endl;
return 0;
}
int cnt = 0;
void printtree(ttnode *t,string parent="begin"){
if(t==NULL) return;
string s="";
cnt++;
stringstream s1;
s1<<cnt;
s = s1.str()+ ". ";
s+=t->identifier+" ";
if((t->item).size())
s+="( " + t->item + " )";
print(parent,s);
printtree(t->first, s);
printtree(t->second, s);
printtree(t->third, s);
if(parent == "begin") cout << "}";
}