The built-in iterator for java’s PriorityQueue does not traverse the data structure in any particular order. Why?

Because the underlying data structure doesn’t support it. A binary heap is only partially ordered, with the smallest element at the root. When you remove that, the heap is reordered so that the next smallest element is at the root. There is no efficient ordered traversal algorithm so none is provided in Java.

Leave a Comment