二叉树

数据结构二叉树

#include<stdio.h>
#include<stdlib.h>
#define M 100
typedef char Etype;
typedef struct BiTNode
{
Etype data;
struct BiTNode *lch,*rch;
}BiTNode,*BiTree;
BiTree que[M];
int front=0,rear=0;

BiTNode *creat_bt1();
BiTNode *creat_bt2();
void preorder(BiTNode *p);
void inorder(BiTNode *p);
void postorder(BiTNode *p);
void enqueue(BiTree);
BiTree delqueue();
void levorder(BiTree);
int treedepth(BiTree);
void prtbtree(BiTree,int);
void exchange(BiTree);
int leafcount(BiTree);
void paintleaf(BiTree);
BiTNode *t;
int count=0;

void main()
{

char ch;int k;
do{
printf("\n\n\n");
printf("\n==========================================");
printf("\n\n 1.建立二叉树方法");
printf("\n\n 2.建立二叉树");
printf("\n\n 3.先根递归遍历二叉树");
printf("\n\n 4.中根递归遍历二叉树");
printf("\n\n 5.后根递归遍历二叉树");
printf("\n\n 6.打印二叉树");
printf("\n\n 0.结束程序运行");
printf("\n===========================================");
printf("\n请输入你的选择(1,2,3,4,5,6)");
scanf("%d,&k");
switch(k)
{
case 1:t=creat_bt1();break;
case 2:printf("\n请输入二叉树各节点的值:");fflush(stdin);
t=creat_bt2();break;
case 3:if(t){
printf(“先根遍历二叉树:”);
preorder(t);
printf("\n");
}
else printf(“二叉树为空!”);
break;
case 4:if(t)
{
printf(“中根遍历二叉树”);
inorder(t);
printf("\n");
}
else printf(“二叉树为空!”);
break;
case 5:if(t)
{
printf(“中根遍历二叉树”);
postorder(t);
printf("\n");
}
else printf(“二叉树为空!”);
break;
case 0:exit(0);
}
}which(k>=1&&k<=10);
printf(“再见!按Enter键,返回…”);
ch=getchar();
}

BiTNode *creat_bt1()
{
BiTNode *t,*p,*v[20];int i,j;Etype e;
printf("\n请输入二叉树各节点的编号和对应的值:");
scanf("%d,%c",&i,&e);
while(i!=0&&e!=’#’);
{
p=(BiTNode *)malloc(sizeof(BiTNode));
p->date=e;p->lch=NULL;p->rch=NULL;
v[i]=p;
if(i1) t=p;
else{
j=i/2;
if(i%2
0) v[j]->lch=p;
else v[j]->rch=p
}
printf("\n请继续输入二叉树各节点的编号和对应的值:");
scanf("%d,%c",&i,&e);
}
return(t);
}
BiTNode *creat_bt2()
{
BiTNode *t;Etype e;
scanf("%c",&e);
if(e==’#’) t=NULL;
else {
t=(BiTNode *)malloc(sizeof(BiTNode));
t->date=e;
t->lch=creat_bt1();
t->rch=creat_bt1();
}
return (y);
}
void preorder(BiTNode *p)
{
if§{
printf("%3c",p->date);
preorder(p->lch)lch;
preorder(p->rch)rch;
}
}
void inorder(BiTNode *p)
{
if§{
inorder(p->lch)lch;
printf("%3c",p->date);
inorder(p->rch)rch;
}
}
void postorder(BiTNode *p)
{
if§{
postorder(p->lch)lch;
postorder(p->rch)rch;
printf("%3c",p->date);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值