Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

priorityQueue does not poll out as expected #1070

Open
zonglinlee opened this issue Sep 6, 2023 · 4 comments
Open

priorityQueue does not poll out as expected #1070

zonglinlee opened this issue Sep 6, 2023 · 4 comments

Comments

@zonglinlee
Copy link

describe('priorityQueue Test:', () => {
 it('should return correct priorityQueue results:', () => {
    const pq = new PriorityQueue();
    pq.add(1);
    pq.add(2);
    pq.add(3);
    pq.add(4);
    expect(pq.toString()).toBe('1,2,3,4');
    expect(pq.poll()).toBe(1);
    expect(pq.poll()).toBe(2);
    expect(pq.poll()).toBe(3);
    expect(pq.poll()).toBe(4);
  });
});

now it poll out 1, 4, 3, 2 , expected: 1,2,3,4

@stiven104
Copy link

They have a writing error, the correct one is "if" and not 'it'.

@PranjalPatel14
Copy link

You need to show me the poll method in priorityQueue? I think issue is occured due to that thing.

@v-a14
Copy link

v-a14 commented Oct 24, 2023

Is this issue is still open ?

@fahad-ejaz
Copy link

fahad-ejaz commented Nov 18, 2023

The priority queue extends the heap Class.
The poll() method, inherited by the priority queue class, is part of the Heap Class. I have pasted a portion of the code from the method below:

// Move the last element from the end to the head.
   this.heapContainer[0] = this.heapContainer.pop();
   this.heapifyDown();

As you can see, the poll method pushes the last element in the queue to the front. In your case, after the first element is 'polled', the last element, which is 4, will be moved to the front of the queue. The 'heapifyDown' method, then, sorts the queue based on a comparator function. The priority queue class overrides the compactor function of the heap Class to ensure the queue is sorted based on priority of the item and not its value.

this.compare = new Comparator(this.comparePriority.bind(this));

comparePriority(a, b) {
    if (this.priorities.get(a) === this.priorities.get(b)) {
      return 0;
    }
    return this.priorities.get(a) < this.priorities.get(b) ? -1 : 1;
  }

Since the priorities of all the items in the queue are the same, the 'heapifyDown' method does not sort the queue after 4 is moved to the front.

So if you were to log the output of the toString method:

pq.poll()
console.log(pq.toString())

Output:
'4,3,2'

instead of
'2,3,4'

If you are not going to assign priorities to the items in the queue, try using the minHeap data structure. When you use that data structure, the 'heapifydown' method will sort the queue based on the value of the item. You would get the output you were expecting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants