您的位置:首页 > 其它

二叉树的创建

2016-06-03 23:29 197 查看
二叉树以lson-rson链接方式存储,以菜单方式设计并完成功能任务:建立并存储树、输出前序遍历结果、输出中序遍历结果、输出后序遍历结果、交换左右子树、统计高度,其中对于中序、后序的遍历运算要求采用非递归方式。




#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
struct treenode
{
char c;
int tag;
struct treenode *lson;
struct treenode *rson;
};
int main()
{
void mune();                           //菜单函数
struct treenode *creat_bitree();       //创建二叉树
void preorder(struct treenode *T);     //前序遍历
void inorder(struct treenode *T);      //中序遍历
void postorder(struct treenode *T);    //后序遍历
void fdg_inorder(struct treenode *T);  //非递归中序遍历
void fdg_postorder(struct treenode *T);//非递归后序遍历
void exchange(struct treenode *T);     //交换左右子树
int height(struct treenode *T);        //求树的深度
int yezi(struct treenode *T);          //求树的叶子结点个数
struct treenode *T;
int choice;
mune();
while(1)
{
printf("请输入选择:");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:printf("请输入树结点:\n");T=creat_bitree();break;
case 2:printf("前序遍历结果:\n");preorder(T);break;
case 3:printf("中序遍历结果:\n");inorder(T);break;
case 4:printf("后序遍历结果:\n");postorder(T);break;
case 5:printf("非递归中序遍历结果:\n");fdg_inorder(T);break;
case 6:printf("非递归后序遍历结果:\n");fdg_postorder(T);break;
case 7:printf("交换成功!");exchange(T);break;
case 8:printf("树的高度为:%d",height(T));break;
case 9:printf("叶节点的个数为:%d",yezi(T));break;
case 10:printf("bye!\n");return 0;
}
printf("\n");
system("pause");
system("cls");
mune();
}
return 0;
}
void mune()
{
printf("1.建二叉树          2.前序遍历\n");
printf("3.中序遍历          4.后序遍历\n");
printf("5.非递归中序遍历    6.非递归后序遍历\n");
printf("7.交换左右子树      8.统计树高度\n");
printf("9.统计叶节点        10.退出\n");
}
struct treenode *creat_bitree()
{
char c;
struct treenode *T;
scanf("%c",&c);
if(c!='#')
{
T=(struct treenode *)malloc(sizeof(struct treenode));
T->c=c;
T->lson=creat_bitree();
T->rson=creat_bitree();
}
else
T=NULL;
return T;
};
void preorder(struct treenode *T)
{
char c;
if(T!=NULL)
{
c=T->c;
printf("%c ",c);
preorder(T->lson);
preorder(T->rson);
}
else
return ;
}
void inorder(struct treenode *T)
{
char c;
if(T!=NULL)
{
inorder(T->lson);
c=T->c;
printf("%c ",c);
inorder(T->rson);
}
else
return ;
}
void postorder(struct treenode *T)
{
char c;
if(T!=NULL)
{
postorder(T->lson);
postorder(T->rson);
c=T->c;
printf("%c ",c);
}
else
return ;
}
void fdg_inorder(struct treenode *T)
{
struct treenode *s[100];
struct treenode *p=T;
int i=0;
while(p!=NULL||i!=0)
{
while(p)
{
s[i++]=p;
p=p->lson;
}
if(i!=0)
{
i--;
p=s[i];
printf("%c ",p->c);
p=p->rson;
}
}
}
void fdg_postorder(struct treenode *T)
{
struct treenode *s[100];
struct treenode *p=T;
int i=0;
while(p!=NULL||i!=0)
{
while(p)
{
s[i++]=p;
p->tag=0;
p=p->lson;
}
if(i!=0)
{
i--;
p=s[i];
if(p->tag==0)
{
p->tag=1;
s[i++]=p;
p=p->rson;
}
else if(p->tag==1)
{
printf("%c ",p->c);
p=NULL;
}
}
}
}
void exchange(struct treenode *T)
{
struct treenode *p;
if(T!=NULL)
{
p=T->rson;
T->rson=T->lson;
T->lson=p;
exchange(T->lson);
exchange(T->rson);
}
else
return ;
}
int height(struct treenode *T)
{
int h1,h2;
if(T!=NULL)
{
h1=height(T->lson);
h2=height(T->rson);
if(h1>h2)
return h1+1;
else
return h2+1;
}
else
return 0;
}
int yezi(struct treenode *T)
{
static  int count=0;
if(T!=NULL)
{
count=yezi(T->lson);
if(T->lson==NULL&&T->rson==NULL)
count++;
count=yezi(T->rson);
}
return count;
}
谢谢来访!如有问题可与本人联系  QQ:2201774151
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: