#include<iostream>
using namespace std;
class node
{
private:
node* left;
node* right;
int data;
public:
friend class bstree;
node();
node(int item)
{
data=item;
left=NULL;
right=NULL;
}
void setdata(int item)
{
data=item;
}
int getdata()
{
return data;
}
void setleft(node* left)
{
this->left=left;
}
node* getleft()
{
return left;
}
void setright(node *right)
{
this->right=right;
}
node* getright()
{
return right;
}
};
class bstree
{
private:
node* root;
public:
bstree():root(NULL){};
node* getroot()
{
return root;
}
bool toleft(int a,int b)
{
return( a<b? true:false);
}
bool toright(int a,int b)
{
return (a>b? true:false);
}
void create()
{
int getnum;
node* current;
while(cin>>getnum)
{
current=new node(getnum);
if(NULL==root)
root=current;
else
{
addnode(current,root);
}
}
}
void addnode(node* newnode,node* itemroot)
{
if(toleft(newnode->data,itemroot->data))
{
if(NULL==itemroot->left)
itemroot->left=newnode;
else
{
addnode(newnode,itemroot->left);
}
}
else if(toright(newnode->data,itemroot->data))
{
if(NULL==itemroot->left)
itemroot->right=newnode;
else
{
addnode(newnode,itemroot->right);
}
}
}
void preorder(node* t)
{
if(t)
{
cout<<t->getdata()<<" ";
preorder(t->getleft());
preorder(t->getright());
}
}
void inorder(node* t)
{
if(t)
{
inorder(t->left);
cout<<t->data<<" ";
inorder(t->right);
}
}
void postorder(node* t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
cout<<t->getdata()<<" ";
}
}
node* finddata(node* t,int item)
{
node* p;
if(NULL==t)
return NULL;
else if(t->getdata()==item)
return t;
else if(NULL!=(p=finddata(t->getleft(),item)))
return p;
else
return finddata(t->getright(),item);
}
node* findfather(node* t,node* p)
{
node* q;
if(NULL==t||NULL==p)
return NULL;
if(t->getleft()==p||t->getright()==p)
return t;
else if(NULL!=(q=findfather(t->getleft(),p)))
return q;
else
return findfather(t->getright(),p);
}
void deletesubtree(node* t)
{
if(NULL==t)
return ;
else if(t==root)
{
deleteall(t);
root=NULL;
return;
}
node *p,*q;
p=t;
q=findfather(root,p);
if(q)
{
if((q->getleft())==p)
q->setleft(NULL);
else
{
q->setright(NULL);
}
}
deleteall(p);
}
void deleteall(node* t)
{
if(NULL!=t)
{
deleteall(t->getleft());
deleteall(t->getright());
deleteall (t);
}
}
};
int main()
{
bstree tree;
tree.create();
tree.preorder (tree.getroot());
return 0;
}
using namespace std;
class node
{
private:
node* left;
node* right;
int data;
public:
friend class bstree;
node();
node(int item)
{
data=item;
left=NULL;
right=NULL;
}
void setdata(int item)
{
data=item;
}
int getdata()
{
return data;
}
void setleft(node* left)
{
this->left=left;
}
node* getleft()
{
return left;
}
void setright(node *right)
{
this->right=right;
}
node* getright()
{
return right;
}
};
class bstree
{
private:
node* root;
public:
bstree():root(NULL){};
node* getroot()
{
return root;
}
bool toleft(int a,int b)
{
return( a<b? true:false);
}
bool toright(int a,int b)
{
return (a>b? true:false);
}
void create()
{
int getnum;
node* current;
while(cin>>getnum)
{
current=new node(getnum);
if(NULL==root)
root=current;
else
{
addnode(current,root);
}
}
}
void addnode(node* newnode,node* itemroot)
{
if(toleft(newnode->data,itemroot->data))
{
if(NULL==itemroot->left)
itemroot->left=newnode;
else
{
addnode(newnode,itemroot->left);
}
}
else if(toright(newnode->data,itemroot->data))
{
if(NULL==itemroot->left)
itemroot->right=newnode;
else
{
addnode(newnode,itemroot->right);
}
}
}
void preorder(node* t)
{
if(t)
{
cout<<t->getdata()<<" ";
preorder(t->getleft());
preorder(t->getright());
}
}
void inorder(node* t)
{
if(t)
{
inorder(t->left);
cout<<t->data<<" ";
inorder(t->right);
}
}
void postorder(node* t)
{
if(t)
{
postorder(t->left);
postorder(t->right);
cout<<t->getdata()<<" ";
}
}
node* finddata(node* t,int item)
{
node* p;
if(NULL==t)
return NULL;
else if(t->getdata()==item)
return t;
else if(NULL!=(p=finddata(t->getleft(),item)))
return p;
else
return finddata(t->getright(),item);
}
node* findfather(node* t,node* p)
{
node* q;
if(NULL==t||NULL==p)
return NULL;
if(t->getleft()==p||t->getright()==p)
return t;
else if(NULL!=(q=findfather(t->getleft(),p)))
return q;
else
return findfather(t->getright(),p);
}
void deletesubtree(node* t)
{
if(NULL==t)
return ;
else if(t==root)
{
deleteall(t);
root=NULL;
return;
}
node *p,*q;
p=t;
q=findfather(root,p);
if(q)
{
if((q->getleft())==p)
q->setleft(NULL);
else
{
q->setright(NULL);
}
}
deleteall(p);
}
void deleteall(node* t)
{
if(NULL!=t)
{
deleteall(t->getleft());
deleteall(t->getright());
deleteall (t);
}
}
};
int main()
{
bstree tree;
tree.create();
tree.preorder (tree.getroot());
return 0;
}