Tejas Gitkhane Roll No.: S511042
Tejas Gitkhane Roll No.: S511042
Div. :A
Batch : A2
Experiment 4
Aim : construct a exprssion tree from postfix and prefix expression. Perform recursive and
non recursive in order, pre order and post order traversal
#include <iostream>
char data;
} node;
//================================================
node* data;
} stacknode;
//===============================================
class stack {
stacknode *top;
public:
stack() {
top = NULL;
}
node* topp() {
return top->data;
int isempty() {
void push(node* a) {
stacknode *p;
p = new stacknode();
p->data = a;
p->next = top;
top = p;
node* pop() {
stacknode *p;
node* x;
x = top->data;
p = top;
top = top->next;
return x;
};
//========================================================
node* create_pre(char prefix[10]);
//==========================================================
node *p;
stack s;
if (isalnum(token)) {
p = new node();
p->data = token;
p->left = NULL;
p->right = NULL;
s.push(p);
} else {
p = new node();
p->data = token;
p->right = s.pop();
p->left = s.pop();
s.push(p);
return s.pop();
}
//==========================================================
node *p;
stack s;
int i;
if (isalnum(token)) {
p = new node();
p->data = token;
p->left = NULL;
p->right = NULL;
s.push(p);
} else {
p = new node();
p->data = token;
p->left = s.pop();
p->right = s.pop();
s.push(p);
return s.pop();
//===============================================================
int main() {
int x;
do {
switch (ch) {
case 1:
expression\nchoice=";
if (choice == 1) {
r = create_post(postfix);
} else {
r = create_pre(prefix);
break;
case 2:
inorder(r);
cout << "\n Without recursion:\t";
inorder_non_recursive(r);
break;
case 3:
preorder(r);
preorder_non_recursive(r);
break;
case 4:
postorder(r);
postorder_non_recursion(r);
break;
return 0;
//==========================================================
if (p != NULL) {
inorder(p->left);
inorder(p->right);
preorder(p->left);
preorder(p->right);
if (p != NULL) {
postorder(p->left);
postorder(p->right);
//============================================================
stack s;
while (t != NULL) {
s.push(t);
t = t->left;
while (!s.isempty()) {
t = s.pop();
t = t->right;
while (t != NULL) {
s.push(t);
t = t->left;
}
}
stack s;
while (t != NULL) {
s.push(t);
t = t->left;
while (!s.isempty()) {
t = s.pop();
t = t->right;
while (t != NULL) {
s.push(t);
t = t->left;
stack s, s1;
node *t1;
while (t != NULL) {
s.push(t);
s1.push(NULL);
t = t->left;
while (!s.isempty()) {
t = s.pop();
t1 = s1.pop();
if (t1 == NULL) {
s.push(t);
s1.push((node *)1);
t = t->right;
while (t != NULL) {
s.push(t);
s1.push(NULL);
t = t->left;
} else {
}
OUTPUT: