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...
-
Of the total U.S. population, 37% live in the South. If 200 U.S. residents are selected at random, find the probability that at least 80 live in the South.
-
Oldfield Enterprises Limited was formed on 1 January 2005 to manufacture and sell a new type of lawn mower. The bookkeeping staff of the company have produced monthly figures for the first ten months...
-
Edison Systems has estimated the cash flows over the 5-year lives for two projects, A and B. These cash flows are summarized in the table below. a. If project A were actually a replacement for...
-
[Note:- Instead of $5,500, use $5,000 as current book value.] 1. How much is the Year 1 operating cash flow? 2. How much is the EBIT in Year 4? 3. How much is the net working capital cash flow in...
-
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...
-
Data for the North, South, East, and West divisions of Free Bird Company are as follows: a. Determine the missing items, identifying each by the letters (a) through (l). Round percents and investment...
-
Using information from THE ESTE LAUDER COMPANIES INC. 2022 Annual report (use the following link- https://www.elcompanies.com/en/investors/earnings-and-financials/annual-reports )Links to an external...
-
6. questions Given below is the table of values for the function f(x), f'(x), g(x), and g'(x), answer the following X f(x) f'(x) g(x) g'(x) 10 0 2 3 4 5 -1 -2 -3 -4 If h(x)=3f(x)+4g(x) - 2, find...
-
How can price be used as a strategic variable to achieve specific financial goals? Explain any two conditions under which the use of penetration or skimming as a strategy could be appropriate....
-
Write a method called max that accepts a map whose keys are strings and whose values are double as a parameter and returns the double value that has the highest value in the map. If the map is empty,...
-
On December 31, 2017, Pace Co. paid $3,000,000 to Sanders Corp. shareholders to acquire 100% of the net assets of Sanders Corp. Pace Co. also agreed to pay former Sanders shareholders$200,000 in cash...
-
1. Utilizing the information provided and available from web sources, use the ethical decision-making techniques discussed in the chapter to form an opinion about whether Mercks decisions regarding...
-
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...
-
22. All information recorded on the PCR must be: A.typewritten or printed. B.considered confidential. C.a matter of public record. D.reflective of your opinion
-
In other math, what is the definition of a polygon? Options: A ) A shape with curved sides and no angles. B ) A three - dimensional object with a circular base and a curved top. C ) A closed plane...
-
1.) Which asset below is a current asset? a.) Computer b.) Fleet of company cars c.) Pr inter ink d.) Warehouse
Study smarter with the SolutionInn App