Assigment 4 - ExpressionTree
Assigment 4 - ExpressionTree
#include<cstring>
class tree
tree *left,*right;
char data;
public:
};
class stack
public:
tree *t;
stack *link;
int f;
};
newnode->t=temp;
newnode->link=NULL;
newnode->f=f1;
if(top==NULL)
top=newnode;
else
newnode->link=top;
top=newnode;
return top;
{
stack *temp=top;
if(temp==NULL)
cout <<"empty";
else
top=top->link;
delete temp;
return top;
newnode->data=element;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
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;
if(root==NULL)
return;
else
inorder(root->left);
cout<<root->data;
inorder(root->right);
if(root==NULL)
return;
else
{
cout<<root->data;
preorder(root->left);
preorder(root->right);
if(root==NULL)
return;
else
postorder(root->left);
postorder(root->right);
cout<<root->data;
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;
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 *curr=root;
stack *top=NULL;
int flag;
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<< "\n----------------";
cin>>choice;
switch(choice)
case 1:
cin>>postfix;
root=obj.ConstructPostfix(root,postfix);
break;
case 2:
root=obj.ConstructPrefix(root,prefix);
break;
case 3:
obj.inorder(root);
cout<<"\n";
break;
case 4:
obj.preorder(root);
cout<<"\n";
break;
case 5:
obj.postorder(root);
cout<<"\n";
break;
case 6:
obj.iterativeInorder(root);
cout<<"\n";
break;
case 7:
cout<<"\n";
break;
case 8:
obj.iterativePostorder(root);
cout<<"\n";
break;
case 9:
exit(1);
default:
break;
while(choice!=0);
return 0;