java min heap implementation code example

Example 1: heap in java

In Java PriorityQueue can be used as a Heap.

Min Heap
PriorityQueue<Integer> minHeap = new PriorityQueue<>();


Max Heap:
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());

Example 2: use Java NetBeans to write code to implement the list [5, 3, 17, 10, 84, 19, 6, 22, 9] in a Max Heap data structure. For each parent node, display the left and right child of the node.

The Max Heap is 
 PARENT : 52 LEFT CHILD : 21 RIGHT CHILD :23
 PARENT : 21 LEFT CHILD : 15 RIGHT CHILD :13
 PARENT : 23 LEFT CHILD : 7 RIGHT CHILD :16
 PARENT : 15 LEFT CHILD : 5 RIGHT CHILD :9
The max val is 52

Tags:

Cpp Example