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...
-
You saw an interesting article describing a Suitable Suits program at Barnard College. Its College of Career Development kept a closet filled with 21 crisp black suits that students could borrow for...
-
Identify the singularity and find the nature of solution near the singularity for van der Pol's equation: \[\ddot{x}-\alpha\left(1-x^{2} ight) \dot{x}+x=0\]
-
Petal Providers Corporation opens and operates mega floral stores in the United States. The idea behind the superstore concept is to model the U.S. floral industry after its European counterparts,...
-
Pablo and Jacob are running a half marathon. Jacob is d = 42.3m behind Pablo, and both are running at the same speed, v 0 = 2.96m / s At a particular moment, Jacob begins to accelerate forward at a...
-
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;...
-
The phase diagram shown is for mixtures of HCl and H 2 O at a pressure of 1 atm. The red curve represents the normal boiling points of solutions of HCl(aq) of various mole fractions. The blue curve...
-
How did they get the credit sales and opportunity cost? Credit Sales Cost of Goods Sold (81.98 % of sales ) Gross Margin Additional Marketing Cost the new credit policy (assuming 1% of credit sales)...
-
1. Calculate Tour de Force (TdF) plc's annual growth rate for sales, assets, and operating profit in 2019 and 2020. Explain fully what are the underlying drivers of these growth rates?
-
Use the financial reports below to answer the two questions in the required. CostCutter Inc. Balance Sheet October 31, 2020 Assets Current Assets Cash Accounts receivable Allowance for doubtful...
-
1. Consider the Pohlig-Hellman exponentiation cipher. For each of the problems below, the data to decipher the given cryptogram and produce the plaintext. (a) Let p = 263, e = 73, and C c = (246, 18,...
-
Solve the following question from the data given below. 4 1.9 7.8 16 0.71 2 [1.22 X X 43 #1719 Similar question X 04 08 y 800 980 8.7 solve using Gauss- Jordan solve using LU Decomposition 2 2.3...
-
1. How do weight and mass differ? 2. What is the basic metric unit of weight? 3. A microsecond is one-_____ of a second. 4. Why must we concern ourselves with significant digits? 5. Can the sum or...
-
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.
-
Dundee Company issued $ 1 , 0 0 0 , 0 0 0 face ( i . e . , par ) value 1 0 - year bonds at 1 0 2 on January 1 , 2 0 X 5 , which Mega Corporation purchased. The coupon rate on the bonds is 9 percent....
-
Price of Property: $ 3 4 0 , 0 0 0 Down payment ( 2 0 % of the Price of the Property ) : $ 3 4 0 , 0 0 0 * . 2 0 = $ 6 8 , 0 0 0 The monthly amount you can save towards your down payment is $ 6 0 4 ....
-
Direct Materials and Direct Labor Variance Analysis Abbeville Company manufactures faucets in a small manufacturing facility. The faucets are made from brass. Manufacturing has 9 0 employees. Each...
Study smarter with the SolutionInn App