#include<stdio.h>
#include"stdlib.h"
typedef struct Node
{
char data;
struct Node *Lchild;
struct Node *Rchild;
}Bitnode,*BiTree;
/*void CreateBitree(BiTree *root)
{
char ch;
ch=getchar();
if(ch=='^') *root=NULL;
else
{
*root=(BiTree)malloc(sizeof(Bitnode));
(*root)->data=ch;
CreateBitree(&((*root)->Lchild));
CreateBitree(&((*root)->Rchild));
}
}*/
//层次遍历用到队列
typedef struct
{
BiTree date[100];
int first;
int last;
}sequeue;
int empty(sequeue *p) //是否为空
{
if(p->first==p->last)
{
return 1;
}
return 0;
}
void initqueue(sequeue **p) //初始化
{
*p=(sequeue*)malloc(sizeof(sequeue));
if((*p)!=NULL)
{
(*p)->first=-1;
(*p)->last=-1;
}
else
{
printf("memory overflow!\n");
exit (0);
}
}
void Enqueue(sequeue *p,BiTree root) //入队
{
if(p->last==100-1)
{
printf("队列已满\n");
getch();
}
else
{
p->last++;
p->date[p->last]=root;
}
}
BiTree outqueue(sequeue *p) //出队
{
BiTree root;
if(empty(p))
{
printf("队列为空\n");
}
else
{
p->first++;
root=p->date[p->first];
return root;
}
}
BiTree CreateBitree()
{
BiTree root;
char ch;
ch=getchar();
if(ch=='^') root=NULL;
else
{
root=(BiTree)malloc(sizeof(Bitnode));
(root)->data=ch;
root->Lchild=CreateBitree();
root->Rchild=CreateBitree();
}
return root;
}
void PreOrder(BiTree root) //先序遍历
{
if(root)
{
printf("%c",root->data);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BiTree root) //中序遍历
{
if(root)
{
InOrder(root->Lchild);
printf("%c",root->data);
InOrder(root->Rchild);
}
}
void PosOrder(BiTree root) //后序遍历
{
if(root)
{
PosOrder(root->Lchild);
PosOrder(root->Rchild);
printf("%c",root->data);
}
}
void Levelorder(BiTree root) //层次遍历
{
sequeue *q;
BiTree p;
initqueue(&q);
Enqueue(q,root);
while(!empty(q))
{
p=outqueue(q);
printf("%c",p->data);
if(p->Lchild!=NULL) Enqueue(q,p->Lchild);
if(p->Rchild!=NULL) Enqueue(q,p->Rchild);
}
}
void leaf(BiTree root) //求叶子结点
{
if(root)
{
leaf(root->Lchild);
if(root->Lchild==NULL && root->Rchild==NULL)
printf("%c",root->data);
leaf(root->Rchild);
}
}
int leafsum(BiTree root) //叶子结点数目
{
int nl,nr;
if(root==NULL) return 0;
if(root->Lchild==NULL && root->Rchild==NULL) return 1;
nl=leafsum(root->Lchild);
nr=leafsum(root->Rchild);
return(nl+nr);
}
void printBiTree(BiTree root,int n) //打印树
{
int i;
if(root!=NULL)
{
printBiTree(root->Rchild,n+1);
for(i=0;i<3*n;i++) printf(" ");
printf("%c\n",root->data);
printBiTree(root->Lchild,n+1);
}
}
int BiTreedepth(BiTree root) //求树的深度
{
int hl,hr,h;
if(root=NULL) return 0;
else
{
hl=BiTreedepth(root->Lchild);printf("n");
hr=BiTreedepth(root->Rchild);
h=(hl>hr?hl:hr)+1;
return h;
}
}
int d=0;
void deep(BiTree root ,int h) //求树的深度
{
if(root)
{
if(h>d) d=h;
deep(root->Lchild,h+1);
deep(root->Rchild,h+1);
}
}
BiTree parent(BiTree root,char Data)//寻找双亲结点
{
BiTree p;
if(root==NULL) return NULL;
if(root->Lchild->data==Data||root->Rchild->data==Data) return root;
p=parent(root->Lchild,Data);
if(p!=NULL) return p;
else return(parent(root->Rchild,Data));
}
void main()
{
BiTree root,p;
int sum,n=1,h=1;
char Data;
printf("请输入二叉树的结点:");
root=CreateBitree();
//CreateBitree(&root);
printf("\n先序遍历:");
PreOrder(root);
printf("\n中序遍历:");
InOrder(root);
printf("\n后序遍历:");
PosOrder(root);
printf("\n叶子结点为:");
leaf(root);
sum=leafsum(root);
printf("\n叶子结点数为:%d",sum);
printf("\n按树状打印:\n") ;
printBiTree(root,n);
deep(root,h);
//h=BiTreedepth(root);
printf("树的深度:%d\n",d);
printf("请输入结点:");
getchar();
Data=getchar();
p=parent(root,Data);
/* if(p!=NULL)*/ printf("双亲结点为:%c\n",p->data);
//else printf("没有该结点");
printf("层次遍历:");
Levelorder(root);
}