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.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;="">
{
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");
}
}