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% (3 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...
-
1. Identify and describe the problem discussed in Vodafone case. What management, organization, and technology factors contributed to the problem? 2. Why did Vodafone have to spend so much time...
-
In December 2008, Jason Garcia signed a motor vehicle sales contract with Mac Haik Dodge Chrysler Jeep, a dealer. In the contract, Garcia agreed to purchase a 2009 Dodge Ram 1500. The contract...
-
TipTop Flight School offers flying lessons at a small municipal airport. The schools owner and manager has been attempting to evaluate performance and control costs using a variance report that...
-
Let G = (V,E) be a simple graph with |V| 2. The complement graph G of G is the simple graph whose vertex set is V and whose edge set consists of all the edges that have as endpoints nonadjacent...
-
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...
-
Use the moment-area theorems and determine the slope at B and displacement at B. EI is constant. A 2 m 9,kN Probs. F7-17/18 2 m B
-
Pellham Company is using debt to finance a new expansion. It sold 2,000 10-year bonds with a $1,000 par value. The bonds had annual coupon rates of 9.8%, paid semiannually, and they are selling for...
-
Explore the strategic merits and drawbacks associated with being an initial entrant into a market, analyzing the potential benefits and pitfalls inherent in seizing early-mover advantages, while also...
-
Listed below are two (2) common sources of microbiological contamination of food. You are required to identify at least two (2) effects this has on health. Common sources Effects C Fungi Bacteria
-
Chartreuse Co. has purchased a brand new machine to produce its High Flight line of shoes. The machine has an economic life of four years. The depreciation schedule for the machine is straight-line...
-
What are the primary challenges associated with the implementation of a Warehouse Management System (WMS) in Malaysia?
-
Miller Wigs Company supplies wigs and hair care products to beauty salons throughout California and the Pacific Northwest. The accounts receivable clerk for Miller Wigs prepared the following...
-
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.
-
F4U Berhad markets a range of franchises which it makes available to its customers, the franchisees. F4U supplies the franchisee with information of the mode of operation, detailed operation...
-
How does due diligence differ from due care? Why are both important?
-
Discuss The three overarching principles set out in section 11(7) of the Children (Scotland) Act 1995 are the cornerstone to decision-making in Scots family law actions. ?
Study smarter with the SolutionInn App