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"); } }
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");
}
}
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)