-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressionTree.cpp
More file actions
108 lines (90 loc) · 2.26 KB
/
Copy pathexpressionTree.cpp
File metadata and controls
108 lines (90 loc) · 2.26 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
//varun ved cisp 430
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct node{
char data;
node *left;
node *right;
};
char postfix[35];
int top=-1;
node *arr[35];
int checkChar(char inputchar){ //for checking symbol is operand or operator
if(inputchar=='+' || inputchar=='-' || inputchar=='*' || inputchar=='/')
return(-1);
else if(inputchar>='a' || inputchar<='z')
return(1);
else if(inputchar>='A' || inputchar<='Z')
return(1);
else return(-99); //for error
}
//it is used for inseting an single element in//a tree, i.e. is pushing of single element.
void push(node *tree){
top++;
arr[top]=tree;
}
node *pop(){
top--;
return(arr[top+1]);
}
void create_expr_tree(char *suffix){
char symbol;
node *newl,*ptr1,*ptr2;
int flag; //flag=-1 when operator and flag=1 when operand
symbol = suffix[0]; //Read the first symbol from the postfix expr.
//for(int i=1;symbol!=NULL;i++){ //continue till end of the expr.
int i =1;
while(symbol!='\0')
{
flag=checkChar(symbol); //check symbol is operand or operator.
if(flag == 1)//if symbol is operand.
{
newl = new node;
newl->data = symbol;
newl->left = NULL;
newl->right = NULL;
push(newl);
}
else{ //If the symbol is operato pop two top elements.
ptr1=pop();
ptr2=pop();
newl = new node;
newl->data = symbol;
newl->left = ptr2;
newl->right = ptr1;
push(newl);
}
symbol=suffix[i];
i++;
}
}
bool isLeaf(struct node* tree) {
return tree->left == 0 && tree->right == 0;
}
void inOrder(node *tree){
if( tree!=NULL){
if(isLeaf(tree)==true)
{
cout << tree->data;
}
else
{
cout << "(";
inOrder( tree->left);
cout<< tree->data;
inOrder(tree->right);
cout << ")";
}
}
}
int main(){
cout<<"Postfix? : ";
cin>>postfix;
//Creation of Expression Tree
create_expr_tree(postfix);
//Traversal Operation on expression tree
cout<<"\nIn-Order Traversal : ";
inOrder(arr[0]);
}