Question: I need help with the following Binary Search Tree code. the methods are expected to be without parameters for example sum(), how do i fix

I need help with the following Binary Search Tree code.

the methods are expected to be without parameters for example sum(), how do i fix this ?

import java.util.*; public class BinarySearchTree {

private class BinaryNode { private int element; private BinaryNode left; private BinaryNode right;

private BinaryNode(int element) { this.element = element; } }

private BinaryNode root;

public void insert(int newNumber) { // special case: empty tree if (root == null) { root = new BinaryNode(newNumber); return; }

BinaryNode parent = null; BinaryNode child = root; while (child != null) { parent = child; if (newNumber == child.element) { //number already in tree return; } else if (newNumber < child.element) { child = child.left; } else { child = child.right; } }

if (newNumber < parent.element) { parent.left = new BinaryNode(newNumber); } else { parent.right = new BinaryNode(newNumber); } }

public int maximumRecursive(BinaryNode root) { if (root.right == null) return root.element; return maximumRecursive(root.right); }

public int maximumIterative() {

if (root == null) { throw new NoSuchElementException(); }

BinaryNode current = root; while (current.right != null) current = current.right;

return (current.element); }

public int height(BinaryNode root) { if (root == null) return 0;

return 1 + Math.max(height(root.left), height(root.right)); }

public int sum(BinaryNode root) { if (root == null) return 0;

return root.element + sum(root.left) + sum(root.right); }

public String reverseOrder(BinaryNode root) { if (root == null) { return ""; } return reverseOrder(root.right) + " " + ((Integer) root.element).toString() + " " + reverseOrder(root.left); }

own below you will find the class BinarySearchTree with the private inner class BinaryNode.Some methods are already specified. The Improved Methods may not be changed.

following methods were added

int maximumRecursive() Finds the maximum in the entire binary search tree (using a recursive helper method) and returns it. If the tree is empty, a java.util.NoSuchElementException is thrown out

int maximumIterative() : Finds the maximum in the binary search tree iteratively. If the tree is empty, a java.util.NoSuchElementException is thrown out

int height() : Calculates the height of the entire binary search tree. A tree without elements has height 0, a tree with exactly one element has height 1

int sum() : Calculates the sum of all numbers in the binary search tree. For an empty search tree the result should be 0

String reverseOrder() : Returns a string representation of the tree with all elements sorted in descending order. 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!