Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is lab4.java. This code implements a min heap. Fill in the code for the following methods in the MinHeap class: int min(): this method

Here is lab4.java. This code implements a min heap.

Fill in the code for the following methods in the MinHeap class:

int min(): this method returns the minimum value in the heap, but DOES NOT remove it from the heap.

int removeMin(): this method returns the minimum value in the heap, and removes it from the heap.

void insert(int value): this method inserts a new value into the heap. You can assume the array used for representing the heap is not full.

lab4.java:

/*Use this code for lab 4*/

/*Fill in the two functions min, removeMin and insert*/

public class lab4

{

public static void main(String[] args){

int[] a = {50,22,18,14,60,11,32,6,1}; // items to be inserted in the heap

int min;

// build a heap by successively inserting an item

MinHeap h = new MinHeap(100);

for (int i = 0; i < a.length; i++)

h.insert(a[i]);

System.out.print("Heap h built from successive insertion:");

h.printArray();

// now do a few removals

for (int i = 0; i < a.length; i=i+2){

System.out.print("Remove " + h.removeMin() + " from h:");

h.printArray();

}

}

}

class MinHeap{

// array storing items in the min-heap

int[] arr;

int size; // heap size

public MinHeap(int MAXSIZE){

// construct an array and set the size to 0

arr = new int[MAXSIZE];

size = 0;

}

public int min(){

// return the minimum value in the heap (do not remove it from the minHeap)

}

public int removeMin(){

// remove the minimum value from the minHeap return return it

// fill in your code here

}

public void insert(int value){

//insert value into the minHeap

// fill in your code here

}

public void printArray(){

// print the array representation of the heap

for (int i=0; i

System.out.print(arr[i]+" ");

System.out.println();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

5. Arranging for the training facility and room.

Answered: 1 week ago

Question

1. Discuss the five types of learner outcomes.

Answered: 1 week ago