C++ program to evaluate expression tree
C++ program to evaluate expression tree
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
struct tree_node
{
char var[10];
tree_node *left,*right;
} ;
class exp_tree
{
char post[30];
tree_node *root;
public:
exp_tree()
{
root=NULL;
}
void get_exp()
{
cout<<"Enter the postfix expression ";
cin.getline(post,30);
}
tree_node* get_root()
{
return root;
}
void exp_to_tree();
float evl_tree(tree_node *);
void inorder(tree_node *);
void preorder(tree_node *);
void postorder(tree_node *);
};
float exp_tree::evl_tree(tree_node *temp)
{
if(temp->var[0]>47&&temp->var[0]<58)
return atof(temp->var);
switch(temp->var[0])
{
case '+':
return evl_tree(temp->left)+evl_tree(temp->right);
case '-':
return evl_tree(temp->left)-evl_tree(temp->right);
case '*':
return evl_tree(temp->left)*evl_tree(temp->right);
case '/':
return evl_tree(temp->left)/evl_tree(temp->right);
case '^':
return powf(atof((temp->left)->var),atof((temp->right)->var));
}
}
void exp_tree::exp_to_tree()
{
int top=-1,z;
tree_node *stack[20];
for(int i=0;post[i]!=0;i++)
{
if(post[i]==' ')
continue;
stack[++top]=new tree_node;
if(post[i]>=48&&post[i]<=57)
{
int j;
for(j=0;post[i]!=' ';j++,i++)
stack[top]->var[j]=post[i];
stack[top]->var[j]='\0';
stack[top]->left=stack[top]->right=NULL;
}
else
{
stack[top]->var[0]=post[i];
stack[top]->var[1]='\0';
stack[top]->left=stack[top-2];
stack[top]->right=stack[top-1];
top=top-2;
root=stack[top]=stack[top+2];
}
}
}
void exp_tree::inorder(tree_node *temp)
{
if(temp==NULL)
return;
inorder(temp->left);
cout<<temp->var<<" ";
inorder(temp->right);
}
void exp_tree::postorder(tree_node *temp)
{
if(temp==NULL)
return;
postorder(temp->left);
postorder(temp->right);
cout<<temp->var<<" ";
}
void exp_tree::preorder(tree_node *temp)
{
if(temp==NULL)
return;
cout<<temp->var<<" ";
preorder(temp->left);
preorder(temp->right);
}
int main()
{
exp_tree exp;
exp.get_exp();
exp.exp_to_tree();
cout<<"Expression tree Successfully created "<<endl;
cout<<"Inorder Traversal : ";
exp.inorder(exp.get_root());
cout<<endl<<"Postorder Traversal : ";
exp.postorder(exp.get_root());
cout<<endl<<"Preorder Traversal : ";
exp.preorder(exp.get_root());
cout<<endl<<endl<<"Result : "<<exp.evl_tree(exp.get_root())<<endl<<endl;
return 0;
}
#include<stdlib.h>
#include<math.h>
using namespace std;
struct tree_node
{
char var[10];
tree_node *left,*right;
} ;
class exp_tree
{
char post[30];
tree_node *root;
public:
exp_tree()
{
root=NULL;
}
void get_exp()
{
cout<<"Enter the postfix expression ";
cin.getline(post,30);
}
tree_node* get_root()
{
return root;
}
void exp_to_tree();
float evl_tree(tree_node *);
void inorder(tree_node *);
void preorder(tree_node *);
void postorder(tree_node *);
};
float exp_tree::evl_tree(tree_node *temp)
{
if(temp->var[0]>47&&temp->var[0]<58)
return atof(temp->var);
switch(temp->var[0])
{
case '+':
return evl_tree(temp->left)+evl_tree(temp->right);
case '-':
return evl_tree(temp->left)-evl_tree(temp->right);
case '*':
return evl_tree(temp->left)*evl_tree(temp->right);
case '/':
return evl_tree(temp->left)/evl_tree(temp->right);
case '^':
return powf(atof((temp->left)->var),atof((temp->right)->var));
}
}
void exp_tree::exp_to_tree()
{
int top=-1,z;
tree_node *stack[20];
for(int i=0;post[i]!=0;i++)
{
if(post[i]==' ')
continue;
stack[++top]=new tree_node;
if(post[i]>=48&&post[i]<=57)
{
int j;
for(j=0;post[i]!=' ';j++,i++)
stack[top]->var[j]=post[i];
stack[top]->var[j]='\0';
stack[top]->left=stack[top]->right=NULL;
}
else
{
stack[top]->var[0]=post[i];
stack[top]->var[1]='\0';
stack[top]->left=stack[top-2];
stack[top]->right=stack[top-1];
top=top-2;
root=stack[top]=stack[top+2];
}
}
}
void exp_tree::inorder(tree_node *temp)
{
if(temp==NULL)
return;
inorder(temp->left);
cout<<temp->var<<" ";
inorder(temp->right);
}
void exp_tree::postorder(tree_node *temp)
{
if(temp==NULL)
return;
postorder(temp->left);
postorder(temp->right);
cout<<temp->var<<" ";
}
void exp_tree::preorder(tree_node *temp)
{
if(temp==NULL)
return;
cout<<temp->var<<" ";
preorder(temp->left);
preorder(temp->right);
}
int main()
{
exp_tree exp;
exp.get_exp();
exp.exp_to_tree();
cout<<"Expression tree Successfully created "<<endl;
cout<<"Inorder Traversal : ";
exp.inorder(exp.get_root());
cout<<endl<<"Postorder Traversal : ";
exp.postorder(exp.get_root());
cout<<endl<<"Preorder Traversal : ";
exp.preorder(exp.get_root());
cout<<endl<<endl<<"Result : "<<exp.evl_tree(exp.get_root())<<endl<<endl;
return 0;
}
No comments:
Post a Comment