#include<stdlib.h>
typedef struct TreeNode
{
char ch;
struct TreeNode *lchild;
struct TreeNode *rchild;
}BTNode,*PBTNode;
void createBTree(PBTNode *root)
{
char ch;
ch=getchar();
if(ch=='#')
*root=NULL;
else{
*root=(PBTNode)malloc(sizeof(BTNode));
(*root)->ch=ch;
(*root)->lchild=NULL;
(*root)->rchild=NULL;
createBTree(&(*root)->lchild);
createBTree(&(*root)->rchild);
}
}
void preOrder(PBTNode root)
{
if(root==NULL)
return;
printf("%-3c",root->ch);
preOrder(root->lchild);
preOrder(root->rchild);
}
void inOrder(PBTNode root)
{
if(root==NULL)
return;
preOrder(root->lchild);
printf("%-3c",root->ch);
preOrder(root->rchild);
}
void postOrder(PBTNode root)
{
if(root==NULL)
return;
preOrder(root->lchild);
preOrder(root->rchild);
printf("%-3c",root->ch);
}
void displyLeaf(PBTNode root)
{
if(root==NULL)
return;
if(root->lchild==NULL&&root->rchild==NULL)
printf("%-3c",root->ch);
else{
displyLeaf(root->lchild);
displyLeaf(root->rchild);
}
}
void inseartLeftNode(PBTNode root,char ch)
{
PBTNode p,newNode;
if(root==NULL)
return;
p=root->lchild;
newNode=(PBTNode)malloc(sizeof(BTNode));
newNode->ch=ch;
newNode->rchild=NULL;
newNode->lchild=p;
root->lchild=newNode;
}
void inseartRightNode(PBTNode root,char ch)
{
PBTNode p,newNode;
if(root==NULL)
return;
p=root->rchild;
newNode=(PBTNode)malloc(sizeof(BTNode));
newNode->ch=ch;
newNode->lchild=NULL;
newNode->rchild=p;
root->rchild=newNode;
}
void clear(PBTNode* root)
{
PBTNode pl,pr;
if(*root==NULL)
return;
pl=(*root)->lchild;
pr=(*root)->rchild;
(*root)->lchild=NULL;
(*root)->rchild=NULL;
free((*root));
*root=NULL;
clear(&pl);
clear(&pr);
}
void deleteLeftTree(PBTNode root)
{
if(root==NULL)
return;
clear(&root->lchild);
root->lchild=NULL;
}
void deleteRightTree(PBTNode root)
{
if(root==NULL)
return;
clear(&root->rchild);
root->rchild=NULL;
}
PBTNode search(PBTNode root,char ch)
{
PBTNode p;
if(root==NULL)
return NULL;
if(root->ch==ch)
return root;
else{
if((p=search(root->lchild,ch))!=NULL)
return p;
else
return search(root->rchild,ch);
}
}
int BTreeHeight(PBTNode root)
{
int lchildHeight,rchildHeight;
if(root==NULL)
return 0;
lchildHeight=BTreeHeight(root->lchild);
rchildHeight=BTreeHeight(root->rchild);
return (lchildHeight>rchildHeight)?(1+lchildHeight):(1+rchildHeight);
}
int countLeaf(PBTNode root)
{
if(root==NULL)
return 0;
if(root->lchild==NULL&&root->rchild==NULL)
return 1;
else{
return countLeaf(root->lchild)+countLeaf(root->rchild);
}
}
int countAll(PBTNode root)
{
if(root==NULL)
return 0;
return countAll(root->lchild)+countAll(root->rchild)+1;
}
PBTNode copyBTree(PBTNode root)
{
PBTNode p,lchild,rchild;
if(root==NULL)
return NULL;
lchild=copyBTree(root->lchild);
rchild=copyBTree(root->rchild);
p=(PBTNode)malloc(sizeof(BTNode));
p->ch=root->ch;
p->lchild=lchild;
p->rchild=rchild;
return p;
}