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: 66% (3 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...
-
The following contingency table describes types of collisions versus driving environments for a random sample of two-vehicle accidents that occurred in a given region last year. At the 0.01 level of...
-
Describe the difference between accounting for governmental funds and proprietary funds. AppendixLO1
-
Wendy's Wind Toys manufactures decorative kites, banners, and windsocks. During the month of January, Wendy received orders for 3,000 Valentine's Day banners and 1,200 Easter kites. Because several...
-
X Company, a merchandiser, had the following transactions in August: 1. Borrowed $28,000 from a bank. 2. Bought equipment costing $9,400, paying the manufacturer $5,400 in cash and promising to pay...
-
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;...
-
Suppose you fit the model y = 0 + 1x1 + 2x21 + 3x2 + 4x1x2 to n = 25 data points and find that 0 = 1.26 1 = -2.43 2 = .05 3 = .62 4 = 1.81 = s1 = 1.21 s2 = .16 s3 = .26 s4 = 1.49 SSE = .41 R2 = .83...
-
Listed below are pulse rates (beats per minute) from samples of adult males and females. Does there appear to be a difference? Find the coefficient of variation for each of the two samples; then...
-
When the price of an item goes down, people buy more of it because at a lower price Options: -some new people may be willing to enter the market. -the item is more valuable. -the marginal cost is...
-
2. Find the sum of the following sequences (3 points) a) 1, 2, 3, 4, 5, 150 ... b) 1, 3, 5, 7, 9, ..., 99 c) -4, 1, 6, 11, 16, 21, . 201 ...
-
Solving for Rates What annual rate of return is implied on a $2,600 loan taken next year when $6,300 must be repaid in year 12?
-
Significant Values. In Exercises 9-12, use the range rule of thumb to identify (a) the values that are significantly low, (b) the values that are significantly high, and (c) the values that are...
-
According to the above excerpt, what factors influence the price elasticity of demand for cigarettes? What other factors not mentioned in the article might also influence the price elasticity of...
-
All of the following assets can be depreciated, except: (a) A bulldozer (b) A copper mine (c) A surgical robot (d) A conveyor belt
-
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.
-
Mediocre Company has sales of $120,000, fixed expenses of $24,000, and a net income of $12,000. If sales rose 10%, the new net income would be: Question 18 options: $16,800 $36,000 $13,200 $15,600
-
1. Why might managers of small restaurants decide not to adopt the standard work hour approach to controlling labour cost? (minimum 150 words )
-
Which statement is true regarding the U.S. GAAP impairment test for limited life intangibles? A. U.S. GAAP impairment is likely to be greater than IFRS impairment. B. The impairment test for limited...
Study smarter with the SolutionInn App