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

后端 未结 5 1023
说谎
说谎 2020-11-22 08:43

This is straight from the Java Docs:

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

5条回答
  •  天涯浪人
    2020-11-22 09:30

    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.

提交回复
热议问题