import java.util.Scanner;
class ed {
int fr, r;
int q[];
int n;
ed(int x) {
n = x;
fr = -1;
r = -1;
q = new int[n];
}
void enque(int n) {
int val = n;
while (r < n-1) {
if (r==n-1) {
System.out.println("Overflow");
break;
}
else if (fr==-1 && r==-1) {
fr=0;
r=0;
q[r] = val;
}
else {
r += 1;
q[r] = val;
}
}
}
void deque() {
if (fr==-1 && r==-1) {
System.out.println("Underflow");
}
else if (fr==r) {
fr=-1;
r=-1;
}
else {
fr += 1;
}
}
void reverse(int[] q) {
int a = q[0];
deque();
reverse(q);
enque(a);
}
void printq() {
for (int i = fr; i<=r; i++) {
System.out.print(q[i] + " ");
}
}
}
public class q1 {
static Scanner f = new Scanner (System.in);
public static void main(String[] args) {
int n = f.nextInt();
ed que = new ed(n);
for (int i=0; i<n; i++) {
int x = f.nextInt();
que.enque(x);
}
// que.deque();
// que.printq();
que.reverse(que.q);
}
}
My aim is to reverse a queue (Array) using a recursive function, but in VS Code, the loop is running infinite times and I'm not getting a chance to see the error. I'd like to know my mistake, and any improvement is highly appreciated.
The class ed contains a constructor which initializes the array and the front, rear values. Enque method adds an element to the queue at the rear, deque method removes the front element. Reverse method takes an array input (queue), stores the foremost element in the variable a, deques it, calls itself, then enques it at the back. VS Code is showing the error at line 48 (reverse(q), when it calls itself) but it's not showing the error as it's so far up.
A lot of things are not going the right way in your queue implementation using arrays.
Like, in enque function, you can fill values from rear = 0 to rear = n - 1, because you have n positions available in the q array.
Your code was too long, unstructured, and a bit messy with no proper variable names, So, I didn't read it any further.
But one thing I can make out is that you need to read how to implement a queue using the array.
Now, coming to queue reversal using the recursion part.
Your approach was correct, you just missed out the base case condition.
Steps for reversing queue:
Your queue has some elements, we get the first element out of the queue.
Then, we assume I have a recursive function that reverses the rest of the queue.
In this reversed queue, I just have to push that first element to the back.
And coming to the base case, each time queue size is decreasing by 1, so at the end, the queue will become empty then we don't have to do anything, just return. THUS STOPPING THE RECURSION (which you missed).
Here, is my implementation if you need some reference:
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class Queue {
private int front, rear, capacity;
private int queue[];
Queue(int c) {
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
int size() {
return rear - front;
}
void enqueue(int data) {
if (capacity == rear) {
System.out.printf("Queue is full.\n");
return;
}
else {
queue[rear] = data;
rear++;
}
}
void dequeue() {
if (front == rear) {
System.out.printf("Queue is empty.\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
}
int front() {
if (front == rear) {
System.out.printf("\nQueue is Empty.\n");
return -1;
}
return queue[front];
}
void print() {
int i;
if (front == rear) {
System.out.printf("Queue is Empty.\n");
return;
}
for (i = front; i < rear; i++) {
System.out.printf(" %d, ", queue[i]);
}
System.out.println("");
return;
}
}
class GFG {
static Scanner scanner = new Scanner(System.in);
public static void reverseQueue(Queue queue) {
if (queue.size() == 0) {
return;
}
int frontElement = queue.front();
queue.dequeue();
reverseQueue(queue);
queue.enqueue(frontElement);
}
public static void main (String[] args) {
int queueSize = scanner.nextInt();
Queue queue = new Queue(queueSize);
for (int i = 0; i < queueSize; i++) {
int element = scanner.nextInt();
queue.enqueue(element);
}
queue.print();
reverseQueue(queue);
queue.print();
}
}
You can comment if anything is wrong, or need more clarification.
Related
This is my Queue class. I have implemented it using arrays.
public class QueueUsingArray {
private int data[];
private int firstElementIndex;
private int nextElementIndex;
private int size;
public QueueUsingArray() {
data = new int[10];
firstElementIndex = -1;
nextElementIndex = 0;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size() == 0;
}
private void checkEmpty() throws QueueEmptyException {
if (size == 0) {
QueueEmptyException e = new QueueEmptyException();
throw e;
}
}
public int front() throws QueueEmptyException {
checkEmpty();
return data[firstElementIndex];
}
public int dequeue() throws QueueEmptyException {
checkEmpty();
int output = data[firstElementIndex];
size--;
data[firstElementIndex] = 0;
firstElementIndex = (firstElementIndex + 1) % data.length;
if (size == 0) {
firstElementIndex = -1;
nextElementIndex = 0;
size = 0;
}
return output;
}
public void enqueue(int element) {
if (size == data.length) {
int[] temp = data;
data = new int[data.length * 2];
int k = 0;
for (int i = firstElementIndex; i < temp.length; i++) {
data[k] = temp[i];
k++;
}
for (int i = 0; i < firstElementIndex; i++) {
data[k] = temp[i];
k++;
}
firstElementIndex = 0;
nextElementIndex = temp.length;
}
if (size == 0) {
firstElementIndex = 0;
}
data[nextElementIndex] = element;
size++;
nextElementIndex = (nextElementIndex + 1) % data.length;
}
}
Here is my QueueUse class.
public class QueueUse {
public static void main(String[] args) throws QueueEmptyException {
QueueUsingArray q=new QueueUsingArray();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.enqueue(60);
q.enqueue(70);
q.enqueue(80);
q.enqueue(90);
q.enqueue(100);
q.enqueue(110);
q.enqueue(120);
q.enqueue(130);
q.enqueue(140);
q.enqueue(150);
q.enqueue(160);
q.enqueue(170);
q.enqueue(180);
q.enqueue(190);
q.enqueue(200);
q.enqueue(210);
q.enqueue(220);
q.enqueue(230);
q.enqueue(240);
q.enqueue(250);
q.enqueue(260);
q.enqueue(270);
q.enqueue(280);
q.enqueue(290);
q.enqueue(300);
System.out.println("All elements");
for(int i=0;i<q.size();i++){
try {
System.out.println(q.dequeue());
} catch (QueueEmptyException e) {
System.out.println("Sorry");
}
}
}
}
My output is not complete. Output is not showing all the elements in my queue. What is the error. Output is only showing until 140 and not beyond that.
Your print method does not work because you decrement the size every time you invoke your dequeue() method in the for loop. If you are going to use a for loop you should be using a fixed size. Basically in this statement ( i < q.size() ) as i grows size decreases. If i = 0 and size = 4 the first loop i would be 1 and size would be 3. Your never going to get to the 0th or 1st element in your queue because by the next loop your already at i = 2 and size = 3.
first you should add a getter for the queue items
public int getElementAt(int index){
return data[index];
}
then you can call the method in the for loop for every index in data
int length = q.size();
for(int i = 0; i < length; i++){
System.out.println(q.getElementAt(i));
}
If it is not mandatory to do an array implementation, I would suggest using the Vector class because it has a better API for a queue. I would also suggest slowly tracing your program every time you have issues and to start with smaller test data sets to make tracing a easier.
public class Queue {
private Vector<String> data ;
Queue(){
data = new Vector();
}
public void enqueue(String item){
data.add(item);
}
public void dequeue(){
data.remove(0);
}
public void printQueueItems(){
int length = data.size();
for(int i = 0; i < length; i++){
System.out.println(data.get(i));
}
}
public static void main(String[] args) {
Queue myQ = new Queue();
myQ.enqueue("hello");
myQ.enqueue("world");
myQ.enqueue("!");
myQ.printQueueItems();
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need help on java Programming. Can Anyone tell me where I did wrong????
My Queue class is working like a Stack, not a Queue. If I enqueue 1, 2, and 3 (in that order) and then dequeue, it removes the 3, not the 1.
Thank you in advance!
This a Circular Array, and here my code:
import java.util.NoSuchElementException;
public class Queue implements QueueADT
{
private int front = 0;
private int back = 0;
private int [] a;
private int size = 10;
public Queue(){
a = new int [10];
}
public Queue(int size){
a = new int [size];
this.size = size;
}
//enqueue - adds an element to the back of the queue
public void enqueue(int element){
if(((back+1) - front) == -1 || ((back+1) - front) == (a.length - 1))
resizeArray();
if (back == a.length - 1)
back = 0;
a[back++] = element;
}
//dequeue - removes and returns the element from the
//front of the queue
public int dequeue(){
if(isEmpty())
throw new NoSuchElementException("Dequeue: Queue is empty");
if (front < back){
front ++;
return a[front-1];
}
else if (front > back){
front--;
return a[front++];
}
return 1;
}
//peek - returns but does not remove the element from
//the front of the queue
public int peek(){
if(isEmpty())
throw new NoSuchElementException("Peek: Queue is empty");
return a[front];
}
//isEmpty - determines if the queue is empty or not
public boolean isEmpty(){
return size() == 0;
}
private void resizeArray()
{
//double the size of the array
int[] newA = new int[a.length * 2];
int x = 0;
while(x < size - 1){
newA[x] = dequeue();
x++;
}
size = size *2;
front = 0;
back = x;
a = newA;
}
//size - returns the number of elements in our Queue
public int size(){
if (front > back ){
return size - (front - (back + 1));}
return back - front + 1;}
//toString - returns a String representation of our Queue
public String toString(){
if(isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
{
String s = "[ ";
//print queue
for(int i = 0; i < size(); i++)
s += a[i] + " ";
//print array
for(int j = size(); j < a.length; j++)
s += "* ";
s += "]";
return s;
}
}
//equals - determines if two queues are equivalent
//i.e. do they have the same elements in the same sequence
//ignoring the structure they are stored in
public boolean equals(Object otherQ){
if(otherQ == null)
return false;
else
{
//Figure out how many interfaces the other guy
//implements
int numIs = otherQ.getClass().getInterfaces().length;
boolean flag = false;
//look at all of the other guys interfaces
//and if he doesn't implement QueueADT, then
//he clearly isn't a Queue and we return false
for(int i = 0; i < numIs; i++)
{
if(this.getClass().getInterfaces()[0] ==
otherQ.getClass().getInterfaces()[i])
{
flag = true;
}
}
if(!flag)
return false;
else //we know that the other guy exists and
//we know that he implements QueueADT
{
QueueADT q = (QueueADT)otherQ;
if(this.size() != q.size())
return false;
else
{
boolean queuesEqual = true;
for(int i = 0; i < this.size(); i++)
{
int ourElement = this.dequeue();
int qElement = q.dequeue();
if(ourElement != qElement)
queuesEqual = false;
this.enqueue(ourElement);
q.enqueue(qElement);
}
//return our final answer
return queuesEqual;
}
}
}
}
public void showInnerWorkings(){
System.out.print("[");
for (int i = 0; i < this.a.length; i++)
System.out.print(this.a[i] +
(i != this.a.length - 1 ? ", " : ""));
System.out.println("]");
}
}
Your queue does not behave like a stack. It behaves like a buggy queue.
public static void main(String[] args) {
Queue q = new Queue();
q.enqueue(7);
q.enqueue(8);
q.enqueue(9);
System.out.println(q.dequeue()); // 7 (ok)
System.out.println(q.dequeue()); // 8 (ok)
System.out.println(q.dequeue()); // 9 (ok)
System.out.println(q.dequeue()); // 1 (bug)
System.out.println(q.dequeue()); // 1 (bug)
System.out.println(q.dequeue()); // 1 (bug)
}
Where is your code that made you conclude it was removing the 3 on the first call to dequeue?
Your Queue is seriously buggy.
Here is a start of a Unit Test which tests your Queue and shows some of the bugs.
import org.junit.Test;
import java.util.NoSuchElementException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class QueueTest {
final Queue queue = new Queue();
#Test
public void givenNewQueue_thenSizeIsZero() {
assertEquals(0, queue.size());
}
#Test
public void givenNewQueue_thenIsEmpty() {
assertTrue(queue.isEmpty());
}
#Test(expected = NoSuchElementException.class)
public void givenNewQueue_whenDequeing_thenThrowsException() {
queue.dequeue();
}
#Test
public void givenNewQueue_whenQueueingElement_thenIsNotEmpty() {
queue.enqueue(0xA113);
assertFalse(queue.isEmpty());
}
#Test
public void givenNewQueue_whenQueueingElement_thenSizeIsOne() {
queue.enqueue(0xA113);
assertEquals(1, queue.size());
}
#Test
public void givenNewQueue_whenQueueingElement_thenReturnsElement() {
queue.enqueue(0xA113);
assertEquals(0xA113, queue.dequeue());
}
}
Here is the beginning of my code from my sub class RECORD.
class Record {
private int shares;
private int pricePerShare;
// constructor
Record(int sharesNewValue, int pricePerShareNewValue) {
shares = sharesNewValue;
pricePerShare = pricePerShareNewValue;
}
// inspectors
public int getShares() {
return shares;
}
public int getPricePerShare() {
return pricePerShare;
}
// modifiers
public void setShares(int sharesNewValue) {
shares = sharesNewValue;
}
public void setPricePerShare(int pricePerShareNewValue) {
pricePerShare = pricePerShareNewValue;
}
}
And I want to access the value of shares in my main method that is in a different class.I have the RECORD class linked to another subclass named QUEUE. And in my main method, I have a link to QUEUE with this:
class Lab04a {
public static Queue Q = new Queue();
}
Later on in the code, I need to subtract an int value from the SHARES variable in the Record class, but because that is of type Record, I have no clue how to do this!
I'm not sure if I was clear enough when explaining this, should you have any further questions I'll be more than happy to reply.
Thank you.
Due to my inability to coherently state what I'm trying to accomplish in this lab assignment, I'll just post my other two classes in their entirety:
class Queue {
private int count; // number of elements in the queue
private int head; // index of head element, -1 if queue is empty
private int tail; // index of tail element, -1 if queue is empty
private int MAXSIZE = 1; // Physical size of the queue. DO NOT CHANGE!
private Record[] array; // circular array to store the elements of the queue
// constructor
Queue() {
count = 0;
head = -1;
tail = -1;
array = new Record[MAXSIZE];
}
// inspectors
public boolean empty() {
// Returns true if the queue is empty. Otherwise returns false.
return (count != 0);
}
public int size() {
// Returns the number of elements in the queue
return count;
}
public Record front(){
// Returns the head element of the queue if the queue is not empty.
// Otherwise returns a Record with its data parts set to -1.
if (count == 0)
return new Record(-1, -1);
else
return array[head];
}
public Record rear(){
// Returns the tail element of the queue if the queue is not empty.
// Otherwise returns a Record with its data parts set to -1.
if (count ==0)
return new Record(-1, -1);
else
return array[tail];
}
public String toString() {
// Returns the elements of the queue
String str = "< ";
int h = head;
for (int i = 0; i < count; i++){
str += "(" + array[h].getShares() + ", " + array[h].getPricePerShare() + ") ";
h = (h+1) % MAXSIZE;
}
str += ">";
return str;
}
// modifiers
public boolean dequeue() {
// Removes the head element of the queue.
if (count == 0)
return false;
if (count == 1) {
count = 0;
head = -1;
tail = -1;
}
if (count > 1){
head = (head + 1) % MAXSIZE;
count--;
}
return true;
}
public void enqueue(Record element) {
// Enqueues element to the tail of the queue.
//if max size is reached, it doubles the size to allow for more values
if (count == MAXSIZE) {
Record[] array2 = new Record[MAXSIZE * 2];
for (int i = 0; i < count; i++) {
array2[i] = array[i];
}//closes for loop
array = array2;
MAXSIZE *= 2;
}
tail = (tail + 1) % MAXSIZE;
array[tail] = element;
if (count == 0)
head = tail;
count++;
}//close enqueue method
}//closes class
And then here is my MAIN parent class:
class Lab04a {
public static Queue Q = new Queue(); // creates global object
public static Record R = Record;
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int option, buyPrice, buyShares, sellPrice, sellShares, totalShares, totalValues, totalSellPrice;
option = 0;
totalShares = 0;
totalValues = 0;
Queue Q2 = Q;
while (option != 3) {
System.out.print("Enter option (1:buy, 2:sell, 3:quit): ");
option = scan.nextInt();
if (option == 1) {
System.out.print("Enter shares to buy and price per share: ");
buyShares = scan.nextInt();
buyPrice = scan.nextInt();
Record r = new Record(buyShares, buyPrice);
Q.enqueue(r);
totalShares = totalShares + buyShares;
totalValues = totalValues + (buyShares * buyPrice);
}// ends if
if (option == 2) {
System.out.print("Enter shares to sell and price per share: ");
sellShares = scan.nextInt();
sellPrice = scan.nextInt();
totalSellPrice = sellPrice * sellShares;
if (sellShares > totalShares) {
System.out.println("You do not own enough shares for this sale.");
}
for (int i = sellShares; i > 0; ) {
if (sellShares == Q.front().getShares()) {
i -= Q.front().getShares();
Q.dequeue();
}
if (sellShares < Q.front().getShares()){
Record minus;
minus = Q.front() - sellShares;
Q.front().setShares(minus);
Q.front().setShares(Q.front().getShares());
i -= sellShares;
}
}
}// ends if
// Prints content of Queue
System.out.println("Queue: " + Q.toString());
System.out.println("Total Shares: " + totalShares);
System.out.println("Total Shares Value: $" + totalValues);
System.out.println();
}// ends while loop
System.out.println(Q.toString());
}// ends main method
}
If I understand your question, you can add accessor and mutator methods (or getters and setters)
private int shares;
private int pricePerShare;
public int getShares() {
return shares;
}
public void setShares(int shares) {
this.shares = shares;
}
public int getPricePerShare() {
return pricePerShare;
}
public void setPricePerShare(int pricePerShare) {
this.pricePerShare = pricePerShare;
}
Edit
To use it,
Record record = Q.front(); // <-- I assume your Q contains Record(s).
if (record.getShares() >= sellShares) {
record.setShares(record.getShares() - sellShares); // <-- for example
}
Make sure Q.front() is a method that returns a Record.
If that is true, you should be able to use the line
Q.front().setShares(Q.front().getShares()-MINUS_VALUE))
I was required to create a simple queue array implementation with basic methods as enqueue, dequeue, isEmpty, and stuff like that. My only problem is that Im stuck when it comes to the resize method, because if I want to add more values to my queue (with fixed size because is an array) I do not know how to make it work and keep all the values in place.
Everything works just in case you were wondering, the only thing is that doesnt work is my resize (the method wrote in here wasn't the only one I tried).
I'm going to put my main method as well if you want to try it, hope you can help, thanks.
Main Method:
public class MainQueue {
public static void main(String[] args) {
int capacity=10;
Queue<Integer> queue = new Queue<Integer>(capacity);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.enqueue(6);
queue.enqueue(7);
queue.enqueue(8);
queue.enqueue(9);
queue.enqueue(10);
System.out.println("Queue: "+ queue);
//WORKS SO FAR
queue.enqueue(11);
//11 is placed at the beginning of the queue
//instead at the end and my last value is null (?)
Class queue:
import java.util.NoSuchElementException;
public class Queue <E>{
private E[] elements;//array in generic
private int front;//first element or front of the queue
private int back;//last element or back of the queue
private int capacity; //capacity of the queue
private int count; //indicates number of elements currently stored in the queue
#SuppressWarnings("unchecked")
public Queue(int size)
{
capacity = size;
count = 0;
back = size-1;
front = 0;
elements =(E []) new Object[size]; //array empty
}
//Returns true if the queue is empty or false
public boolean isEmpty()
{
return count==0;//means its true
}
//Add elements to the queue
public void enqueue(E item)
{
if(count == capacity)
{
resize(capacity*2);
// System.out.println("Queue is full");
}
back =(back+1) % capacity; //example back=(0+1)%10=1
elements[back]=item;
//elements[0]=0
//item=elements[count];
count++;
}
//Public resize
public void resize(int reSize){
E[] tmp = (E[]) new Object[reSize];
int current = front;
for (int i = 0; i < count; i++)
{
tmp[i] = elements[current];
current = (current + 1) % count;
}
elements = tmp;
}
//Dequeue method to remove head
public E dequeue()
{
if(isEmpty())
throw new NoSuchElementException("Dequeue: Queue is empty");
else
{
count--;
for(int x = 1; x <= count; x++)
{
elements[x-1] = elements[x];
}
capacity--;
return (E) elements;
}
}
//peek the first element
public E peek()
{
if(isEmpty())
{
throw new NoSuchElementException("Peek: Queue is empty");
}
else
return elements[front];
}
//Print queue as string
public String toString()
{
if(isEmpty()) {
System.out.println("Queue is empty.");
//throw new NoSuchElementException("Queue is empty");
}
String s = "[";
for(int i = 0; i <count; i++)
{
if(i != 0)
s += ", ";
s = s + elements[i];// [value1,value2,....]
}
s +="]";
return s;
}
public void delete() { //Delete everything
count = 0;
}
}
you forgot to update stuff when resizing:
front, capacity and back .
public void resize(int reSize){
E[] tmp = (E[]) new Object[reSize];
int current = front;
for (int i = 0; i < count; i++)
{
tmp[i] = elements[current];
current = (current + 1) % count;
}
elements = tmp;
front = 0;
back = count-1;
capacity=reSize;
}
You have few mistakes in resizing when enqueing item which expand queue.
in resize algorithm
current = (current + 1) % count; should be (current + 1) % capacity
You have to change capacity value in resize function
capacity = resize;
Why are you changing capacity when dequeing?
My issue is more semantic than functional, As the code does seem to implement the deQueue and enQueue functions correctly.
The reheapDown and reheapUp functions are being used incorrectly, And i believe the issue lies in my heap function
package priqueue;
public class Hosheap{
private Patient[] elements;
private int numElements;
public Hosheap(int maxSize)
{
elements= new Patient[maxSize];
numElements=maxSize;
}
public void ReheapDown(int root,int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild=root*2+1;
rightChild=root*2+2;
if (leftChild<=bottom)
{
if(leftChild==bottom)
maxChild=leftChild;
else
{
if(elements[leftChild].getPriority() <= elements[rightChild].getPriority())
maxChild=rightChild;
else
maxChild=leftChild;
}
if(elements[root].getPriority()<elements[maxChild].getPriority())
{
Swap(root,maxChild);
ReheapDown(maxChild,bottom);
}
}
}
public void ReheapUp(int root,int bottom)
{
int parent;
if(bottom>root)
{
parent=(bottom-1)/2;
if(elements[parent].getPriority()<elements[bottom].getPriority())
{
Swap(parent,bottom);
ReheapUp(root,parent);
}
}
}
public void Swap(int Pos1, int Pos2)
{
Patient temp;
temp = elements[Pos1];
elements[Pos1]=elements[Pos2];
elements[Pos2]=temp;
}
public Patient getElement(int e)
{
return elements[e];
}
public void setElement(Patient p, int n)
{
elements[n]=p;
}
}
The idea is to rearrange a simple priority queue system so when a patient object is removed, ReheapUp or down correctly rearranges the queue, Which the code does not accomplish. Should i also include the priority queue code, Or is this already too lengthy?
I am using NetBeans IDE 6.0.1, If that helps.
Depending on your usage requirements, the answer relating to TreeSets will most probably do what you want.
However if you really need a queue, as opposed to a sorted collection, then the inbuilt PriorityQueue may be of use.
Not exactly answering your question, but with Java you may want to look into the built-in Collection classes. You can get priority queue behavior but using a TreeSet (a type of ordered-set) and implementing a custom Comparator for Patient instances. Depending what you're trying to achieve, this may be preferable. It would look something like this:
In Patient.java ...
class Patient implements Comparator {
...
public int compareTo(Patient other) {
return getPriority() > other.getPriority() ? 1 : 0;
}
Then in the place you want to use the queue
Set<Patient> queue = new TreeSet<Patient>();
queue.add(p1);
queue.add(p2);
//traverse in order of priority
for(Patient p : queue) {
doStuff();
}
Here is a simple implementation of a PriorityHeap. I coded it up pretty quick so it may have some flaws but I have implemented the pushUp() and pushDown() logic.
import java.util.Random;
public class Heap {
private Double[] data;
private int lastItem;
public Heap(int initialSize) {
// to simplify child/parent math leave the first index empty
// and use a lastItem that gives us the size
data = new Double[initialSize];
lastItem = 0;
}
public void insert(Double d) {
// double size if needed
// should have a matching shrink but this is example code
if (lastItem + 1 >= data.length) {
Double[] doubled = new Double[data.length * 2];
System.arraycopy(data, 0, doubled, 0, data.length);
data = doubled;
}
data[lastItem + 1] = d;
lastItem++;
pushUp(lastItem);
}
public void pushDown(int index) {
if (lastItem > 1) {
int leftChildIndex = index * 2;
int rightChildIndex = leftChildIndex + 1;
// assume that neither child will dominate (in priority)
// the item at index
int indexToPromote = index;
// there may not be a left child
if (leftChildIndex <= lastItem) {
Double leftChild = data[leftChildIndex];
Double tmp = data[index];
if (tmp.compareTo(leftChild) < 0) {
indexToPromote = leftChildIndex;
}
// there might not be a right child
if (rightChildIndex <= lastItem) {
Double rightChild = data[rightChildIndex];
tmp = data[indexToPromote];
if (tmp.compareTo(rightChild) < 0) {
indexToPromote = rightChildIndex;
}
}
}
// did either child dominate the item at index
// if so swap and push down again
if (indexToPromote != index) {
swap(index, indexToPromote);
pushDown(indexToPromote);
}
}
}
public void pushUp(int index) {
if (index > 1) {
// equivalent to floor((double)index/2.0d);
// if item at index is greater than its parent
// push the item up to until if finds a home
int parentIndex = index >>> 1;
Double parent = data[parentIndex];
Double item = data[index];
if (item.compareTo(parent) > 0) {
swap(parentIndex, index);
pushUp(parentIndex);
}
}
}
public Double removeTop() {
// assume size is zero then examine other cases
Double top = null;
if (lastItem > 1) {
// save the top item and take the bottom item and place it
// at the top the push the new top item down until it
// finds a home
top = data[1];
Double bottom = data[lastItem];
lastItem--;
data[1] = bottom;
pushDown(1);
} else if (lastItem == 1) {
top = data[1];
lastItem--;
}
return top;
}
public int size() {
return lastItem;
}
private void swap(int index1, int index2) {
Double temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}
public static void main(String[] args) {
Heap heap = new Heap(4);
Random r = new Random();
for (int i = 0; i < 100000; i++) {
Double d = Double.valueOf(r.nextDouble() * 100.0d);
heap.insert(d);
}
double max = Double.MAX_VALUE;
while (heap.size() > 0) {
Double top = heap.removeTop();
if (top.doubleValue() > max) {
System.out.println("bad ordering...");
}
max = top.doubleValue();
System.out.println(max);
}
System.out.println("done...");
}
}