Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where can  min heap be a maximum elememt, when all the elements are different?
 
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finding the maximum elememt in min heap when all the elements are different
 
Houssam El
Ranch Hand
Posts: 36
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i merge two minimum-heaps to one heap in efficiency?
public static MinHeap mergeTwoHeaps(MinHeap h1, MinHeap h2){….}
 
Bartender
Posts: 4100
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can show me in code?
 
Piet Souris
Bartender
Posts: 4100
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic