package algs24;
import stdlib.StdIn;
import stdlib.StdOut;
/**
* The <tt>PtrHeap</tt> class is the priorityQ class from Question 2.4.24.
* It represents a priority queue of generic keys.
*
* It supports the usual <em>insert</em> and <em>delete-the-maximum</em>
* operations, along with methods for peeking at the maximum key,
* testing if the priority queue is empty, and iterating through
* the keys.
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/24pq">Section 2.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*/
public class PtrHeap<K extends Comparable<? super K>> {
@SuppressWarnings("unchecked")
/** Create an empty priority queue */
public PtrHeap() {
}
/** Is the priority queue empty? */
public boolean isEmpty() { return true; }
/** Is the priority queue full? */
public boolean isFull() { return true; }
/** Return the number of items on the priority queue. */
public int size() { return 0; }
/**
* Return the largest key on the priority queue.
* Throw an exception if the priority queue is empty.
*/
public K max() {return null;
}
/** Add a new key to the priority queue. */
public void insert(K x) { return;
}
/**
* Delete and return the largest key on the priority queue.
* Throw an exception if the priority queue is empty.
*/
public K delMax() {return null;
}
private void showHeap() {
// a method to print out the heap
// useful for debugging
}
public static void main(String[] args) {
PtrHeap<String> pq = new PtrHeap<>();
StdIn.fromString("10 20 30 40 50 - - - 05 25 35 - - - 70 80 05 - - - - ");
while (!StdIn.isEmpty()) {
StdOut.print ("pq: "); pq.showHeap();
String item = StdIn.readString();
if (item.equals("-")) StdOut.println("max: " + pq.delMax());
else pq.insert(item);
}
StdOut.println("(" + pq.size() + " left on pq)");
}
}
Import StdIn and StdOut and then run the program.
import stdlib.StdIn;
import stdlib.StdOut;
public class PtrHeap<K extends Comparable<? super K>> {
private K[]
pq;
// store items at indices 1 to N
private int
N;
// number of items on priority queue
private int MAXN;
@SuppressWarnings("unchecked")
/** Create an empty priority queue */
public PtrHeap(int initCapacity) {
MAXN =
initCapacity;
pq = (K[]) new
Comparable[initCapacity + 1];
N = 0;
}
/** Is the priority queue empty? */
public boolean isEmpty() { return N == 0; }
/** Is the priority queue full? */
//public boolean isFull() { return N == MAXN;
}
/** Return the number of items on the
priority queue. */
public int size() { return N; }
/**
* Return the largest key on the priority
queue.
* Throw an exception if the priority queue
is empty.
*/
public K max() {
if (isEmpty()) throw
new Error("Priority queue underflow");
return pq[1];
}
/** Add a new key to the priority queue.
*/
public void insert(K x) {
//if (isFull()) throw
new Error("Priority queue overflow");
pq[++N] = x;
}
/**
* Delete and return the largest key on the
priority queue.
* Throw an exception if the priority queue
is empty.
*/
public K delMax() {
if (N == 0) throw new
Error("Priority queue underflow");
K max = pq[1];
pq[N+1] = null;
return max;
}
private void showHeap() {
// a method to print out
the heap
for (int i = 1; i <=
N; i++)
StdOut.print (pq[i] + " ");
StdOut.println ();
}
public static void main(String[] args)
{
PtrHeap<String> pq
= new PtrHeap<>(100);
StdIn.fromString("10 20
30 40 50 - - - 05 25 35 - - - 70 80 05 - - - - ");
while (!StdIn.isEmpty())
{
StdOut.print ("pq: "); pq.showHeap();
String item = StdIn.readString();
if (item.equals("-")) StdOut.println("max: " + pq.delMax());
else pq.insert(item);
}
StdOut.println("(" +
pq.size() + " left on pq)");
}
}
package algs24; import stdlib.StdIn; import stdlib.StdOut; /** * The <tt>PtrHeap</tt> class is the priorityQ class from...
import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...
Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{ BSTTreeNode left, right; String data; public BSTTreeNode(){ left = null; right = null; data = null; } public BSTTreeNode(String n){ left = null; right = null; data = n; } public void setLeft(BSTTreeNode n){ left = n; } public void setRight(BSTTreeNode n){ ...
P1 is below
package p6_linkedList;
import java.util.*;
public class LinkedList
{
public Node header;
public LinkedList()
{
header = null;
}
public final Node Search(int key)
{
Node current = header;
while (current != null && current.item != key)
{
current = current.link;
}
return current;
}
public final void Append(int newItem)
{
Node newNode = new Node(newItem);
newNode.link = header;
header = newNode;
}
public final Node Remove()
{
Node x = header;
if (header != null)
{
header...
// can someone explain the code line by line shortly.thanks import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; // Java 8 example for RSA encryption/decryption. // Uses strong encryption with 2048 key size. public class GlobalMembers { public static void main(String[] args) throws Exception { String plainText = "Hello World!"; // Generate public and private keys using RSA Map<String, Object> keys = getRSAKeys(); PrivateKey privateKey = (PrivateKey) keys.get("private"); PublicKey publicKey = (PublicKey)...
To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems; public HighArray(int max) { a = new long[max]; nElems = 0; } public boolean find(long searchKey) { int...
Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
JAVA Modify the existing (methods already in the file may be modified but not deleted) the code to a MaxHeap and write a driver class to do the following: 1. Insert item into a Heap 2. Delete Max from the Heap 3. Display the Heap array 4. Convert an array to Heap 1,2 & 3 must all work on the same heap array. Option 4 is independent and must take in input from the user of an integer array and...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...