Priority queue with dynamic item priorities

前端 未结 5 488
抹茶落季
抹茶落季 2020-12-10 02:21

I need to implement a priority queue where the priority of an item in the queue can change and the queue adjusts itself so that items are always removed in the correct order

5条回答
  •  眼角桃花
    2020-12-10 02:41

    A standard binary heap supports 5 operations (the example below assume a max heap):

    * find-max: return the maximum node of the heap
    * delete-max: removing the root node of the heap
    * increase-key: updating a key within the heap
    * insert: adding a new key to the heap
    * merge: joining two heaps to form a valid new heap containing all the elements of both.
    

    As you can see, in a max heap, you can increase an arbitrary key. In a min heap you can decrease an arbitrary key. You can't change keys both ways unfortunately, but will this do? If you need to change keys both ways then you might want to think about using a a min-max-heap.

提交回复
热议问题