Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a sorted array a min-heap? What's the minimum value of a max-heap?

Tags:

I have studied min-heaps and max-heaps, and I have a couple of questions:

  1. Is a sorted array a min-heap?
  2. What is the minimum value of a max-heap?
like image 961
user355002 Avatar asked Jun 24 '10 19:06

user355002


People also ask

How do you find the minimum value in max heap?

Minimum element in a max heap in C++.In max heap value of the root node is always greater than its childer. Because of this property, we can conclude that value will be present in one of the leaf nodes. If heap contains n nodes then there will be ceil(n/2) leaves.

Is a sorted array a min-heap?

Is a sorted array a min-heap? Yes, if you're using the typical array-stored heap convention.

What are the minimum and maximum number of elements in the heap?

(CLRS 6.1-1) What are the minimum and maximum number of elements in a heap of height h? Solution: The minimum number of elements is 2h and the maximum number of elements is 2h+1 − 1. 2.

What is heap explain min-heap and max heap?

A heap is a tree-based data structure that allows access to the minimum and maximum element in the tree in constant time. The constant time taken is Big O(1). This is regardless of the data stored in the heap. There are two types of heaps: Min-heap and Max-heap.


2 Answers

An array sorted from lowest to highest is a min-heap when using the array-based heap implementation. The min-heap property that the parent node value is less than or equal to it's child nodes (2i + 1 and 2i + 2, using zero-based arrays) holds for all nodes that have children.

The minimum value of a max heap is in one of the leaf nodes, but you don't know which. Since the minimum node cannot, by definition, have any child nodes, it must be a leaf. The heap property, however, does not specify how leaf nodes compare with each other, only with their parent.

like image 185
tvanfosson Avatar answered Nov 10 '22 05:11

tvanfosson


Is a sorted array a min-heap?

Yes, if you're using the typical array-stored heap convention.

Where is the minimum value of a max-heap?

At one of the leaves. Which exactly is undefined.

like image 31
Ben S Avatar answered Nov 10 '22 05:11

Ben S