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

Assignment 2

The assignment focuses on constructing an expression tree from a given postfix expression using a stack data structure and performing non-recursive traversals (in-order, pre-order, and post-order). It provides theoretical background on expression trees and postfix expressions, along with a code implementation in C++. The conclusion emphasizes the importance of understanding tree data structures and their applications in expression evaluation and algorithm efficiency.

Uploaded by

adityaparade20
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 views5 pages

Assignment 2

The assignment focuses on constructing an expression tree from a given postfix expression using a stack data structure and performing non-recursive traversals (in-order, pre-order, and post-order). It provides theoretical background on expression trees and postfix expressions, along with a code implementation in C++. The conclusion emphasizes the importance of understanding tree data structures and their applications in expression evaluation and algorithm efficiency.

Uploaded by

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

ASSIGNMENT NO.

TITLE: Expression Tree Construction from Postfix Expression

AIM:
A. Construct an expression tree from a given postfix expression and perform non-recursive traversal
operations.
a. Build the expression tree using a stack data structure.
b. Perform non-recursive traversals: in-order, pre-order, and post-order.

Relevant Course Objectives:

● To understand the concept and structure of expression trees.

● To learn how to convert a postfix expression into an expression tree.

● To implement non-recursive traversal techniques for tree structures.

Theory:

● Expression Tree: A binary tree used to represent expressions. In an expression tree, each
internal node corresponds to an operator and each leaf node corresponds to an operand
(constant or variable).

● Postfix Expression (Reverse Polish Notation): An expression where the operator follows the
operands. For example, the postfix expression AB+ corresponds to the infix expression A + B.

● Construction of Expression Tree:

o Read the postfix expression from left to right.

o For each operand, create a new tree node and push it onto a stack.

o For each operator, pop the top two nodes from the stack, create a new node with
the operator, and make the two nodes the left and right children of the new node.
Then push the new node back onto the stack.

o At the end of the expression, the stack will contain the root of the expression tree.

● Non-Recursive Tree Traversals:

o In-Order Traversal: Left, Root, Right

o Pre-Order Traversal: Root, Left, Right

o Post-Order Traversal: Left, Right, Root

Terminologies:

● Node: The basic unit of an expression tree, containing an operator or operand.

● Root: The top node of the expression tree.

● Leaf Node: A node that represents an operand with no children.


● Stack: A data structure used to store nodes temporarily during the construction of the tree
and traversal.

Code:-
#include <iostream>
#include <stack>
#include <string>

using namespace std;

struct Node
{
char data;
Node *left, *right;
};

Node *newNode(char data)


{
Node *node = new Node();
node->data = data;
node->left = node->right = nullptr;
return node;
}

bool isOperand(char ch)


{
return isalpha(ch) || isdigit(ch);
}

Node *constructTree(string postfix)


{
stack<Node *> st;
for (char ch : postfix)
{
Node *t;
if (isOperand(ch))
{
t = newNode(ch);
}
else
{
t = newNode(ch);
t->right = st.top();
st.pop();
t->left = st.top();
st.pop();
}
st.push(t);
}
return st.top();
}

void inorder(Node *root)


{
stack<Node *> st;
Node *curr = root;

while (curr != nullptr || !st.empty())


{
while (curr != nullptr)
{
st.push(curr);
curr = curr->left;
}

curr = st.top();
st.pop();
cout << curr->data << " ";
curr = curr->right;
}
}
void preorder(Node * root)
{
if (root == nullptr)
return;

stack<Node *> st;


st.push(root);

while (!st.empty())
{
Node *curr = st.top();
st.pop();
cout << curr->data
<< " ";

if (curr->right)
st.push(curr->right);
if (curr->left)
st.push(curr->left);
}
}

void postorder(Node *root)


{
if (root == nullptr)
return;

stack<Node *> s1, s2;


s1.push(root);

while (!s1.empty())
{
Node *curr = s1.top();
s1.pop();
s2.push(curr);

if (curr->left)
s1.push(curr->left);
if (curr->right)
s1.push(curr->right);
}

while (!s2.empty())
{
cout << s2.top()->data << " ";
s2.pop();
}
}

int main()
{
string postfix;
cout << "Enter postfix expression: ";
cin >> postfix;
Node *root = constructTree(postfix);
cout << "Inorder traversal of expression tree: ";
inorder(root);
cout << endl;
cout << "preorder traversal: ";
preorder(root);
cout << endl;
cout << "postorder traversal: ";
postorder(root);
cout << endl;

return 0;
}
Output: PS D:\cpp> cd "d:\cpp\.vscode\2nd Yr\" ; if ($?) { g++ DSA2.cpp -o DSA2 } ; if ($?) { .\DSA2 }

Enter postfix expression: Abc*d+-

Inorder traversal of expression tree: A - b * c + d

preorder traversal: - A + * b c d

postorder traversal: A b c * d + -

PS D:\cpp\.vscode\2nd Yr>

Case Studies:

1. Analyze the efficiency of building an expression tree from different postfix expressions.

2. Compare non-recursive traversal methods with recursive methods in terms of performance


and stack usage.

3. Evaluate how the structure of the postfix expression affects the complexity of the
constructed expression tree.

Conclusion:
Constructing an expression tree from a postfix expression demonstrates the principles of tree data
structures and their application in expression evaluation. By implementing non-recursive traversal
techniques, this assignment highlights the flexibility of tree manipulations and provides insights into
efficient algorithms for expression handling in computational contexts. Understanding these
concepts is fundamental for further studies in data structures and algorithms.

4o mini

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy