#include <iostream>
using namespace std;
struct node{
int left,right;
char value;
}data[101];
int root=0,cnt;
char ch;
int buildTree(int bt){
cin>>ch;
if(ch=='.'){
bt=0;
return bt;
}
else{
bt=++cnt;
data[bt].value=ch;
data[bt].left=data[bt].right=0;
data[bt].left=buildTree(bt);
data[bt].right=buildTree(bt);
}
return bt;
}
void postorder(int bt){
if(bt){
postorder(data[bt].left);
postorder(data[bt].right);
cout<<data[bt].value;
}
}
void preorder(int bt){
if(bt){
cout<<data[bt].value;
preorder(data[bt].left);
preorder(data[bt].right);
}
}
void inorder(int bt){
if(bt){
inorder(data[bt].left);
cout<<data[bt].value;
inorder(data[bt].right);
}
}
int main(){
root=buildTree(0);
cout<<"后序:";
postorder(root);
cout<<endl<<"中序:";
inorder(root);
cout<<endl<<"先序:";
preorder(root);
cout<<endl;
return 0;
}
|