aspose file tools
The moose likes Beginning Java and the fly likes Convert max heap to min Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Convert max heap to min" Watch "Convert max heap to min" New topic
Author

Convert max heap to min

mike fusc
Greenhorn

Joined: Mar 07, 2010
Posts: 15
Im reviewing for a final and I came across a review problem that says to convert a max string heap stored in an array to a min heap.

Now i know the answer to the question because it's not asking to code, but how would one code this in O(n) time. Basically I am thinking since the children are (2k+1) and (2k+2) in the heap you can use that to your advantage, but I'm lost other then calling SiftUp to traverse up the tree at each node.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

can't you ignore the fact that you have a heap (of any kind), traverse through the elements one at a time, and insert them into a new min-heap?

I don't recall if building a heap from scratch is O(n) or not, so I may be way wrong...


Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Convert max heap to min
 
Similar Threads
Question3 for IBM Test 340
app on WS4.0 not releasing memory
JVM memory allocation
Math.random().....any formula to adjust required range?
Nested if statements