*JAVA*
complete method Heap tree
Delete the largest value
removeMax();
Delete the smallest value
removeMin();
class BinarHeap<T> {
int root;
static int[] arr;
static int size;
public BinarHeap() {
arr = new int[50];
size = 0;
}
public void insert(int val) {
arr[++size] = val;
bubbleUP(size);
}
public void bubbleUP(int i) {
int parent = (i) / 2;
while (i > 1 && arr[parent] > arr[i]) {
int temp = arr[parent];
arr[parent] = arr[i];
arr[i] = temp;
i = parent;
}
}
public int retMin() {
return arr[1];
}
public void removeMin() {
}
public void removeMax() {
}
public void print() {
for (int i = 0; i <= size; i++) {
System.out.print( arr[i] + " ");
}
}
}
public class BinarH {
public static void main(String[] args) {
BinarHeap Heap1 = new BinarHeap();
Heap1.insert(12);
Heap1.insert(9);
Heap1.insert(24);
Heap1.insert(4);
Heap1.insert(71);
// print inorder traversal of the BH
System.out.printf("My Binar Heap:%n");
Heap1.print();
System.out.printf("%n%nCall Min:%n[%d]", Heap1.retMin());
System.out.printf("%n%nAfter remove Min:%n");
Heap1.removeMin();
Heap1.print();
System.out.printf("%n%nAfter remove Max:%n");
Heap1.removeMax();
Heap1.print();
}
}
Step by stepSolved in 3 steps with 2 images
- explain this codes : class Heap{ int [] heapArray; int maxSize, heapSize; public Heap(int max) { maxSize=max; heapSize=0; heapArray = new int[maxSize]; } public boolean insert(int ele) { if((heapSize+1) == maxSize) return false; heapArray[++heapSize]=ele; int pos = heapSize; while(pos != 1 && ele < heapArray[pos/2]) { heapArray[pos] = heapArray[pos/2]; pos = pos/2; } heapArray[pos] = ele; return true; } public void displayHeap() { System.out.println("The contents of heap are: "); for(int i = 1;i <= heapSize;i++) System.out.print(heapArray[i]+" "); }}public class HeapTest { public static void main(String args[]){ Heap h=new Heap(7); h.insert(7); h.insert(8);h.insert(2);h.insert(4);h.insert(12);h.insert(5);h.displayHeap();}}arrow_forwardA Dictionary implementation using Binary Search Trees Program requirements and structure You should be able to do the following: Add dictionary entries Search for an entry Print the whole dictionary You will be using the .compareTo method from the String class in order to move through your tree. Recursive method to print the tree in inorder traversal (you need little mods below code) public void printTree(Node root){ if(root != null){ printTree(root.getLeftChild()); System.out.println(root.toSting( )); printTree(root.getRightChild()); } } Instructions: Please use CODE TEMPLATE!arrow_forwardpublic class HeapPriQ<T> implements PriQueueInterface<T>{ protected ArrayList<T> elements; // priority queue elements protected int lastIndex; // index of last element in priority queue protected int maxIndex; // index of last position in ArrayList protected Comparator<T> comp; public HeapPriQ(int maxSize) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; comp = new Comparator<T>() { public int compare(T element1, T element2) { return ((Comparable)element1).compareTo(element2); } }; } public HeapPriQ(int maxSize, Comparator<T> comp) // Precondition: T implements Comparable { elements = new ArrayList<T>(maxSize); lastIndex = -1; maxIndex = maxSize - 1; this.comp = comp; } public boolean isEmpty() // Returns true if this priority queue is empty; otherwise, returns false. {…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education