I need help trying to get this particular output to work. currently I got a sorted array before insertion of an element but i need it to display the element thats being inserted, sorted array with the instereted element, key comparision before insertion, and post insertion just need this to match the output. Expected output: Sorted array pre insertion: 2 5 6 8 element inserted: 3 Sorted array post insertion: 2 3 5 6 8 Key comparision differences in pre insertion: 4 key comparision difference in post insertion: 7 Need this code to match the expected output. class BST {     class Node     {         int key;         Node left, right;           public Node(int item)         {             key = item;             left = right = null;         }     }       // Root of BST     Node root;       // Constructor     BST()     {         root = null;     }       void insert(int key)     {         root = insertRec(root, key);     }           Node insertRec(Node root, int key)     {           if (root == null)         {             root = new Node(key);             return root;         }         if (key < root.key)             root.left = insertRec(root.left, key);         else if (key > root.key)             root.right = insertRec(root.right, key);           return root;     }           void inorderRec(Node root)     {                  if (root != null)         {             inorderRec(root.left);             System.out.print(" "+ root.key +" ");             inorderRec(root.right);         }     }     void treeins(int arr[])     {         for(int i = 0; i < arr.length; i++)         {             insert(arr[i]);         }               } }   public class PAssign07{     public static void main(String[] args)     {         BST tree = new BST();         int arr[] = {6, 2, 8, 5};                   tree.treeins(arr);                  System.out.printf("sorted array pre insertion :%n");         tree.inorderRec(tree.root);         System.out.printf("%nelement inserted");         System.out.printf("%nsorted array post insertion");         System.out.printf("%nkey comparision pre insertion");         System.out.printf("%nkey comparision post insertion");              }     }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

I need help trying to get this particular output to work. currently I got a sorted array before insertion of an element but i need it to display the element thats being inserted, sorted array with the instereted element, key comparision before insertion, and post insertion just need this to match the output.

Expected output:

Sorted array pre insertion: 2 5 6 8

element inserted: 3

Sorted array post insertion: 2 3 5 6 8

Key comparision differences in pre insertion: 4

key comparision difference in post insertion: 7

Need this code to match the expected output.

class BST
{
    class Node
    {
        int key;
        Node left, right;
 
        public Node(int item)
        {
            key = item;
            left = right = null;
        }
    }
 
    // Root of BST
    Node root;
 
    // Constructor
    BST()
    {
        root = null;
    }
 
    void insert(int key)
    {
        root = insertRec(root, key);
    }
     
    Node insertRec(Node root, int key)
    {
 
        if (root == null)
        {
            root = new Node(key);
            return root;
        }

        if (key < root.key)
            root.left = insertRec(root.left, key);
        else if (key > root.key)
            root.right = insertRec(root.right, key);
 
        return root;
    }
     
    void inorderRec(Node root)
    {
        
        if (root != null)
        {
            inorderRec(root.left);
            System.out.print(" "+ root.key +" ");
            inorderRec(root.right);
        }
    }
    void treeins(int arr[])
    {
        for(int i = 0; i < arr.length; i++)
        {
            insert(arr[i]);
        }
         
    }
}
 
public class PAssign07{
    public static void main(String[] args)
    {
        BST tree = new BST();
        int arr[] = {6, 2, 8, 5};
         
        tree.treeins(arr);
        
        System.out.printf("sorted array pre insertion :%n");
        tree.inorderRec(tree.root);
        System.out.printf("%nelement inserted");
        System.out.printf("%nsorted array post insertion");
        System.out.printf("%nkey comparision pre insertion");
        System.out.printf("%nkey comparision post insertion");
        
    }
    }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY