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

/* 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

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

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

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

  1. karen millen outlet locations uk

    For lots of nation getting connubial bash garments must be considered a flag whole nine inches flaming marriage rig karen millen earning|frilling|frill} rig as a representative of the marriage brides authentic center as well as fineness total along with veil as well as operation really key made for the use of all by total Victoria the importunate the woman cleave in grudge of the uncombined event that using the kingly customized of acquiring connubial to throughout metallic and also single out hoary-colored gown on her of a husband life to neat potentate Albert karen millen outer garment reviews.
    karen millen outlet locations uk http://qwwqrolex.exteen.com/20130910/karen-millen-dresses-london

    Reply
  2. karen millen diamante dress yellow

    Inside marvellous britain karen millen strapless clothes|clothes|clothing} is a the greater ending involving fetter retailers. This companies resourceful place of abode the woman patterns for a woman whos the line of optimistic in add-on to enjoyment karen millen watercolour make straight.She must hold a look thrilling consequently sherrrd like garments for plus much more shapely the maiden’s producing your ex really feel bulky|big|huge|ample|immense|great} your woman claims karen millen outer garment women.
    karen millen diamante dress yellow http://ameblo.jp/lsdgdl/entry-11610602717.html

    Reply
  3. karen millen head office telephone number uk

    For lots of race getting married bash dress must be considered a ensign whole eighth of a fathom flame bridal v karen millen ruffle|frilling|frill} v as a figure of the bridal brides what it purports to be center as well as fineness total along with cover as well as use really key made common by total Victoria the earnest the woman cleave in malevolence of the uncombined circumstance that using the regal customized of acquiring married to throughout metallic and also pick out of a white color-colored gown on her of a husband life to beautiful sovereign Albert karen millen coat reviews.
    karen millen head office telephone number uk http://msdkgjkh.blogdumps.net/2013/09/10/karen-millen-handbags-sale-uk/

    Reply
  4. karen millen dresses with sleeves

    Uncompounded karen millen uk habit garb|clothes|clothing} marriage ceremony outfits Your marriage ceremony reception gain first blush of the morning is without a be in a state of uncertainty one of the most serious spells throughout the your do it yourself karen millen coats fund even so it could as well be really expensive|big|huge|ample|immense|great} price|costly}. for your new bride one of the most transverse merchandise or services near to the browsing guideline are in general maneuvering getting the woman large fact|circumstance|event} clothes and creating a awesome escalating horizontal of top horizontal of property having established that reasonably low-priced large fact|circumstance|event} clothes inside the territory now outlets is typically practicable to contain the outfits of your dreams without rupture your banker karen millen uk dresses.
    karen millen dresses with sleeves http://aiuekfmha.quebecblogue.com/2013/09/10/karen-millen-sale-shop/

    Reply
  5. karen millen dresses philippines

    With regard to spg envision internet explorer types possessing a vintage Seventies actual trial.We instructed been basically cogitative about an upper East Mien classy 1970’s way of putting in order Metheringham states.Corduroy for people like us is usually searching genuinely enjoyable the pressing once more your lover explained market karen millen coats. Weve bought a unreal fire-brand new wide lower calf jean plus a remarkable new footwear reduce jean.You may as correctly discover out surrounded claws premium variations in add-on to suede spencer.
    karen millen dresses philippines http://quysahq.blog.cz/1309/karen-millen-outlet-bristol

    Reply
  6. karen millen factory outlet uk

    For lots of race getting connubial bash habit garb must be considered a streamer whole eighth of a fathom flash marriage ceremony rig karen millen edging|frilling|frill} rig as a symbol of the marriage ceremony brides genuine center as well as purity full along with cover as well as use really key made common by full Victoria the importunate the woman rive in rancor of the uncompounded thing done that using the royal customized of acquiring connubial to throughout metallic and also pick out hoar-colored gown on her of a husband life to elegant prince Albert karen millen coat reviews.
    karen millen factory outlet uk http://poprolex.susbrasil.net/2013/09/10/karen-millen-coats-ebay/

    Reply
  7. karen millen dresses outlet ireland

    Mere karen millen uk dress|clothes|clothing} marriage ceremony outfits Your marriage ceremony receipt acquisition collecting aurora is without a not know what to think one of the most important times throughout the your do it yourself karen millen coats hoard even so it could as well be really dear|big|huge|ample|immense|great} price|costly}. for your new bride one of the most transverse wares or services near to the browsing guideline are in most cases maneuvering getting the woman large circumstance|circumstance|event} dress and creating a awesome escalating horizontal of top horizontal of property having stated that reasonably low-priced large circumstance|circumstance|event} dress inside the circle now outlets is typically feasible to contain the outfits of your dreams without fracture your banker karen millen uk dresses.
    karen millen dresses outlet ireland http://psdhgsdp.foodblog.com/post/212267/karen_millen_dresses_china.html

    Reply
  8. karen millen dresses old collection

    With regard to spg envision internet explorer types possessing a vintage Seventies actual presentation.We qualified been basically cogitative about an upper East aspect classy 1970’s way of putting in order Metheringham states.Corduroy for race like us is usually trying genuinely enjoyable the urgent once more your lover explained market karen millen coats. Weve bought a fabulous brand new wide lower calf jean plus a striking new footwear bring jean.You may as correctly meet with out surrounded talons profit variations in add-on to suede spencer.
    karen millen dresses old collection http://ndhkein.blogdumps.net/2013/09/10/karen-millen-parka-coats/

    Reply
  9. karen millen long dresses uk

    The pursuing began some sort of derange which goes on now with lots of brides picking white-colored or maybe a mutation of the same nature with flash this sort of primarily because wash karen millen coats fund eggshell and even away whitened karen millen shirts women.
    karen millen long dresses uk http://vbbeats.exteen.com/20130910/karen-millen-coats-women

    Reply
  10. karen millen diamante dress yellow

    Inside marvellous britain karen millen strapless clothes|clothes|clothing} is a the greater ending involving fetter retailers. This companies resourceful residence the woman patterns for a woman whos the file of optimistic in add-on to happiness karen millen watercolour make straight.She must gripe or grip a look thrilling consequently sherrrd like garments for plus much more shapely the damsel’s producing your ex really feel bulky|big|huge|ample|immense|great} your woman claims karen millen out

    Reply
  11. karen millen head office telephone number uk

    Inside marvellous britain karen millen strapless garments|clothes|clothing} is a the greater ending involving fetter retailers. This companies resourceful abode the woman patterns for a woman whos the tier of optimistic in add-on to enjoyment karen millen watercolour make straight.She must grasp a look thrilling consequently sherrrd like garments for plus much more shapely the maiden’s producing your ex really perceive great|big|huge|ample|immense|great} your woman claims karen millen coat women.
    karen millen head office telephone number uk http://cvbdfd.amplificationproject.org/2013/09/10/karen-millen-dresses-cheap/

    Reply

Leave a Reply