0% found this document useful (0 votes)
11 views3 pages

Ds Practical 3

The document presents a C++ program that constructs a binary expression tree from a given postfix expression and performs inorder and postorder traversals. It defines a Node class for tree nodes, functions to check operators, construct the tree, and traverse it non-recursively. The main function demonstrates the construction and traversal of the expression represented by the postfix string 'a b c * - d e / - f +'.

Uploaded by

karan.ppjgk22
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)
11 views3 pages

Ds Practical 3

The document presents a C++ program that constructs a binary expression tree from a given postfix expression and performs inorder and postorder traversals. It defines a Node class for tree nodes, functions to check operators, construct the tree, and traverse it non-recursively. The main function demonstrates the construction and traversal of the expression represented by the postfix string 'a b c * - d e / - f +'.

Uploaded by

karan.ppjgk22
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/ 3

Practical No.

Q. For given expression eg. a-b*c-d/e+f construct inorder sequence and traverse
it using postorder traversal (non recursive)

#include <iostream>

#include <stack>

#include <string>

using namespace std;

class Node {

public:

char data;

Node* left;

Node* right;

Node(char val) {

data = val;

left = right = nullptr;

};

bool isOperator(char c) {

return (c == '+' || c == '-' || c == '*' || c == '/');

Node* constructTree(string postfix) {

stack<Node*> st;

for (char ch : postfix) {

if (isspace(ch)) continue;

if (!isOperator(ch)) {
st.push(new Node(ch));

} else {

Node* node = new Node(ch);

node->right = st.top(); st.pop();

node->left = st.top(); st.pop();

st.push(node);

return st.top();

void inorder(Node* root) {

if (root == nullptr) return;

if (isOperator(root->data)) cout << "(";

inorder(root->left);

cout << root->data;

inorder(root->right);

if (isOperator(root->data)) cout << ")";

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

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

while (!s2.empty()) {

cout << s2.top()->data << " ";

s2.pop();

cout << endl;

int main() {

string postfix = "a b c * - d e / - f +";

Node* root = constructTree(postfix);

cout << "Inorder (reconstructed expression): ";

inorder(root);

cout << endl;

postorderNonRecursive(root);

return 0;

Output:-

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