Tuesday, 7 March 2017

Creation or insertion, deletion, inorder, preorder, postorder traversals in BST : JAVA and C program

minimum height of bst = log2(n+1)-1 if height of the tree is 0 if it contains single node.




import java.util.Scanner;
class BSTNode
{
    int data;
    BSTNode left,right;
}
class BST
{
    BSTNode root=null;
    public BSTNode insert(BSTNode root, int val)
    {
        if(root == null)
        {
            BSTNode newnode = new BSTNode();
            newnode.data=val;
            newnode.left=newnode.right=null;
            root=newnode;
        }
        else if(val<root.data)
            root.left=insert(root.left,val);
        else
            root.right=insert(root.right,val);
        return root;
    }
    public void inorder(BSTNode root)
    {
        if(root != null)
        {
            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }
     public void preorder(BSTNode root)
    {
        if(root != null)
        {
            System.out.println(root.data);
            preorder(root.left);
            preorder(root.right);
        }
    }
    public void postorder(BSTNode root)
    {
        if(root != null)
        {
            postorder(root.left);
            postorder(root.right);
            System.out.println(root.data);
        }
    }
    public BSTNode findmin(BSTNode root)
    {
        if(root == null)
            return null;
        if(root.left == null)
            return root;
        return findmin(root.left);
    }
    
     public BSTNode delete(BSTNode root, int val)
    {
        if(root == null)
        {
                 System.out.println("deleted element not found");
                 return root;
        }
        else if(val<root.data)
            root.left=delete(root.left,val);
        else if(val>root.data)
            root.right=delete(root.right,val);
        else
        {
            if(root.left == null && root.right == null)
                return null;
            else if(root.left == null && root.right != null) /*if only right child exists*/
                return root.right;
            else if(root.left != null && root.right == null) /* if only left child exists*/
                return root.left;
            else
                {
                    BSTNode c = findmin(root.right); /*find the minimum element in right subtree*/
                    root.data=c.data;  /* copy the minimum element value into the root */
                    root.right=delete(root.right,c.data); /*delete the duplicate element.*/
                    return root;
                }
        }
        return root;
    }
}
public class BSTDemo
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        BST ob = new BST();
        while(true)
        {
             System.out.println("1. insert 2. inorder 3. preorder 4. postorder 5. delete 6. exit");
             int choice = sc.nextInt();
             switch(choice)
             {
                 case 1: 
                         System.out.println("enter the value to be inserted");
                         int val=sc.nextInt();
                         ob.root=ob.insert(ob.root,val);
                         break;
                case 2:
                        ob.inorder(ob.root);
                        break;
                case 3:
                        ob.preorder(ob.root);
                        break;
                case 4:
                        ob.postorder(ob.root);
                        break;
                case 5:
                        System.out.println("enter the value to be deleted");
                        val=sc.nextInt();
                        ob.root=ob.delete(ob.root,val);
                        break;
                default:
                        System.exit(0);
             }
        }
    }
}

Sample input for which you can check the program
1 10
1 20
1 30
1 2
1 45
1 55
2
5 55
2
5 10
2
5 20
2

6
===========================================================

#include <stdio.h>

#include <stdlib.h>

struct node * insert(struct node *,int l);

void inorder(struct node *);

void preorder(struct node *);

void postorder(struct node *);

struct node * findMin(struct node *);

struct node * delete(struct node *, int);

struct node

{

               int data;

               struct node *left,*right;

};

void main()

{

               int val,opt;

               struct node *root = NULL,*c;

  while(1)

  {

               printf("\n Press 1. insert \t 2. inorder\t 3. preorder \t4. postorder \t 5. delete \t 6. exit\n");

               scanf("%d",&opt);

               switch(opt)

               {

                              case 1:  printf("\nenter a value");

                                             scanf("%d",&val);

                                             root=insert(root,val);

                                             break;

                              case 2: printf("\n inorder traversal\t");

                                             inorder(root);

                                             break;

                              case 3:  printf("\n preorder traversal\t");

                                             preorder(root);

                                             break;

                              case 4:  printf("\n postorder traversal\t");

                                             postorder(root);

                                             break;

                              case 5:  printf("\n enter the element to delete");

                                             scanf("%d",&val);

                                             root=delete(root,val);

                                             break;

                              case 6: exit(0);

               }

    }

}


struct node * insert(struct node *root,int val)

{

               if(root == NULL)

               {

                              struct node *newnode=(struct node *)malloc(sizeof(struct node));

                              newnode->data = val;

                              newnode->left = NULL;

                              newnode->right = NULL;

                              root=newnode;

               }

               else if(val < root->data)

                              root->left=insert(root->left,val);

               else

                              root->right = insert(root->right,val);

return root;

}

void inorder(struct node * root)

{

               if(root != NULL)

               {

                              inorder(root->left);

                              printf("%d\t",root->data);

                              inorder(root->right);

               }

}

void preorder(struct node * root)

{

               if(root != NULL)

               {

                              printf("%d\t",root->data);

                              preorder(root->left);

                              preorder(root->right);

               }

}

void postorder(struct node * root)

{

               if(root != NULL)

               {

                              postorder(root->left);

                              postorder(root->right);

                              printf("%d\t",root->data);

               }

}

struct node * findMin(struct node * root)

{

               if(root == NULL)

                              return NULL;

               else if(root->left == NULL)

                              return root;

               else

                              return findMin(root->left);

}

struct node * delete(struct node *root, int val)

{

               struct node *c;

               if(root == NULL)
               {
                              printf("deleted element not found");
                              return root;
               }
               else if( val < root->data)
                              root->left=delete(root->left,val);

               else if( val > root->data)
                              root->right=delete(root->right,val);

               else
               {
                              if(root->left == NULL && root->right == NULL)

                                             return NULL;

                               else if(root->right != NULL && root->left == NULL) /*if only right child exists*/

                                             return root->right;

                              else if(root->left != NULL && root->right == NULL)  /* if only left child exists*/

                                             return root->left;

                              else

                              {
                                             c=findMin(root->right); /*find the minimum element in right subtree*/

                                             root->data=c->data;           /* copy the minimum element value into the root */

                                             root->right=delete(root->right,c->data);  /*delete the duplicate element.*/
                                             return root;
                              }
               }
}

No comments:

Post a Comment