Demo entry 6751450

C++

   

Submitted by anonymous on Jun 23, 2018 at 09:30
Language: C++. Code size: 3.3 kB.

//¶þ²æÊ÷µÄ½¨Á¢,±éÀú£¬Óëͳ¼ÆÒ¶×Ó½Úµã. 
#include<iostream>
#define MAX 100
using namespace std;
int x=0;
int y=50;
char s[100][100];  //×ø±êϵ 
typedef struct BiNode//Ò¶×ӵĽṹÌå 
{
	char data;//Ò¶×ÓµÄÃû×Ö 
	int number;
	int xx;   //ºá×ø±ê 
	int yy;   //Êú×ø±ê 
	int yx;   // 
	int judge=0;  //ÅжϱäÁ¿ Ϊ0´ú±í×óº¢×Ó,Ϊ1´ú±íÓÒº¢×Ó 
	struct BiNode *lchild,*rchild;//×óÓÒº¢×Ó 
}BiNode,*Bitree;
void creatTree(Bitree &t)   //¹¹½¨¶þ²æÊ÷,"ÀàËƲã´Î±éÀú˳ÐòµÄ¹¹Ôì 
{
	cout<<"¿ªÊ¼½¨Á¢¶þ²æÊ÷"<<endl;
	char ch; 
	Bitree point;
	int rear,front;//¶ÓÁÐÖеÄ"Ö¸Õë"; 
	rear=0;front=0;
	Bitree Q[MAX];
	cin>>ch;
	if(ch=='.')//ÓÃ.ÅжÏÖ¸ÕëÊÇ·ñ´æÔÚ 
	{
	   t=NULL;
    }
	else
	{
		t=new BiNode;
		t->data=ch;
		t->xx=0;
		t->yy=50;
		Q[rear]=t;
		rear=(rear+1)%100;
		while(front!=rear)//Ñ­»·ÖÕÖ¹µÄÌõ¼þ 
		{
			point=Q[front];
			front=(front+1)%MAX;
			cin>>ch;
			if(ch=='.')
			{
			   point->lchild=NULL;
		    }
			else
			{
			   point->lchild=new BiNode;
			   point->lchild->data=ch;
			     point->lchild->xx=point->xx+2;//×ø±êµÄ¸³Öµ 
			     point->lchild->yy=point->yy-2;
			     point->lchild->yx=point->yy-1;
			   Q[rear]=point->lchild;
			   rear=(rear+1)%100;
		   }
		   cin>>ch;
			if(ch=='.')
			{
			   point->rchild=NULL;
		    }
			else
			{
			   point->rchild=new BiNode;
			   point->rchild->data=ch;
			      point->rchild->xx=point->xx+2;//×ø±êµÄ¸³Öµ 
			      point->rchild->yy=point->yy+2;
			      point->rchild->yx=point->yy+1;
			      point->rchild->judge=1;//judgeΪ1´ú±íÓÒº¢×Ó 
			   Q[rear]=point->rchild;
			   rear=(rear+1)%100;
		   }
		   
	}
}
}

	void pre(Bitree t)//ÏÈÐò±éÀú µÝ¹é 
	{
		if(t)
		{
		s[t->xx][t->yy]=t->data;
		if(t->judge==0)
		{
		s[t->xx-1][t->yx]='/';
	}
		else
		{
		s[t->xx-1][t->yx]='\\';
	}
		cout<<t->data;
		pre(t->lchild); 
		pre(t->rchild);
	}
	 
}
	void cen(Bitree t)//ÖÐÐò±éÀúµÝ¹é 
	{
		if(t)
		{
		s[t->xx][t->yy]=t->data;
		if(t->judge==0)
		{
		s[t->xx-1][t->yx]='/';
	}
		else
		{
		s[t->xx-1][t->yx]='\\';
	}
	   cen(t->lchild);
	   cout<<t->data;
	   cen(t->rchild);
	   }
}
	void aft(Bitree t)//ºóÐò±éÀú 
	{
		if(t)
		{
		s[t->xx][t->yy]=t->data;
		if(t->judge==0)
		{
		s[t->xx-1][t->yx]='/';
	}
		else
		{
		s[t->xx-1][t->yx]='\\';//'\'Õâ¸ö²»ÄÜÊä³ö·´Ð±¸Ü,Õâ¸ö¿ÉÒÔ'\\'. 
	}
		aft(t->lchild);
		aft(t->rchild);
		cout<<t->data;
	}
}	
void E_initialize()//³õʼ»¯×ø±êÖá 
{
	for(int i=0;i<100;i++)
	 for(int j=0;j<100;j++)
	  s[i][j]=' ';
}
void E_show()
{	
    for(int i=0;i<100;i++)
	 {
	 for(int j=0;j<100;j++)
	 cout<<s[i][j];
     cout<<endl;
 }
}
void E_display(Bitree t)  //½çÃ溯Êý 
{
    	if(t)
    	{
    	int choice;
    	cout<<"1(ÏÈÐò±éÀúµÝ¹é²é¿´),  2(ÖÐÐò±éÀúµÝ¹é²é¿´),  3(ºóÐò±éÀú²é¿´)"<<endl;	
    	cin>>choice;
    	switch(choice)
    	{
		case 1:
			pre(t);
			cout<<endl;
			break;
		case 2:
			cen(t);
			cout<<endl;
			break;
		case 3:
			aft(t);
			cout<<endl;
			break;
		default:
		    cout<<"ÊäÈë´íÎó,ÇëÖØÐÂÊäÈë:";		
		    break;
	   }
       }
}
	   int main()
	   {
	   	E_initialize();
	   	Bitree tree;
	   	creatTree(tree);
        E_display(tree);
	   	cout<<endl;
	   	E_show();
	   } 

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).