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: 75% (4 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...
-
What are some considerations and concerns for a company when setting up a training program? Please use one outside source and feel free to cite a real or imaginary company to explain your answer.
-
Derive Eqs. (13.113b) and (13.116b) for the Mathieu equation. Equation 13.113b and 13.116b:- a 11 + E 62 - 8 + (13.113b)
-
The income statement of Tawls Company for the year ended December 31, 2010, shows the following: Required a. Compute the net earnings remaining after removing nonrecurring items. b. Determine the...
-
A party was held to celebrate the bicentennial of Clarksville, the county seat of Caroline County. Six-hundred attendees were estimated, but 700 arrived. The food and entertainment budget was $6,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...
-
Refer to Practice 1421. Make all journal entries necessary on the lending companys books in connection with the loan during Year 2, Year 3, Year 4, and Year 5. Assume that all cash payments are...
-
Mendota Foods sells a variety of food products around the world. The company engags directly with individuals with an Interest in cooking by offering ideas and recipes in various staff blogs and...
-
Jack is Australian resident for tax purpose, and he works for DHL as delivery man in last few years. He is now considering to run his own delivery business as sole trader to start with. This is the...
-
Below are the daily price for ABC stock in the month June 2021. There are 30 observations. Write a VBA subroutine to calcualte the mean price and display the result in the blue cell. Date 01-06-2021...
-
This table shows the U.S. federal income tax rates from 2017. (Source: Internal Revenue Service) Single Married Filing Jointly Married Filing Separately Head of Household $0-$9,325 $0-$18,650...
-
1 2 3 4 5 6 7 8 00 9 10 11 12 13 14 15 FAB Company sells a wide range of merchandise, which are initially purchased on account. Short-term notes are frequently issued to obtain cash. a) On January...
-
1. When a value is multiplied or divided by 1, the value is (a) Increased. (b) Unchanged. (c) Decreased. (d) None of the above. 2. The lateral surface area of a solid is (a) Always equal to total...
-
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.
-
Income Statement, Statement of Owner's Equity, and Balance Sheet 1. Prepare a multiple-step income statement. 2. Prepare a statement of owner's equity 3. Prepare a balance sheet. Paulson's Pet Store...
-
How does a modern operating system manage memory allocation and deal with issues such as fragmentation and virtual memory paging ?
-
The stockholders' equity accounts of Whispering Winds Corp. on January 1, 2025, were as follows. Preferred Stock (7%, $100 par noncumulative, 6,000 shares authorized) $360,000 Common Stock ($4 stated...
Study smarter with the SolutionInn App