can yall help me-i need to be able to output in preorder (root, left, right) and in post order (left, right, root)- when i output now it is in order (left, root, right)- how come it does this automatically, just wondering about that-but i need help with preorder and postorder, can't seem to figure it out
thanks

you want to get the output in preorder so you can use this code

#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define max 20

class tree;

class node
{	int data;
	node *left,*right;
	friend class tree;
};

class tree
{       node *root;
	public:
		void rcreate(int item)
		{	rrcreate(root,item);
		}
		void rrcreate(class node*,int item);

		void rpreorder()
		{	rrpreorder(root);
		}
		void rrpreorder(class node*);

		void rpostorder()
		{	rrpostorder(root);
		}
		void rrpostorder(class node*);

		void rinorder()
		{	rrinorder(root);
		}
		void rrinorder(class node*);

		void create();
		void preorder();
		void inorder();
		void postorder();
		tree();
		~tree();
};

tree::tree()
{
	root=new node;
	root->left=NULL;
	root->right=NULL;
}

tree::~tree()
{
	delete root;
}

void tree::rrcreate(node *root1, int item)
{
	char ch, ch1;
	node *temp;
	root1->data=item;
	cout<<"\nDo you want to enter the data in the left node (y/n) ? ";
	cin>>ch;
	if(ch=='y'||ch=='Y')
	{	cout<<"Enter the data for the new node : ";
		cin>>item;
		temp=new node;
		temp->left=NULL;
		temp->right=NULL;
		root1->left=temp;
		rrcreate(temp,item);
	}
	cout<<"\nDo you want to enter the data in the right node (y/n) ? ";
	cin>>ch;
	if(ch=='y'||ch=='Y')
	{	cout<<"Enter the data for the new node : ";
		cin>>item;
		temp=new node;
		temp->left=NULL;
		temp->right=NULL;
		root1->right=temp;
		rrcreate(temp,item);
	}
}

void tree::rrpreorder(class node *root1)
{
	if(root1!=NULL)
	{	cout<<"\t"<<root1->data;
		rrpreorder(root1->left);
		rrpreorder(root1->right);
	}
}

void tree::rrpostorder(class node *root1)
{
	if(root1->left!=NULL)
		rrpostorder(root1->left);
	if(root1->right!=NULL)
		rrpostorder(root1->right);
	cout<<"\t"<<root1->data;
}

void tree::rrinorder(class node *root1)
{
	if(root1->left!=NULL)
		rrinorder(root1->left);
	cout<<"\t"<<root1->data;
	if(root1->right!=NULL)
		rrinorder(root1->right);
}

void tree::inorder()
{
	node *stack[max], *p;
	int top=0, flag=0;
	p=root;
	do
	{	while(p!=NULL)
		{
			stack[++top]=p;
			p=p->left;
		}
		if(top!=0)
		{	p=stack[top--];
			cout<<"\t"<<p->data;
			p=p->right;
		}
		else
			flag=1;
	}while(flag==0);
}


void tree::preorder()
{
	node *stack[max], *p;
	int top=0, flag=0;
	p=root;
	do
	{	while(p!=NULL)
		{	cout<<"\t"<<p->data;
			stack[++top]=p;
			p=p->left;
		}
		if(top!=0)
		{	p=stack[top--];
			p=p->right;
		}
		else
			flag=1;
	}while(flag==0);
}
void tree::postorder()
{
	node *stack[max], *p;
	int top=0, flag=0;
	p=root;
	do
	{	while(p!=NULL)
		{	stack[++top]=p;
			p=p->left;
		}
		if(stack[top]->right==NULL)
		{	p=stack[top--];
			cout<<"\t"<<p->data;
		}
		while(p==stack[top]->right && top>=0)
		{	p=stack[top--];
			cout<<"\t"<<p->data;
		}
		if(top!=0)
			p=stack[top]->right;
		else
			flag=1;
	}while(flag==0);
}

void tree::create()
{	node *temp,*curr;
	int flag;
	char ch,ans;
	cout<<" Enter data for root : ";
	cin>>root->data;
	cout<<"\n Do you want to generate another node : ";
	cin>>ans;
	while(ans=='y'||ans=='Y')
	{	curr=root;
		flag=0;
		temp=new node;
		temp->left=NULL;
		temp->right=NULL;
		cout<<" Enter data for node : ";
		cin>>temp->data;
		while(flag==0)
		{	cout<<"\n Enter L for left and R for right : ";
			cin>>ch;
			if(ch=='l'||ch=='L')
			{	if(curr->left==NULL)
				{	curr->left=temp;
					flag=1;
				}
				else
					curr=curr->left;
			}
			else if(ch=='r'||ch=='R')
			{	if(curr->right==NULL)
				{	curr->right=temp;
					flag=1;
				}
				else
					curr=curr->right;
			}
		}
		cout<<"\n Do you want to generate another node : ";
		cin>>ans;
	};
}



void main()
{
	class tree bl;
	int ch,item;
	do
	{	clrscr();
		cout<<"\n ---------------MENU---------------";
		cout<<"\n  ----------CREATE  LIST---------- ";
		cout<<"\n   1. Recursive";
		cout<<"\n   2. Non-Recursive";
		cout<<"\n\n  ------RECURSIVE TRAVERSALS----- ";
		cout<<"\n   3. Pre-order";
		cout<<"\n   4. Post-order";
		cout<<"\n   5. In-order";
		cout<<"\n\n  ----NON-RECURSIVE TRAVERSALS--- ";
		cout<<"\n   6. Pre-order";
		cout<<"\n   7. Post-order";
		cout<<"\n   8. In-order";
		cout<<"\n\n   9. Exit";
		cout<<"\n\n   Enter your choice : ";
		cin>>ch;
		switch(ch)
		{	case 1:
				cout<<"Enter data for the root node : ";
				cin>>item;
				bl.rcreate(item);
				break;
			case 2:
				bl.create();
				break;
			case 3:
				bl.rpreorder();
				break;
			case 4:
				bl.rpostorder();
				break;
			case 5:
				bl.rinorder();
				break;
			case 6:
				bl.preorder();
				break;
			case 7:
				bl.postorder();
				break;
			case 8:
				bl.inorder();
				break;

			case 9:
				break;
		}
		getch();
	}while(ch!=9);
}

<< moderator edit: added [code][/code] tags >>


can yall help me-i need to be able to output in preorder (root, left, right) and in post order (left, right, root)- when i output now it is in order (left, root, right)- how come it does this automatically, just wondering about that-but i need help with preorder and postorder, can't seem to figure it out
thanks

This article has been dead for over six months. Start a new discussion instead.