If we know the size of heap we can get the minimum element in O(n);
int findMinFromMaxHeap(int ar[], int n)
{
if ( (n+1) & n == 0)
{
for (i = n>>1; i < n; i++)
min = min > ar[i] ? ar[i] : min;
}
else
{
int x = n, y = 1;
while(x >1)
{
y <<= 1;
x >>= 1;
}
y -= 1;
x = (n - y + 1) / 2;
for (i = y / 2 + x; i < n; i++)
min = min > ar[i] ? ar[i] : min;
}
return min;
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.