Write a program based on the program of Figs. 21.15 and 21.16 that inputs a line of
Question:
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 and postorder traversals of the 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 == 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== nul1) { 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: 100% (2 reviews)
To write a program as per your request we need to make some modifications to the TreeTest program from Fig 2116 The new program should read a line of ...View the full answer
Answered By
PALASH JHANWAR
I am a Chartered Accountant with AIR 45 in CA - IPCC. I am a Merit Holder ( B.Com ). The following is my educational details.
PLEASE ACCESS MY RESUME FROM THE FOLLOWING LINK: https://drive.google.com/file/d/1hYR1uch-ff6MRC_cDB07K6VqY9kQ3SFL/view?usp=sharing
3.80+
3+ 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
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Design and write a complete test program to test if the BST class in Listing 25.5 meets all requirements. Listing 1 public class BST 2 extends AbstractTree { protected TreeNode root; protected int...
-
Atuheire presented the following data for the two activity levels as under; CAPACITY 60% 100% Budgeted production(units) 300500 Insurance(shillings) 3000 3000 Wages (shillings) 3600 6000 Consumables...
-
In Problems 1158, perform the indicated operation, and write each expression in the standard form a + bi. 3i(-3 + 4i)
-
Rydberg Atoms Rydberg atoms are atoms whose outermost electron is in an excited state with a very large principal quantum number. Rydberg atoms have been produced in the laboratory and detected in...
-
Testing the effect of a diet and exercise regimen on subjects cholesterol levels, a sports medicine clinic enlists 40 volunteers whose cholesterol levels are measured before and after the 1-month...
-
The various classifications of advertising to consumer and business-to-business markets are shown in Figure 1-4. Choose one category of advertising to consumer markets and one to the...
-
Using 2008 as the base year, prepare a trend analysis of the following data, and tell whether the situation shown by trends is favorable or unfavorable. 2012 20 2010 2009 2008 Net sales Cost of goods...
-
Which of the following requires that before a rule or regulation may be adopted by an agency, interested parties must be given notice of the proposed rules and an opportunity to express their views...
-
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...
-
Modify Figs. 21.15 and 21.16 to allow the binary tree to contain duplicates. Fig. 21.15 Fig. 21.16 I // Fig. 21.15: Tree.java 2 // TreeNode and Tree class declarations for a binary search tree. 3...
-
Fill in the Blank Retained Earnings during a period = Revenue (Expenses + __________).
-
In the following vignette write out three examples of a MI intervention in response to what the client has shared. Mark is a 19-year-old, male who lives with his parents and younger brother. His...
-
Photon Technologies, Inc., a manufacturer of batteries for mobile phones, signed a contract with a large electronics manufacturer to produce three models of lithiumion battery packs for a new line of...
-
Most human behaviour: can be easily explained has multiple causes stems from unconscious desires depends on social influence
-
In what ways are the information processing theories of cognitive development discussed in PSY393 different from the embodied cognition perspective? In what ways are they similar? In your opinion,...
-
Problem 15-6B Accounting for share investments LO4 CHECK FIGURE: 2. Carrying value per share, $19.63 River Outdoor Supply Corporation (River Corp.) was organized on January 2, 2023. River Corp....
-
In recent years, school choice has been a hotly debated issue. Explain whether or not education is a public good. If education is not a public good, why should the government provide it?
-
The maximum pressure that can be developed for a certain fluid power cylinder is 15.0 MPa. Compute the required diameter for the piston if the cylinder must exert a force of 30 kN.
-
What is the relationship between D-AMPS and AMPS?
-
Repeat Problem P16-9 for D-AMPS. Problem P16-9 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...
-
What is GSM?
-
ABC Company engaged in the following transaction in October 2 0 1 7 Oct 7 Sold Merchandise on credit to L Barrett $ 6 0 0 0 8 Purchased merchandise on credit from Bennett Company $ 1 2 , 0 0 0 . 9...
-
Lime Corporation, with E & P of $500,000, distributes land (worth $300,000, adjusted basis of $350,000) to Harry, its sole shareholder. The land is subject to a liability of $120,000, which Harry...
-
A comic store began operations in 2018 and, although it is incorporated as a limited liability company, it decided to be taxed as a corporation. In its first year, the comic store broke even. In...
Eu Competition Law Between Public And Private Enforcement 1st Edition - ISBN: 9041146776 - Free Book
Study smarter with the SolutionInn App