Powered By Blogger

Saturday, April 30, 2011

Binary Search Tree complete operations

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node *l,*r;
};
struct node *root=NULL;
int item,item2,item3;
struct node *insert(struct node *root,int item);
void search(struct node *root,int item);
void preorder(struct node *root);
void inorder(struct node *root);
void postorder(struct node *root);
void delnode(struct node *root,int item3);

void main()
{
    int choice,ch=0;
    do
    {
        printf("\t\t\tBinary Search Tree Operations :\n");
        printf("\t\t1.Insert a node in the tree.\n");
        printf("\t\t2.Traverse and display the tree in inorder.\n");
        printf("\t\t3.Traverse and display the tree in preorder.\n");
        printf("\t\t4.Traverse and display the tree in postorder.\n");
        printf("\t\t5.Delete a node from the tree.\n");
        printf("\t\t6.Search for an element.\n");
        printf("Enter your choice : ");
        scanf_s("%d",&choice);
        switch(choice)
        {
        case 1:root=insert(root,item);
            break;
        case 2:inorder(root);
            break;
        case 3:preorder(root);
            break;
        case 4:postorder(root);
            break;
        case 5:delnode(root,item3);
            break;
        case 6:search(root,item2);
            break;
        default:printf("Wrong choice !!\n");
            break;
        }
        printf("Press 0 to continue.... ");
        scanf("%d",&ch);
    }while(ch==0);
}

struct node *insert(struct node *root,int item)
{
    struct node *p,*f;
    printf("Enter the item : ");
    scanf_s("%d",&item);
    p=root;
    if(root==NULL)
    {
        root=(struct node *)malloc(sizeof(struct node));
        root->data=item;
        root->l=NULL;
        root->r=NULL;
       
    }
    else
    {
       
        while(p!=NULL)
        {
            f=p;
            if(item>p->data)
            {
                p=p->r;
            }
            else
                p=p->l;
        }
        if(item>f->data)
        {
            f->r=(struct node *)malloc(sizeof(struct node));
            f=f->r;
            f->data=item;
            f->l=f->r=NULL;
        }

        else
        {
            f->l=(struct node *)malloc(sizeof(struct node));
            f=f->l;
            f->data=item;
            f->l=f->r=NULL;
        }
    }
    return root;
}

void inorder(struct node *root)
{
    struct node *p;
    p=root;
    if(p!=NULL)
    {
       
       
        inorder(p->l);
        printf("%3d",p->data);
        inorder(p->r);
    }
}

void preorder(struct node *root)
{
    struct node *p;
    p=root;
    if(p!=NULL)
    {
       
       
        printf("%d",p->data);
        preorder(p->l);
        preorder(p->r);
    }
}

void postorder(struct node *root)
{
    struct node *p;
    p=root;
    if(p!=NULL)
    {
       
       
        postorder(p->l);
        postorder(p->r);
        printf("%d",p->data);
    }
}

void search(struct node *root,int item2)
{
    struct node *p;
    if(root==NULL)
    {
        printf("Tree is empty!!\n");
        return;
    }
    else
    {
        p=root;
        printf("Enter the element to be searched : ");
        scanf("%d",&item2);
        while(p!=NULL)
        {
            if(item2==p->data)
            {
                printf("Item found.\n");
                return;
            }
            else if(item2>p->data)
                p=p->r;
            else
                p=p->l;
        }
        printf("Element not found.\n");
    }
}

void delnode(struct node *root,int item3)
{
    struct node *curr,*par,*temp,*tmp;
    int found=0; curr=root;
    if(curr==NULL)
    {
        printf("The tree is empty !!\n");
        return;
    }
    else
    {
        printf("Enter the element to delete : ");
        scanf("%d",&item3);
        while(curr!=NULL)
        {
            par=curr;
            if(item3==curr->data)
            {
                found=1;
                break;
            }
            else if(item3<curr->data)
                curr=curr->l;
            else
                curr=curr->r;
        }
        if(!found)
        {
            printf("Element not found !!\n");
            return;
        }
        if(curr->l==NULL && curr->r==NULL) //Leaf node
        {
            if(par->l==curr)
                par->l=NULL;
            else
                par->r=NULL;
            free(curr);
            return;
        }
        //Node having only RST
        if(curr->l==NULL && curr->r!=NULL) 
        {
            if(par->l==curr)
            {
                par->l=curr->r;
                free(curr);
            }
            else
            {
                par->r=curr->r;
                free(curr);
            }
            return;
        }
        //Node having only LST
        if(curr->l!=NULL && curr->r==NULL)
        {
            if(par->l==curr)
            {
                par->l=curr->l;
                free(curr);
            }
            else
            {
                par->r=curr->l;
                free(curr);
            }
            return;
        }
        if(curr->l!=NULL && curr->r!=NULL)
        {
            //Replace current node's data with smallest element
            struct node *chkr,*lcurr,*lcurrp,*tmp;
            chkr=curr->r;
            if((chkr->l==NULL) && (chkr->r==NULL))
            {
                curr->data=chkr->data;
                curr->r=NULL;
                free(chkr);
                return;
            }
            else if(chkr->l!=NULL)
            {
                //lcurr is the node to be replaced
                lcurr=chkr;
                //lcurrp is the parent of lcurr
                lcurrp=chkr->l;
                while(lcurr->l!=NULL)
                {
                    lcurrp=lcurr;
                    lcurr=lcurr->l;
                }
                curr->data=lcurr->data;
                lcurrp->l=NULL;
                free(lcurr);
            }
            else
            {
                tmp=curr->r;
                curr->data=tmp->data;
                curr->r=tmp->r;
                free(tmp);
            }
        }
   }
   printf("Element terminated\n");
}

       

No comments:

Post a Comment