I want to add a few more practical methods to

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

}

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Must be 750-words in response to one of the questions

Must be 750-words in response to one of the questions posed at the end of your chosen case. Summarize the case and the values at stake in it. Summarize relevant facts of the case. Clarify concepts used to analyze the case. Justify the ethical standard applied to the case. State

You are a recent immigrant who has moved to the

 You are a recent immigrant who has moved to the United States to attend college. Your family has never been to the United States, and you are excited to tell them all about it, in particular the fundamentals of the U.S. government you are learning about in one of your

Species Briefing Report Assignment

For this report you will select a California native plant or an introduced alien plant that is causing problems for California ecosystems, conduct research on the plant, and write a short briefing report. The report should be around 800 words in length, excluding citations. You may include a few selected

Aging And Nervous System Article Summary Nursing Assignment Help

I’m working on a health & medical question and need guidance to help me learn. Choose, read and briefly summarize one scientific journal article relevant to aging and the nervous system. Summaries should be 1-2 paragraphs and must include the source. Articles must be published after 2015.   Expert Solution

As future practicing nurses, evidence-based practice helps you stay updated

  As future practicing nurses, evidence-based practice helps you stay updated about new medical protocols for patient care. EBP, or evidence-based practice, is an indispensable part of health care and plays a critical role in reducing practical variability, enhancing health care quality, perfecting the health care system, improving patients’ outcomes, and

Thank you for offering this weeks alternate posting opportunity. I

  Thank you for offering this weeks alternate posting opportunity. I personally am really struggling right now. I feel like I have bitten off more that I can chew and that I am juggling competing priorities minute to minute. I work full time as an educator for ICU and I

To Prepare: Review the Resources and think about the national

  To Prepare: Review the Resources and think about the national healthcare issue/stressor you previously selected for study in Module 1. Reflect on the competing needs in healthcare delivery as they pertain to the national healthcare issue/stressor you previously examined. Post an explanation of how competing needs, such as the needs

Overview In today’s information age, customers have access to various

  Overview In today’s information age, customers have access to various digital platforms to access and share information. Customers also use these platforms to communicate with service providers, share opinions, and provide feedback. Therefore, it is important for product owners and service providers to be ready with a response plan

Cure / care: compare and contrast. Basic care: Nutrition, hydration,

  Cure / care: compare and contrast. Basic care: Nutrition, hydration, shelter, human interaction. Are we morally obliged to do this? Why? Example Swallow test, describe; when is it indicated? When is medically assisted N/H indicated? Briefly describe Enteral Nutrition (EN), including: NJ tube NG tube PEG Briefly describe Parenteral

need a employee handbook created. Topic think as if you

need a employee handbook created. Topic think as if you was starting a business create a employee handbook for your workers with the following involved : Company’s mission EEO policy Hours of work Paid holidays Annual leave, vacation leave, sick leave, bereavement leave, military leave, etc Compensation Diversity Training Benefits

APA STYLE ONLY Your responses should be reflective and analytical

APA STYLE ONLY  Your responses should be reflective and analytical in nature and should have a minimum of 350 words and a maximum of 500 words double-spaced (for all questions).  1.  Assess the relevance and significance of Universal Human Rights today (provide a specific example such as the Refugees crisis

Draft a 6-page report on outcome measures, issues, and opportunities

Draft a 6-page report on outcome measures, issues, and opportunities for the executive leadership team or applicable stakeholder group. Introduction Note: Each assessment in this course builds on the work you completed in the previous assessment. Therefore, you must complete the assessments in this course in the order in which

1-Review the resource “Why is an Internship Important?” Select two

1-Review the resource “Why is an Internship Important?” Select two items from the list of benefits presented and discuss why a volunteer or internship experience is beneficial for obtaining a position in your desired career field. 2-Review the resources related to the benefits of keeping a journal as it relates

Plagiarism is very serious – safe assign check is used.

Plagiarism is very serious – safe assign check is used.  Final Project Prompt: The final portfolio project is a three- part activity. You will respond to three separate prompts but prepare your paper as one research paper. Be sure to include at least one UC library source per prompt, in addition