PriorityQueue/Heap Update

前端 未结 6 733
礼貌的吻别
礼貌的吻别 2020-12-05 07:07

Does Java have an easy way to reevaluate a heap once the priority of an object in a PriorityQueue has changed? I can\'t find any sign of it in Javadoc, but the

6条回答
  •  囚心锁ツ
    2020-12-05 07:35

    PriorityQueue has the heapify method which re-sorts the entire heap, the fixUp method, which promotes an element of higher priority up the heap, and the fixDown method, which pushes an element of lower priority down the heap. Unfortunately, all of these methods are private, so you can't use them.

    I'd consider using the Observer pattern so that a contained element can tell the Queue that its priority has changed, and the Queue can then do something like fixUp or fixDown depending on if the priority increased or decreased respectively.

提交回复
热议问题