Exp Tree
Exp 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 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.
*/