0% found this document useful (0 votes)
15 views6 pages

Dsa Prac 4

The document contains a C++ program that constructs an expression tree from a given prefix expression and performs post-order traversal. It includes definitions for a node structure, a tree class for managing the expression tree, and a stack class for handling nodes. The program also demonstrates the deletion of the entire tree after traversal, with a sample output showing the input and results.

Uploaded by

nikamshivani112
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)
15 views6 pages

Dsa Prac 4

The document contains a C++ program that constructs an expression tree from a given prefix expression and performs post-order traversal. It includes definitions for a node structure, a tree class for managing the expression tree, and a stack class for handling nodes. The program also demonstrates the deletion of the entire tree after traversal, with a sample output showing the input and results.

Uploaded by

nikamshivani112
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/ 6

/* NAME: SHIVANI NIKAM

CLASS: SE. DIV: B

ROLL NO. : B 64

Lab Asssignment : 4

Statement: Construct an expression tree from the given prefix and traverse it using post order
traversal and then delete the entire tree */

#include<iostream>

#include<string.h>

using namespace std;

struct node{

char data;

node *left;

node *right;

};

class tree

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() {

return (top == -1);

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 = strlen(prefix);

for(int 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 << root->data;

display(root->left);

display(root->right);

void tree::non_rec_postorder(node *top) {

stack1 s1, s2;

node *T = top;

cout << "\n";


s1.push(T);

while (!s1.empty()) {

T = s1.pop();

s2.push(T);

if (T->left != NULL)

s1.push(T->left);

if (T->right != NULL)

s1.push(T->right);

while (!s2.empty()) {

top = s2.pop();

cout << top->data;

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;

t.expression(expr);

cout << "Postorder traversal (non-recursive): ";

t.non_rec_postorder(t.top);

t.del(t.top);

return 0;

}
NAME: SHIVANI NIKAM

CLASS: SE​ DIV: B

ROLL NO. : B 64

OUTPUT:

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

Enter prefix expression: *+ab-cd

Postorder traversal (non-recursive):

ab+cd-*

deleting node: a

deleting node: b

deleting node: +

deleting node: c

deleting node: d

deleting node: -

deleting node: *

=== Code Execution Successful ===

You might also like