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% (1 review)
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...
-
The mean precipitation for Miami in August is 8.9 inches. Assume that the standard deviation is 1.6 inches and the variable is normally distributed. a. Find the probability that a randomly selected...
-
The accountant of Hook, Line and Sinker, a partnership of seven people, has asked your advice in dealing with the following items in the partnership accounts for the year to 31 May 2007. (a) (i)...
-
All Honors Industries is considering a new product for its Trophy Division. The product, which would feature an alligator, is expected to have global market appeal and to become the mascot for many...
-
Can you please help me with this question E18.23 (LO3) (Repurchase Agreement) Zagat Ltd. enters into an agreement on March 1, 2019, to sell Werner Metal aluminum ingots. As part of the agreement, Z...
-
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...
-
Why cant the company be forced to pay all shareholders the par value of their stock first and corporate debts second?
-
How can the Lakers fix their coaching problem during this 4 game losing streak?
-
You are an audit senior at K & K Chartered Accountants and assigned the forthcoming audit of ABC. Manufacturing Inc, a client your firm has been working with for quite some time. You haven't been...
-
a) Is the cell phone changing our views about polite and impolite behaviour? For example, is it polite for someone to be talking on their cell phone while ordering a drink at Starbucks? Zaidi says,...
-
How does HRM play a role in managing diversity and inclusion in the workplace?
-
a) Vectors a = (x 3-2) and b = (3-1 2) are 93.7 degrees apart. Solve for the highest value of x. (15 marks) b) Use Bresenham's algorithm to draw the line y = 0.5x+1 from (1,1) to (7,4). List the...
-
What should Ron consider in making his decision? The discussion between Don Chambers, the CEO, and Ron Smith, the CFO, was getting heated. Sales and margins were below expectations, and the stock...
-
State whether each statement is true or false. If false, give a reason. {purple, green, yellow} = {green, pink, yellow}
-
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?
-
5/8-18 UNF-2B 7 4X.24 40 .20 [Chapter 18: eating S-12 UNF 20 Xr Knurled Nut #10-24 UNC-2B 44 1.38 50 .50 MEDIUM KNURL 3 .19 .25 EQUALLY SPACED V-BOTTOM
-
Assuming all other conditions to be same and the sample homogeneous, which of the sampling techniques is probable to give the largest sampling error? SELECT THE CORRECT ANSWER Random sampling...
-
Which three statements are valid reasons for making adjusting journal entries? (Choose 3.) Note: You will receive partial credit for each correct entry. A To record depreciation. B To record...
Study smarter with the SolutionInn App