0% found this document useful (0 votes)
67 views

Expression Tree Creation and Traversal

This C++ program defines classes for binary tree nodes and stacks. It implements recursive and non-recursive tree traversal algorithms including preorder, inorder, and postorder. The tree class includes methods to create a tree from a postfix expression, get the root node, and perform the different traversal orders. The main function tests the tree creation and traversal methods by prompting the user to select an operation and outputting the results.

Uploaded by

RONAK THAKARE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Expression Tree Creation and Traversal

This C++ program defines classes for binary tree nodes and stacks. It implements recursive and non-recursive tree traversal algorithms including preorder, inorder, and postorder. The tree class includes methods to create a tree from a postfix expression, get the root node, and perform the different traversal orders. The main function tests the tree creation and traversal methods by prompting the user to select an operation and outputting the results.

Uploaded by

RONAK THAKARE
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <iostream>

#include<stdlib.h>

using namespace std;

class node{

public :

char data;

node *left,*right;

node(char x) //Constructor

data=x;

left=right=NULL;

};

class Stack

node *data[30];

int top;

public:

Stack()

{ top = -1;}

int empty()

if(top==-1)

return 1;

return 0;

void push (node *p) // push an elemnt into stack


{

data[++top]=p;

node *pop() //pop an element of stack

return (data[top--]);

};

class stackint // Integer stack class

public:

int arr[30];

int top;

stackint()

top=-1;

int empty()

if(top==-1)

return 1;

return 0;

void push(int temp){


arr[++top]=temp;

int pop(){

return (arr[top--]);

};

class tree

node *root;

public:

tree()

{ root=NULL;} //Initialize root to null

void create(); //Creating expression tree from postfix/prefix

node *rootval()

return root;

void inorder(node *root);

void preorder(node *root);

void postorder(node *root);

void non_rec_inorder();

void non_rec_preorder();

void non_rec_postorder();

};

void tree::create()

char exp[30];

node *temp,*temp1,*temp2;
Stack s;

char ch;

cout<<"Enter Postfix expression";

cin>>exp;

int i=0;

while((ch=exp[i])!='\0')

if(isalnum(ch))//operand

temp =new node(ch);

s.push(temp);

else //operator

temp2=s.pop();

temp1=s.pop();

temp=new node(ch);

temp->left=temp1;

temp->right=temp2;

s.push(temp);

i++;

root=s.pop();

void tree::inorder(node *T)

if(T!=NULL)

{
inorder(T->left);

cout<<" "<<T->data;

inorder(T->right);

void tree::preorder(node *T)

if(T!=NULL)

cout<<" "<<T->data;

preorder(T->left);

preorder(T->right);

void tree::postorder(node *T)

if(T!=NULL)

postorder(T->left);

postorder(T->right);

cout<<" "<<T->data;

void tree::non_rec_inorder()

Stack s;

node *T = root;

cout<<"\n";
while(T!=NULL) //To reach to Left Most Node

s.push(T);

T=T->left;

while(!s.empty())

T=s.pop();

cout<<" "<<T->data;

T=T->right;

while(T!=NULL) //To reach to Left Most Node

s.push(T);

T=T->left;

void tree::non_rec_postorder()

Stack s;

stackint s1;

int flag=0;

node *T = root;

cout<<"\n";

if(T==NULL){

return;
}

while(T!=NULL) //To reach to Left Most Node

s.push(T);

s1.push(0);

T=T->left;

while(!s.empty())

T=s.pop();

flag=s1.pop();

if(flag!=0)

cout<<" "<<T->data;

else{

s.push(T);

s1.push(1);

T=T->right;

while(T!=NULL)

s.push(T);

s1.push(0);

T=T->left;

}
}

void tree::non_rec_preorder()

Stack s;

node *T = root;

cout<<"\n";

while(T!=NULL) //To reach to Left Most Node

s.push(T);

cout<<" "<<T->data;

T=T->left;

while(!s.empty())

T=s.pop();

T=T->right;

while(T!=NULL)

cout<<" "<<T->data;

s.push(T);

T=T->left;

int main()
{

tree t1;

node *root1;

int x,op;

do{

cout<<"\n\n1)Create\n2)Preorder(recursive)\n3)Inorder(recursive)\n4)Postorder(recursive)\n5)Preo
rder(Non-recursive)\n6)Inorder(Non-recursive)\n7)Postorder(Non-recursive)\n8)Exit";

cout<<"\nEnter Your Choice";

cin>>op;

switch(op)

case 1:

t1.create();

root1=t1.rootval();

break;

case 2:

cout<<"\nPreorder Traversal(Recursive)";

t1.preorder(root1);

break;

case 3:

cout<<"\nInorder Traversal(Recursive)";

t1.inorder(root1);

break;

case 4:

cout<<"\nPostorder Traversal(Recursive)";

t1.postorder(root1);

break;

case 5:

cout<<"\nPreorder Traversal(Non-Recursive)";
t1.non_rec_preorder();

break;

case 6:

cout<<"\nInorder Traversal(Non-Recursive)";

t1.non_rec_inorder();

break;

case 7:

cout<<"\nPostorder Traversal(Non-Recursive)";

t1.non_rec_postorder();

break;

case 8:

cout<<"Thank You!";

break;

default :

cout<<"Enter a Valid Choice";

}while(op!=8);

return 0;

You might also like