Modify Figs. 21.15 and 21.16 so the Tree class provides a method getDepth that determines how many
Question:
Modify Figs. 21.15 and 21.16 so the Tree class provides a method getDepth that determines how many levels are in the tree. Test the method in an application that inserts 20 random integers into a Tree.
Fig. 21.15
Fig. 21.16
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== nul1) { 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% (6 reviews)
To modify the Tree class to provide a method getDepth and test it in the TreeTest applicatio...View the full answer
Answered By
Rustia Melrod
I am a retired teacher with 6 years of experience teaching various science subjects to high school students and undergraduate students. This background enables me to be able to help tutor students who are struggling with the science of business component of their education. Teaching difficult subjects has definitely taught me patience. There is no greater joy for me than to patiently guide a student to the correct answer. When a student has that "aha!" moment, all my efforts are worth it.
The Common Core standards are a useful yardstick for measuring how well students are doing. My students consistently met or exceeded the Common Core standards for science. I believe in working with each student's individual learning styles to help them understand the material. If students were struggling with a concept, I would figure out a different way to teach or apply that concept. I was voted Teacher of the Year six times in my career. I also won an award for Innovative Teaching Style at the 2011 National Teaching Conference.
4.90+
4+ 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
-
A list of 30 exam scores is: 31, 70, 92, 5, 47, 88, 81, 73, 51, 76, 80, 90, 55, 23, 43,98,36,87,22,61, 19,69,26,82,89,99, 71,59,49,64 Write a computer program that determines how many grades are...
-
How many levels are required to produce the micromotor shown in Fig. 13.22d?
-
Modify the SortedList class from Exercise 21.7 to include a merge method that can merge the SortedList it receives as an argument with the SortedList that calls the method. Write an application to...
-
In Problems 1968, solve each equation, if possible. -4 x + 4 || -3 x+6
-
The wave function for a hydrogen atom in the 2s state is(a) Verify that this function is normalized.(b) In the Bohr model, the distance between the electron and the nucleus in the n = 2 state is...
-
To the left of z = 1.12 Find the area under the standard normal distribution curve.
-
The chief accountant of Uncertain Ltd is not sure of the appropriate accounting treatment for a number of events occurring during the year 2005/6. (i) A significant number of employees have been made...
-
Oasis Faucet Company manufactures faucets in a small manufacturing facility. The faucets are made from zinc. Manufacturing has 80 employees. Each employee presently provides 40 hours of labor per...
-
Exercise 8-6. analyzing a special order [Lo 1] PowerDrive produces a hard disk drive that sells for $175 per unit. The cost of producing 25,000 drives in the prior year was: Direct material 725,000...
-
Modify the List class of Fig. 21.3 to include method printListBackward that recursively outputs the items in a linked-list object in reverse order. Write a test program that creates a list of...
-
Write a program based on the program of Figs. 21.15 and 21.16 that inputs a line of text, tokenizes it into separate words, inserts the words in a binary search tree and prints the inorder, preorder...
-
What is the uncertainty in using pressure measurement as an altimeter? A gage on an airplane measures a local pressure of 54 kPa with an uncertainty of 3 kPa. The lapse rate is 0.006 K/m with an...
-
How would you answer these for a service like amazon fresh? Product Range What is your total, core, and auxiliary product? What are the product's ingredients? What are the product's features? How is...
-
Josh Motlop ceased to be an Australian resident for tax purposes on 30 April 2022.Josh derived taxable income of $36,000 during the portion of the 2021/22 tax yearthat he was a resident.( a...
-
8) A child sits on a plank that is supported by his parents, as depicted in the given diagram below. There is no acceleration. Compare the forces that are provided by the mother (F1) and the father...
-
(a) What is the maximum number that we can count up to using 11 bits? [2 mark] (b) Given that Y = 13810 and X= 1728, using 8-bit 2'S complement signed binary arithmetic, perform X-Y. (c) Consider the...
-
a) b) Vectors a = (x 3-2) and b = (3-1 2) are 93.7 degrees apart. Solve for the highest value of x. (15 marks) Use Bresenham's algorithm to draw the line y = 0.5x+1 from (1,1) to (7,4). List the...
-
1. What does an ethical analysis add to Viscusi`s actuarial analysis? 2. Would an ethical analysis change the conclusion reached? Why? Antismoking advocates cheered in the summer of 1997 when the...
-
a. Why does the Wi-Fi Alliance release compatibility testing profiles in waves instead of combining the entire standards features initially? 27a1.) An 802.11ac Wi-Fi compatibility testing profile...
-
What is the maximum number of callers in each cell in an IS-95 system?
-
What is AMPS?
-
Find the efficiency of AMPS in terms of simultaneous calls per megahertz of bandwidth. In other words, find the number of calls that can be used in 1-MHz bandwidth allocation.
-
Purpose REII 211 2023: Coding_challenge 1 This assignment aims to create a nearest-neighbour heuristic, and optimal search for the robot tour optimisation problem (a.k.a Travelling Salesman Problem)...
-
A concrete slab is 3m by 8m and has thickness of 15cm. How much heat flows each second through the slab if the difference in temperature between the top and bottom is 10 degrees celsius? Take the...
-
The sample space refers to O a. the set of all possible experimental outcomes O b. any particular experimental outcome O c. an event O d. the sample size minus one
Study smarter with the SolutionInn App