User:Pankaj14121992/sandbox

Non recursive program for trarverse the inorder,preorder,postorder

using namespace std; class btree { int data; btree *lchild; btree *rchild; friend class treeoperation; }*root; class treeoperation { btree *neww; int top; public: void maketree { int k; while(k) { neww=new btree; cout<<"\nenter the data\n";	cin>>neww->data; neww->lchild=NULL; neww->rchild=NULL; fixroot(root); cout<<"\n0-for exit\n"; cin>>k; } } void fixroot(btree *temp) { if(root==NULL) { root=neww; return; } if(temp->data>neww->data) { if(!temp->lchild) temp->lchild=neww; else fixroot(temp->lchild); } else if(temp->datadata) { if(!temp->rchild) temp->rchild=neww; else fixroot(temp->rchild); } else if(temp->data==neww->data) {	cout<<"\nduplication occurs"; h: cout<<"\n1-for left dublication\n2-right duplication\nenter the choice:";int ch;cin>>ch; switch(ch) {	case 1: if(!temp->lchild) temp->lchild=neww; else fixroot(temp->lchild); break; case 2: if(!temp->rchild) temp->rchild=neww; else fixroot(temp->rchild); break; default: {			cout<<"wrong choice\n"; goto h;			break; } } } } void inorder(btree *temp) {	if(temp!=NULL) {	inorder(temp->lchild); cout<data<<"->"; inorder(temp->rchild); } } void preorder(btree *temp) {	if(temp!=NULL) {	cout<data<<"->"; preorder(temp->lchild); preorder(temp->rchild); } } void postorder(btree *temp) {	if(temp!=NULL) {	postorder(temp->lchild); postorder(temp->rchild); cout<data<<"->"; } } void doubleorder(btree *temp) {	if(temp!=NULL) {	cout<data<<"->"; doubleorder(temp->lchild); cout<data<<"->"; doubleorder(temp->rchild); } }
 * 1) include
 * 2) include

void nonrecursive(btree *temp) {	btree *current,*pre; current =new btree; pre=new btree; current=root; if(root==NULL) return; while(current!=NULL) {		if(current->lchild==NULL) {		cout<data<<"->"; current=current->rchild; }	else {		pre=current->lchild; while(pre->lchild!=NULL&&pre->rchild!=current) pre=pre->rchild; if(pre->rchild == NULL) {       pre->rchild = current; current = current->lchild; }       else {       pre->rchild = NULL; printf(" %d ",current->data); current = current->rchild; } /* End of if condition pre->right == NULL */ } /* End of if condition current->left == NULL*/ } /* End of while */ } }; int main {	treeoperation p;	p.maketree; cout<<"\n-\n"; cout<<"\ninorder:"; p.inorder(root); cout<<"\n-\n"; cout<<"\npreorder:"; p.preorder(root); cout<<"\n-\n"; cout<<"\npostorder:"; p.postorder(root); cout<<"\n-\n"; cout<<"\ndoubleorder:"; p.doubleorder(root); cout<<"\n-\n"; cout<<"\nnon recurcive order:"; p.nonrecursive(root); cout<<"\n-\n"; getch; }