1 #include <iostream> 2 #include <cstdlib> 3 struct BSTNode{ 4 int v; 5 struct BSTNode *left,*right; 6 }; 7 8 struct BSTNode *root=NULL; 9 10 struct BSTNode* createNode(int data){ 11 struct BSTNode *newNode; 12 newNode=(struct BSTNode*)malloc(sizeof(struct BSTNode)); 13 newNode->v=data; 14 newNode->left=NULL; 15 newNode->right=NULL; 16 return newNode; 17 } 18 19 void insertion(struct BSTNode **node,int data){ 20 if(*node==NULL){ 21 *node=createNode(data); 22 }else if(data<(*node)->v){ 23 insertion(&(*node)->left,data); 24 }else if(data>(*node)->v){ 25 insertion(&(*node)->right,data); 26 } 27 } 28 void traverse(struct BSTNode *node){ 29 if(node!=NULL){ 30 traverse(node->left); 31 std::cout<<node->v; 32 traverse(node->right); 33 } 34 return; 35 } 36 int main(){ 37 int data,ch; 38 insertion(&root,10); 39 insertion(&root,16); 40 insertion(&root,8); 41 insertion(&root,20); 42 insertion(&root,21); 43 traverse(root); 44 std::cin>>data; 45 }