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

PR 5 B-7

The document contains C++ code for a binary tree implementation that processes prefix expressions. It includes methods for pushing and popping nodes, displaying the tree, and converting prefix expressions to postfix format. The main function prompts the user for a prefix expression and executes the conversion and deletion of nodes in the tree.

Uploaded by

khodesnehal2503
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 views3 pages

PR 5 B-7

The document contains C++ code for a binary tree implementation that processes prefix expressions. It includes methods for pushing and popping nodes, displaying the tree, and converting prefix expressions to postfix format. The main function prompts the user for a prefix expression and executes the conversion and deletion of nodes in the tree.

Uploaded by

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

#include <iostream> {

#include <string.h> if (top == -1)

using namespace std; return 1;

return 0;

struct node }

{ void push(node *p)

char data; {

node *left; data[++top] = p;

node *right; }

}; node *pop()

class tree {

{ return (data[top--]);

char prefix[20]; }

};

public: void tree::expression(char prefix[])

node *top; {

void expression(char[]); char c;

void display(node *); stack1 s;

void non_rec_postorder(node *); node *t1, *t2;

void del(node *); int len, i;

}; len = strlen(prefix);

class stack1 for (i = len - 1; i >= 0; i--)

{ {

node *data[30]; top = new node;

int top; top->left = NULL;

top->right = NULL;

public: if (isalpha(prefix[i]))

stack1() {

{ top->data = prefix[i];

top = -1; s.push(top);

} }

int empty()
else if (prefix[i] == '+' || prefix[i] == '*' || prefix[i] s2.push(T);
== '-' || prefix[i] == '/')
if (T->left != NULL)
{
s1.push(T->left);
t2 = s.pop();
if (T->right != NULL)
t1 = s.pop();
s1.push(T->right);
top->data = prefix[i];
}
top->left = t2;
while (!s2.empty())
top->right = t1;
{
s.push(top);
top = s2.pop();
}
cout << top->data;
}
}
top = s.pop();
}
}
void tree::del(node *node)
void tree::display(node *root)
{
{
if (node == NULL)
if (root != NULL)
return;
{
/* first delete both subtrees */
cout << root->data;
del(node->left);
display(root->left);
del(node->right);
display(root->right);
/* then delete the node */
}
cout <<endl<<"Deleting node : " << node-
} >data<<endl;

void tree::non_rec_postorder(node *top) delete node;

{ }

stack1 s1, s2; /*stack s1 is being used for flag . A NULL int main()
data implies that the right subtree has not been visited
{
*/
char expr[20];
node *T = top;
tree t;
cout << "\n";

s1.push(T);
cout <<"Enter prefix Expression : ";
while (!s1.empty())
cin >> expr;
{
cout << expr;
T = s1.pop();
cout <<"Postfix Expression : "; t.non_rec_postorder(t.top);

t.expression(expr); t.del(t.top);

//t.display(t.top); // t.display(t.top);

//cout<<endl; }

You might also like