Write a solution class to implement a heap using Java’s ArrayList (Links to an external site.). This is a max heap, which means the highest priority element is stored at the root level in a complete or full binary tree. Natural order is used in ranking the priority of the elements in the heap, namely, 6 has higher priority than 5, which has higher priority than 2, so on and so forth.
You are responsible for implementing the following solution methods:
- void enqueue(int element): this method enqueues an integer element into heap.
- void dequeue(): this method dequeues the highest priority element from heap.
- void reheapUp(int element): this method is called by enqueue(element); it either iteratively or recursively shifts the element up the tree to “reheap” it.
- void reheapDown(int element): this method is called by dequeue(); it either iteratively or recursively shifts the element down the tree to “reheap” it.
Your Java implementation may start with the following template.


0 comments