#include<bits/stdc++.h>
using namespace std;
struct Bst_Node{
int val;
Bst_Node*left,*right;
};
Bst_Node*Insert(Bst_Node*root,int x)
{
Bst_Node*newnode=new Bst_Node();
newnode->val=x;
newnode->left=NULL;
newnode->right=NULL;
Bst_Node*currnode;
currnode=root;
if(root==NULL)
{
root=newnode;
return root;
}
while(1)
{
if(x<currnode->val)
{
if(currnode->left!=NULL)
{
currnode=currnode->left;
}
else
{
currnode->left=newnode;
return root;
}
}
else
{
if(currnode->right!=NULL)
{
currnode=currnode->right;
}
else
{
currnode->right=newnode;
return root;
}
}
}
}
void Preorder(Bst_Node*parent)
{
if(parent!=NULL)
{
cout<<parent->val<<" ";
}
if(parent->left!=NULL)
{
Preorder(parent->left);
}
if(parent->right!=NULL)
{
Preorder(parent->right);
}
}
int main()
{
Bst_Node*root=NULL;
root=Insert(root,4);
root=Insert(root,5);
root=Insert(root,1);
Preorder(root);
cout<<endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IEJzdF9Ob2RlewppbnQgdmFsOwpCc3RfTm9kZSpsZWZ0LCpyaWdodDsKfTsKQnN0X05vZGUqSW5zZXJ0KEJzdF9Ob2RlKnJvb3QsaW50IHgpCnsKQnN0X05vZGUqbmV3bm9kZT1uZXcgQnN0X05vZGUoKTsKbmV3bm9kZS0+dmFsPXg7Cm5ld25vZGUtPmxlZnQ9TlVMTDsKbmV3bm9kZS0+cmlnaHQ9TlVMTDsKQnN0X05vZGUqY3Vycm5vZGU7CmN1cnJub2RlPXJvb3Q7CmlmKHJvb3Q9PU5VTEwpCnsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQp3aGlsZSgxKQp7CmlmKHg8Y3Vycm5vZGUtPnZhbCkKewppZihjdXJybm9kZS0+bGVmdCE9TlVMTCkKewpjdXJybm9kZT1jdXJybm9kZS0+bGVmdDsKfQplbHNlCnsKY3Vycm5vZGUtPmxlZnQ9bm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