#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);
}