#include <stdio.h>
#include <malloc.h>
typedef struct BiTNode
{
int data;
struct BiTNode *lchild,*rchild;
} BiTNode;
void Exchange(BiTNode *bt)
{
BiTNode *temp;
if(bt){
temp = bt -> lchild;
bt -> lchild = bt -> rchild;
bt -> rchild = temp;
Exchange(bt -> lchild);
Exchange(bt -> rchild);
}
}
void DispBST(BiTNode *bt)
{
if (bt!=NULL)
{
printf("%d",bt->data);
if (bt->lchild!=NULL || bt->rchild!=NULL)
{
printf("(");
DispBST(bt->lchild);
if (bt->rchild!=NULL) printf(",");
DispBST(bt->rchild);
printf(")");
}
}
}
int InsertBST(BiTNode *&p,int k)
{
if (p==NULL)
{
p=(BiTNode *)malloc(sizeof(BiTNode));
p->data=k;
p->lchild=p->rchild=NULL;
return 1;
}
else if (k==p->data)
return 0;
else if (k<p->data)
return InsertBST(p->lchild,k);
else
return InsertBST(p->rchild,k);
}
BiTNode *CreateBST(int A[],int n)
{
BiTNode *bt=NULL;
int i=0;
while (i<n)
{
InsertBST(bt,A[i]);
i++;
}
return bt;
}
int main()
{
BiTNode *bt,*p;
int a[]= {25,18,46,2,53,39,32,4,74,67,60,11},n=12,x=46;
bt=CreateBST(a,n);
DispBST(bt);
printf("\n");
Exchange(bt);
DispBST(bt);
printf("\n");
return 0;
}