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

Need to write an outline and a 1,250 word. It

Need to write an outline and a 1,250 word. It is based on a character from the book “Jesus Son” Here are the instructions. MLA format. My character is: Fuckhead Here is the link to the book: https://read.amazon.com/kp/kshare?asin=B006JR7XLC&id=uy7qjbidizdnpkma44lfqvpwva&ref_=r_sa_glf_b_0_hdrw_ss_CAAUAAA&reshareId=H2EQEA3QMN936RXAFVRJ&reshareChannel=system Here is an example of an outline the the professor gave us

ASSIGNMENT 1 INSTRUCTIONS IS ON THE LAB 12 ATTACHMENT MAKE

ASSIGNMENT 1  INSTRUCTIONS IS ON THE LAB 12 ATTACHMENT MAKE SURE TO FILL EVERYTHING OUT ON DOCUMENT  ———————————————————– ASSIGNMENT 2  Go to the website below. Pick one article and provide a brief summary and your comment on it. Make sure to cite the Authorand the name of the publication or

Overview As a starting point for creating a comprehensive write

Overview As a starting point for creating a comprehensive write up on an ideal society, you will develop a detailed outline. An outline allows you to evaluate your content, organize the information, create a flow, and integrate sources. A skeleton outline (i.e., a few words for each heading and subheading) does

Your comprehensive health history that was assigned in Module 01

  Your comprehensive health history that was assigned in Module 01 is now due. Complete a comprehensive history, utilizing the form linked below, on either someone over the age of 65 or someone that you know has a lot of medical problems. Write the results in narrative format and include

Planning is the key to successful completion of this course Nursing Assignment Help

Planning is the key to successful completion of this course and program-related objectives. The Individual Success Plan (ISP) assignment requires early collaboration with the course faculty and your course preceptor. Students must establish a plan for successful completion of The required 50 community direct clinical practice experience hours, 50 leadership

1. What is the HEDIS system and what does it

1. What is the HEDIS system and what does it measure?(Hint: www.ncqa.org) Why are “why questions” risky early in the CQI process? What is variation?  How is it measured? Only use references from school library, log in info attached and how to find the peer reviewed articles as well  IMPORTANT:

Critical Thinking Questions Please complete the following questions. Use a

    Critical Thinking Questions Please complete the following questions. Use a word processor to complete your work. Remember to use complete sentences and include both the questions and answers when you submit your work. Submit your work as a file attachment using the dropbox. The answers to the Critical Thinking

Download the document Week 5 Assessment Tools [DOCX], and read

  Download the document Week 5 Assessment Tools [DOCX], and read about the three assessment tools: the DiSC assessment, the Cognitive Criteria Aptitude Test (CCAT), and the Emotional Intelligence Appraisal. Read each hiring scenario (these are also included in the document): Scenario 1: Your human resources department needs to hire

Students will need to post a link to an article

 Students will need to post a link to an article from Wall Street Journal regarding a current event (something in the last 60 days before each post) regarding a topic of your choice from related Chapters.  Choose one of the following topics: 1- Investment criteria – net present value, payback,

[Odsłoń] 13 sposobów na czyszczenie białych butów naczyniami KAŻDY DOM

  [Odsłoń] 13 sposobów na czyszczenie białych butów naczyniami KAŻDY DOM MA! Zadrapania na butach ze skóry i imitacji skóry będą miejscem, w którym przywiera błoto i brud, a ich czyszczenie jest bardzo trudne. Wólka Kosowska hurtownia obuwia Zmywacz do paznokci sprawdza się również do czyszczenia białych butów wyjątkowo dobrze, o

Response #1 Respond to one of your peers in your

 Response #1 Respond to one of your peers in your discussion group. Identify the additional workup that is needed to rule in or rule out these differential diagnoses. What clinical signs/symptoms would you expect to see with these two differential diagnoses ? Peer response   According to McCance and Huether

Critical Chain Project Management, WBS, Planning and Resource

You should identify a specific planning and resource management problem that is impacting on the ability to deliver a project within your organisation; this can either be a project that you are currently working on or have recently worked on. You should critically analyse the problem and make specific recommendations

5 surefire tips to ace your next programming assignment No

   5 surefire tips to ace your next programming assignment No one ever said programming was easy – but it can be a lot less frustrating if you follow some simple tips. Whether you’re struggling with loops or arrays, these five guidelines will help you ace your next programming assignment.

Organizations often try to develop an entrepreneurial culture through a

Organizations often try to develop an entrepreneurial culture through a strong orientation toward entrepreneurship. REQUIREMENTS A.  Prepare a brief summary, listing and defining at least four reasons why entrepreneurial organizations seek opportunities for innovation. B.  Acknowledge sources, using APA-formatted in-text citations and references, for content that is quoted, paraphrased, or

Virtual Simulation Assignment We are going to explore simple harmonic

Virtual Simulation Assignment We are going to explore simple harmonic motion through the PhET Pendulum Lab to find out the relationship between the mass of the bob, the length of the string, the period of the pendulum, and the total energy of the system with respect to the variation of

Forbes’ global economic forecast for 2017-2018

Forbes’ global economic forecast for 2017-2018: macro-level discussion on some expectations and possible connections. Think of possible implications for the CA startup economy and entrepreneurship in general? Have expectations been met? https://www.forbes.com/sites/billconerly/2016/09/16/u-s-economic-forecast-2017-2018-mild-rebound/#dfb57b85337f (Links to an external site.)Links to an external site. An overview of the discussion of the tech background

Provide a brief historical perspective of the counseling profession

Counseling professionals from different specializations frequently need to work together to provide effective services to students, families, and individual clients. This assignment asks you to address this need in two parts. In Part 1, focus on theory and concepts, creating your own description and evaluation of your specialization (substance abuse

Task 1.1: Case study Proposal (W1) Assessment type: Discussion forum

Task 1.1: Case study Proposal (W1)Assessment type: Discussion forumDescription: Develop the proposed title of the Case Study Task 1.2: Case study – Draft 1 (W3) Assessment type: Discussion forum Description: Develop the table of contents of the Case Study Proposal Task 1.3: Case Study Analysis (W5)Assessment type: Written assignmentDescription: Develop