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

In this problem, you will write some Java code for simple operations on binary s

ID: 3900812 • Letter: I

Question

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, are 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...

       4
      /
     2   5
    /
   1   3

Produces the output "1 2 3 4 5".

Note: You will need an assistant/helper method.

(d) Add method public void printPostorder() to the BinarySearchTree class that prints out the nodes of the tree according to a "postorder" traversal. So the tree...

       4
      /
     2   5
    /
   1   3

Produces the output "1 3 2 5 4".

Note: You will need an assistant/helper method.

Explanation / Answer

Below are your methods. Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments

(a)

public int keySum() {

return keySumHelper(root);

}

int keySumHelper(BinarySearchTreeNode node) {

if (node != null) {

int sum = node.key);

sum += keySumHelper(node.left);

sum += keySumHelper(node.right);

return sum;

}

return 0;

}

(b)

public void deleteMin() {

if(root == null)

return;

else if(root.left == null) // root is min

root = root.right;

else

deleteMinNode(root);

}

// recursive version

void deleteMinNode(BinarySearchTreeNode prev) {

if(prev.left.left == null)

prev.left = prev.left.right; // partial credit: prev.left = null;

else

deleteMinNode(prev.left);

}

// iterative version [helper method not needed]

void deleteMinNode(BinarySearchTreeNode prev) {

while(prev.left.left != null)

prev = prev.left;

prev.left = prev.left.right; // partial credit: prev.left = null;

}

// A third version that uses the provided delete method

public void deleteMin() {

if(root == null)

return;

BinarySearchTreeNode n = root;

while(n.left != null)

n = n.left;

delete(n.key);

}

c)

public void printTree() {

printTree(root);

}

public void printTree(BinarySearchTreeNode node) {

if (node != null) {

printTree(node.left);

System.out.println(node.key + " ");

printTree(node.right);

}

}

d)

// Prints a post-order traversal of this tree.

public void printPostorder() {

printPostorder(overallRoot);

}

// Prints a post-order traversal of the tree starting at the specified root

private void printPostorder(BinarySearchTreeNode node) {

if(node != null) {

printPostorder(node.left);

printPostorder(node.right);

System.out.print(node.key + " ");

}

}

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