Homework Help Global logo
  • Home
  • Coursework Help
  • Dissertation Help
  • Homework Help
  • My account
  • Order now
Order Now
Homework Help

Binary search tree in java

2 min read
Posted on 
July 13th, 2022
Home Homework Help Binary search tree in java
I want to add a few more practical methods to a binary search tree (and practice a bit of recursion in the process). down 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.

i want to add following methods

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. The string should – similar to the return of toString – have the following form have: 12, 8, 2, 0, -1, .

The code

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

}

Order an Essay Now & Get These Features For Free:

Turnitin Report

Formatting

Title Page

Citation

Outline

Place an Order
Share
Tweet
Share
Tweet
Calculate the price
Pages (275 words)
$0.00
Homework Help Global
Company
  • Coursework Help
  • Law Homework Help
  • Business Paper Help
  • Thesis Writing Help
Legal
  • Dissertation Help
  • Homework Help
  • Essay Writing
  • Custom Writing
How Our Service is Used:
Homework Help Global essays are NOT intended to be forwarded as finalized work as it is only strictly meant to be used for research and study purposes. Homework Help Global does not endorse or condone any type of plagiarism.
Subscribe
No Spam
© 2023 Homework Help Global. All rights reserved.
Homework Help Global will be listed as ‘Homework Help Global’ on your bank statement.