#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
char data;
struct Node *LChild;
struct Node *RChild;
}BiTNode, *BiTree;
int depth = 0;
BiTree Creat(BiTree *bt)
{
char ch;
ch = getchar();
if(ch =='.')
*bt = NULL;
else
{
*bt = (BiTree)malloc(sizeof(BiTNode));
(*bt)->data = ch;
Creat(&((*bt)->LChild));
Creat(&((*bt)->RChild));
}
}
BiTree Preorder(BiTree *bt)
{
if((*bt) != NULL)
{
printf("%c", (*bt)->data);
Preorder(&((*bt)->LChild));
Preorder(&((*bt)->RChild));
}
}
BiTree printLeaf(BiTree bt)
{
if((bt) != NULL)
{
if((bt->LChild == NULL) && (bt->RChild == NULL))
printf("%c", bt->data);
printLeaf(bt->LChild);
printLeaf(bt->RChild);
}
}
int Leaf(BiTree bt)
{
int count;
if(bt == NULL)
count = 0;
else if((bt->LChild == NULL) && (bt->RChild == NULL))
count = 1;
else
count = Leaf(bt->LChild) + Leaf(bt->RChild);
return count;
}
int postDepth(BiTree bt)
{
int hl,hr,max;
if(bt != NULL)
{
hl = postDepth(bt->LChild);
hr = postDepth(bt->RChild);
max = hl>hr?hl:hr;
return (max+1);
}
else
return 0;
}
int preDepth(BiTree bt, int h)
{
if(bt != NULL)
{
if(h>depth)
depth = h;
preDepth(bt->LChild, h+1);
preDepth(bt->RChild, h+1);
}
return depth;
}
void PrintTree(BiTree Boot, int nLayer)
{
int i;
if(Boot == NULL)
return;
PrintTree(Boot->RChild, nLayer+1);
for(i=0; i<nLayer; i++)
printf(" ");
printf("%c \n", Boot->data);
PrintTree(Boot->LChild, nLayer+1);
}
int main()
{
int count, h = 1, height1, height2, nLayer = 5;
BiTree bt;
Creat(&bt);
count = Leaf(bt);
height2 = preDepth(bt, h);
PrintTree( bt, nLayer) ;
return 0;
}
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char data;
char Ltag ;
char Rtag ;
struct Node *LChild;
struct Node *RChild;
}BiTNode, *BiTree;
BiTree Creat(BiTree *bt)
{
char ch;
ch = getchar();
if(ch =='.')
*bt = NULL;
else
{
*bt = (BiTree)malloc(sizeof(BiTNode));
(*bt)->data = ch;
Creat(&((*bt)->LChild));
Creat(&((*bt)->RChild));
}
}
BiTree pre = NULL;
void Inthread(BiTree root)
{
if(root!=NULL)
{
Inthread(root->LChild);
if(root->LChild==NULL)
{
root->Ltag=1;
root->LChild=pre;
}
if(pre!=NULL&& pre->RChild==NULL)
{
pre->Rtag=1;
pre->RChild=root ;
}
pre=root;
Inthread(root->RChild);
}
}
BiTNode *InPre(BiTNode *p,BiTree pre)
{
BiTree q;
if(p->Ltag==1)
pre= p->LChild;
else
{
for(q= p->LChild; q->Rtag!=1; q=q->RChild);
pre=q;
}
return pre;
}
BiTNode *InSucc(BiTNode *p)
{
BiTree succ, q;
if(p->Rtag==1 || p->RChild==NULL)
succ= p-> RChild;
else
{
for(q=p->RChild;q->Ltag!=1; q=q->LChild);
succ=q;
}
printf("%c", succ->data);
return succ;
}
void TraverseInorderThrTree(BiTree p)
{
if(p)
{
while(p->Ltag != 1)
{
p = p->LChild;
}
do
{
printf("%c ", p->data);
p = InSucc(p);
}while(p);
}
}
void PrintTree(BiTree Boot, int nLayer)
{
int i;
if(Boot == NULL)
return;
PrintTree(Boot->RChild, nLayer+1);
for(i=0; i<nLayer; i++)
printf(" ");
printf("%c \n", Boot->data);
PrintTree(Boot->LChild, nLayer+1);
}
int main()
{
BiTree bt;
int nLayer = 5;
printf("输入前序二叉树:\n");
Creat( &bt);
Inthread(bt);
printf("输出中序序列:\n");
TraverseInorderThrTree(bt);
printf("\n");
return 0;
}