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

public class BinaryHeap { private int data; private BinaryHeap left; private Bin

ID: 3690691 • Letter: P

Question

public class BinaryHeap {

   private int data;
   private BinaryHeap left;
   private BinaryHeap right;
      
   public BinaryHeap(int x) {
       data = x;
       left = null;
       right = null;
   }
  
   public int getData() {
       return data;
   }
   public void setData(int data) {
       this.data = data;
   }
   public BinaryHeap getLeft() {
       return left;
   }
   public void setLeft(BinaryHeap left) {
       this.left = left;
   }
   public BinaryHeap getRight() {
       return right;
   }
   public void setRight(BinaryHeap right) {
       this.right = right;
   }
}


public class BinarySearchTree {

   private BinaryHeap root = null;
  
   /*
   * insert the new element 'x' into the BinarySearchTree
   * create a NEW BinaryHeap with data = 'x', left = NULL and right = NULL
   *
   * if root is NULL then
   *            insert the new BinaryHeap into root
   * else
   *            find the location where the NEW BinaryHeap has to be inserted
   *
   * @Param
   *        -   'x' - New integer
   */
   public void insert(int x) {

   }
  
   /*
   * Find the maximum element in the BinarySearchTree
   *
   * @return
   *        -   Maximum element (integer) in the whole BinarySearchTree
   */
   public int findMaximum() {
      
   }
  
   /*
   * Find the minimum element in the BinarySearchTree
   *
   * @return
   *        -   Minimum element (integer) in the whole BinarySearchTree
   */
   public int findMinimum() {
      
   }

}

public class Main {

   public static void main(String[] args) {

       BinarySearchTree tree = new BinarySearchTree();
       tree.insert(5);
       tree.insert(3);
       tree.insert(7);
       tree.insert(6);

       System.out.println("Max : "+tree.findMaximum());
       System.out.println("Mini : "+tree.findMinimum());

   }

}

In the Binary Search Tree the elements on the left side of the root are less than root element (left

Explanation / Answer

public class BinaryHeap {
private int data;
private BinaryHeap left;
private BinaryHeap right;
  
public BinaryHeap(int x) {
data = x;
left = null;
right = null;
}
  
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public BinaryHeap getLeft() {
return left;
}
public void setLeft(BinaryHeap left) {
this.left = left;
}
public BinaryHeap getRight() {
return right;
}
public void setRight(BinaryHeap right) {
this.right = right;
}
}

public class BinarySearchTree {
private BinaryHeap root = null;
  
/*
* insert the new element 'x' into the BinarySearchTree
* create a NEW BinaryHeap with data = 'x', left = NULL and right = NULL
*
* if root is NULL then
* insert the new BinaryHeap into root
* else
* find the location where the NEW BinaryHeap has to be inserted
*
* @Param
* - 'x' - New integer
*/
public void insert(int x) {

   BinaryHeap newNode = new BinaryHeap(x);
       if(root==null){
           root = newNode;
           return;
       }
       BinaryHeap current = root;
       BinaryHeap parent = null;
       while(true){
           parent = current;
           if(id<current.getData()){              
               current = current.left;
               if(current==null){
                   parent.left = newNode;
                   return;
               }
           }else{
               current = current.right;
               if(current==null){
                   parent.right = newNode;
                   return;
               }
           }
       }
}
  
/*
* Find the maximum element in the BinarySearchTree
*
* @return
* - Maximum element (integer) in the whole BinarySearchTree
*/
public int findMaximum() {
   BinaryHeap node = root;
if(node ==null)
       return 0;
   while(node.right!=null){
       node = node.right;  
   }
   return node.getdata();
}
  
/*
* Find the minimum element in the BinarySearchTree
*
* @return
* - Minimum element (integer) in the whole BinarySearchTree
*/
public int findMinimum() {
BinaryHeap node = root;
if(node ==null)
       return 0;
   while(node.left!=null){
       node = node.left;  
   }
   return node.getdata();
}
}
public class Main {
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(5);
tree.insert(3);
tree.insert(7);
tree.insert(6);
System.out.println("Max : "+tree.findMaximum());
System.out.println("Mini : "+tree.findMinimum());
}
}