#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//CEI#J##F#GK##H##D##
typedef struct BiTNode{
char data;
BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void print(){
puts("**************************");
puts("1. 先序遍历二叉树");
puts("2. 中序遍历二叉树");
puts("3. 后序遍历二叉树");
puts("4. 层次遍历二叉树");
puts("5. 计算结点个数");
puts("6. 计算叶子结点个数");
puts("7. 计算单分支结点个数");
puts("8. 计算二叉树的深度");
//puts("9.交换左右子树");
puts("9.计算二叉树的宽度");
puts("0. 退出");
puts("**************************");
}
void CreateBiTree(BiTree &T){ //前序建立二叉树
char ch;
scanf("%c",&ch);
if(ch=='#') T=NULL;
else{
T=(BiTree)malloc(sizeof(BiTNode));
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void travel1(BiTree T){ //前
if(T){
printf("%c",T->data);
travel1(T->lchild);
travel1(T->rchild);
}
}
void travel2(BiTree T){ //中
if(T){
travel2(T->lchild);
printf("%c",T->data);
travel2(T->rchild);
}
}
void travel3(BiTree T){ //后
if(T){
travel3(T->lchild);
travel3(T->rchild);
printf("%c",T->data);
}
}
void levelwise(BiTree T){ //层次遍历
BiTree queue[100],s;
int front=0,rear=0;
queue[0]=T;
while(front<=rear){
s=queue[front++];
printf("%c",s->data);
if(s->lchild!=NULL) queue[++rear]=s->lchild;
if(s->rchild!=NULL) queue[++rear]=s->rchild;
}
}
int NodeSum(BiTree T){ //结点总数
if(!T) return 0;
else return NodeSum(T->lchild)+NodeSum(T->rchild)+1;
}
int NodeNumber_1(BiTree T){ //单分支结点个数
if(!T) return 0;
if(((!T->lchild) && (T->rchild)) || ((T->lchild) && (!T->rchild))) return 1;
else return NodeNumber_1(T->lchild)+NodeNumber_1(T->rchild);
}
int NodeNumber_0(BiTree T){ //叶子结点个数
if(!T) return 0;
if((!T->lchild) && (!T->rchild)) return 1;
else return NodeNumber_0(T->lchild)+NodeNumber_0(T->rchild);
}
int TreeH(BiTree T){ //高度
if(!T) return 0;
else return TreeH(T->lchild)>TreeH(T->rchild)?(TreeH(T->lchild)+1):(TreeH(T->rchild)+1);
}
int TreeK(BiTree T){ //求二叉树的宽度
BiTree queue[100],p;
int qu[100]; //记录每个结点所在的层数
int rear=0,front=0,max=0,i=1,count[100];
for(i=0;i<=100;i++) count[i]=0;
queue[0]=T;
qu[0]=1;
while(rear>=front){
i=qu[front];
p=queue[front++];
if(p->lchild!=NULL) {
queue[++rear]=p->lchild;
qu[rear]=i+1;
count[i+1]++;
}
if(p->rchild!=NULL){
queue[++rear]=p->rchild;
qu[rear]=i+1;
count[i+1]++;
}
}
int n=i+1;
for(int i=2;i<n;i++){
if(count[i]>max) max = count[i];
printf("%d",count[i]);
}
return max;
}
void exchange(BiTree T,BiTree &NewT) // 交换左右子树
{
if(T==NULL)
{
NewT=NULL;
return ;
}
else
{
NewT=(BiTNode *)malloc(sizeof(BiTNode));
NewT->data=T->data;
exchange(T->lchild,NewT->rchild);
exchange(T->rchild,NewT->lchild);
}
}
int main(){
BiTree Tree,NewTree;
Tree=NULL;
print();
int n;
printf("请输入前序二叉树;");
CreateBiTree(Tree);
while(1){
scanf("%d",&n);
switch(n){
case 1:
printf("前序遍历二叉树为:");
travel1(Tree);
printf("\n");
break;
case 2:
printf("中序遍历二叉树为:");
travel2(Tree);
printf("\n");
break;
case 3:
printf("后序遍历二叉树为:");
travel3(Tree);
printf("\n");
break;
case 4:
printf("层次遍历二叉树为:");
levelwise(Tree);
printf("\n");
break;
case 5:
printf("计算结点个数为:%d",NodeSum(Tree));
printf("\n");
break;
case 6:
printf("计算单分支结点个数:%d",NodeNumber_1(Tree));
printf("\n");
break;
case 7:
printf("计算叶子结点个数: %d",NodeNumber_0(Tree));
printf("\n");
break;
case 8:
printf("计算二叉树的高度: %d",TreeH(Tree));
printf("\n");
break;
case 9:
// printf("交换左右子树:");
// exchange(Tree,NewTree);
// Tree=NewTree;
// printf("交换成功!可输入前面操作查看结果 \n");
// break;
//
printf("计算二叉树的宽度: %d",TreeK(Tree));
printf("\n");
break;
default:
exit(0);
break;
}
}
return 0;
}
//CEI#J##F#GK##H##D##