Priority Queue with a find function - Fastest Implementation

前端 未结 7 2148
一向
一向 2021-02-07 13:24

I am looking at implementing a priority queue with an added requirement, a find/search function which will tell whether an item is anywhere within the queue. So the functions wi

7条回答
  •  太阳男子
    2021-02-07 13:56

    If you need the benefits of more than one data structure then you can use them in composition. For example, if you need the benefits of a priority queue and a binary search tree then make your desired actions on both of them.

    If it's insert then insert the element to both of them.

    If it's find then you can find the element using the binary search tree and if it was found then continue on to find it in the priority queue.

    If it's min then remove it first from the priority queue and now that you know which element it is then you can remove it from the binary search tree.

    if it's del then first find it in the binary search tree and remove it then continue to find it in the priority queue and remove it from there too.

    It is assumed that the nodes of the binary tree and the nodes of the priority queue are pointers to your elements.

提交回复
热议问题