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

pr-4 Expression tree

The document presents a C++ program for constructing expression trees from postfix and prefix expressions. It includes implementations for both recursive and non-recursive in-order, pre-order, and post-order traversals of the trees. The main function demonstrates the construction and traversal of trees using sample postfix and prefix expressions.

Uploaded by

amarkamthe22
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)
4 views

pr-4 Expression tree

The document presents a C++ program for constructing expression trees from postfix and prefix expressions. It includes implementations for both recursive and non-recursive in-order, pre-order, and post-order traversals of the trees. The main function demonstrates the construction and traversal of trees using sample postfix and prefix expressions.

Uploaded by

amarkamthe22
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

Name : Arya Kiran Dandnaik

Roll No : S511073

Division : A

Subject : DSA

Problem Statement : To construct an Expression Tree from postfix and prefix expression. Perform
recursive and non- recursive in-order, pre-order and post-order traversals.

#include <iostream>

#include <stack>

#include <string>

using namespace std;

struct Node

char value;

Node* left;

Node* right;

Node(char val) : value(val), left(nullptr), right(nullptr) {}

};

Node* constructPostfixTree(const string& postfix) {

stack<Node*> st;

for (char ch : postfix)

if (isalnum(ch))

st.push(new Node(ch));

else

Node* right = st.top(); st.pop();


Node* left = st.top(); st.pop();

Node* node = new Node(ch);

node->left = left;

node->right = right;

st.push(node);

return st.top();

Node* constructPrefixTree(const string& prefix)

stack<Node*> st;

for (int i = prefix.length() - 1; i >= 0; --i)

char ch = prefix[i];

if (isalnum(ch))

st.push(new Node(ch));

else

Node* left = st.top(); st.pop();

Node* right = st.top(); st.pop();

Node* node = new Node(ch);

node->left = left;

node->right = right;

st.push(node);

return st.top();
}

void inorderRecursive(Node* node)

if (node)

inorderRecursive(node->left);

cout << node->value << " ";

inorderRecursive(node->right);

void inorderNonRecursive(Node* root)

stack<Node*> st;

Node* current = root;

while (current || !st.empty())

while (current) {

st.push(current);

current = current->left;

current = st.top(); st.pop();

cout << current->value << " ";

current = current->right;

void preorderRecursive(Node* node)

if (node)
{

cout << node->value << " ";

preorderRecursive(node->left);

preorderRecursive(node->right);

void preorderNonRecursive(Node* root)

if (!root) return;

stack<Node*> st;

st.push(root);

while (!st.empty())

Node* current = st.top(); st.pop();

cout << current->value << " ";

if (current->right) st.push(current->right);

if (current->left) st.push(current->left);

void postorderRecursive(Node* node)

if (node)

postorderRecursive(node->left);

postorderRecursive(node->right);

cout << node->value << " ";

}
void postorderNonRecursive(Node* root)

if (!root) return;

stack<Node*> stack1, stack2;

stack1.push(root);

while (!stack1.empty())

Node* current = stack1.top(); stack1.pop();

stack2.push(current);

if (current->left) stack1.push(current->left);

if (current->right) stack1.push(current->right);

while (!stack2.empty())

cout << stack2.top()->value << " ";

stack2.pop();

int main()

string postfix = "AB+C*";

string prefix = "*+ABC";

Node* postfixTree = constructPostfixTree(postfix);

Node* prefixTree = constructPrefixTree(prefix);

cout << "Postfix Expression Tree Traversals:" << endl;

cout << "In-order (Recursive): ";

inorderRecursive(postfixTree);

cout << "\nIn-order (Non-Recursive): ";


inorderNonRecursive(postfixTree);

cout << "\nPre-order (Recursive): ";

preorderRecursive(postfixTree);

cout << "\nPre-order (Non-Recursive): ";

preorderNonRecursive(postfixTree);

cout << "\nPost-order (Recursive): ";

postorderRecursive(postfixTree);

cout << "\nPost-order (Non-Recursive): ";

postorderNonRecursive(postfixTree);

cout << endl;

cout << "Prefix Expression Tree Traversals:" << endl;

cout << "In-order (Recursive): ";

inorderRecursive(prefixTree);

cout << "\nIn-order (Non-Recursive): ";

inorderNonRecursive(prefixTree);

cout << "\nPre-order (Recursive): ";

preorderRecursive(prefixTree);

cout << "\nPre-order (Non-Recursive): ";

preorderNonRecursive(prefixTree);

cout << "\nPost-order (Recursive): ";

postorderRecursive(prefixTree);

cout << "\nPost-order (Non-Recursive): ";

postorderNonRecursive(prefixTree);

cout << endl;

return 0;

}
Output :

You might also like