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

Complete the implementation of the instance method isValid() for the class Binar

ID: 3823358 • Letter: C

Question

Complete the implementation of the instance method isValid() for the class BinarySearchTree below. This recursive method returns true if and only all the nodes of the tree are locally valid, and false otherwise.

public class Binary SearchTree E extends Comparable left; private Node F> right private Node F value this value value left null right null private Node E root null public boolean isValid return isValid private boolean. isValid Node E current boolean is Valid true if current null if current left null is Valid if is Valid return isValid && &&

Explanation / Answer

public boolean isValid(){
   return isValid(root);
}

public boolean isValid(Node<E> current){
   boolean isValid = true;
   if(current != null ){
       if(current.left!=null){
           isValid = isValid(current.left) && isValid(current.right);
       }
       if(current.left!=null){
           isValid = isValid(current.left) && isValid(current.right);
       }
   }
   return isValid;
  
}