0% found this document useful (0 votes)
4 views13 pages

Assigment 4 - ExpressionTree

The document contains a C++ implementation of an expression tree that supports construction from postfix and prefix expressions, as well as various tree traversal methods (inorder, preorder, postorder) both recursively and iteratively. It defines two classes: 'tree' for managing the tree structure and operations, and 'stack' for handling stack operations used in the traversal algorithms. The main function provides a user interface to interact with the expression tree, allowing users to input expressions and choose traversal methods.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Assigment 4 - ExpressionTree

The document contains a C++ implementation of an expression tree that supports construction from postfix and prefix expressions, as well as various tree traversal methods (inorder, preorder, postorder) both recursively and iteratively. It defines two classes: 'tree' for managing the tree structure and operations, and 'stack' for handling stack operations used in the traversal algorithms. The main function provides a user interface to interact with the expression tree, allowing users to input expressions and choose traversal methods.

Uploaded by

jobidad431
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

#include <iostream>

#include<cstring>

using namespace std;

class tree

tree *left,*right;

char data;

public:

tree* ConstructPostfix(tree *, char []);

tree* ConstructPrefix(tree *, char []);

tree* create(tree *, char );

void inorder(tree *);

void preorder(tree *);

void postorder(tree *);

tree* iterativePreorder(tree *);

tree* iterativeInorder(tree *);

tree* iterativePostorder(tree *);

};

class stack

public:

tree *t;

stack *link;
int f;

stack* push(stack *,tree *,int );

stack* pop(stack *);

};

stack* stack :: push(stack *top,tree *temp,int f1)

stack *newnode=new stack;

newnode->t=temp;

newnode->link=NULL;

newnode->f=f1;

if(top==NULL)

top=newnode;

else

newnode->link=top;

top=newnode;

return top;

stack* stack :: pop(stack *top)

{
stack *temp=top;

if(temp==NULL)

cout <<"empty";

else

top=top->link;

delete temp;

return top;

tree* tree :: create(tree *root, char element)

tree *newnode=new tree;

newnode->data=element;

newnode->left=NULL;

newnode->right=NULL;

return newnode;

tree* tree :: ConstructPostfix(tree *root, char post[])

int i=0;
stack *top=NULL;

while(post[i]!='\0')

if(isalnum(post[i]))

root=create(root,post[i]);

i++;

top=top->push(top,root,0);

else

root=create(root,post[i]);

i++;

root->right=top->t;

top=top->pop(top);

root->left=top->t;

top=top->pop(top);

top=top->push(top,root,0);

root=top->t;

top=top->pop(top);

return root;

}
tree* tree :: ConstructPrefix(tree *root, char pre[])

stack *top=NULL;

int i=strlen(pre);

i--;

for(i;i>-1;i--)

if(isalnum(pre[i]))

root=create(root,pre[i]);

top=top->push(top,root,0);

else

root=create(root,pre[i]);

root->left=top->t;

top=top->pop(top);

root->right=top->t;

top=top->pop(top);

top=top->push(top,root,0);

root=top->t;

top=top->pop(top);
return root;

void tree :: inorder(tree *root)

if(root==NULL)

return;

else

inorder(root->left);

cout<<root->data;

inorder(root->right);

void tree :: preorder(tree *root)

if(root==NULL)

return;

else

{
cout<<root->data;

preorder(root->left);

preorder(root->right);

void tree :: postorder(tree *root)

if(root==NULL)

return;

else

postorder(root->left);

postorder(root->right);

cout<<root->data;

tree* tree :: iterativePreorder(tree *root)

stack *curr=NULL;

while(true)

{
while(root)

cout<<root->data;

curr=curr->push(curr,root->right,0);

root=root->left;

if(curr!=NULL)

root=curr->t;

curr=curr->pop(curr);

else

break;

tree* tree :: iterativeInorder(tree *root)

stack *curr=NULL;

while(true)

while(root)

{
curr=curr->push(curr,root,0);

root=root->left;

if(curr!=NULL)

root=curr->t;

cout<<root->data;

curr=curr->pop(curr);

root=root->right;

else

break;

tree* tree :: iterativePostorder(tree *root)

tree *curr=root;

stack *top=NULL;

int flag;

while (curr!=NULL || top!=NULL)

while(curr!=NULL)
{

top=top->push(top,curr,0);

curr=curr->left;

flag=top->f;

curr=top->t;

top=top->pop(top);

if(flag==1)

cout<<curr->data;

curr=NULL;

else

top=top->push(top,curr,1);

curr=curr->right;

int main()

int choice;

tree *root=NULL;

char postfix[20],prefix[20];

tree obj;
do

cout<< "\n----------------";

cout<< "\nExpression Tree";

cout<< "\n----------------";

cout<< "\n1 : Postfix Expression";

cout<< "\n2 : Prefix Expression";

cout<< "\n3 : Recursive Inorder Traversal ";

cout<< "\n4 : Recursive Preorder Traversal ";

cout<< "\n5 : Recursive Postorder Traversal ";

cout<< "\n6 : Non-recursive Inorder Traversal ";

cout<< "\n7 : Non-recursive Preorder Traversal ";

cout<< "\n8 : Non-recursive Postorder Traversal ";

cout<< "\n9 : Exit.";

cout<< "\nEnter your choice : ";

cin>>choice;

switch(choice)

case 1:

cout<< "\nEnter postfix expression : ";

cin>>postfix;

root=obj.ConstructPostfix(root,postfix);

break;

case 2:

cout<< "\nEnter prefix expression : ";


cin>>prefix;

root=obj.ConstructPrefix(root,prefix);

break;

case 3:

cout<< "\nConverted expression(inorder traversal) --> ";

obj.inorder(root);

cout<<"\n";

break;

case 4:

cout<< "\nConverted expression(preorder traversal) --> ";

obj.preorder(root);

cout<<"\n";

break;

case 5:

cout<< "\nConverted expression(postorder traversal) --> ";

obj.postorder(root);

cout<<"\n";

break;

case 6:

cout<< "\nConverted expression(inorder traversal) --> ";

obj.iterativeInorder(root);

cout<<"\n";

break;

case 7:

cout<< "\nConverted expression(preorder traversal) --> ";


obj.iterativePreorder(root);

cout<<"\n";

break;

case 8:

cout<< "\nConverted expression(postorder traversal) --> ";

obj.iterativePostorder(root);

cout<<"\n";

break;

case 9:

exit(1);

default:

cout<< "Enter proper choice";

break;

while(choice!=0);

return 0;

You might also like