why heap data structure?

Mar 2, 2014 at 12:00pm
1
2
3
4
5
                         6
                     /       \
                     17       14
                   /    \    /   \    
                 68      98  18  15
I am going through heap data structure and it says it should have two properties:all nodes should have two child except bottom level and parent values should be less than or equal to children(min heap) or parent value should be more then children(max heap).

I constructed a mini heap(or max heap I am not sure :)) above.

1
2
If I try to read the above it would read:
6 , 17 ,14 ,68 ,98 ,18 ,15 which is absolute crap.
These numbers don't follow any order(either ascending or descending)
My doubt is what is the use in constructing these heaps because these structures are not implying any order for their elements.How can we create priority queues out of such orderless data structures?
Mar 2, 2014 at 12:17pm
Did you try the wiki?
http://en.wikipedia.org/wiki/Heap_%28data_structure%29
Basically in heap amortized time of extracting or adding an element is O(log n) which is better that O(1) of adding + O(n) of finding and extracting min element of the array. However on small sequences heap can be slower than naive array approach. Also in special cases (when new element are likely to be firs in priority) it is possible to outperform it even with arrays (or vectors) using that information.
Heaps are fast when you expect wide range of new element sithout any similar traits or as universal solution. In really speed critical cases, you might be able to get better results by using a data structure better suited to current case.
Last edited on Mar 2, 2014 at 12:18pm
Mar 2, 2014 at 12:20pm
> (or max heap I am not sure :))
rtfm

> How can we create priority queues out of such orderless data structures?
There are two operations that you can perform: add another value, or remove the top. After any operation the heap property must hold.
So you could get the sorted sequence by simple removing the top until the heap is empty.
Mar 2, 2014 at 7:38pm
Thanks @Minipaa

@ne555

very precise and clear answer..thanks
Topic archived. No new replies allowed.