Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish the parts labeled //Finish. Insertion of nodes into a binary search tree.

ID: 3764362 • Letter: F

Question

Finish the parts labeled //Finish. Insertion of nodes into a binary search tree.

public class BinarySearchTreeNode {
   int key;
   BinarySearchTreeNode left;
   BinarySearchTreeNode right;

   BinarySearchTreeNode(int k) {
       key = k;
       left = null;
       right = null;
   }

   void insert(int newKey) {
       if(newKey<=key) {
           //FINISH
       } else {
           //FINISH
       }
   }
  
   @Override
   public String toString() {
       String leftString = "";
       if(left!=null) leftString = left.toString();
       String rightString = "";
       if(right!=null) rightString = right.toString();
       return leftString+" "+key+" "+rightString;
   }
  
   public static void main(String[] args) {
       BinarySearchTreeNode root = new BinarySearchTreeNode(30);
       System.out.println(root);
       root.insert(40);
       System.out.println(root);
       root.insert(24);
       System.out.println(root);
       root.insert(58);
       System.out.println(root);
       root.insert(48);
       System.out.println(root);
       root.insert(26);
       System.out.println(root);
       root.insert(11);
       System.out.println(root);
       root.insert(13);
       System.out.println(root);
   }
}

Explanation / Answer

void insert(int newKey) {
if(newKey<=key) {
left->key = newKey;
} else {
right->key = newKey;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote