C++ program that uses non-recursive functions to traverse a binary tree in Post-order

Thursday, March 11th, 2010

/* Write C++ program that uses non-recursive functions to traverse a binary tree in Post-order */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
 
class node
{
public:
class node *left;
class node *right;
int data;
};
 
class tree: public node
{
public:
int stk[50],top;
node *root;
tree()
{
root=NULL;
top=0;
}
void insert(int ch)
{
	node *temp,*temp1;
	if(root== NULL)
	{
		root=new node;
		root->data=ch;
		root->left=NULL;
		root->right=NULL;
		return;
	}
	temp1=new node;
	temp1->data=ch;
	temp1->right=temp1->left=NULL;
	temp=search(root,ch);
	if(temp->data>ch)
		temp->left=temp1;
	else
		temp->right=temp1;
 
}
node *search(node *temp,int ch)
{
	if(root== NULL)
	{
		cout <<"no node present";
		return NULL;
	}
	if(temp->left==NULL && temp->right== NULL)
		return temp;
 
	if(temp->data>ch)
	     {  if(temp->left==NULL) return temp;
		search(temp->left,ch);}
	else
	      { if(temp->right==NULL) return temp;
	      search(temp->right,ch);
 
}              }
 
void display(node *temp)
{
	if(temp==NULL)
	 return ;
	display(temp->left);
       cout<<temp->data << " ";
	display(temp->right);
}
void postorder( node *root)
{
	node *p;
	p=root;
	top=0;
 
	while(1)
	{
	while(p!=NULL)
	{
		stk[top]=p->data;
		top++;
		if(p->right!=NULL)
			stk[top++]=-p->right->data;
		p=p->left; 
	}
	while(stk[top-1] > 0 || top==0)
	{
	   if(top==0) return;
	   cout << stk[top-1] <<" ";
	   p=pop(root);
	}
	if(stk[top-1]<0)
	{
	  stk[top-1]=-stk[top-1];
	  p=pop(root);
      }	}
 
}
node * pop(node *p)
{
int ch;
ch=stk[top-1];
if(p->data==ch)
{
top--;
return p;
}
if(p->data>ch)
pop(p->left);
else
pop(p->right);
}
};
void main()
{
	tree t1;
	int ch,n,i;
	clrscr();
	while(1)
	{
		cout <<"\n1.INSERT\n2.DISPLAY 3.POSTORDER TRAVERSE\n4.EXIT\nEnter your choice:";
		cin >> ch;
		switch(ch)
		{
		case 1:   cout <<"enter no of elements to insert:";
			  cout<<"\n enter the elements";
			  cin >> n;
			  for(i=1;i<=n;i++)
			  {  cin >> ch;
			     t1.insert(ch);
			  }
			   break;
		case 2:   t1.display(t1.root);break;
		case 3:   t1.postorder(t1.root); break;
		case 4:   exit(1);
		}
	}
}

OUTPUT

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:1
enter no of elements to insert:
enter the elements7
5 24 36 11 44 2 21

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:2
2 5 11 21 24 36 44

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:3
2 21 11 44 36 24 5

1.INSERT
2.DISPLAY 3.POSTORDER TRAVERSE
4.EXIT
Enter your choice:4

Avatar of Ranjith

Author Name :
Ranjith

Total : 2 Comments


2 Responses to “C++ program that uses non-recursive functions to traverse a binary tree in Post-order”

  1. more easy code :
    file : a.in
    _____________________________
    10
    LR 6
    L 3
    RL 5
    RR 8
    R 7
    LLL 4
    _____________________________

    code :
    /***********************************
    Algorithm: Create and Traverse a Binary Tree
    implementation: Link List[non recursively]
    Copy right @ rizoan toufiq
    ************************************/
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    #define s 100
    typedef struct tree{
    int data;
    struct tree *right;
    struct tree *left;
    }node;
    node *root=NULL;
    /******creat a root of tree***************/
    void makeroot(int v){
    root=(node *)malloc(sizeof(node));
    (root)->right=NULL;
    (root)->left=NULL;
    (root)->data=v;
    }
    /****** create tree ********************/
    void creattree(char path[],int v){
    //path sent by value
    node *r=NULL,*t=NULL;
    int i;
    r=root;
    for(i=0;ileft==NULL){//no node
    t=(node *)malloc(sizeof(node));
    t->right=NULL;
    t->left=NULL;
    t->data=-1;
    r->left=t;
    r=r->left;
    }
    else//move left
    r= r->left;
    }
    else{
    if(r->right==NULL){//no node
    t=(node *)malloc(sizeof(node));
    t->right=NULL;
    t->left=NULL;
    t->data=-1;
    r->right=t;
    r=r->right;
    }
    else//move left
    r= r->right;
    }
    }
    r->data = v;//set value of empty node
    }
    /************ inorder traverse ***************/
    void inorder(struct tree *r){//call by value
    stackstk;
    while(1){
    //push left most path
    if(r!=NULL){
    stk.push(r);
    r=r->left;
    }
    else{
    //no node for back track
    if(stk.empty())
    break;
    else{//backtracking
    r=stk.top();
    stk.pop();
    //process data
    if(r->data==-1)
    printf(“NULL_NODE “);
    else
    printf(“%d “,r->data);
    r=r->right;
    }
    }
    }
    }
    /************preorder traverse*****************/
    void preorder(struct tree *r){
    stackstk;
    stk.push(NULL);
    while(r!=NULL){
    if(r->data==-1)
    printf(“NULL_NODE “);
    else
    printf(“%d “,r->data);

    if(r->right!=NULL)
    stk.push(r->right);
    if(r->left!=NULL)
    r=r->left;
    else{
    r=stk.top();
    stk.pop();
    }
    }
    }
    /***********postorder traverse*****************/
    void postorder(struct tree *r){
    stackstk;
    bool a[s]={true};//identify left node
    int c = 0;// for break/count stack data
    stk.push(NULL);
    while(1){
    //Push left most path on stack
    while(r!=NULL){
    stk.push(r);
    c++;
    a[c] = true;
    if(r->right!=NULL){
    stk.push(r->right);
    c++;
    a[c]=false;
    }
    r= r->left;
    }
    //pop node from stack
    r=stk.top();
    stk.pop();
    while(a[c]==true){
    //process in data
    if(r->data==-1)
    printf(“NULL_NODE “);
    else
    printf(“%d “,r->data);
    c–;
    if(c<=0)
    break;
    r=stk.top();
    stk.pop();
    if(stk.empty())
    break;
    }
    c–;
    if(c<=0)
    break;
    }
    }
    /************* main **************************/
    int main(){
    int value;
    char str[s];
    FILE *F=freopen("a.in","r",stdin);
    if(F==NULL){
    printf("Ops!\n");
    return 1;
    }
    scanf("%d\n",&value);
    makeroot(value);
    while(scanf("%s %d\n",str,&value)!=EOF){
    creattree(str,value);
    }

    printf("\nInorder BT:\n———–\n\t");
    inorder(root);
    printf("\n\n");

    printf("\npreorder BT:\n———–\n\t");
    preorder(root);
    printf("\n\n");

    printf("\npostorder BT:\n———–\n\t");
    postorder(root);
    printf("\n\n");
    return 0;
    }
    /******************** end *******************/

  2. Assassin says:

    Rizoan Toufiq your code is excellent. tanx

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: