Modify class Tree of Fig. 21.15 to include method contains, which attempts to locate a specified value
Question:
Modify class Tree of Fig. 21.15 to include method contains, which attempts to locate a specified value in a binary-search-tree object. The method should take as an argument a search key to locate. If the node containing the search key is found, the method should return a reference to that node’s data; otherwise, it should return null.
Fig. 21.15
Transcribed Image Text:
I // Fig. 21.15: Tree.java 2 // TreeNode and Tree class declarations for a binary search tree. 3 package com.deitel.datastructures; 4 5 // class TreeNode definition 6 class TreeNode { 7 8 9 10 IL 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 } 42 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 // package access members TreeNode leftNode; 99 100 101 } E data; // node value TreeNode rightNode; // constructor initializes data and makes this a leaf node public TreeNode (E nodeData) { data nodeData; leftNode= rightNode = null; // node has no children } // locate insertion point and insert new node; ignore duplicate values public void insert (E insertValue) { // insert in left subtree } if (insertValue.compareTo (data) < 0) { // insert new TreeNode if (leftNode == null) { leftNode=new TreeNode (insertValue); } } } else { // continue traversing left subtree recursively leftNode.insert(insertValue); } // insert in right subtree else if (insertValue.compareTo (data) > 0) { // class Tree definition public class Tree { private TreeNode root; } // constructor initializes an empty Tree of integers public Tree() {root = null;} //insert new TreeNode. if (rightNode == null) { rightNode = new TreeNode (insertValue); } else { // continue traversing right subtree recursively rightNode.insert(insertValue); } // insert a new node in the binary search tree public void insertNode(E insertValue) { if (root == null) { root = new TreeNode (insertValue); // create root node } } } else { root.insert(insertValue); // call the insert method // begin preorder traversal public void preorderTraversal() {preorderHelper (root); } // recursive method to perform preorder traversal private void preorderHelper (TreeNode node) { if (node == null) { return; } System.out.printf("%s", node.data); // output node data preorderHelper (node.leftNode); // traverse left subtree preorderHelper(node.rightNode); // traverse right subtree } // begin inorder traversal public void inorderTraversal() {inorderHelper (root); } // recursive method to perform inorder traversal private void inorderHelper (TreeNode node) { if (node == null) { return; } inorderHelper (node.leftNode); // traverse left subtree System.out.printf("%s ", node.data); // output node data inorderHelper (node.rightNode); // traverse right subtree } // begin postorder traversal public void postorderTraversal() {postorderHelper (root); } // recursive method to perform postorder traversal private void postorderHelper (TreeNode node) { if (node == null) { return; } postorderHelper (node.leftNode); // traverse left subtree postorderHelper (node.rightNode); // traverse right subtree System.out.printf("%s", node.data); // output node data
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 50% (2 reviews)
To add a contains method to the provided binary search tree implementation I will describe the steps required to modify the Tree class since that is w...View the full answer
Answered By
Ashington Waweru
I am a lecturer, research writer and also a qualified financial analyst and accountant. I am qualified and articulate in many disciplines including English, Accounting, Finance, Quantitative spreadsheet analysis, Economics, and Statistics. I am an expert with sixteen years of experience in online industry-related work. I have a master's in business administration and a bachelor’s degree in education, accounting, and economics options.
I am a writer and proofreading expert with sixteen years of experience in online writing, proofreading, and text editing. I have vast knowledge and experience in writing techniques and styles such as APA, ASA, MLA, Chicago, Turabian, IEEE, and many others.
I am also an online blogger and research writer with sixteen years of writing and proofreading articles and reports. I have written many scripts and articles for blogs, and I also specialize in search engine
I have sixteen years of experience in Excel data entry, Excel data analysis, R-studio quantitative analysis, SPSS quantitative analysis, research writing, and proofreading articles and reports. I will deliver the highest quality online and offline Excel, R, SPSS, and other spreadsheet solutions within your operational deadlines. I have also compiled many original Excel quantitative and text spreadsheets which solve client’s problems in my research writing career.
I have extensive enterprise resource planning accounting, financial modeling, financial reporting, and company analysis: customer relationship management, enterprise resource planning, financial accounting projects, and corporate finance.
I am articulate in psychology, engineering, nursing, counseling, project management, accounting, finance, quantitative spreadsheet analysis, statistical and economic analysis, among many other industry fields and academic disciplines. I work to solve problems and provide accurate and credible solutions and research reports in all industries in the global economy.
I have taught and conducted masters and Ph.D. thesis research for specialists in Quantitative finance, Financial Accounting, Actuarial science, Macroeconomics, Microeconomics, Risk Management, Managerial Economics, Engineering Economics, Financial economics, Taxation and many other disciplines including water engineering, psychology, e-commerce, mechanical engineering, leadership and many others.
I have developed many courses on online websites like Teachable and Thinkific. I also developed an accounting reporting automation software project for Utafiti sacco located at ILRI Uthiru Kenya when I was working there in year 2001.
I am a mature, self-motivated worker who delivers high-quality, on-time reports which solve client’s problems accurately.
I have written many academic and professional industry research papers and tutored many clients from college to university undergraduate, master's and Ph.D. students, and corporate professionals. I anticipate your hiring me.
I know I will deliver the highest quality work you will find anywhere to award me your project work. Please note that I am looking for a long-term work relationship with you. I look forward to you delivering the best service to you.
3.00+
2+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
In this exercise, we discuss deleting items from binary search trees. The deletion algorithm is not as straightforward as the insertion algorithm. Three cases are encountered when deleting an itemthe...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Expand the class from Project 10 or 11 so that there is an extra method that produces a Java Iterator for the bag. Data from Project 10 Write a class for a bag of strings, where the strings are...
-
In Problems 1130, solve each equation by factoring. x 2 + 4x = 0
-
The normalized radial wave function, (41.2), for the 2p state of the hydrogen atom is R2p, = (1/24a5) re r/2a. After we average over the angular variables, the radial probability function becomes...
-
If cases in a cell influence each others scores on the dependent variable, then the ____ assumption is violated.
-
\(\sqrt{75}\) Simplify the square root by expressing it in lowest terms.
-
The Deluxe Store is located in midtown Madison. During the past several years, net income has been declining because of suburban shopping centers. At the end of the companys fiscal year on November...
-
Derrick owns a farm in eastern North Carolina. A hurricane hit the area (a national disaster area was declared) and destroyed a farm building and some farm equipment and damaged a barn. Item Adj...
-
The program of Figs. 21.15 and 21.16 illustrated three recursive methods of traversing a binary treeinorder, preorder and postorder traversals. This exercise presents the level-order traversal of a...
-
Modify the List class of Fig. 21.3 to include method search that recursively searches a linked-list object for a specified value. The method should return a reference to the value if its found;...
-
What do you have to do to get a government contract? Check out the Web sites www.fedbizopps.gov and www.governmentbids.com to find out. Does it seem worth the effort?
-
How did you fare in terms of the objectives you had during your most recent performance review?
-
Straight wooden stick has mass M=0.70kg, Length L=1.6m, uniform cross-section A=14cm 2 , and constant density has a small mass m=0.20kg attached to its one end. The stick is partially submerged in...
-
Management at a newly created amusement park evaluated its customer service performance against that of Disney World s Magic Kingdom. It found gaps and used those to create a new set of best...
-
How can different laws and regulations around data privacy and security be considered when developing a communication system
-
The Guardians of the Universe launch a satel- lite that goes into orbit at a distance of 16287 km from the center of the earth. If the orbit is circular what is its speed? The universal gravitational...
-
Explain why corporations are legally responsible to shareholders but are strategically responsible to other stakeholders as well.
-
The ultimate goal of Google, Bing, and other consumer search engines is to provide users with search listings that contain useful information on the topic of their search. What recommendations would...
-
In Figure 16.5, do we mean that downstream and upstream data are transmitting at the same time? Explain. Figure 16.5 Figure 16.5 WIMAX frame structure at the physical layer UR: Upstream reservation...
-
When we make a wireless Internet connection from our desktop at home, do we use fixed or mobile WiMAX?
-
Draw a cell pattern with a frequency-reuse factor of 5.
-
Which term describes the maximum displacement from the undisturbed level of a wave? 00 wave speed frequency wavelength amplitude period
-
Do Homework - Chapter 1.1 Ho X P Do Homework - Chapter 1 Less com/Student/PlayerHomework.aspx?homeworkId=668280558&questionId=1&flushed=false&cid=7741105&back=https://m... P Learning Numbers i... ath...
-
- - Part-B: A 13. 18 Ax) = x + o + e, find (1) b. a. 2 c. 1+e d. 1 Hosting & To 14. Find the point on the curve = e* + ex where the tangent is parallel to the line y = 2ex. a. (1, 2e) b. (0, 1) C....
Study smarter with the SolutionInn App