Modify Figs. 21.15 and 21.16 to allow the binary tree to contain duplicates. Fig. 21.15 Fig. 21.16
Question:
Modify Figs. 21.15 and 21.16 to allow the binary tree to contain duplicates.
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 == nul1) { 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: 100% (1 review)
To modify the binary search tree to allow duplicates we need to adjust the insert behavior In the cu...View the full answer
Answered By
Aysha Ali
my name is ayesha ali. i have done my matriculation in science topics with a+ . then i got admission in the field of computer science and technology in punjab college, lahore. i have passed my final examination of college with a+ also. after that, i got admission in the biggest university of pakistan which is university of the punjab. i am studying business and information technology in my university. i always stand first in my class. i am very brilliant client. my experts always appreciate my work. my projects are very popular in my university because i always complete my work with extreme devotion. i have a great knowledge about all major science topics. science topics always remain my favorite topics. i am also a home expert. i teach many clients at my home ranging from pre-school level to university level. my clients always show excellent result. i am expert in writing essays, reports, speeches, researches and all type of projects. i also have a vast knowledge about business, marketing, cost accounting and finance. i am also expert in making presentations on powerpoint and microsoft word. if you need any sort of help in any topic, please dont hesitate to consult with me. i will provide you the best work at a very reasonable price. i am quality oriented and i have 5 year experience in the following field.
matriculation in science topics; inter in computer science; bachelors in business and information technology
_embed src=http://www.clocklink.com/clocks/0018-orange.swf?timezone=usa_albany& width=200 height=200 wmode=transparent type=application/x-shockwave-flash_
4.40+
11+ Reviews
14+ 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
-
In this exercise, we discuss deleting items from binary search trees. The deletion algorithm is not as straightforward as the insertion algorithm. Three cases are encountered when deleting an itemthe...
-
implement a binary search tree to allow duplicates have each node store a data structure of items that are considered duplicates (using the first item in this structure) to control branching
-
Our linked-list class allowed insertions and deletions at only the front and the back of the linked list. These capabilities were convenient for us when we used composition to produce a stack class...
-
In Problems 1158, perform the indicated operation, and write each expression in the standard form a + bi. 2 + i i
-
Consider a hydrogen atom in the state. (a) For what value of, is the potential energy U(r) equal to the total energy E? Express your answer in terms of a. This value of, is called the classical...
-
Manufacturing overhead was estimated to be $400,000 for the year along with 20,000 direct labor hours. Actual manufacturing overhead was $415,000, and actual labor hours were 21,000. Which of the...
-
a. Nonlinearity in mass b. Nonlinearity in damping c. Linear equation d. Nonlinearity in spring \(\ddot{x}+\omega_{0}^{2}\left(x-\frac{x^{3}}{6} ight)=0\)
-
Refer to the information in Problem 6-35. Assume the following: Animal Gear (AG) does not make any sales on credit. In Problem 6-35 Animal Gear accounts for direct materials using a FIFO cost flow...
-
New Madrid Chocolate Corporation manufactures bulk chocolate that it sells to Venus Candy Company. Currently, New Madrid packages and ships their bulk chocolate in 50-pound bars that are then shipped...
-
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...
-
Write a program that simulates a checkout line at a supermarket. The line is a queue object. Customers (i.e., customer objects) arrive at random integer intervals of from 1 to 4 minutes. Also, each...
-
Explain how to implement first fit and best fit in O(N logN) time.
-
Smith Contractors, Inc. began operations on October 1, 2020. The first three business transactions for the company have already been recorded as reflected in the transactions below and on the...
-
Questions for Figure 13 What important conclusions can your make? List 2 important conclusions 1) A Questions for Figure 15 What are the measures of the angles below? Angle ABE Angle ADC What...
-
Instructions a) Use the T-Accounts to record the transactions below for Dixie Incorporated in its first year of business. HINT: Now that we are recording entries on the account level. The Retained...
-
Questions for Figure 7 What are the measures of the angles below? Angle AOB Angle AEC Angle AOC Angle COD Angle AEO Questions for Figure 9 What important conclusions can you make? List 2 important...
-
Jim's annuity of $10/year is deferred for 10 years, then payable for 10 years with payments made at the end of each year. If 10 S10 i = K, what is the present value of Jim's annuity? 8. When i =...
-
Use the rules of measurements to add the following measurements: 1. 0.0250 s; 0.075 s; 0.00080 s; 0.024 s 2. 2100 N; 36,800 N; 24,000 N; 14.5 N; 470 N Use the rules for multiplication and division of...
-
The domain of the variable in the expression x 3/x + 4 is________.
-
Repeat Problem P16-9 for GSM. 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 1-MHz...
-
What is the function of the CDMA in IS-95?
-
Repeat Problem P16-9 for IS-95. 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 1-MHz...
-
How do modern operating systems handle device management and communication, including strategies for device discovery, driver management, and device arbitration in heterogeneous hardware environments?
-
(a) Find the equation of the line perpendicular to 2x-7y=11 that goes through the point (-3,-1) (b) Find the equation of the line going through the point (-3,-6) and parallel to 5y-7x=9 (c) Find the...
-
a) Write C++ statements for the algorithms in Figure 2. (10 marks) Initialize total to zero Initialize counter to zero Input the first mark while the user has not as yet entered the sentinel add this...
Study smarter with the SolutionInn App