-2

In a binary Heap of 100 elements time taken to find the 99th element?

or in a binary heap on "n" elements, time taken to find (n-1)th element?

=========================================================================

My take is O(1) as you can simply go to 99th cell of array.

Geeklovenerds
  • 327
  • 6
  • 21

1 Answers1

3

In a binary min heap of n elements, the (n-1)th element (i.e. the next to largest) will either be on the last level of the tree, or next-to-last level of the heap. In a full heap, there are (n+1)/2 items on the last level, and (n+1)/4 items on the next-to-last level.

It's an O(n) operation because you potentially have to search (3*(n+1))/4 elements.

It's not O(1) because a heap is not necessarily sorted. You can't guarantee that the 99th smallest item in a heap of 100 items is at the 99th position in the array. Consider these two min heaps:

        1                   1
    2       3           5       2    
  4   5   6   7       6   7   3   4

Both of those are valid min heaps, but in the second one, the second smallest item is at position 3 in the array.

Update

Actually, in a full binary heap, the next-to-last item has to be on the leaf level. In a not-full heap, it can be anywhere on the leaf level, or on the level, or on the level above. Consider, for example:

      1
   2     6            
  3 5   7

About the (3*(n+1)/2) question:

The next-to-smallest can be anywhere on the last level (4 nodes) or the next-to-last level (2 nodes). Total of 6 possible nodes. That works out to (7+1)/2 nodes for the last level, and (7+1)/4 for the next-to-last level. Simplifying, that's (3*(7+1))/4.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Thank you so much for always answering my questions, but I'm only asking about 99the element, not the 2nd smallest element or any smallest element, simply the 99th element and Sir, why you took only Min heap. – Geeklovenerds Jun 20 '18 at 15:50
  • @MicroLegions The 99th element in a binary heap of 100 elements is the 2nd smallest. Depending on the size of the heap, whether it's a min-heap or a max-heap, the 99th element will be somewhere within the first 7 levels of the heap. So worst case is that you'll have to search 127 items to find it. – Jim Mischel Jun 20 '18 at 16:47
  • @MicroLegions: see my answer to a similar question here: https://stackoverflow.com/a/49642419/56778 – Jim Mischel Jun 20 '18 at 18:50
  • @JimMischel great answer. Micro_Leg you must clearify what do you mean by 99th element,whether it is min/max heap.otherwise the answer given is perfect – laura Jun 21 '18 at 06:55
  • @JimMischel **It's an O(n) operation because you potentially have to search (3*(n+1))/4 elements.** Sir, I get that, we've to search till the last but 1 level, i.e till second last level of heap or one level above leaf nodes and we've (n+1)/4 nodes/elements there, but why're you multiplying by 3? I took a min heap of 7 elements and at 2nd level I get (7+1)/4 = 2 nodes, but now in above level there's only one node i.e root. So, where's 3 coming from in search? – Geeklovenerds Jun 21 '18 at 07:07
  • @JimMischel Sir, thank you so very much! You've been so kind to share your knowledge. I look up to you! :) – Geeklovenerds Jun 21 '18 at 14:16