Revise BST in Listing 25.5, using a generic parameter and a Comparator for comparing objects. Define a
Question:
Revise BST in Listing 25.5, using a generic parameter and a Comparator for comparing objects. Define a new constructor with a Comparator as its argument as follows:BST(Comparator comparator)
Listing
Transcribed Image Text:
1 public class BST
1 public class BST> extends AbstractTree { protected TreeNode root; protected int size = 0; 2 3 4. 5 /** Create a default binary search tree */ public BST() { 8 10 /** Create a binary search tree from an array of objects */ public BST(E[] objects) { for (int i = 0; i < objects.length; i++) insert(objects[i]); 11 12 13 14 15 @Override /** Return true if the element is in the tree */ public boolean search(E e) { TreeNode current = root; // Start from the root 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 while (current != nul1) { if (e.compareTo(current.element) < 0) { current = current.left; else if (e.compareTo(current.element) > 0) { current = current.right; else // element matches current.element return true; // Element is found return false; 34 @Override /** Insert element e into the binary search tree. * Return true if the element is inserted successfully. * / public boolean insert(E e) { if (root == null) root = createNewNode (e); // Create a new root else { // Locate the parent node TreeNode parent = null; TreeNode current = root; while (current != null) if (e.compareTo(current.element) < 0) { parent = current; current = current.left; 35 36 37 38 39 40 41 42 43 44 45 46 47 else if (e.compareTo(current.element) > 0) { parent = current; current = current.right; 48 49 50 51 52 else 53 return false; // Duplicate node not inserted 54 // Create the new node and attach it to the parent node if (e.compareTo(parent.element) < 0) parent.left = createNewNode (e); else parent.right = createNewNode (e); 55 56 57 58 59 60 61 62 size++; return true; // Element inserted successfully 63 64 65 66 protected TreeNode createNewNode(E e) { return new TreeNode<>(e); 67 68 69 @Override /** Inorder traversal from the root */ public void inorder() { inorder(root); 70 71 72 73 74 75 /** Inorder traversal from a subtree */ protected void inorder (TreeNode root) { if (root == null) return; inorder(root.left); System.out.print(root.element + " "); inorder(root.ri ght); 76 77 78 79 80 81 82 83 @Override /** Postorder traversal from the root */ public void postorder() { postorder(root); 84 85 86 87 88 /** Postorder traversal from a subtree */ protected void preorder(TreeNode root) { if (root -- null) return; postorder(root.left); postorder(root.right); System.out.print(root.element + " "); 89 90 91 92 93 94 95 96 @Override /** Preorder traversal from the root */ public void preorder() { preorder(root); 97 98 99 100 101 /** Preorder traversal from a subtree */ protected void postorder(TreeNode root) { if (root == nul1) return; System.out.print(root.element + " "); preorder (root.left); preorder(root.right); 102 103 104 105 106 107 108 109 110 111 112 113 /** This inner class is static, because it does not access any instance members defined in its outer class */ public static class TreeNode» { protected E element; protected TreeNode left;
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 66% (6 reviews)
Program Plan Create a circle class called Circle java Define accessor and mutator for Circle radius ...View the full answer
Answered By
Joemar Canciller
I teach mathematics to students because I love to share what I have in this field.
I also want to see the students to love math and be fearless in this field.
I've been tutoring these past 2 years and I would like to continue what I've been doing.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Revise MyPriorityQueue in Listing 24.9, using a generic parameter for comparing objects. Define a new constructor with a Comparator as its argument as follows:PriorityQueue(Comparator comparator)...
-
Revise Heap in Listing 23.9, using a generic parameter and a Comparator for comparing objects. Define a new constructor with a Comparator as its argument as follows:Heap(Comparator comparator)...
-
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...
-
A chemical factory discharges waste products into a river. The waste products affect a fishery downstream. Suppose MD = 6E and MAC = 600 - 4E. Consider a liability law requiring the polluter to...
-
Prove the following useful corollary of the triangle inequality: for any x, y in a normed linear space The preceding corollary of the triangle inequality implies that the norm converges along with a...
-
Assume a 0.10 interest rate. How much is a perpetuity of $1,000 per year worth?
-
On April 23, 2014, Calvin Loyer admitted his wife, Edeltrud Loyer, to a nursing home administered by Signature Healthcare. During the admissions process, Calvin signed an arbitration agreement...
-
Ginocera Inc. is a designer, manufacturer, and distributor of low-cost, high-quality stainless steel kitchen knives. A new kitchen knife series called the Kitchen Ninja was released for production in...
-
2) A math tutor makes $15.00 per hour in the tutoring lab at her school. She earns an additional $7.50 more per hour overtime for any hours worked which exceed her normal 40-hr work week. b) Write a...
-
Ten years ago 53% of American families owned stocks or stock funds. Sample data collected by the Investment Company Institute indicate that the percentage is now 46% (The Wall Street Journal, October...
-
Redefine TreeNode by adding a reference to a node?s parent, as shown below: Reimplement the insert and delete methods in the BST class to update the parent for each node in the tree. Add the...
-
Modify Listing 25.9, BSTAnimation.java, to add three new buttons?Show Inorder, Show Preorder, and Show Postorder?to display the result in a label, as shown in Figure 25.24. You need also to modify...
-
Let A and B be the points where the ray of angle intersects the two concentric circles of radii r A/ 0 B P R -X
-
1. Calculate the amount of simple interest paid on a loan of $8,000, at a rate of 5.2% per year for six years. Then calculate the total amount of the loan. 2. Calculate the amount of simple interest...
-
Company has preferred stock that promises its holders a fixed annual dividend of $8. Assuming a required return of 12.8% for the preferred stock based on the risk profile of the company, what is the...
-
What is the significance of the natural rate of unemployment? Why might the natural rate differ across countries?
-
Dr. Wright (2002), an ORU professor, provides the foundational principles for estate planning in his book Inheritance by a Promise. 1) We cannot shirk our responsibilities as stewards no matter how...
-
The price of a certain cough syrup decreased from $10 to $5 a bottle which caused the the quantity demanded to increase from 700 bottles to 1000 bottles per day. is demand elastic inelastic or...
-
Two reports are attached to Reitmans (Canada) Limited's financial statements presented in Appendix A of this book: (1) Management's Responsibility for Financial Statements and (2) The Independent...
-
What are the 5 Cs of marketing channel structure?
-
Following our analysis of randomized quick-sort in Section 12.2.1, show that the probability that a given input element x belongs to more than 2logn subproblems in size group i is at most 1/n 2 .
-
If the conditional at line 14 of our quickSortInPlace implementation of Code Fragment 12.6 were changed to use condition left < right, instead of condition left /** Sort the subarray S[a.b]...
-
If the outermost while loop of our implementation of quickSortInPlace (line 9 of Code Fragment 12.6) were changed to use condition left < right, instead of condition left /** Sort the subarray S[a.b]...
-
1. Define a project system. 2. List and discuss five major functions in project planning. 3. Describe the role of the project manager in project planning. 4. Develop a project planning model for the...
-
John and Frank are in an automobile accident. John sues Frank for $100,000.00 in a comparative negligence state. John has been assigned 40% of the fault by the jury and Frank 60% of the fault by the...
-
If you were running a campaign that had lower than expected impressions, what should you do?
Study smarter with the SolutionInn App