-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDAG.cpp
87 lines (84 loc) · 1.97 KB
/
DAG.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
class DAG
{
public:
char label;
char data;
DAG *left;
DAG *right;
DAG(char x)
{
label = '_';
data = x;
left = NULL;
right = NULL;
}
DAG(char lb, char x, DAG *lt, DAG *rt)
{
label = lb;
data = x;
left = lt;
right = rt;
}
};
int main()
{
int n;
n = 3;
string st[n];
st[0] = "A=x+y";
st[1] = "B=A*z";
st[2] = "C=B/x";
unordered_map<char, DAG *> labelDAGNode;
for (int i = 0; i < 3; i++)
{
string stTemp = st[i];
for (int j = 0; j < 5; j++)
{
char tempLabel = stTemp[0];
char tempLeft = stTemp[2];
char tempData = stTemp[3];
char tempRight = stTemp[4];
DAG *leftPtr;
DAG *rightPtr;
if (labelDAGNode.count(tempLeft) == 0)
{
leftPtr = new DAG(tempLeft);
}
else
{
leftPtr = labelDAGNode[tempLeft];
}
if (labelDAGNode.count(tempRight) == 0)
{
rightPtr = new DAG(tempRight);
}
else
{
rightPtr = labelDAGNode[tempRight];
}
DAG *nn = new DAG(tempLabel, tempData, leftPtr, rightPtr);
labelDAGNode.insert(make_pair(tempLabel, nn));
}
}
cout << "Label ptr leftPtr rightPtr" << endl;
for (int i = 0; i < n; i++)
{
DAG *x = labelDAGNode[st[i][0]];
cout << st[i][0] << " " << x->data << " ";
if (x->left->label == '_')
cout << x->left->data;
else
cout << x->left->label;
cout << " ";
if (x->right->label == '_')
cout << x->right->data;
else
cout << x->right->label;
cout << endl;
}
return 0;
}