0% found this document useful (0 votes)
9 views3 pages

B4 (1) - 1

The document outlines a C++ program that constructs an expression tree from a given prefix expression and performs a non-recursive postorder traversal of the tree. It includes the implementation of a tree structure, a stack for managing nodes, and functions for displaying, traversing, and deleting the tree. The main function prompts the user for a prefix expression, builds the tree, traverses it, and deletes it afterward.

Uploaded by

rautrajeshree985
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)
9 views3 pages

B4 (1) - 1

The document outlines a C++ program that constructs an expression tree from a given prefix expression and performs a non-recursive postorder traversal of the tree. It includes the implementation of a tree structure, a stack for managing nodes, and functions for displaying, traversing, and deleting the tree. The main function prompts the user for a prefix expression, builds the tree, traverses it, and deletes it afterward.

Uploaded by

rautrajeshree985
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/ 3

Name : Anushka Gaikwad

Div : A
Roll No : 33

Title : Construct an expression tree from the given prefix expression


eg. +--a*bc/def and traverse it using postor dertraversal (non
recursive) and then delete the entire tree.

-----------------------------------------------------------------------------------
------------------------------------------------------

#include<iostream>
#include<string.h>
using namespace std;
struct node
{ char data;
node *left;
node *right;
}; class
tree
{
char prefix[20];
public: node
*top;
void expression(char[]); void
display(node*); void
non_rec_postorder(node*);
void del(node*);
};
class stack1
{
node *data[30];
int top;
public:
stack1()
{ top = -
1;
}
int empty()
{ if(top == -
1) return 1;
return 0;
}
void push(node *p)
{
data[++top]=p;
}
node *pop()
{
return(data[top--]);
}
};
void tree::expression(char prefix[])
{ char c; stack1 s;
node *t1,*t2; int len,
i; len =
strlen(prefix); for(i =
len-1;i>=0;i--)
{ top= new node; top-
>left = NULL; top-
>right = NULL; if
(isalpha(prefix[i]))
{
top->data = prefix[i];
s.push(top);
}
else if (prefix[i]=='+'||prefix[i]=='*'||prefix[i]=='-'||prefix[i]=='/')
{ t2 =s.pop(); t1
=s.pop(); top->data =
prefix[i]; top->left =
t2; top->right = t1;
s.push(top);
}
}
top = s.pop();
}
void tree::display(node *root)
{
if (root!=NULL)
{
cout<<" \n"; cout<<root-
>data; display(root->left);
display(root->right);
}
}
void tree::non_rec_postorder(node *top)
{
stack1 s1,s2;/*stack s1 is being used for flag. A NULL data implies that the right
subtree has not been visited*/
node*T= top; cout<<"
\n";
s1.push(T); while
(!s1.empty())
{
T=s1.pop();
s2.push(T); if(T-
>left!=NULL)
s1.push(T->right);
}
while(!s2.empty())
{ top= s2.pop();
cout<<top->data;
}
}
void tree::del(node*node)
{ if(node ==
NULL) return;
/* first delete both subtree */
del(node->left); del(node-
>right);
/* then delete the node */ cout<<endl<<"Deleting
node:"<< node->data<<endl;
delete(node);
} int
main()
{ char
expr[20]; tree
t;
cout<<"Enter prefix Expression:";
cin>>expr;
t.expression(expr);
//t.display(t.top);
//cout<<endl;
t.non_rec_postorder(t.top);
t.del(t.top);
//t.display(t.top);
}

You might also like