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

Problem#31 (35 pts) In this problem, you will write some Java code for simple op

ID: 3751726 • Letter: P

Question

Problem#31 (35 pts) In this problem, you will write some Java code for simple operations on binary search trees where keys are integers. Assume you already have the following code and assume that the method bodies, even though not shown,Tare correct and implement the operations as we defined them in class. public class BinarySearchTreeNode public int key; public BinarySearchTreeNode left; public BinarySearchTreeNode right; public class BinarySearchTree private BinarySearchTreeNode root; public void insert(int key) . public void delete(int key) public boolean find(int key) ) a) Add a method public int keySum() to the BinarySearchTree class that returns the sum of all the keys in the tree. You will need an assistant/helper method b) Add method public void deleteMin() to the BinarySearchTree class that deletes the minimum element in the tree (or does nothing if the tree has no elements). c) Add method public void printTree() to the BinarySearchTree class that iterates over the nodes to print then in increasing order. So the tree... 1

Explanation / Answer

public int keySum() {
   if(root==null) return 0;
   int sum = 0;
   LinkedList<BinarySearchTreeNode> q = new LinkedList<>();
   q.addLast(root);
   BinarySearchTreeNode b;
   while(!q.isEmpty()) {
       b = q.removeFirst();
       sum+=b.key;
       if(b.left!=null) q.addLast(b.left);
       if(b.right!=null) q.addLast(b.right);
   }
   return sum;
}

BinarySearchTreeNode deleteRec(BinarySearchTreeNode root, int key)
{
    if (root == null) return root;
    if (key < root.key)
        root.left = deleteRec(root.left, key);
    else if (key > root.key)
        root.right = deleteRec(root.right, key);
    else
    {
        if (root.left == null)
            return root.right;
        else if (root.right == null)
            return root.left;
       root.key = minValue(root.right);
        root.right = deleteRec(root.right, root.key);
    }

    return root;
}

void deleteKey(int key)
{
    root = deleteRec(root, key);
}

int minValue(BinarySearchTreeNode root)
{
    int min = root.key;
    while (root.left != null)
    {
        min = root.left.key;
        root = root.left;
    }
    return min;
}

public void printTree() {
   printInc(root);
   System.out.println();
}
public void printInc(BinarySearchTreeNode root) {
   if(root==null) return;
   printInc(root.right);
   System.out.print(root.key+" ");
   printInc(root.left);
}

public void printPostorder() {
   printPost(root);
   System.out.println();
}
public void printPost(BinarySearchTreeNode root) {
   if(root==null) return;
   printPost(root.left);
   printPost(root.right);
   System.out.print(root.key+" ");
}

(e) false

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