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

code: MAIN /*import com.binarytree.BinarySearchTree;*/ public class Main { publi

ID: 3690646 • Letter: C

Question

code:

MAIN

/*import com.binarytree.BinarySearchTree;*/

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());

}

}

BINARY HEAP

package com.binarytree;

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;
   }
}

BINARYSEARCHTREE

package com.binarytree;

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() {
      
   }

}

Explanation / Answer

Hello there ,

Please find below code for BinarySearchTree  and it's O/P. I have bit modified the signature for insert .

package com.binarytree;

public class BinarySearchTree {
   private BinaryHeap root = null;

   public void insert(int x) {
       root = insert(root, x);
   }

   /*
   * 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 BinaryHeap insert(BinaryHeap node, int x) {
       if (node == null) {
           return new BinaryHeap(x);
       }
       if (x <= node.getData()) { // <= ?
           node.setLeft(insert(node.getLeft(), x));
       } else if (x > node.getData()) {
           node.setRight(insert(node.getRight(), x));
       }
       return node;
   }

   /*
   * Find the maximum element in the BinarySearchTree
   *
   * @return - Maximum element (integer) in the whole BinarySearchTree
   */

   public int findMaximum() {
       return findMaximum(root);
   }

   public int findMaximum(BinaryHeap r) {
       while (r.getRight() != null) {
           r = r.getRight();
       }
       return r.getData();
   }

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

   public int findMinimum(BinaryHeap r) {
       while (r.getLeft() != null) {
           r = r.getLeft();
       }
       return r.getData();
   }

}

======== No Change=====

package com.binarytree;

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;
}
}

====No Change===

package com.binarytree;

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());
   }
   }

==O/P==

Max : 7
Mini : 3

Let me know if you have any queries.

Thanks.