Reputation: 51
I just picked up "Introduction to algorithms", and I started implementing heaps and heapsort algorithm in c#.
Implementing a function that constructs a min/max heap from an array of doubles, i noticed that the constructed heap has some interesting properties.
A built minimum heap can be read left to right, and top to bottom (from root to leaves, by levels), and it will be sorted.
Is this a property of a minheap, or I am just unable to get a case which this property does not apply. Max heap doesnt work this way, atleast what i am getting here.
Output:
2345 7 34 6 3 5 4 5 1 2 3 2 1 3 1 3 2 1 (maxheap)
1 1 1 1 2 2 2 3 3 3 3 4 5 5 6 7 34 2345 (minheap)
Thanks for any responses in advance!
Upvotes: 1
Views: 1775
Reputation: 36
To make the implementation of the heap more robust, people usually call the two operations which you can make to a node: sift
and percolate
sift
is bringing the node as much down as possible on the tree by switching the node with the best of the two sons you find.
there is a possible implementation of the sift
procedure
public void sift (int i)
{
while (i != 0)
{
if (compare(heapData[i], parentValue(i)))
{
switchValuesAtIndexes(parentIndex(i), i);
i = parentIndex(i);
}
else
break;
}
}
percolate
is bringing the node as much up as possible on the tree by switching the node with the parent.
public void percolate (int i)
{
while (true)
{
if (indexExists(leftIndex(i)) && (!indexExists(rightIndex(i)) && compare(leftValue(i), heapData[i]) || indexExists(rightIndex(i)) && compare(leftValue(i), rightValue(i)) && compare (leftValue (i), heapData[i])))
{
switchValuesAtIndexes(leftIndex(i), i);
i = leftIndex(i);
}
else
if (indexExists(rightIndex(i)) && (compare (rightValue(i), leftValue(i)) && compare (rightValue(i), heapData[i])))
{
switchValuesAtIndexes(rightIndex(i), i);
i = rightIndex(i);
}
else
break;
}
}
To build the heap, you will need to apply sift
or percolate
(or both) to each element of the array
To sort the heap you constructed you will have to print the first element (which is the best of all heap accorting to a comparer which you define, min, max, etc)
Then you will have to "overwrite" the first element with the last and delete the last and then you'll have to apply sift
on the first element to bring it to it's right position in the heap. And you repeat until there's no element left in the heap
Upvotes: 0
Reputation: 2895
Heaps only have relation between Parent node and their respective child nodes.
There is no relation between nodes at same level.
For Min Heaps: Value of Parent node <= Value of its child nodes
For Max Heaps: Value of Parent node >= Value of its child nodes
So its not necessary for Min Heap to be sorted when travelled Top-to-Bottom,Left-to-Right. Its just a coincidence in your case.
For eg: Consider this sequence: 1, 3, 2, 5, 4, 10, 9
These are in MinHeapify order but clearly not in sorted order.
Visit this Interactive Heap Link or this link for proper visualisation on how heap is built.
Upvotes: 6