• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

I have written the code for Min heap. Is this code correct to implement Min heap?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please verify the following code is correct for inserting, extracting, traversing elements in min heap?


 
Marshal
Posts: 65114
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for using code tags, but you didn't quite get them right. Don't worry: I have corrected that, and doesn't your code look better Remind yourself how the code button works.

No, your code isn't correct, I am afraid. Even without knowing what you are trying to achieve, I can see repeated code there. Don't swap two array elements like that. Create a utility class with a swapTwoElementsInArray() method, or similar.
Please explain what you are trying to achieve. Without knowing what you are trying to do (see second link above) we cannot tell whether you have done it.
 
Divyadharshini Karthikeyan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie
Thank  you for the links, especially the second link I feel it's written for me.
 
Campbell Ritchie
Marshal
Posts: 65114
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That link was written for many many people. If you don't know what you want to do, you won't do it. Please tell us what you want to do, so we can help you.
 
Saloon Keeper
Posts: 10433
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Make your classes final.
  • Why does your heap have a default capacity of 11?
  • Why is the size field called that way when it really keeps track of the capacity?
  • Why use a separate field to keep track of capacity at all, when you can just look at the length of the backing array?
  • What if I actually want to insert Integer.MIN_VALUE in the heap? How does the heap distinguish between it and an empty cell?
  • If I insert an element into a full heap, how will I know that nothing happened?
  • Why do your two heapify methods not use any uppercase letters in their name? Why does the SwapTwoElements method name start with an uppercase letter?
  • Why is the heapifybottomtotop() method public?
  • In your heapifybottomtotop() method, why do you perform a return in an else-clause, when all you have to do is add the last statement of the method to the if-clause?
  • Why do all your inspection methods like peek() and size() print the inspected properties, instead of returning them? Now there's no way for a client of your class to get the size programmatically.
  • Method names should be verbs: traverse() not traversal().
  • You might want to wrap the logic for getting the index of a child or a parent of a node into separate methods.

  • Personally I would make the class generic, let it extend AbstractQueue and base its implementation on an ArrayList, not an array:

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!