#Include Using Namespace #Include Struct
#Include Using Namespace #Include Struct
h> struct node { node *left; int data; node *right; }; node *root=NULL; node *parent; void insert() { node *temp=new node; cout<<"enter data for new node"; cin>>temp->data; temp->left=NULL; temp->right=NULL; if(root==NULL) //is tree empty?? root=temp; else// if tree is not empty { node *child=root;// point the top of the tree while(child!=NULL) // untill you get empty space { parent=child;// B4 moving save the current memory (parent) if(temp->data>child->data) child=child->right; //move to next(child) else child=child->left; } if(temp->data>parent->data) parent->right=temp; else parent->left=temp;
} void inorder(node *n) { if(n==NULL)//base function return; else { /// recursive function inorder(n->left); cout<<n->data<<" , "; inorder(n->right); } } void main() { while(1) { int ch; cout<<"enter 1 to add data in the tree"<<endl; cout<<"enter 2 to show the values by inorder traversal"<<endl; cin>>ch; switch(ch) { case 1: insert(); break; case 2: inorder(root); break; } } }