/* Note:Your choice is C IDE */
#include "stdio.h"
#include<stdlib.h>
typedef struct bitnode
{
int data;
struct bitnode *lchild,*rchild;
}bitnode,*bittree;
bittree cre_tree()
{
int x;
bittree root;
scanf("%d",&x);
if(x==-1)root=NULL;
else
{
root=(bittree)malloc(sizeof(bitnode));
root->data=x;
root->lchild=cre_tree();
root->rchild=cre_tree();
}
return root;
}
void preorder(bittree root)
{
if(root)
{
printf("%d",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(bittree root)
{
if(root)
{
inorder(root->lchild);
printf("%d",root->data);
inorder(root->rchild);
}
}
void postorder(bittree root)
{
if(root)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
}
}
bittree exchang(bittree root)
{
bittree t;
if(root)
{
if(root->lchild!=NULL&&root->rchild!=NULL)
{
t=root->lchild;
root->lchild=root->rchild;
root->rchild=t;
}
root->lchild=exchang(root->lchild);
root->rchild=exchang(root->rchild);
}
return root;
}
int hightree(bittree root)
{
int H,H1,H2;
if(root==NULL)H=0;
else
{
H1=hightree(root->lchild);
H2=hightree(root->rchild);
H=(H1>H2?H1:H2)+1;
}
return H;
}
int fine=0;
void searchtree(bittree root,int x,bittree *p)
{
if(root&&!fine)
if(root->data==x)
{ fine=1;*p=root; }
//若找到,则通过p带回该结点的指针
else
{
*p=NULL;
searchtree(root->lchild,x,p);
searchtree(root->rchild,x,p);
}
}
int n=0;
void leafcount(bittree root)
{
if(root)
{
if(root->lchild==NULL&&root->rchild==NULL)
n++;
leafcount(root->lchild);
leafcount(root->rchild);
}
}
void main()
{
int H,x;
bittree root,p;
root=cre_tree();
preorder(root);printf("\n\n");
inorder(root);printf("\n\n");
postorder(root);printf("\n\n");
root=exchang(root);preorder(root);printf("\n\n");
H=hightree(root);
printf("二叉树的深度为:");printf("%d\n",H);
scanf("%d",&x);searchtree(root,x,&p);
printf("要找的数据为:");
printf("%d",p->data);printf("\n");
leafcount(root);
printf("叶子的结点个数为:");printf("%d",n);
}