0% found this document useful (0 votes)
4 views4 pages

dsa5

The document contains a C++ program that constructs an expression tree from a given prefix expression. It includes functions for constructing the tree, performing a non-recursive post-order traversal, and deleting the tree. The main function demonstrates these functionalities using the prefix expression '+--a*bc/def'.

Uploaded by

Sadiya Sayed
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)
4 views4 pages

dsa5

The document contains a C++ program that constructs an expression tree from a given prefix expression. It includes functions for constructing the tree, performing a non-recursive post-order traversal, and deleting the tree. The main function demonstrates these functionalities using the prefix expression '+--a*bc/def'.

Uploaded by

Sadiya Sayed
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/ 4

Experiment No:5

Aliya Sayyed Roll No:4

Code

#include <iostream>

#include <stack>

#include <string>

using namespace std;

// Node definition

struct Node {

char val;

Node* left;

Node* right;

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

};

// Utility to check if a character is an operator

bool isOperator(char c) {

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

// Step 1: Construct Expression Tree from Prefix

Node* constructTree(const string& prefix) {

stack<Node*> st;

// Process the string from right to left

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


char c = prefix[i];

Node* node = new Node(c);

if (isOperator(c)) {

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

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

st.push(node);

return st.top(); // Root of the tree

// Step 2: Post-order Traversal (Non-Recursive)

void postOrderNonRecursive(Node* root) {

if (!root) return;

stack<Node*> s1, s2;

s1.push(root);

while (!s1.empty()) {

Node* node = s1.top(); s1.pop();

s2.push(node);

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

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

while (!s2.empty()) {

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

s2.pop();
}

cout << endl;

// Step 3: Delete Tree

void deleteTree(Node* root) {

if (!root) return;

stack<Node*> st;

st.push(root);

while (!st.empty()) {

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

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

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

delete node;

// Main function

int main() {

string prefix = "+--a*bc/def";

Node* root = constructTree(prefix);

cout << "Post-order traversal (non-recursive): ";

postOrderNonRecursive(root);

deleteTree(root);
return 0;

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