#include <iostream>
#include "malloc.h"
using namespace std;
typedef struct BiTNode{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create(BiTree T){
char ch;
ch=getchar();
if(ch=='0')
T=NULL;
else{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
cout<<"Error!";
T->data=ch;
T->lchild=Create(T->lchild);
T->rchild=Create(T->rchild);
}
return T;
}
void Preorder(BiTree T){
if(T){
cout<<T->data;
Preorder(T->lchild);
Preorder(T->rchild);
}
}//先序遍历
void Inorder(BiTree T){
if(T){
Inorder(T->lchild);
cout<<T->data;
Inorder(T->rchild);
}
}//中序遍历
void Postorder(BiTree T){
if(T){
Postorder(T->lchild);
Postorder(T->rchild);
cout<<T->data;
}
}//后序遍历
int main(){
BiTree T;int choice;
cout<<"前序输入二叉树的结点序列,以0表示结点为空,按回车结束:"<<endl;
T=Create(T);
cout<<"选择遍历算法:先序遍历-1,中序遍历-2,后序遍历-3"<<endl;
cin>>choice;
if(choice==1){
Preorder(T);
cout<<endl;
}
else if(choice==2){
Inorder(T);
cout<<endl;}
else {Postorder(T);
cout<<endl;
}system("pause");
}
无聊写写代码,恰逢数据结构考试,顺便写了下二叉树...