Wednesday 7 October 2015

BST program ( Binary Search Tree) creation / insertion/ deletion/preorder travesal C program

today we will be discussing about very important trees that is binary search trees. Here the parent node is smaller than the right child and greater than the left child. and left and right children are also a binary tree . 
if we want to add the elements in binary search tree we have to maintain the BST property see the program and you will understand about this . 

#include<stdio.h>
struct node
{
    int key ;
    struct node *left;
    struct node *right;
};
typedef struct node* NODE;
void main ()
{
    NODE root = NULL;
    insertion(&root,40);
    insertion(&root,25);
    insertion(&root,70);
    insertion(&root,22);
    insertion(&root,35);
    insertion(&root,60);
    insertion(&root,80);
    insertion(&root,90);
    insertion(&root,10);
    insertion(&root,30);
    deletion(&root,45);
    inorder(root);

}
void insertion(NODE* m,int value)
{
    NODE current= *m;
    NODE temp = (NODE)malloc(sizeof(struct node));
    temp->key= value ;
      temp->left=NULL;
    temp->right=NULL;
    if (current== NULL) /* if the root is node is already empty */
    {
        *m= temp;
    }
    else if(current->key > value ) /* we will try to find the exact position by comparing keys */
    {
        insertion(&current->left,value);

    }
    else if(current->key <value)
    {
        insertion(&current->right,value);
    }

}

void deletion(NODE **m,int value)
{


    NODE current= *m;
    if (current==NULL)       /* if current node is NULL it means that node doesn't exit because null is                                                last node in a tree */
    {
        printf ("node doesn't exit");
        return;
    }
    if (current->key > value ) /*we will try to find the exact location where the node we are deleting                                                    will lie */
    {
          deletion(&current->left,value);

    }
    else if(current->key <value)
    {
        deletion(&current->right,value);
    }

    else       /* we are on the node where key == value we are deleting it can have four cases */
    {
        if((current->left==NULL)&&(current->right==NULL)) /*1> both children are null */
        {
            *m==NULL;
        }
        else if(current->right==NULL) /* 2>only right child is null but on left there is a valid child */
        {
            *m=current->left;

        }
        else if(current->left==NULL) /*3>only left child is null but on right there is a valid child */
        {
            *m= current->right;
        }
        else       /* both children exist */
        {
            int next_big = successor(current); /* try to find the next element */
            current->key = next_big;    
            deletion(&current->right,next_big);  /*delete the node successor from there */

        }


    }



}

int successor(NODE m )
{
     NODE x= m->right;
    if (x->left!=NULL )
    {
        x=x->left;
    }
    return x;

}

void inorder(NODE m )
{
    if (m==NULL)
    return ;
    else
    {
        inorder(m->left);
        printf("%d\n",m->key);
        inorder(m->right);
    }



}

No comments:

Post a Comment