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
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);
}
}
}
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.