-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePrec.cpp
More file actions
242 lines (194 loc) · 4.17 KB
/
BinaryTreePrec.cpp
File metadata and controls
242 lines (194 loc) · 4.17 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <string>
#include <fstream>
//트리를 탐색하고 괄호를
using namespace std;
char prec[4][2] = {'*',2, '/',2, '+',1, '-',1};
class Node
{
friend class Tree;
char data;
Node *left;
Node *right;
int prior;
};
class Tree
{
public:
Node *head;
Tree();
void createTree(string s);
void inorder(Node *head);
void inorder2(Node *head);
void preorder(Node *head);
void postorder(Node *head);
void operand(Node *temp);
void operator1(Node *temp);
int evaluation(Node *head);
};
int main(){
string s;
ifstream infile;
infile.open("input.txt");
for (int i = 0; i<3; i++){
infile >> s;
int ans;
Tree t;
t.createTree(s);
cout<<"InOrder :";
t.inorder(t.head);
cout << endl;
cout<<"PostOder :";
t.postorder(t.head);
cout << endl;
cout<<"PreOrder :";
t.preorder(t.head);
cout << endl;
ans = t.evaluation(t.head);
cout << "Evaluation : " << ans << endl;
cout<<"Tree structure"<<endl;
t.inorder2(t.head);
cout <<endl;
cout<<endl;
}
return 0;
}
Tree::Tree()
{
head = 0;
}
void Tree::createTree(string s)
{
int prec[4][2] = {'*',2, '/',2, '+',1, '-',1};
cout<< s.size()<<endl;
for(int i=0; i<s.size(); i++)
{
Node *temp = new Node();
temp->data = s[i];
temp->prior = 4;
int j;
for(j=0; j<4; j++)
{
if(temp->data == prec[j][0])
{
temp->prior = prec[j][1];
break;
}
}
if(j == 4)
{
operand(temp); // 피연산자
}
else
{
operator1(temp); //연산자
}
}
}
void Tree::operand(Node *temp)
{
Node *p;
//head가 없으면 temp를 head로 설정
if(head == 0)
{
head = temp;
}
//이미 head가 있는 경우
else
{
p = head;
if(p->right == 0) //두번째 피연산자가 처음 들어가는 경우
{
p->right = temp;
}
else
{
while(p->right != 0)
{
p = p->right;
}
p->right = temp;
}
}
}
void Tree::operator1(Node *temp)
{
if(head->prior >= temp->prior)
{
temp->left = head;
head = temp;
}
else
{
temp->left = head->right;
head->right = temp;
}
}
void Tree::inorder(Node *head)
{
if(head)
{
inorder(head->left);
cout << head->data;
inorder(head->right);
}
}
void Tree::inorder2(Node *head){
if(head){
if(head->data == '+' || head->data == '-' || head->data == '*' || head->data == '/')
cout<<"(";
inorder2(head->left);
cout<<head->data;
inorder2(head->right);
if(head->data == '+' || head->data == '-' || head->data == '*' || head->data == '/')
cout<<")";
}
}
void Tree::postorder(Node *head)
{
if(head)
{
postorder(head->left);
postorder(head->right);
cout << head->data;
}
}
void Tree::preorder(Node *head)
{
if(head)
{
cout << head->data;
preorder(head->left);
preorder(head->right);
}
}
int Tree::evaluation(Node *head1){
Node *p ;
p = head1;
int value;
if ( p!= 0){
if ( p->data >= '0' && p-> data <= '9' ){
value = p-> data -'0';
}
else
{
int left = evaluation(p->left);
int right = evaluation(p->right);
switch(p->data){
case '+': value = left +right;
break;
case '-': value = left - right;
break;
case '*': value = left * right;
break;
case '/': value = left / right;
break;
}
}
}
else
{
cout << "Empty tree"<<endl;
}
return value;
}