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...
-
Goodwill is an intangible asset that represents the excess of the purchase price over the fair value of identifiable net assets acquired in a business combination. While it adds value to a company's...
-
Prove that, for the system considered in Section 13.5.1, the minimum value of \(\omega^{2}\) for which the amplitude of subharmonic oscillations \(A\) will have a real value is given by...
-
Crimson Company has two departmentsDye and Dryand manufactures three products. Budgeted unit production for the coming year is 21,000 of Product J, 36,000 of Product C, and 30,000 of Product B. The...
-
You are considering purchasing a new car and will need to finance this purchase. Using a calculator calculate the interest for a loan of $14,000 for one year, using simple interest, at the following...
-
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...
-
If f is a bounded function on [a, b] such that f(x) = 0 except for x in {c1, c2, . . . , cn} in [a, b], show that U(f) = L(f) = 0.
-
Hokey Pokey Pte Ltd (HPPL) produces baseball bats, and adopts a standard costing system for control purposes. The following standard cost has been developed: Direct materials Direct labour Variable...
-
*Harry Consulting is a consulting firm owned and operated by Harry Smith. The end-of-period spreadsheet shown below was prepared for the year ended July 31, 2015: Unadjusted Trial Balance Account...
-
Tiana Daniles is the sole owner of T-Square Enterprise located in Spanish Town, St. Catherine. The business' trial balance on December 2021 was as follows: Trial Balance at 31 December 2021 Accounts...
-
Nandipha-Thabo partnership has built up a small but successful business that has been in operation for over ten years. The business makes a range of ladies' dresses which are sold through boutiques...
-
In 2022, Nina and Bob are married and reported the following items of income at the end of the tax year: Nina Bob Total Salary $40,000 $0 $40,000 Interest Income $ 1,000 $200 $ 1,200 Total $41,000...
-
What are the basic metric units for length, mass, and time? (a) Foot, pound, hour (b) Newton, litre, second (c) Metre, kilogram, second (d) Mile, ton, day
-
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?
-
What are the key differences between monolithic and microkernel architectures in operating systems, and how do they impact system performance, scalability, and reliability ?
-
Discuss the role of process scheduling algorithms in multitasking operating systems, exploring concepts such as preemptive versus non-preemptive scheduling, priority inversion, and real-time...
-
Explain the mechanisms and challenges involved in implementing file systems in distributed operating environments, including issues related to data consistency, fault tolerance, and distributed file...
Study smarter with the SolutionInn App