Overview With a few years as Java programmers you may have had an opportunity to use...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Overview With a few years as Java programmers you may have had an opportunity to use a Java PriorityQueue. In this assignment we will build our own priority queue. There are multiple ways to build this data structure. We have already built one last assignment when we built an ordered list. A (minimum) binary heap is a specialized data structure that also implements a priority queue more efficiently than an ordered list. For this reason we will build our priority queue out of a (minimum) binary heap. To complete this you will implement one public class: • MyPriority Queue Formal Specifications MyPriorityQueue |-heap MyArrayList +insert(item : Type) +removeMin() : Type +min(): Type +size(): int +isEmpty(): boolean +toString(): String -bubbleUp() -sinkDown () -parent (index: int) : int -right(index : int) : int -left(index: int) : int Field summary • heap-Stores the values in the heap in an underlying MyArrayList. Method summary .insert - Inserts the item into the heap and corrects the invariant. It does so by following these steps: a. Inserts the item at the end of the array list. b. Calls bubble Up to move the item to the correct location. This method should run in O(Ign) time. • removeMin - Removes the first element and corrects the invariant. It does so by following these steps: a. Swaps the first element with the last element. b. Removes the last element. c. Calls sinkDown to move the first element to the correct location. d. Returns the element removed. This method should run in O(Ign) time. min - Returns the first element. This method should run in O(1) time. • • size - Returns the number of elements in the heap. This method should run in O(1) time. • isEmpty - Returns true if the heap is empty and false otherwise. This method should run in O(1) time. • toString - Returns the contents of the heap in String format. This method should run in O(n) time. • bubbleUp - Shifts the last element up to a position where it belongs to correct the heap invariant. It does so by swapping the element with its parent if they are out of order (using the compare To method). Remember that a node should always be greater than its parent in a minimum heap. This method should run in O(Ign) time. • sinkDown - Shifts the first element down to a position where it belongs to correct the heap invariant. It does so by swapping the element with its smallest child if they are out of order (using the compare To method). Remember that a node should always be less than its children in a minimum heap. This method should run in O(Ign) time. • parent - Returns the index of the parent node in the heap. This method should run in O(1) time. • left - Returns the index of the left child node in the heap of the index passed in. This method should run in O(1) time. • right - Returns the index of the right child node in the heap of the index passed in. This method should run in O(1) time. Testing It is important that you test your code to ensure it works on all potential inputs. Please do this in a separate Main class, and without using additional tools (like JUnit). You will not submit your test code. Instead, your data structures will be tested by code developed by the instructor and/or grader. This code will not be provided to you, as you should test your code before submitting it. If your code fails our tests we will let you know which tests it fails so you can find and correct your errors. Here is the output from my solution: min: null Heap contents: [] heap.insert(4) min: 4 Heap contents: [4] heap.insert(7) min: 4 Heap contents: [47] heap.insert(5) min: 4 Heap contents: [4, 7,5] heap.insert(2) min: 2 Heap contents: [2, 4, 5, 7] heap.insert(3) min: 2 Heap contents: [2, 3, 5, 7, 4] heap.insert(6) min: 2 Heap contents: [2, 3, 5, 7, 4, 6] heap.insert(8) min: 2 Heap contents: [2, 3, 5, 7, 4, 6, 8] heap.insert(9) min: 2 Heap contents: [2, 3, 5, 7, 4, 6, 8, 9] heap.insert(1) min: 1 Heap contents: [1, 2, 5, 3, 4, 6, 8, 9, 7] heap.insert(0) min: 0 Heap contents: [0, 1, 5, 3, 2, 6, 8, 9, 7, 4] removeMin: 0 Heap contents: [1, 2, 5, 3, 4, 6, 8, 9, 7] removeMin: 1 Heap contents: [2, 3, 5, 7, 4, 6, 8, 9] removeMin: 2 Heap contents: [3, 4, 5, 7, 9, 6, 8] removeMin: 3 Heap contents: [4, 7, 5, 8, 9, 6] removeMin: 4 Heap contents: [5, 7, 6, 8, 9] removeMin: 5 Heap contents: [6, 7, 9, 8] removeMin: 6 Heap contents: [7, 8, 9] removeMin: 7 Heap contents: [8, 9] removeMin: 8 Heap contents: [9] removeMin: 9 Heap contents: [] removeMin: null Heap contents: [] Submission You will submit a .zip file containing: • MyArrayList.java • MyPriorityQueue.java Overview With a few years as Java programmers you may have had an opportunity to use a Java PriorityQueue. In this assignment we will build our own priority queue. There are multiple ways to build this data structure. We have already built one last assignment when we built an ordered list. A (minimum) binary heap is a specialized data structure that also implements a priority queue more efficiently than an ordered list. For this reason we will build our priority queue out of a (minimum) binary heap. To complete this you will implement one public class: • MyPriority Queue Formal Specifications MyPriorityQueue |-heap MyArrayList +insert(item : Type) +removeMin() : Type +min(): Type +size(): int +isEmpty(): boolean +toString(): String -bubbleUp() -sinkDown () -parent (index: int) : int -right(index : int) : int -left(index: int) : int Field summary • heap-Stores the values in the heap in an underlying MyArrayList. Method summary .insert - Inserts the item into the heap and corrects the invariant. It does so by following these steps: a. Inserts the item at the end of the array list. b. Calls bubble Up to move the item to the correct location. This method should run in O(Ign) time. • removeMin - Removes the first element and corrects the invariant. It does so by following these steps: a. Swaps the first element with the last element. b. Removes the last element. c. Calls sinkDown to move the first element to the correct location. d. Returns the element removed. This method should run in O(Ign) time. min - Returns the first element. This method should run in O(1) time. • • size - Returns the number of elements in the heap. This method should run in O(1) time. • isEmpty - Returns true if the heap is empty and false otherwise. This method should run in O(1) time. • toString - Returns the contents of the heap in String format. This method should run in O(n) time. • bubbleUp - Shifts the last element up to a position where it belongs to correct the heap invariant. It does so by swapping the element with its parent if they are out of order (using the compare To method). Remember that a node should always be greater than its parent in a minimum heap. This method should run in O(Ign) time. • sinkDown - Shifts the first element down to a position where it belongs to correct the heap invariant. It does so by swapping the element with its smallest child if they are out of order (using the compare To method). Remember that a node should always be less than its children in a minimum heap. This method should run in O(Ign) time. • parent - Returns the index of the parent node in the heap. This method should run in O(1) time. • left - Returns the index of the left child node in the heap of the index passed in. This method should run in O(1) time. • right - Returns the index of the right child node in the heap of the index passed in. This method should run in O(1) time. Testing It is important that you test your code to ensure it works on all potential inputs. Please do this in a separate Main class, and without using additional tools (like JUnit). You will not submit your test code. Instead, your data structures will be tested by code developed by the instructor and/or grader. This code will not be provided to you, as you should test your code before submitting it. If your code fails our tests we will let you know which tests it fails so you can find and correct your errors. Here is the output from my solution: min: null Heap contents: [] heap.insert(4) min: 4 Heap contents: [4] heap.insert(7) min: 4 Heap contents: [47] heap.insert(5) min: 4 Heap contents: [4, 7,5] heap.insert(2) min: 2 Heap contents: [2, 4, 5, 7] heap.insert(3) min: 2 Heap contents: [2, 3, 5, 7, 4] heap.insert(6) min: 2 Heap contents: [2, 3, 5, 7, 4, 6] heap.insert(8) min: 2 Heap contents: [2, 3, 5, 7, 4, 6, 8] heap.insert(9) min: 2 Heap contents: [2, 3, 5, 7, 4, 6, 8, 9] heap.insert(1) min: 1 Heap contents: [1, 2, 5, 3, 4, 6, 8, 9, 7] heap.insert(0) min: 0 Heap contents: [0, 1, 5, 3, 2, 6, 8, 9, 7, 4] removeMin: 0 Heap contents: [1, 2, 5, 3, 4, 6, 8, 9, 7] removeMin: 1 Heap contents: [2, 3, 5, 7, 4, 6, 8, 9] removeMin: 2 Heap contents: [3, 4, 5, 7, 9, 6, 8] removeMin: 3 Heap contents: [4, 7, 5, 8, 9, 6] removeMin: 4 Heap contents: [5, 7, 6, 8, 9] removeMin: 5 Heap contents: [6, 7, 9, 8] removeMin: 6 Heap contents: [7, 8, 9] removeMin: 7 Heap contents: [8, 9] removeMin: 8 Heap contents: [9] removeMin: 9 Heap contents: [] removeMin: null Heap contents: [] Submission You will submit a .zip file containing: • MyArrayList.java • MyPriorityQueue.java
Expert Answer:
Answer rating: 100% (QA)
The detailed answer for the above question is provided below Solution MyPriorityQueuejava import javautilArrayList public class MyPriorityQueue privat... View the full answer
Related Book For
Auditing Cases An Interactive Learning Approach
ISBN: 9780134421827
7th Edition
Authors: Mark S Beasley, Frank A. Buckless, Steven M. Glover, Douglas F Prawitt
Posted Date:
Students also viewed these computer engineering questions
-
Code this in python In this assignment we are going to practice reading from a file and writing the results of your program into a file. We are going to write a program to help Professor X automate...
-
In this assignment we would like to implement a version of the rock paper scissors game, where the character R represents rock, P represents paper, and S represents scissors. The expression (R&S)...
-
Share any experiences you may have had where an organization and / or a website has collected your information. What has been your concern about sharing this information, if you had any concerns? And...
-
One way to delete nodes from a known position in a leftist heap is to use a lazy strategy. To delete a node, merely mark it deleted. When a findMin or deleteMin is performed, there is a potential...
-
Develop a POQ solution and calculate total relevant costs for the data in the preceding table.
-
Perform the following calculations with the correct number of significant figures. a. 159.31 204.6 b. 5.1125 + 0.67 + 3.2 c. 7.662 - 7.425 d. 16.5/3.45
-
List the five main goals that policymakers attempt to achieve with the income tax system.
-
Jan Gentry is the owner of a small company that produces electric scissors used to cut fabric. The annual demand is for 8,000 scissors, and Jan produces the scissors in batches. On the average, Jan...
-
Your co-worker correctly used a 1000-period model to estimate the value of the following European call option: S = 45 . X = 50 r 0.0324052% h 4.9170655% 1=-4.6866213% T = 3.6 years where r is the...
-
Paolo is a 50% partner in the Capri Partnership and has decided to terminate his partnership interest. Paolo is considering two options as potential exit strategies. The first is to sell his...
-
Exercise I 1. Define: a) PRODUCTIVITY b) FACTORS OF PRODUCTION c) GOODS d) DURABLE GOODS e) SERVICES f) PRIMARY PRODUCTION g) SECONDARY PRODUCTION h) TERTIARY PRODUCTION 2. Label the diagram below...
-
What is collateral on a loan that remains in the possession of the borrower and not the bank?
-
List the major types of collateral. Discuss the priorities of parties with conflicting interests in collateral when default occurs. PLEASE AVOID PLAGIARISM. "Plagiarized work will not be accepted."
-
4. Now we can use Solver to maximize this expression by changing the weight of the equity input cell. The constraint is that the standard deviation of the portfolio is equal to the standard deviation...
-
What alternative does not describe a type of collateral for a loan?
-
Question Calculate Z score for each year for the national detergent Co SAOG Compare and evaluate the performance of each company based on interpretation of values of Z score values link...
-
An oil drilling company's resources are being depleted and known reserves are becoming scarcer. As a result, the company's earnings are declining at a rate of 8% each year. If the current earnings...
-
You are maintaining a subsidiary ledger account for Police-Training Expenditures for 2013. The following columns are used: Inventory purchases are initially recorded as expenditures. Record the...
-
Anne Aylor, Inc. (Anne Aylor) is a leading national specialty retailer of high-quality womens apparel, shoes, and accessories sold primarily under the Anne Aylor brand name. Anne Aylor is a highly...
-
On January 24, 2008, Socit Gnrale, Frances second largest bank announced the largest trading loss in history, a staggering 4.9 billion Euro ($7.2 billion U.S.), which it blamed on a single rogue...
-
Henrico Retail, Inc. is a first year audit client. The audit partner obtained the following description of the sales system after recently meeting with client personnel at the corporate office....
-
Given an arbitrary frontier portfolio \(w^{*}\), with \(w^{*} eq w^{\mathrm{MVP}}\), there exists a unique frontier portfolio \(w^{\mathrm{zc}}\) such that...
-
In the context of Proposition 3.4, show that \(\mathbb{E}\left[u^{\prime \prime}\left(\widetilde{W}^{*} ight) \widetilde{W}^{*}\left(\tilde{r}-r_{f} ight) ight] \geq 0\) if the utility function \(u\)...
-
Consider a strictly increasing and strictly concave utility function \(u\) and suppose that there exist a risk free asset with return \(r_{f}>0\) and \(N\) risky assets whose returns...
Study smarter with the SolutionInn App