typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *Lchild,*Rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T)
{//按先序次序输入二叉树中结点值,创建二叉链表表示的二叉树T
char ch;cin>>ch;//字符型数据,'#'表示为空二叉树
if(ch=='#') T=NULL;
else
{
BiTNode *T = new BiTNode;
T->data = ch;
CreateBiTree(T->Lchild);
CreateBiTree(T->Rchild);
}
}
void Copy(BiTree T,BiTree &NewT)
{
if(T==NULL) NewT=NULL;
else
{
BiTNode *NewT = new BiTNode;
NewT->data = T->data;
Copy(T->Lchild,NewT->Lchild);
Copy(T->Rchild,NewT->Rchild);
}
}
void Depth(BiTree T)
{
if(T==NULL) return 0;
else
{
int m=Depth(T->Lchild);
int n=Depth(T->Rchild);
return max(m,n)+1;
}
}
void NodeCount(BiTree T)
{//统计二叉树T中结点的个数
if(T==NULL) return 0;
else return NodeCount(T->Lchild) + NodeCount(T->Rchild) + 1;;
}
void InOrderTraverse(BiTree T)
{//中序遍历二叉树T的递归算法
if(T)
{
InOrderTraverse(T->Lchild);
cout<<T->data<<endl;
InOrderTraverse(T->Rchild);
}
}
void InOrderTraverse_Un(BiTree T)
{
LinkStack S;InitStack(S);
BiTNode *p=T;
BiTNode *q = new BiTNode;
while(p || !StackEmpty(S))
{
if(p)
{
Push(S,p);
p = p->Lchild;
}
else
{
Pop(S,q);
cout<<q->data<<endl;
p = q->data;
}
}
}
void LeafNodeCount(BiTree T)
{//统计叶子结点的个数
if(T==NULL) return 0;
else if(T->Lchild == NULL &&T->Rchild == NULL) return 1;
else return LeafNodeCount(T->Lchild)+LeafNodeCount(T->Rchild);
}
bool CompareTree(BiTree T1,BiTree T2)
{
bool Tree1IsNull = (Tree1==NULL);
bool Tree2IsNull = (Tree2==NULL);
if(Tree1IsNull!=Tree2IsNull) return false;
if(Tree1IsNull && Tree2IsNull) return true;
if(T1->data !T2->data) return false;
bool a = CompareTree(T1->Lchild,CompareTree(T2->Lchild))&&CompareTree(T1->Rchild,T2->Rchild);
bool b = CompareTree(T1->Lchild,CompareTree(T2->Rchild))&&CompareTree(T1->Rchild,T2->Lchild);
return a||b;
}
void ChangeLR(BiTree &T)
{
BiTree temp;
if(T->Lchild == NULL && T->Rchild == NULL) return;
else
{
temp = T->Lchild;
T->Lchild = T->Rchild;
T->Rchild = temp;
}
ChangeLR(T->Lchild);
ChangeLR(T->Rchild);
}
void DoubleTraverse(BiTree T)
{
if(T == NULL) return;
else if(T->Rchild == NULL && T->Lchild == NULL)
cout<<T->data<<endl;
else
{
cout<<T->data<<endl;
DoubleTraverse(BiTree T->Lchild);
cout<<T->data<<endl;
DoubleTraverse(T->Rchild);
}
}
int Width(BiTree T)
{
if(T==NULL) return 0;
else
{
BiTree Q[];
int Front=1,Rear=1;Last=1;
int temp = 0,maxw=0;
Q[Rear]=bt;
while(Front <= Last)
{
BiTNode *p = Q[Front++];
temp++;
if(p->Lchild!=NULL) Q[++rear]=p->Lchild;
if(p->Rchild!=NULL) Q[++rear]=p->Rchild;
if(Front>Last)
{
Last = Rear;
maxw=max(temp,maxw);
temp=0;
}
}
}
return maxw;
}