#include
#include
#define NULL 0
#define maxsize 100
#define LEN sizeof(struct student)
typedef struct student
{struct student *lchild,*rchild;
char data;
}*STU;
STU stree_creat(char *s,int k)
{STU p;
if(s[k]==' '||k>maxsize)
return NULL;
else
{p=(STU)malloc(LEN);
p->data=s[k];
p->lchild=stree_creat(s,2*k 1);
p->rchild=stree_creat(s,2*k 2);
return p;
}
}
void print(STU p)
{ STU h[maxsize]={NULL};
int top=0,base=0;
h[top]=p;
printf("The chengci order:
");
while(h[base]!=NULL)
{
printf("[%c]",h[base]->data);
h[ top]=h[base]->lchild;
h[ top]=h[base]->rchild;
base ;
}
printf("
Success。
。。。。。
!!!");
}
/*前序遍历*/
void Preorder(STU p)
{if(p)
{
printf("[%c]",p->data);
Preorder(p->lchild);
Preorder(p->rchild);
}
}
/*中序遍历*/
void Inorder(STU p)
{if(p)
{
Inorder(p->lchild);
printf("[%c]",p->data);
Inorder(p->rchild);
}
}
/*后序遍历*/
void Postorder(STU p)
{if(p)
{
Postorder(p->lchild);
Postorder(p->rchild);
printf("[%c]",p->data);
}
}
/*交换该二叉树中所有结点左右孩子的非递归算法*/
void conver(char a[],int m)
{int i,j,h=1,k=2;
char t;
for(i=1;i<=(int)(log(m 1) 1);i )
{for(j=0;j {t=a[h j];a[h j]=a[k-j];a[k-j]=t;}
h=2*h 1;
k=2*k 2;
}
}
main()
{STU root=NULL;
int i=0,j=0,h=0;
char a[maxsize]={' '};
clrscr();
/*用一个数组存放一连串的字符串*/
gets(a);
while(a!=' ')
i ;
/*建立二叉树*/
root=stree_creat(a,0);
printf("
");
/*输出数组中的内容*/
print(root);
/*前序遍历*/
printf("Preorder Traversal。
。。。。。。。
");
Preorder(root);
printf("
");
/*中序遍历*/
printf("
Inorder Traversal。
。。。。。。。
");
Inorder(root);
printf("
");
/*后序遍历*/
printf("
Postorder Traversal。
。。。。。。。
");
Postorder(root);。
全部