0

What is the worst-case time complexity for search in a set backed by a max heap? If you look for the smallest item in the set, that could take order of O(n) time, right? Is there a faster way?

Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
Max Chang
  • 129
  • 1
  • 1
  • 8

1 Answers1

1

Finding the smallest item in a max-heap is an O(n) operation. You could implement a Min-max heap, which will give you O(1) access to the smallest and largest items, while keeping the O(log n) insertion and removal. Note, however, that overall a min-max heap will be a bit slower than a max-heap, due to constant factors.

You could replace the max-heap with a skip list, which would give you O(log n) access to the smallest item. But implementing a skip list is a bit more involved than implementing a binary heap, and likely will consume more memory.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351