0% found this document useful (0 votes)
17 views

Expression Tree: Void Expressiontree::Inorder (Node T)

This document defines an ExpressionTree class that represents a binary tree. It includes functions to create nodes, traverse the tree using preorder, inorder, and postorder ordering, and print the results. These functions are used to build a tree from a postfix expression, then print the expression in each traversal order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Expression Tree: Void Expressiontree::Inorder (Node T)

This document defines an ExpressionTree class that represents a binary tree. It includes functions to create nodes, traverse the tree using preorder, inorder, and postorder ordering, and print the results. These functions are used to build a tree from a postfix expression, then print the expression in each traversal order.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Expression Tree

#include<iostream> void ExpressionTree::inorder(node *t)


#include<cstdlib> {
using namespace std; if(t!=NULL)
{
struct node inorder(t->left);
{ printf("%c",t->data);
char data; inorder(t->right);
struct node *left,*right; }
}; }
typedef struct node node;
void ExpressionTree::postorder(node *t)
class ExpressionTree {
{ if(t!=NULL)
public: {
node *create(char x); postorder(t->left);
void preorder(node *t); postorder(t->right);
void inorder(node*t); printf("%c",t->data);
void postorder(node*t); }
}; }

node* ExpressionTree::create(char x) int main()


{ {
node *t=new node; char postfix[30];
int i,top=-1;
t->data=x; node *t,*stk[30];
t->left=NULL; ExpressionTree obj;
t->right=NULL; cout<<"enter postfix expression : ";
return(t); cin>>postfix;
} for(i=0;postfix[i]!='\0';i++)
{
t=obj.create(postfix[i]);
void ExpressionTree::preorder(node *t) if(!isalpha(postfix[i]))
{ {
if(t!=NULL) t->right=stk[top--];
{ t->left=stk[top--];
printf("%c",t->data); }
preorder(t->left); stk[++top]=t;
preorder(t->right); }
} t=stk[top];
} cout<<"\npreorder expression is : ";
obj.preorder(t);
cout<<"\ninorder expression is : ";
obj.inorder(t);
cout<<"\npostorder expression is : ";
obj.postorder(t);
}

You might also like