Write a code to create a complete binary tree using Linked List for any given sequence of integer elements. The sequence of elements has to be taken as input from the user. After the tree has been formed do the following: 1. Find the minimum and maximum element in the tree 2. Find sum of elements at each level 3. Print the elements beginning from the highest level in a left to right order. 4. Delete any arbitrary element but modify the tree structure to maintain the completeness of the tree. Print the tree level-wise after the deletion.

import java.util.*;

public class assg09

{

 static TreeNode root;

 static int min,max;

 public static void min(TreeNode n)

 {

     if(n.getleft()!=null)

     {

         if(n.getleft().getdata()<min)

         {

             min=n.getleft().getdata();

         }

         min(n.getleft());

     }

     if(n.getright()!=null)

     {

         if(n.getright().getdata()<min)

         {

             min=n.getright().getdata();

         }

         min(n.getright());

     }

 }

 public static void max(TreeNode n)

 {

   

     if(n.getleft()!=null)

     {

         if(n.getleft().getdata()>max)

         {

             max=n.getleft().getdata();

         }

         max(n.getleft());

     }

     if(n.getright()!=null)

     {

         if(n.getright().getdata()>max)

         {

             max=n.getright().getdata();

         }

         max(n.getright());

     }

 }

 public static void main(String[]args)

 {

        Scanner input=new Scanner(System.in);

          assg09 bt=new assg09();

        while(true)

        {

             System.out.println("");

           System.out.println("   1. the minimum and maximum element in the tree ");

           System.out.println("   2. preorder ");

           System.out.println("   3. the elements beginning from the highest level in a left to right order. ");

             int choice = input.nextInt();

              System.out.println("----------------------------*************************************---------------------");

            switch(choice)

               

            {

                case 1 :

                            System.out.println("enter the no terms");

                              int no=input.nextInt();

                          TreeNode[] tn=new TreeNode[no+1];

                           System.out.println("enter the elements");

                          root=new TreeNode(input.nextInt());

                           tn[1]=root;

                              int count=1;

                          for(int i=1;i<no;i++)

                              {

                             for(int j=1;j<=count;j++)

                                {

                                     if(tn[2*j]==null)

                                          {

                                             TreeNode node=new TreeNode(input.nextInt());

                                             tn[j].setleft(node);

                                             tn[2*j]=node;

                                             count++;

                                             break;

                                              }

                                    if(tn[2*j]!=null&&tn[2*j+1]==null)

                                           {

                                              TreeNode node=new TreeNode(input.nextInt());

                                              tn[j].setright(node);

                                              tn[2*j+1]=node;

                                               count++;

                                               break;

                                   }

                                }

                            }

                                    System.out.println(" " + "");

                                 

                                min=root.getdata();

                                 bt.min(root);

                                    System.out.println("minimum value is  "+min);

                                max=root.getdata();

                                    bt.max(root);

                                  System.out.println("maximum value is  "+max);

                                    System.out.println("\n");

                                     System.out.println("----------------------------*************************************---------------------");

                                      break;

                case 2 :      System.out.println();

                           

                                bt.preOrderPrint();

                                System.out.println("\n");

                                 System.out.println("----------------------------*************************************---------------------");

                    break;

                case 3 :    System.out.println("   ELEMENTS IN LEFT TO RIGHT ORDER  ");

                                 bt.printLevelOrder(5);

                               

                                 System.out.println("\n");

                                  System.out.println("----------------------------*************************************---------------------");

                    break;

                   

           

                 

            }

            }

        }

     

     

     

       

     

       

     

public void preOrderPrint()

{

preOrderPrint(root);

}

private void preOrderPrint(TreeNode tree)

{

if (tree != null)

{

System.out.print(tree.getdata() + " ");

preOrderPrint(tree.getleft());

preOrderPrint(tree.getright());

}

}

 public void printLevelOrder(int depth)

 {

     int x=0;

    for (int i = 1; i <= depth; i++)

    {

       //  System.out.print("  LEVEL    "+ i+"  =  ");

        printLevel(root, i);

       

     

    }

}

public void printLevel(TreeNode t, int level)

{

 

    if (t == null)

    {

        return;

    }

   

    if (level == 1)

    {

        System.out.print(t.getdata()+"   ");

           

    }

   

    else if (level > 1)

    {  

        printLevel(t.getleft(), level - 1);

     

        printLevel(t.getright(), level - 1);

     

       

    }

 

}

}  

 class TreeNode {

private int data;

private TreeNode left;

private TreeNode right;

public TreeNode(int element){

data = element;

left = null;

right = null;

}

public void setdata(int d){

    this.data=d;

}

public void setleft(TreeNode d){

    this.left=d;

}

public void setright(TreeNode d){

    this.right=d;

}

public int getdata(){

    return this.data;

}

public TreeNode getleft(){

    return this.left;

}

public TreeNode getright(){

    return this.right;

}

}
Write a code to create a complete binary tree using Linked List for any given sequence of integer elements. The sequence of elements has to be taken as input from the user. After the tree has been formed do the following: 1. Find the minimum and maximum element in the tree 2. Find sum of elements at each level 3. Print the elements beginning from the highest level in a left to right order. 4. Delete any arbitrary element but modify the tree structure to maintain the completeness of the tree. Print the tree level-wise after the deletion. Write a code to create a complete binary tree using Linked List for any given sequence of integer elements. The sequence of elements has to be taken as input from the user. After the tree has been formed do the following: 1.  Find the minimum and maximum element in the tree 2.  Find sum of elements at each level 3.  Print the elements beginning from the highest level in a left to right order. 4.  Delete any arbitrary element but modify the tree structure to maintain the completeness of the tree. Print the tree level-wise after the deletion. Reviewed by Unknown on 03:48 Rating: 5

No comments:

Powered by Blogger.