Design and write a complete test program to test if the BST class in Listing 25.5 meets
Question:
Design and write a complete test program to test if the BST class in Listing 25.5 meets all requirements.
Listing
Transcribed Image Text:
1 public class BST
1 public class BST> 2 extends AbstractTree { protected TreeNode root; protected int size = 0; 4 5 /** Create a default binary search tree */ public BST() { 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 @0verride /** 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 while (current != null) { if (e.compareTo(current.element) < 0) { current = current.left; 21 22 23 else if (e.compareTo(current.element) > 0) { current = current.right; 24 25 26 27 else // element matches current.element return true; // Element is found 28 29 30 31 return false; 32 33 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 48 49 else if (e.compareTo(current.element) > 0) { parent = current; current = current.right; 50 51 else return false; // Duplicate node not inserted 52 53 54 55 // Create the new node and attach it to the parent node if (e.compareTo(parent.element) < 0) parent.left = createNewNode(e); 56 57 58 else parent.right = createNewNode (e); 59 60 61 62 sizet+; return true; // Element inserted successfully 63 64 65 66 protected TreeNode createNewNode(E e) { return new TreeNode<>(e); 67 68 69 70 @Override /** Inorder traversal from the root */ public void inorder() { inorder(root); 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.right); 76 77 78 79 80 81 82 83 84 85 86 87 88 @Override /** Postorder traversal from the root */ public void postorder() { postorder(root); /** Postorder traversal from a subtree */ protected void preorder(TreeNode root) { if (root == nul1) 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 102 /** 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); 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: 87% (16 reviews)
Program Plan Create a new BSTTester class In its constructor test all the methods of BST class Create an object of BST class Add elements to the BST o...View the full answer
Answered By
Utsab mitra
I have the expertise to deliver these subjects to college and higher-level students. The services would involve only solving assignments, homework help, and others.
I have experience in delivering these subjects for the last 6 years on a freelancing basis in different companies around the globe. I am CMA certified and CGMA UK. I have professional experience of 18 years in the industry involved in the manufacturing company and IT implementation experience of over 12 years.
I have delivered this help to students effortlessly, which is essential to give the students a good grade in their studies.
3.50+
2+ 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
-
Design and write a complete test program to test if the AVLTree class in Listing 26.4 meets all requirements. Listing 1 public class TestAVLTree { public static void main(String[] args) { // Create...
-
Design and write a complete test program to test if the MyLinkedList class in Listing 24.6 meets all requirements. Listing 1 public class MyLinkedList extends MyAbstractlist { private Node head,...
-
Design and write a complete test program to test if the MyArrayList class in Listing 24.3 meets all requirements. Listing 1 public class MyArrayList extends MyAbstractList { public static final int...
-
a) which ionization gives the best sensitivity the analysis of chloramphenicol? b) The compound with highest retention factor? c) What is the resolution between yohimbine and chloramphenicol? d) What...
-
Show that the average of the net outputs does not satisfy the requirements of a norm on the production possibility set. -1 yi)/71
-
a. We can make an immediate payment now of $10,000 or pay equal amounts of R for the next four years (first payment due one year from now).With a time value of money of 0.10, what is the maximum...
-
In 2016, a hacker tricked a Lamps Plus employee into disclosing the tax information of approximately 1,300 other employees, resulting in the filing of a fraudulent income tax return of Lamps Plus...
-
Sunto Scientific manufactures GPS devices for a chain of retail stores. Its most popular model, the Magellan XS, is assembled in a dedicated facility in Savannah, Georgia. Sunto is keenly aware of...
-
Find the missing side lengths (in kilometers). (The sketches are not to scale.) 33 km 24 km 48 89 x 15 km 48% 89 22 km X = y = km E E km
-
A manufacturer produces four products, each using a different raw material. The following table provides cost and market value information on these inventories. Required: For each of the above...
-
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...
-
Write a new view class that displays the tree horizontally with the root on the left as shown in Figure 25.23. 33 is deleted from the tree 35) 14 3 Delete Enter a key: 33 Insert
-
Kimberly Cerny is a graduate student. She is 22 years old and works part-time as a graduate assistant in the biology department. In the summer, Kimberly was an intern at Neutrobio, Inc. Details...
-
Suppose {X} is a sequence of random variables where Xn ~Binom (n) (the number of trials increases but the probability of success of each trial proportionally decreases). To what does the sequence...
-
How much money could Honda Inc., a motor maker, afford to spend now on new project instead of spending $900,000 eight years from now, if the companys rate of return is 15% compound interest per year?
-
Kwong Yuen Company is ready to buy an office unit. The company has $1,800,000 for the (1) down payment, (2) costs related to bank loan, and (3) interior renovation cost. Costs related to bank loan...
-
What are the three functions of money? May you offer an example of an asset that is considered money other than US currency?
-
3. You ran column chromatography and collected a fraction each minute for 25 minutes, then you analyzed them all by TLC. The resulting TLC data is below. Please note that in this example ferrocene...
-
In the November 30, 2014, bank reconciliation at Kiran's Kayaks, there were two outstanding cheques: #165 for $812 and #169 for $529. There was a $1,128 deposit in transit as at November 30, 2014....
-
QUESTION 2 The CEO of Farisha Hijab Sdn Bhd insisted on further investigation to be carried out that he also required Mr Muaz to conduct the analysis of variance for the material and labour of the...
-
Suppose we are given two n-element sorted sequences A and B each with distinct elements, but potentially some elements that are in both sequences. Describe an O(n)-time method for computing a...
-
Is our linked-list-based implementation of merge-sort (Code Fragment 12.3) stable? Explain why or why not. /** Merge contents of sorted queues S1 and S2 into empty queue S. */ public static void...
-
Is our array-based implementation of merge-sort given in Section 12.1.2 stable? Explain why or why not.
-
Product launch failing to comply to regulations or standards Brief product description & company descriptions Detailed explanation of situation and which regulations were in question. why did the...
-
Finely shredded waxed paper is fed to a continuous countercurrent extraction system at the rate of 40 kg/hr. Since wax is infinitely soluble in kerosene, the paper is to be de-waxed using kerosene...
-
es Liz's Health Food Store has estimated monthly financing requirements for the next six months as follows: January February March $9,600 April $9,600 3,600 4,600 May 10,600 June 5,600 Short-term...
Study smarter with the SolutionInn App