implement min heap in java code example
Example 1: 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
Example 2: create a min heap in java using priority queue
int arr[]={1,2,1,3,3,5,7};
PriorityQueue<Integer> a=new PriorityQueue<>();
for(int i:arr){
a.add(i);
}
while(!a.isEmpty())
System.out.println(a.poll());
Example 3: min max heap java
PriorityQueue<Integer> prq = new PriorityQueue<>();
PriorityQueue<Integer> prq = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> prq = new PriorityQueue<>((a, b) -> b - a);