Heap is a data structure that is a complete binary tree with the following characteristics:
- Min-heap: The value of the parent node is less than or equal to the value of the child node;
- Max-heap: The value of the parent node is greater than or equal to the value of the child node;
public class Minandmaxheap {//Dagen and small Gan implementations//Priority queues are implemented by default for small Gan static class Maxheapcomparator implements C omparator<integer> {@Override public int compare (integer o1, integer o2) {return o2-o1; }} public static void Main (string[] args) {int[] Arrforheap = {3, 5, 2, 7, 0, 1, 6, 4}; queue<integer> minheap = new priorityqueue<> (); Large root heap implementation queue<integer> Maxheap = new priorityqueue<> (new Maxheapcomparator ()); for (int i =0;i<arrforheap.length;i++) {Minheap.add (arrforheap[i]); Maxheap.add (Arrforheap[i]); } while (!minheap.isempty ()) {System.out.print (Minheap.poll () + ""); } System.out.println (); while (!maxheap.isempty ()) {System.out.print (Maxheap.poll () + ""); }}}result:0 1 2 3 4 5 6 7 7 6 5 4 3 2 1 0 Process finished with exit code 0
Implementation of Java size Gan