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...
-
An interviewer has been told to randomly select respondents and to include x = whether or not they are a college graduate among the data recorded. The categories of the first 30 persons interviewed,...
-
Ms. Debbie, the sole shareholder of Shining Limited, a CCPC, has been considering selling her common shares to Let's-Make-a-Deal Ltd., a CCPC. However, Ms. Debbie recalls reading somewhere that one...
-
Costs per Equivalent Unit The following information concerns production in the Baking Department for August. All direct materials are placed in process at the beginning of production. ACCOUNT Work 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...
-
What was the first major industry in England? What factors enabled the development of this industry?
-
21 West Coast Tours runs boat tours along the west coast of British Columbia. On March 5, 2023, it purchased, with cash, a cruising boat for $936,000, having a useful life of 10 years or 13,800...
-
02 P4-3B. Multi-step Income Statement The adjusted trial balance of Patton Corporation on December 31 is shown below. PATTON CORPORATION Adjusted Trial Balance December 31 Debit Credit Cash.......
-
What is the relevance of shareholder value-chain activities in consideration of the acquisition of a related business diversification? Be descriptive and expansive in your response
-
Kelson Sporting Equipment, Inc., makes two different types of baseball gloves: a regular model and a catcher's model. The firm has 600 hours of production time available in its cutting and sewing...
-
Required information Use the following information for the Exercises 8-10 below. (Algo) [The following information applies to the questions displayed below.] Hemming Company reported the following...
-
Which proposal do you think best serves the interest of a member of Congress and why?
-
Suppose that a flow network G = (V, E) violates the assumption that the network contains a path s t for all vertices V. Let u be a vertex for which there is no path s u t. Show that there must...
-
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...
-
Palisade Creek Co. is a merchandising business that uses the perpetual inventory system. The account balances for Palisade Creek Co. as of May 1, 2019 (unless otherwise indicated), are as follows:...
-
1-When accounting for an acquisition, goodwill is the difference between what two things? 2- What factors should be considered when deciding whether an acquisition should be financed with cash or...
-
What is the main friction Fluidity aims to address? REAL STATE
Study smarter with the SolutionInn App