Binary Search Tree

Write a C program for Binary Search Tree....

#include<stdio.h>
#include<conio.h>
typedef struct BST
{
       int info;
       struct BST *left,*right;
}bst;
bst* createBST(bst*,int);
void preorder(bst*);
void inorder(bst*);
void postorder(bst*);
void nodes(bst*);
void leafnodes(bst*);
void search(bst *,int);
int depth(bst *root);
bst* deletion(bst*,int);
int max(int,int);
int c=0;
int main()
{
    bst *root=NULL;
    int ch,v;
    do
    {
        printf("\n\t\t-----------------\n");
        printf("\n\t\tList of choice:\n");
        printf("\n1-Create BST");
        printf("\n2-Preorder");
        printf("\n3-Inorder");
        printf("\n4-Postorder");
        printf("\n5-count Total Nodes");
        printf("\n6-count Leaf Nodes");
        printf("\n7-Searching");
        printf("\n8-Depth");
        printf("\n9-deletion");
        printf("\n10-exit");
        printf("\nEnter your choice");
        scanf("%d",&ch);
        switch(ch)
        {
                  case 1:printf("\nEnter the value you want to insert: ");
                         scanf("%d",&v);
                         root=createBST(root,v);
                         break;
                  case 2:preorder(root);
                         break;
                  case 3:inorder(root);
                         break;
                  case 4:postorder(root);
                         break;
                  case 5:nodes(root);
                         printf("\nTotal Nodes: %d",c);
                         c=0;
                         break;
                  case 6:leafnodes(root);
                         printf("\nTotal Leaf Nodes: %d",c);
                         c=0;
                         break;
                  case 7:printf("\nEnter value to search: ");
                         scanf("%d",&v);
                         search(root,v);
                         break;
                  case 8:v=depth(root)-1;
                         printf("\nDepth of tree: %d",v);
                         break;
                  case 9:printf("\nEnter value to deleted: ");
                         scanf("%d",&v);
                         root=deletion(root,v);
                         break;
                  case 10:printf("\nPress ENTER to exit");
                         break;
                  default:printf("\nwrong choice!!!");
        }
    }
    while(ch!=10);
    getch();
    return 0;
}
bst* createBST(bst *root,int n)
{
     if(root==NULL)
     {
        root=(bst*)malloc(sizeof(bst));
        root->info=n;
        root->left=NULL;
        root->right=NULL;
     }
     else if(root->info>n)
        root->left=createBST(root->left,n);
     else
        root->right=createBST(root->right,n);
     return root;
}
void preorder(bst *root)
{
     if(root!=NULL)
     {
       printf("%d\n",root->info);
       preorder(root->left);
       preorder(root->right);
     }
}
void inorder(bst *root)
{
     if(root!=NULL)
     {
       inorder(root->left);
       printf("%d\n",root->info);
       inorder(root->right);
     }
}
void postorder(bst *root)
{
     if(root!=NULL)
     {
       postorder(root->left);
       postorder(root->right);
       printf("%d\n",root->info);
     }
}
void nodes(bst *root)
{
     if(root!=NULL)
     {
        c++;
        nodes(root->left);
        nodes(root->right);
     }
}
void leafnodes(bst *root)
{
     if(root->left==NULL&&root->right==NULL)
       c++;
     else
     {
        leafnodes(root->left);
        leafnodes(root->right);
     }
}
void search(bst *root,int v)
{
     while(root!=NULL)
     {
        if(v==root->info)
        {
              c++;
              break;
        }
        else if(v<root->info)
         root=root->left;
        else
         root=root->right;
     }
     if(root==NULL)
      printf("\nRecord Does not Found");
     else
      printf("\nRecord Found");
}
int depth(bst *root)
{
    int h1,h2;
    if(root==NULL)
      return 0;
    else
    {
      h1=depth(root->left)+1;
      h2=depth(root->right)+1;
      return max(h1,h2);
    }
}
int max(int a,int b)
{
    if(a>b)
     return a;
    else
     return b;
}
bst* deletion(bst *root,int n)
{
    bst *pr=NULL,*ch=root,*ssr,*spr;
    while(ch!=NULL)
    {
      if(root->info==n||ch->info==n)
        break;
      else if(ch->info>n)
      {
         pr=ch;
         ch=ch->left;
      }
      else
      {
         pr=ch;
         ch=ch->right;
      }
    }
    if(ch==NULL)
       printf("information does not found");
    else
    {        
       if(ch->left==NULL&&ch->right==NULL)
       {
          if(pr==NULL)
          root=NULL;
          else if(ch==pr->left)
          pr->left=NULL;
          else
          pr->right=NULL;
          free(ch);
       }
       else if(ch->left==NULL||ch->right==NULL)
       {
           if(ch->left==NULL&&ch->right!=NULL)
           {
              if(pr==NULL)
              {
                 root=root->right;
                 ch->right=NULL;
              }
              else if(ch==pr->left)
              {
                 pr->left=ch->right;
              }
              else
              {
                 pr->right=ch->right;
              }
              free(ch);
           }
           if(ch->left!=NULL&&ch->right==NULL)
           {
                if(pr==NULL)
                {
                  root=root->left;
                  ch->left=NULL;
                }
                else if(ch==pr->left)
                {
                    pr->left=ch->left;
                }
                else
                {
                   pr->right=ch->left;
                }
                free(ch);
           }
       }
       else
       {
          ssr=ch->right;
          spr=ch;
          while(ssr->left!=NULL)
          {
             spr=ssr;
             ssr=ssr->left;
          }
          ch->info=ssr->info;
          if(ssr==spr->left)
            spr->left=ssr->right;
          else
            spr->right=ssr->right;
          free(ssr);
       }
         printf("node deleted");
    }
    return root;
}
       
                           




DEFINATION A binary search tree (BST), also known as an ordered binary tree, is a node-based data structure in which each node has no more than two child nodes. Each child must either be a leaf node or the root of another binary search tree. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node.     



No comments:

Post a Comment