top of page
Writer's pictureTravis Martin

Java Min and Max heap

Just posting some code on building a min and max heap, I will use an abstract class since both of these classes will contain similar methods and attributes that can be inherited. All code posted on my github (https://github.com/travism26/)


Abstract Base class

This is just the base class since the min heap and max heap are identical minus the two functions: fixHeapBelow, fixHeapAbove (operators < >). The child classes are super simple only contain the two methods.

Min Heap

This is the min heap implementation extending the parent heap class, I added in a method to verify that the heap is indeed a min heap. I also kept all the comments just to try and explain what is going on.

Max Heap

I left the comments out in this class since I explain most things in the minHeap implementation, the only difference is the comparison <, >.

Running both class' and output

Wire everything together and execute it.



41 views0 comments

댓글


bottom of page