前序遍历如上
中序就是213
后序是231
#include<stdio.h>
#include<stdlib.h>
typedef struct tree{
char c;
struct tree *left,*right;
}tree,*ptree;//定义一个树结点结构体
typedef struct list{
ptree data;
struct list *next;
}list,*plist;//辅助队列
typedef struct listht{
list *front,*rear;
};//队列头尾指针
void preorder(ptree p){
if(p!=NULL){
printf("%c",p->c);
preorder(p->left);
preorder(p->right);
}
}//前序遍历,调用自己递归
void inorder(ptree p){
if(p!=NUll){
preorder(p->left);
printf("%c",p->c);
preorder(p->right);
}
}//中序
void postorder(ptree p){
if(p!=NUll){
preorder(p->left);
preorder(p->right);
printf("%c",p->c);
}
}//后序
int main(){
ptree pnew;
char c;
ptree tree=NULL;
plist phead=NULL,ptail=NULL,listnew=NULL,pcur=NULL;
while(scanf("%c",&c)){
if(c=='\n'){
break;
}
pnew=(ptree) calloc(1,sizeof (tree));
pnew->c=c;
listnew=(plist) calloc(1,sizeof (list));
listnew->data=pnew;
if(NULL==tree){
tree=pnew;
phead=listnew;
ptail=listnew;
pcur=listnew;
}else{
ptail->next=listnew;
ptail=listnew;
if(NULL==pcur->data->left){
pcur->data->left=pnew;
}else if(NULL==pcur->data->right){
pcur->data->right=pnew;
pcur=pcur->next;
}
}
}//创二叉树的过程
preorder(tree);
return 0;
}