page 309
please as soon as possible
please help me
thank you so much
this is question
now gave me answer please
let me know more information
thank you
ALGORITHM 1.4 Bag import java.util.Iterator; public class Bag
- implements Iterable
- { private Node first; // first node in list private class Node { Item item; Node next; } public void add (Item item) { // same as push in Stack Node oldfirst = first; first = new Node(); first.item = item; first.next = oldfirst; } public Iterator
- iterator { return new ListIterator(); } I I private class ListIterator implements Iterator
- { private Node current = first; public boolean hasNext() { return current != null; } public void remove() { } public Item next() { Item item current.item: 4 4 ety N Bo Tools Window Help ms 4th Edition.pdf of 969 Q U Q Search public Iterator
- { private Node current = first; public boolean hasNext() { return current != null; } public void remove() } public Item next() { Item item - - current.item; current = current.next; return item; } } This Bag implementation maintains a linked list of the items provided in calls to addo. Code for isEmpty() and size is the same as in Stack and is omitted. The iterator traverses the list, main- taining the current node in current. We can make Stack and Queue iterable by adding the code highlighted in red to ALGORITHMS 1.1 and 1.2, because they use the same underlying data structure and Stack and Queue maintain the list in LIFO and FIFO order, thespectively. 4 stv RAW 1.pdf 2 of 969 Q U Q Search 2.4 - Priority Queues API The priority queue is a prototypical abstract data type (see SECTION 1.2): it rep- resents a set of values and operations on those values, and it provides a convenient ab- straction that allows us to separate application programs (clients) from various imple- mentations that we will consider in this section. As in SECTION 1.2, we precisely define the operations by specifying an applications programming interface (API) that provides the information needed by clients. Priority queues are characterized by the remove the maximum and insert operations, so we shall focus on them. We use the method names delMax( for remove the maximum and insert() for insert. By convention, we will compare keys only with a helper less() method, as we have been doing for sorting. Thus, if items can have duplicate keys, maximum means any item with the largest key value. To complete the API, we also need to add constructors (like the ones we used for stacks and queues) and a test if empty operation. For flexibility, we use a generic imple- mentation with a parameterized type Key that implements the comparable interface. This choice eliminates our distinction between items and keys and enables clearer and more compact descriptions of data structures and algorithiks. For example, we refer to the largest key" instead of the "largest item" or the "item with the largest key." For convenience in client code, the API includes three constructors, which enable clients to build priority queues of an initial fixed size (perhaps initialized with a given array of keys). To clarify client code, we will use a separate class MinPQ whenever ap- propriate, which is the same as MaxPQ except that it has a delMin() method that deletes and returns an item with the smallest key in the queue. Any MaxPQ implementation is easily converted into a MinPQ implementation and vice versa, simply by reversing the sense of the comparison in less(). public class MaxPQ> 4 ety NA Algorithms 4th Edition.pdf Page 322 of 969 Q a o Q Search public class MaxPQ> MaxPQO create a priority queue MaxPQ(int max) create a priority queue of initial capacity max MaxPQ(Keyl) a) create a priority queue from the keys in a[] void insert(key v) insert a key into the priority queste Key max() return the largest key Key delMax return and remove the largest key boolean isEmpty is the priority queue empty? int size number of keys in the priority queue API for a generic priority queue 1 310 CHAPTER 2 - Sorting A priority-queue client. To appreciate the valme of the prioritvenacabstation.com client order of growth ty AR 80 DOO 73 CODE FS > References Mailings Review View Tell me AT New Comment Previous Delete Next Resolve Show Comments Language Tracking Reviewing Chan Wrapping Priority Queue. Recall the discussion in the class of Feb. 25 of JDK Priority Queue and how "wide" its API is, having all the Collection methods plus more. Also recall the homework 2 problem that used LinkedList to implement Bag. We can say we "wrapped" the LinkedList to implement Bag. Similarly wrap the JDK Priority Queue in a class called MinPQ that has the classic priority queue API given on page 309, with the following changes. Replace max with min there, for the classic case. Drop the constructor taking an array of keys, but add one taking initial capacity and a Comparator. Drop the requirement that the Key is Comparable, i.e., the top line should be public class MinPo, saying that the Keys may or may not be Comparable (if they're not, then the Comparator is needed in the constructor, but there's no way to say that in types.) Write a little program TestMinPQ that creates a MinPQ, adds three strings in non-alphabetical order, and then delMin's them out in order. You don't have to test the Comparable option, just support it in MinPQ itself in that one constructor) by passing the Comparator object on to the appropriate PriorityQueue constructor. In your submission show the MinPQ and TestManPQ code. 1 4 1 17