C语言版二叉树遍历

#include<stdlib.h>
#include<stdio.h>
#define MAX 50
#define MAS 20
#define CHAR 1
#if CHAR
typedef char TElemType;
TElemType Nil=' ';
#define form "%c"
#else
typedef int TElemType;
TElemType Nil=0;
#define form "%d"
#endif
typedef struct node
{TElemType data;
struct node *left;
struct node *right;
struct node *parent;
}BiTNode,*BiTree;
BiTNode *InitBiTree(BiTNode *bt)
{
bt=NULL;
return bt;
}
BiTNode *CreateBiTree(BiTNode *bt)
{TElemType ch;
scanf(form,&ch);
if(ch==Nil) bt=NULL;
else
{bt=(BiTNode *)malloc(sizeof(BiTNode));
if(!bt) exit(0);
bt->data=ch; bt->parent=NULL;
bt->left=CreateBiTree(bt->left);
if(bt->left) bt->left->parent=bt;
bt->right=CreateBiTree(bt->right);
if(bt->right) bt->right->parent=bt;
}
return bt;
}
void PrintTree(BiTNode *bt,int i)
{ if(bt!=NULL)
{PrintTree(bt->right,i+5);
#if CHAR
if(bt->data!=Nil)
printf("%*c\n",i,bt->data);
#else
if(bt->data!=Nil)
printf("%*d\n",i,bt->data);
#endif
PrintTree(bt->left,i+5);
i=i-5;
}
}
void Prorder1(BiTNode *bt,void(*visit)(TElemType))/*先序遍历*/
{if(bt!=NULL)
{visit(bt->data);
Prorder1(bt->left,visit);
Prorder1(bt->right,visit);
}
}
void Prorder2(BiTNode *bt,void(*visit)(TElemType))/*中序遍历*/
{BiTNode *p,*stack[MAS];
int top;
top=0; p=bt;
while(top!=0||p!=NULL)
{while(p!=NULL)
{stack[top]=p; top++;
p=p->left;
}
if(top!=0)
{p=stack[top-1];
top--;
visit(p->data);
p=p->right;
}
}
}
void Prorder3(BiTNode *bt,void(*visit)(TElemType))/*后序遍历*/
{BiTNode *p,*stack[MAS];
int top;
top=0;
stack[top]=bt; top++;
while(top>0)
{p=stack[top-1]; top--;
while(p!=NULL)
{visit(p->data);
stack[top]=p->right;
top++;
p=p->left;
}
}
}
void visit(TElemType e)
{printf(form" ",e);
}
int SumLefts(BiTNode *bt,int sum)
{
if (bt!=NULL)
{
if (bt->left==NULL && bt->right==NULL)
{
printf("%4c",bt->data); sum++;
}
sum=SumLefts(bt->left,sum);
sum=SumLefts(bt->right,sum);
}
return(sum);
}
int SumTree(BiTNode *bt)
{static int sum=0;
if(bt!=NULL)
{printf("%4c",bt->data);
sum++;
sum=SumTree(bt->left);
sum=SumTree(bt->right);
}
return(sum);
}
BiTNode *Findchar(BiTNode *bt,char ch) /*二叉树查找结点*/
{BiTNode *p; /*利用函数名返回结果*/
if(bt!=NULL)
{if(bt->data==ch) p=bt;
p=Findchar(bt->left,ch);
p=Findchar(bt->right,ch);
}
if(p!=NULL) return(p);
else return(NULL);
}

main()
{ int j,i,a,sum=0;
BiTree bt;
bt=InitBiTree(bt);
#if CHAR
printf("请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n");
#else
printf("请先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n");
#endif
bt=CreateBiTree(bt);
printf("输入建立的二叉树!!!\n");
PrintTree(bt,5);
do{
printf("------------------------------------------------------------");
printf("\n 主菜单");
printf("\n 1 二叉树先序遍历");
printf("\n 2 二叉树中序遍历");
printf("\n 3 二叉树后序遍历");
printf("\n 4 二叉树叶子结点数");
printf("\n 5 二叉树结点数");
printf("\n 6 二叉树查找x结点");
printf("\n 0 退出");
printf("\n----------------------------------------------------------");
printf("\n");
printf("输入你要选择的数据:");
scanf("%d",&i);
switch(i)
{case 1: printf("先序遍历结果为:");
Prorder1(bt,visit);
break;
case 2: printf("后序遍历结果为:");
Prorder2(bt,visit);
break;
case 3: printf("中序遍历结果为:");
Prorder3(bt,visit);
break;
case 4: j=SumLefts(bt,sum);
printf("树的叶子结点数为%d:",j);
break;
case 5: j=SumTree(bt);
printf("树的结点数为%d:",j);
break;
case 6: printf("输入要查找的结点字符x:");
scanf("%c",&a); scanf("%*c");
j=Findchar(bt,a);
printf("要查找的结点的指针为%d:",j);
break;
case 0: exit(0);
}
printf("\n");
getch();
}while(i>0||i<8);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值