include <stdio.h>
include <stdlib.h>
//Biinary Search Tree typedef struct NODE { int data; struct NODE * lchild, *rchild; }node;
node * add(node * root, int n) { node temp, parent, current; temp=(node )malloc(sizeof(node)); temp->data=n; temp->lchild=NULL; temp->rchild=NULL;
int opt=1;
if(root==NULL)
{
root=temp;
opt=0;
}
else
{
current=root;
parent=NULL;
}
while(opt)
{
parent=current;
if(n<(parent->data))
{
current=(parent->lchild);
if(current==NULL)
{
current=temp;
opt=0;
}
}
else if(n>parent->data)
{
current=(parent->rchild);
if(current==NULL)
{
current=temp;
opt=0;
}
}
}
return root; }
//Given a binary tree, print its nodes in in-order void printInorder(node * root) { if(root==NULL) { return; } printInorder(root->lchild); printf("%d ", root->data); printInorder(root->rchild); }
int main() { node *root=NULL; int opt, x; do { printf("\nWELCOME!!!!\nEnter 1 to ADD node\nEnter 2 for IN-ORDER\nEnter 3 for PRE-ORDER\nEnter 4 for POST-ORDER\nEnter 5 to exit\n"); scanf("%d", &opt); switch(opt) { case 1 : printf("Enter the Number to add\n"); scanf("%d", &x); root=add(root, x); break; case 2 : printInorder(root); break; default :printf("This option is not yet prepared\n"); break; } }while(opt!=5);
return 0; }