0% found this document useful (0 votes)
2 views

dsa4

The document contains C++ code for constructing and manipulating an expression tree from a prefix expression. It includes functions for validating the prefix expression, building the tree, displaying it level-wise, performing a non-recursive post-order traversal, and deleting the tree. The main function prompts the user for a valid prefix expression and executes these operations accordingly.

Uploaded by

abhinavsargar1
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)
2 views

dsa4

The document contains C++ code for constructing and manipulating an expression tree from a prefix expression. It includes functions for validating the prefix expression, building the tree, displaying it level-wise, performing a non-recursive post-order traversal, and deleting the tree. The main function prompts the user for a valid prefix expression and executes these operations accordingly.

Uploaded by

abhinavsargar1
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/ 8

#include <iostream>

#include <string.h>

#include <cctype>

#include <queue>

using namespace std;

struct node

char data;

node *left;

node *right;

};

class tree

public:

node *top;

void expression(char[]);

void display_levelwise(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--]);

};

bool isValidPrefix(char expr[]) {

int operandCount = 0;

int operatorCount = 0;

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

if (isalnum(expr[i])) {

operandCount++;

} else if (expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/') {


operatorCount++;

} else {

return false; // Invalid character

// For a valid prefix, the number of operands must always be greater than or equal to the
number of operators

if (operandCount <= operatorCount) {

return false;

return (operandCount == operatorCount + 1); // Valid prefix if operands = operators + 1

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 (isalnum(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_levelwise(node *root)

if (root == NULL)

return;

queue<node *> q;

q.push(root);

cout << "Expression Tree (Level Order):\n";

int level = 0;

while (!q.empty())
{

int levelNodeCount = q.size();

cout << "Level " << level++ << ": ";

while (levelNodeCount > 0)

node *node = q.front();

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

q.pop();

if (node->left != NULL)

q.push(node->left);

if (node->right != NULL)

q.push(node->right);

levelNodeCount--;

cout << endl;

void tree::non_rec_postorder(node *top)

stack1 s1, s2; // stack s1 is for flag to check the left-right subtree

node *T = top;

cout << "\nPost 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)

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 << "\nDeleting node: " << node->data;

free(node);

int main()

{
char expr[20];

tree t;

// Get prefix expression input from the user

while (true) {

cout << "Enter a Prefix Expression: ";

cin >> expr;

if (isValidPrefix(expr)) {

break; // valid prefix expression entered

} else {

cout << "Wrong! It is not a Prefix Expression. Try again.\n";

// Build the expression tree from the valid prefix expression

t.expression(expr);

// Displaying the tree level-wise

t.display_levelwise(t.top);

// Post-order traversal of the tree

t.non_rec_postorder(t.top);

// Deleting the entire tree

t.del(t.top);

cout << "\nTree is Deleted. Exiting...\n";

return 0;

}
OUTPUT:

You might also like