#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);
}
}
二叉树的建立与显示
最新推荐文章于 2023-10-27 15:54:16 发布