Question: Write a Java program to implement priority queue using min-heap as we discussed in class. Heap size: 30 nodes with random-generated number between 1`100. Show

Write a Java program to implement priority queue using min-heap as we discussed in class.

Heap size: 30 nodes with random-generated number between 1`100.

Show the state of the min-heap tree using an array after adding each node.

Show the state of the min-heap tree after removing each node.

Sample Log:

Add 30: [30] Add 81: [30, 81] Add 28: [28, 81, 30] Add 25: [25, 28, 30, 81] Add 86: [25, 28, 30, 81, 86] Add 97: [25, 28, 30, 81, 86, 97] Add 29: [25, 28, 29, 81, 86, 97, 30]

.

.Add 3: [2, 8, 3, 25, 12, 18, 15, 29, 41, 28, 25, 29, 72, 76, 21, 81, 95, 55, 44, 86, 41, 39, 92, 97, 55, 96, 84, 88, 80, 30]

Remove 2: [3, 8, 15, 25, 12, 18, 21, 29, 41, 28, 25, 29, 72, 76, 30, 81, 95, 55, 44, 86, 41, 39, 92, 97, 55, 96, 84, 88, 80] size: 29 Remove 3: [8, 12, 15, 25, 25, 18, 21, 29, 41, 28, 39, 29, 72, 76, 30, 81, 95, 55, 44, 86, 41, 80, 92, 97, 55, 96, 84, 88] size: 28 Remove 8: [12, 25, 15, 29, 25, 18, 21, 81, 41, 28, 39, 29, 72, 76, 30, 88, 95, 55, 44, 86, 41, 80, 92, 97, 55, 96, 84] size: 27

. .

Remove 88: [92, 95, 97, 96] size: 4 Remove 92: [95, 96, 97] size: 3 Remove 95: [96, 97] size: 2 Remove 96: [97] size: 1 Remove 97: [] size: 0

BASE CODE:

import java.util.*;

public class HeapT {

public int size;

private int[] myArray;

public int howMany;

private int parent(int index) {

return index / 2;

}

private int leftChild(int index) {

return index * 2;

}

private int rightChild(int index) {

return index * 2 + 1;

}

private boolean hasParent(int index) {

return index > 1;

}

private boolean hasLeftchild(int index) {

return leftChild(index) <= size;

}

private boolean hasRightChild(int index) {

return rightChild(index) <= size;

}

private void swap(int[] a, int index1, int index2) {

int temp = a[index1];

a[index1] = a[index2];

a[index2] = temp;

}

public int size() {

return size;

}

public void add(int value) {

}

public int remove() {

}

public boolean isEmpty() {

return size == 0;

}

public String toString() {

return Arrays.toString(myArray);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!