#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct TreeNode{
float data;
struct TreeNode *left;
struct TreeNode *right;
};
print(struct TreeNode *p){
if(p!=NULL){
cout<<p->data<<" ";
print(p->left);
print(p->right);
}
}
int main(void){
struct TreeNode *p;
struct TreeNode *father;
int position=0;
struct TreeNode *root;
root=NULL;
do{
p=(struct TreeNode*)malloc(sizeof(TreeNode));
p->left=NULL;
p->right=NULL;
cout<<"请输入数值 -1结束"<<endl;
cin>>p->data;
if(p->data==-1){
break;
}
if(root==NULL){
root=p;
}else{
for(struct TreeNode *temp=root;temp!=NULL;){
//存父节点
father=temp;
if(p->data<temp->data){
temp=temp->left;
position=-1;
}else{
temp=temp->right;
position=1;
}
}
//插入位置
if(position==-1){
father->left=p;
}else{
father->right=p;
}
}
}while(true);
print(root);
return 0;
}