Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing tail element of priority queue

How can I remove the tail element of a priority queue? I am trying to implement beam search using a priority queue and once the priority queue is full, I want to remove the last element(the element with the least priority).

Thanks!

like image 461
P R Avatar asked Feb 27 '13 16:02

P R


1 Answers

No easy way. Copy elements from original to new except the last.

PriorityQueue removelast(PriorityQueue pq)
{

    PriorityQueue pqnew = new PriorityQueue();

    while(pq.size() > 1)
    {
        pqnew.add(pq.poll());
    }

    pq.clear();
    return pqnew;
}

called as

pq = removelast(pq);
like image 88
user93353 Avatar answered Sep 17 '22 20:09

user93353