This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Paul Clapham
• Bear Bibeault
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
• Carey Brown
Bartenders:
• salvin francis
• Scott Selikoff
• fred rosenberger

# finding the maximum element in min heap when all the elements are different

Greenhorn
Posts: 12

Where can  min heap be a maximum elememt, when all the elements are different?

Ranch Hand
Posts: 37
1

eden gilad wrote:
Where can  min heap be a maximum elememt, when all the elements are different?

do you mean finding max element either array or list ?
please explain more

eden gilad
Greenhorn
Posts: 12
finding the maximum elememt in min heap when all the elements are different

Houssam El
Ranch Hand
Posts: 37
1

eden gilad wrote:finding the maximum elememt in min heap when all the elements are different

Voila tiny code will help you to solid your understanding

eden gilad
Greenhorn
Posts: 12
how do i merge two minimum-heaps to one heap in efficiency?
public static MinHeap mergeTwoHeaps(MinHeap h1, MinHeap h2){….}

Bartender
Posts: 4109
156
You can find the maximum in a min heap by looking at the bottom layer, or when that layer is not full, the layer above. So calculate how many elements maximal to investigate and start searching from the end.

eden gilad
Greenhorn
Posts: 12
You can show me in code?

Piet Souris
Bartender
Posts: 4109
156
To be frank, I do not have the code.

But here is an example of what I mean. If the size of the heap is N, then the number of layers in use = (int) (Log2(N) + 1). For instance, say we have a size of 12. Then the number of layers in use is 4, with the fourth layer containing 5 elements (the number of elements in the first three layers is 2^3 - 1 = 7). Now, these 5 elements have 3 parents in the layer above, so we need to check that last element of that above layer as well, meaning we have to check the last 6 elements. Using an ArrayList as structure is easier than using an array for this.

But if this sounds pretty complex: Houssams method is much easier!

 What? What, what, what? What what tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply