-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathexpressionTree.cpp
145 lines (130 loc) · 3.19 KB
/
expressionTree.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
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
#include <iostream>
using namespace std;
struct node {
char data;
struct node* right;
struct node* left;
};
struct node* newNode(char data=' ', struct node* left=NULL, struct node* right=NULL) {
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = data;
n->left = left;
n->right = right;
return n;
}
void inorder_pre(struct node* n) {
if(n == NULL) return;
inorder_pre(n->left);
cout<<n->data<<" ";
inorder_pre(n->right);
}
void inorder_post(struct node* n) {
if(n == NULL) return;
inorder_post(n->right);
cout<<n->data<<" ";
inorder_post(n->left);
}
struct stack_node {
struct node* n;
struct stack_node* next;
};
struct stack_node* newStackNode(struct node* n, struct stack_node* next=NULL) {
struct stack_node* s;
s = (struct stack_node*)malloc(sizeof(struct stack_node));
s->n = n;
s->next = next;
return s;
}
class Stack {
struct stack_node* top;
public:
Stack() {
top = NULL;
}
void push(struct node* n) {
top = newStackNode(n, top);
}
void pop() {
if (top == NULL) return;
struct stack_node *p = top;
top = top->next;
free(p);
}
struct node* peek() {
return top->n;
}
};
int eval_pre(struct node* root) {
if (!root)
return 0;
if (!root->left && !root->right)
return (int)(root->data)-48;
int l_val = eval_pre(root->left);
int r_val = eval_pre(root->right);
if (root->data == '+')
return l_val+r_val;
if (root->data == '-')
return l_val-r_val;
if (root->data == '*')
return l_val*r_val;
return l_val/r_val;
}
int eval_post(struct node* root) {
if (!root)
return 0;
if (!root->left && !root->right)
return (int)(root->data)-48;
int l_val = eval_post(root->right);
int r_val = eval_post(root->left);
if (root->data == '+')
return l_val+r_val;
if (root->data == '-')
return l_val-r_val;
if (root->data == '*')
return l_val*r_val;
return l_val/r_val;
}
int main() {
Stack s;
int choice, size;
cin>>choice>>size;
struct node *x,*y;
char pe[40];
if (choice == 1) {
for (int i=0; i<size; i++) {
cin>>pe[i];
if (isalnum(pe[i])) {
s.push(newNode(pe[i]));
}
else {
x = s.peek();
s.pop();
y = s.peek();
s.pop();
s.push(newNode(pe[i],x,y));
}
}
inorder_post(s.peek());
cout<<endl<<eval_post(s.peek())<<endl;
}
else if (choice == 2) {
for (int i=0; i<size; i++)
cin>>pe[i];
for (int i=size-1; i>=0; i--) {
if (isalnum(pe[i])) {
s.push(newNode(pe[i]));
}
else {
x = s.peek();
s.pop();
y = s.peek();
s.pop();
s.push(newNode(pe[i],x,y));
}
}
inorder_pre(s.peek());
cout<<endl<<eval_pre(s.peek())<<endl;
}
return 0;
}