#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stack>
using namespace std;
#define md 1000000007;
struct tree
{
tree * lson;
tree * rson;
int value;
};
tree * head;
void build( tree * tmp,int cc){
if(tmp->value>cc){
if(tmp->lson!=NULL)build(tmp->lson,cc);
else{
tree * tt=NULL;
tt=(tree * )malloc(sizeof(tree));tt->lson=NULL;tt->rson=NULL;
tt->value=cc;
tmp->lson=tt;
}
}
else if(tmp->value<cc){
if(tmp->rson!=NULL)build(tmp->rson,cc);
else{
tree * tt=NULL;
tt=(tree * )malloc(sizeof(tree));tt->lson=NULL;tt->rson=NULL;
tt->value=cc;
tmp->rson=tt;
}
}
}
void preorder(tree * tt){
cout<<tt->value<<" ";
if(tt->lson!=NULL)preorder(tt->lson);
if(tt->rson!=NULL)preorder(tt->rson);
}
void midorder(tree * tt){
if(tt->lson!=NULL)midorder(tt->lson);
cout<<tt->value<<" ";
if(tt->rson!=NULL)midorder(tt->rson);
}
void aftorder(tree * tt){
if(tt->lson!=NULL)aftorder(tt->lson);
if(tt->rson!=NULL)aftorder(tt->rson);
cout<<tt->value<<" ";
}
int main()
{
cout<<"请输入一串不同的正整数,以-1结束,该序列表示插入顺序"<<endl;
int n;cin>>n;
if(n>0){
head=(tree * )malloc(sizeof(tree));
head->value=n;head->lson=NULL;head->rson=NULL;cin>>n;
}
else{
cout<<"no notes"<<endl;
return 0;
}
while(n>=0){
build(head,n);
cin>>n;
}
cout<<"pre:"<<endl;
preorder(head);puts("");
cout<<"mid:"<<endl;
midorder(head);puts("");
cout<<"aft:"<<endl;
aftorder(head);puts("");
return 0;
}