-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Comments
They have a writing error, the correct one is "if" and not 'it'. |
You need to show me the poll method in priorityQueue? I think issue is occured due to that thing. |
Is this issue is still open ? |
The priority queue extends the heap Class.
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.
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:
Output: instead of 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. |
now it poll out
1, 4, 3, 2
, expected:1,2,3,4
The text was updated successfully, but these errors were encountered: