-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_tree.cpp
More file actions
74 lines (72 loc) · 1.54 KB
/
Copy pathexpression_tree.cpp
File metadata and controls
74 lines (72 loc) · 1.54 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
#include<bits/stdc++.h>
using namespace std;
struct node{
char data;
node* left;
node* right;
node(char data){
this->data = data;
left = NULL;
right = NULL;
}
};
class stk{
public:
int top = -1;
node* arr[30];
void push(node* data){
if(top==29) return;
top++;
arr[top]= data;
}
node* pop(){
if(top==-1) return NULL;
node* t = arr[top];
top--;
return t;
}
bool isempty(){
return top == -1;
}
};
class tree{
public:
node* convert(char *ptr,int len){
stk st;
node* last;
while(len--){
node* nn = new node(*ptr);
if(isalpha(*ptr)) {st.push(nn) ;ptr-=1;}
else{
node* t1 = st.pop();
node* t2 = st.pop();
nn->left = t1;
nn->right = t2;
st.push(nn);
ptr-=1;
} }
last = st.pop();
}
void disspost(node* root){
stk s1,s2;
if(root!=NULL) s1.push(root);
while(!s1.isempty()){
node* t = s1.pop();
if(t->left) s1.push(t->left);
if(t->right) s1.push(t->right);
s2.push(t);
}
while(!s2.isempty()){
cout<<(s2.pop())->data;
}
}
};
int main(){
char exp[30];
cin>>exp;
int len = strlen(exp);
tree T;
node* root = T.convert(&exp[len-1],len);
T.disspost(root);
return 0;
}