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

后端 未结 5 1052
说谎
说谎 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:16

    PriorityQueues are implemented using binary heap. A heap is not a sorted structure and it is partially ordered. Each element has a “priority” associated with it. Using a heap to implement a priority queue, it will always have the element of highest priority in the root node of the heap. so in a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. Heap is updated after each removal of elements to maintain the heap property

提交回复
热议问题