二叉樹的建立與顯示

#include<iostream>
#include<vector>

using namespace std;

struct BinaryTreeNode
{
	int value;
	BinaryTreeNode *left;
	BinaryTreeNode *right;
};

BinaryTreeNode *CreatBT();
void preorder(BinaryTreeNode *T);
void inorder(BinaryTreeNode *T);
void postorder(BinaryTreeNode *T);
void leafnum(BinaryTreeNode *T,int &count);
void Nodenum(BinaryTreeNode *T,int &count_total);
void ShowTree(BinaryTreeNode *T);

int main()
{
	BinaryTreeNode *T=NULL;
	int count=0,count_total=0,choice=0;
	do{
		cout<<endl;
		cout<<"二叉樹"<<endl;
		cout<<"********************************************"<<endl;
		cout<<"* *"<<endl;
		cout<<"* 主菜單"<<endl;
		cout<<"* 1 建立二叉樹"<<endl;
		cout<<"* 2 先序遍歷二叉樹"<<endl;
		cout<<"* 3 中序遍歷二叉樹"<<endl;
		cout<<"* 4 後序遍歷二叉樹"<<endl;
		cout<<"* 5 二叉樹的葉子結點數"<<endl;
		cout<<"* 6 顯示二叉樹"<<endl;
		cout<<"* 7 二叉樹的所有結點數"<<endl;
		cout<<"* 8 退出程序"<<endl;
		cout<<"********************************************"<<endl;
		cout<<" 請輸入您的選擇(1,2,3,4,5,6,7,8):"<<endl;
		cin>>choice;

		switch(choice)
		{
		case 1:
			cout<<"二叉樹的建立,以輸入0表示結束。"<<endl;
			cout<<"請輸入根結點:"<<endl;
			count=count_total=0;
			T=CreatBT();
			cout<<"二叉樹成功建立。"<<endl;
			break;
		case 2:
			cout<<"前序遍歷二叉樹:"<<endl;
			preorder(T);
			break;
		case 3:
			cout<<"中序遍歷二叉樹:"<<endl;
			inorder(T);
			break;
		case 4:
			cout<<"後序遍歷二叉樹:"<<endl;
			postorder(T);
			break;
		case 5:
			cout<<"二叉樹的葉子結點數爲:"<<endl;
			leafnum(T,count);
			cout<<count<<endl;
			break;
		case 6:
			ShowTree(T);
			break;
		case 7:
			cout<<"二叉樹的所有結點數爲:"<<endl;
			Nodenum(T,count_total);
			cout<<count_total<<endl;
			break;
		default:
			exit(0);
		}
	}while(choice<=8);
		
	system("pause");
	return 0;
}

/********************************************************************/
//建立二叉樹
BinaryTreeNode *CreatBT()
{
	BinaryTreeNode *t=NULL;
	int x;
	cin>>x;
	if(x!=0)
	{
		t=(BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
		t->value=x;
		cout<<"請輸入"<<t->value<<"結點的左子結點"<<endl;
		t->left=CreatBT();
		cout<<"請輸入"<<t->value<<"結點的右子結點"<<endl;
		t->right=CreatBT();
	}
	return t;
}

/********************************************************************/
//前序遍歷
void preorder(BinaryTreeNode *T)
{
	if(T==NULL)
		return;
	else
	{
		cout<<T->value<<" ";
		preorder(T->left);
		preorder(T->right);
	}
}

/********************************************************************/
//中序遍歷
void inorder(BinaryTreeNode *T)
{
	if(T==NULL)
		return;
	else
	{
		inorder(T->left);
		cout<<T->value<<" ";
		inorder(T->right);
	}
}

/********************************************************************/
//後序遍歷
void postorder(BinaryTreeNode *T)
{
	if(T==NULL)
		return;
	else
	{
		postorder(T->left);
		postorder(T->right);
		cout<<T->value<<" ";
	}
}

/********************************************************************/
//記錄葉子結點數
void leafnum(BinaryTreeNode *T,int &count)
{
	if(T)
	{
		if(T->left==NULL && T->right==NULL)
			++count;
		leafnum(T->left,count);
		leafnum(T->right,count);
	}
}

/********************************************************************/
//顯示二叉樹
void ShowTree(BinaryTreeNode *T)
{
	BinaryTreeNode *stack[100];
	BinaryTreeNode *p;
	
	int level[100][2];
	int top,n,i;
	int width=4;
	
	if(T!=NULL)
	{
		cout<<"二叉樹的表示法"<<endl;
		top=1;
		stack[top]=T;
		level[top][0]=width;
		while(top>0)
		{
			p=stack[top];
			n=level[top][0];
			for(i=1;i<=n;i++)
				cout<<" ";
			if(level[top][1]==2)
				cout<<"right";
			else if(level[top][1]==1)
				cout<<"left";
			else
				cout<<"root";
			cout<<p->value;
			for(int j=0;j!=60;j+=2)
				cout<<"*";
			cout<<endl;
			top--;
			if(p->right!=NULL) //棧中先壓入右子樹,這樣才能先彈出左子樹
			{
				top++;
				stack[top]=p->right;
				level[top][0]=n+width;
				level[top][1]=2;
			}
			if(p->left!=NULL)
			{
				top++;
				stack[top]=p->left;
				level[top][0]=n+width;
				level[top][1]=1;
			}
		}
	}
}

/********************************************************************/
//記錄二叉樹所有結點數
void Nodenum(BinaryTreeNode *T, int &count_total)
{
	if(T)
	{
		++count_total;
		Nodenum(T->left,count_total);
		Nodenum(T->right,count_total);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章