views:

63

answers:

3

Hi

I remembered that heap can be used to search whether an element is in it or not with O(logN) time complexity. But suddenly I can't get the details. I can only find getmin delete add and so on.

Can anybody give a hint?

+1  A: 

I think what you're looking for is a BST (binary search tree).

cosmos
Not helpful if you already have a priority queue and want to check whether it contains a given element.
finnw
A: 

BST can be searched by O(logN), heap is used for sorting, not for searching.

Andrey
+2  A: 

You need to search through every element in the heap in order to determine if an element is inside.

One optimization is possible, though (we assume a max heap here). If you have reached a node with a lower value than the element you are searching for, you don't need to search further from that node. However, even with this optimization, search is still O(N) (need to check N/2 nodes in average).

stubbscroll