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

Exp Tree

The document contains a C++ program that constructs an expression tree from a given prefix expression and allows for in-order traversal of the tree. It includes a menu-driven interface for user interaction, enabling the input of prefix expressions and displaying the tree. The program demonstrates the creation, traversal, and deletion of the expression tree using a stack data structure.
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)
8 views3 pages

Exp Tree

The document contains a C++ program that constructs an expression tree from a given prefix expression and allows for in-order traversal of the tree. It includes a menu-driven interface for user interaction, enabling the input of prefix expressions and displaying the tree. The program demonstrates the creation, traversal, and deletion of the expression tree using a stack data structure.
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

/*S.E.(A). Roll No.:47. Name: Aditi Sunil Gite .

Assignment No. 4 : Construct an expression tree from the given prefix


expression eg. --a*bc/def and traverse it using post order traversal (non
recursive) and then delete the entire tree.
*/

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

class node {
public:
char data;
node* left;
node* right;
};

class stack1 {
node* data[20];
int tos;
public:
stack1() {
tos = -1;
}
int empty() {
return tos == -1;
}
void push(node* p) {
data[++tos] = p;
}
node* pop() {
return data[tos--];
}
};

class tree {
node* top;
public:
tree() {
top = NULL;
}

void expression(char prefix[]) {


stack1 s;
node *t1, *t2;
int len = strlen(prefix);
for (int i = len - 1; i >= 0; i--) {
node* temp = new node;
temp->left = temp->right = NULL;
if (isalpha(prefix[i])) {
temp->data = prefix[i];
s.push(temp);
} else if (prefix[i] == '+' || prefix[i] == '-' || prefix[i]
== '*' || prefix[i] == '/') {
t1 = s.pop();
t2 = s.pop();
temp->data = prefix[i];
temp->left = t1;
temp->right = t2;
s.push(temp);
}
}
top = s.pop();
}

void display() {
displayInOrder(top);
}

private:
void displayInOrder(node* top) {
if (top != NULL) {
displayInOrder(top->left);
cout << top->data << " ";
displayInOrder(top->right);
}
}
};

int main() {
tree t;
char exp[30];
int ch;
do {
cout << "\n---MENU---\n";
cout << "1. Enter expression\n";
cout << "2. Display tree (In-Order)\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "Enter prefix expression: ";
cin >> exp;
t.expression(exp);
break;
case 2:
cout << "In-order traversal: ";
t.display();
cout << endl;
break;
case 3:
cout << "End of program.\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (ch != 3);

return 0;
}
/*
OUTPUT

---MENU---
1. Enter expression
2. Display tree (In-Order)
3. Exit
Enter your choice: 1
Enter prefix expression: -a*bc/def

---MENU---
1. Enter expression
2. Display tree (In-Order)
3. Exit
Enter your choice: 2
In-order traversal: a - b * c

---MENU---
1. Enter expression
2. Display tree (In-Order)
3. Exit
Enter your choice: 3
End of program.
*/

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