0% found this document useful (0 votes)
6 views7 pages

Dsa4-1

The document presents a C++ program that constructs a binary tree from a prefix expression and performs a non-recursive postorder traversal. It includes a stack implementation to facilitate tree operations and defines methods for expression parsing, displaying the tree, and deleting nodes. The main function prompts the user for a prefix expression, builds the tree, displays the traversal, and cleans up memory by deleting the tree nodes.

Uploaded by

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

Dsa4-1

The document presents a C++ program that constructs a binary tree from a prefix expression and performs a non-recursive postorder traversal. It includes a stack implementation to facilitate tree operations and defines methods for expression parsing, displaying the tree, and deleting nodes. The main function prompts the user for a prefix expression, builds the tree, displays the traversal, and cleans up memory by deleting the tree nodes.

Uploaded by

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

PRACTICAL - 4

Name: KUNDAN DHOKE Date:


Div: B
Roll No: 20
-------------------------------------------------------------------------------------------

Program:
#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;
node*T = top;
cout<<"post order traversal: ";
s1.push(T);
while(!s1.empty())
{
T=s1.pop();
s2.push(T);
if(T->left!=NULL)
{
s1.push(T->left);
}
if(T->right!=NULL)
{
s2.push(T->right);
}
}
while(!s2.empty())
{
top= s2.pop();
cout<<top->data<<" ";
}
cout<<endl;
}
void tree::del(node*node)
{
if(node == NULL)
return;
del(node->left);
del(node->right);
cout<<endl<<"Deleting node:"<< node->data<<endl;
delete(node);
}
int main()
{
char expr[20];
tree t;
cout<<"Enter prefix Expression:";
cin>>expr;
cout<<endl;
t.expression(expr);
t.non_rec_postorder(t.top);
t.del(t.top);
}
Output -

You might also like