Binary Search Tree

    /* Program to perform operations on a binary search tree  */
   
    #include
    #include
    #define new1 (nd *)malloc(sizeof(nd))
    typedef struct binary_search_tree
    {
        int info;
        struct binary_search_tree *lc;
        struct binary_search_tree *rc;
    }nd;

    void create(nd **current,int val)
    {
        char ch;
        nd *c=*current,*p,*new=new1;
        new->info=val;
        new->lc=NULL;
        new->rc=NULL;   
        if(*current==NULL)
            *current=new;
        else
        {
            while(c!=NULL)
            {
                if(new->info < c->info)
                {
                    p=c;
                    c=c->lc;
                }
                else if(new->info > c->info)
                {
                    p=c;
                    c=c->rc;
                }
                else if(new->info==c->info)
                {
                    printf("\nElement already present....\n");
                    return;
                }   
            }
            if(c==NULL)
            {
                if(new->info < p->info)
                     p->lc=new;
                else
                    p->rc=new;       
            }
        }
    }

    void inorder(nd *c)
    {
        if(c!=NULL)
        {
            inorder(c->lc);
            printf(" %4d ",c->info);
            inorder(c->rc);
        }
    }
    void preorder(nd *c)
    {
        if(c!=NULL)
        {
            printf(" %4d ",c->info);
            preorder(c->lc);
            preorder(c->rc);
        }
    }
    void postorder(nd *c)
    {   
        if(c!=NULL)
        {
            postorder(c->lc);
            postorder(c->rc);
            printf(" %4d ",c->info);
        }
    }
   
       
    main()
    {
        int val,ch,del;
        printf("\nProgram to perform operations on a binary tree : \n");
        nd *h=NULL;
        do
        {
            printf("[1] Create Node        [2] Traverse In-order \n");
            printf("[3] Traverse Pre-order    [4] Traverse Post-Order \n");
            printf("[5] Quit \n");
            printf("\nEnter your choice : ");
            scanf("%d",&ch);
            switch(ch)
            {
                case 1:
                    printf("\nEnter value to insert : ");
                    scanf("%d",&val);
                    create(&h,val);
                    break;
           
                case 2:
                    printf("\nBinary Tree : \n");
                    inorder(h);
                    printf("\n");
                    break;
                case 3:
                    printf("\nBinary Tree : \n");
                    preorder(h);
                    printf("\n");
                    break;
                case 4:
                    printf("\nBinary Tree : \n");
                    postorder(h);
                    printf("\n");
                    break;
                               
                case 5 : break;

                default : printf("\nEnter correct choice ...\n");
                    break;
            }
        }while(ch!=5);
        printf("\n\n");
    }

Top