-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix Expression.cpp
68 lines (64 loc) · 1.15 KB
/
Infix Expression.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
#include <iostream>
#include <string>
struct node
{
std::string date;
int l, r;
};
node tree[30];
int sum = 0;
int order(int v)
{
sum++;
if (tree[v].l != -1)
{
order(tree[v].l);
}
if (tree[v].r != -1)
{
order(tree[v].r);
}
return 0;
}
int inorder(int root)
{
if (tree[root].l != -1 || tree[root].r != -1)
{
std::cout << "(";
if (tree[root].l != -1)
inorder(tree[root].l);
std::cout << tree[root].date;
if (tree[root].r != -1)
inorder(tree[root].r);
std::cout << ")";
}
else
std::cout << tree[root].date;
return 0;
}
int main()
{
int n;
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> tree[i + 1].date >> tree[i + 1].l >> tree[i + 1].r;
}
int root;
for (int i = 0; i < n; i++)
{
sum = 0;
order(i + 1);
if (sum == n)
{
root = i + 1;
break;
}
}
if (tree[root].l != -1)
inorder(tree[root].l);
std::cout << tree[root].date;
if (tree[root].r != -1)
inorder(tree[root].r);
return 0;
}