0% found this document useful (0 votes)
18 views5 pages

Umesh Prog5

The document describes 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 definitions for a Node structure, a Tree class for managing the tree, and a Stack class for handling node pointers. The program also deletes the entire tree after traversal, ensuring proper memory management.

Uploaded by

softengineer201
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)
18 views5 pages

Umesh Prog5

The document describes 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 definitions for a Node structure, a Tree class for managing the tree, and a Stack class for handling node pointers. The program also deletes the entire tree after traversal, ensuring proper memory management.

Uploaded by

softengineer201
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/ 5

Name : Umesh .V.

Jadhav

Practical No : 5(B)

Batch : S1

Roll No : 20

Branch : AI & DS

/*

Construct an expression tree from the given prefix expression eg. +--a*bc/def and traverse it using
post order traversal (non recursive) and then delete the entire tree.

*/

#include <iostream>

#include <cstring>

#include <cctype>

using namespace std;

// Node structure for the expression tree

struct Node {

char data;

Node *left, *right;

};

// Tree class

class Tree {

public:

Node *top; // Root of the tree

Tree() { top = nullptr; }

void constructExpressionTree(const char prefix[]);

void nonRecursivePostOrder(Node *root);


void deleteTree(Node *node);

};

// Stack class for Node pointers

class Stack {

Node *data[30];

int top;

public:

Stack() { top = -1; }

bool isEmpty() { return top == -1; }

void push(Node *p) { data[++top] = p; }

Node *pop() { return data[top--]; }

};

// Construct the expression tree from the prefix expression

void Tree::constructExpressionTree(const char prefix[]) {

Stack s;

int len = strlen(prefix);

for (int i = len - 1; i >= 0; i--) {

char c = prefix[i];

Node *newNode = new Node;

newNode->data = c;

newNode->left = newNode->right = nullptr;

if (isalpha(c)) {

// If the character is an operand, push it onto the stack

s.push(newNode);

} else if (c == '+' || c == '-' || c == '*' || c == '/') {

// If the character is an operator, pop two nodes, make them children


Node *t1 = s.pop();

Node *t2 = s.pop();

newNode->left = t1;

newNode->right = t2;

s.push(newNode);

// The final node in the stack is the root of the tree

top = s.pop();

// Perform non-recursive postorder traversal

void Tree::nonRecursivePostOrder(Node *root) {

if (!root) return;

Stack s1, s2;

s1.push(root);

// First pass to populate the second stack

while (!s1.isEmpty()) {

Node *node = s1.pop();

s2.push(node);

if (node->left) s1.push(node->left);

if (node->right) s1.push(node->right);

// Second pass to print in postorder

while (!s2.isEmpty()) {

Node *node = s2.pop();

cout << node->data << " ";

cout << endl;

// Delete the entire tree


void Tree::deleteTree(Node *node) {

if (node == nullptr) return;

// Recursively delete left and right subtrees

deleteTree(node->left);

deleteTree(node->right);

// Delete the current node

cout << "Deleting node: " << node->data << endl;

delete node;

int main() {

Tree t;

char expr[20];

cout << "Enter prefix expression: ";

cin >> expr;

// Construct the expression tree

t.constructExpressionTree(expr);

// Non-recursive postorder traversal

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

t.nonRecursivePostOrder(t.top);

// Delete the entire tree

t.deleteTree(t.top);

t.top = nullptr; // Clear the root pointer after deletion

return 0;

}
Output :

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ cd ..

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:/home$ cd computer/

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ g++ Program5.cpp

(base) computer@computer-ThinkCentre-neo-50s-Gen-3:~$ ./a.out

Enter prefix expression: +--a*bc/def

Postorder traversal (non-recursive): a b c * - d e f / - +

Deleting node: a

Deleting node: b

Deleting node: c

Deleting node: *

Deleting node: -

Deleting node: d

Deleting node: e

Deleting node: f

Deleting node: /

Deleting node: -

Deleting node: +

You might also like