Using Java
In this project, you are going to build a max-heap. You will use an array to implement the heap.
Your program should:
? Allow the user to select one of the following two choices (Note that your program needs to implement both choices): o
(1) test your program with 100 randomly generated integers (no duplicates, positive numbers with proper range); o
(2) test your program with the following 100 fixed values from 1, 2, 3, ..., and 100.
? Implement both methods of building a max heap: o Using sequential insertions (its time complexity: ??(??????????)). o Using the optimal (“smart”) method (its time complexity: ??(??)). For both methods, you need to keep track of how many swaps (swapping parent and child) are required to build a heap.
? For choice (1), you need to generate 20 sets of randomly generated integers; compute, the average number of swaps for both methods. Your program should output the average number of swaps for both methods (an average over 20 sets).
? For choice (2), your program should output the first 10 integers in your array and the number of swaps for both methods. Then perform 10 removals on the heap and output the first 10 integers. In your project report, you need to analyze both methods of heap implementation in terms of their efficiency theoretically and experimentally.
import java.util.*;
public class Project2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
System.out.print("Please select how to test the program:\n(1) 20
sets of 100 randomly generated integers\n(2) Fixed integer values
1-100\nEnter choice: ");
int choice = input.nextInt();
switch (choice) {
case 1: Heap heaps[] = new Heap[20];
int totSwaps = 0;
//series of insertions
for (int i = 0; i < 20; i ++) {
heaps[i] = new Heap(100);
for (int j = 0; j < 100;) {
if (heaps[i].add(rand.nextInt(2147483647) + 1)) //if it was added,
meaning it wasn't a duplicate, increase the count
j++;
}
totSwaps += heaps[i].getSwaps();
}
double avgSwaps = totSwaps / 20.0;
System.out.print("\nAverage swaps for series of insertions:
");
System.out.println(avgSwaps);
//optimal method
totSwaps = 0;
for (int i = 0; i < 20; i ++) {
heaps[i] = new Heap(100);
for (int j = 0; j < 100;) {
if (heaps[i].addArbitrarily(rand.nextInt(2147483647) + 1)) //checks
for duplicates
j++; //only increment if it was added, meaning no duplicates
}
heaps[i].optimize();
totSwaps += heaps[i].getSwaps();
}
avgSwaps = totSwaps / 20.0;
System.out.print("Average swaps for optimal method: ");
System.out.println(avgSwaps);
break;
case 2: //series of insertions
Heap heap = new Heap(100);
for (int i = 1; i <= 100; i++) {
heap.add(i);
}
System.out.print("\nHeap built using series of insertions:
");
heap.print(10);
System.out.println();
System.out.print("Number of swaps: ");
System.out.println(heap.getSwaps());
for (int i = 0; i < 10; i++) {
heap.remove();
}
System.out.print("Heap after 10 removals: ");
heap.print(10);
System.out.println();
//optimal method
heap = new Heap(100);
for (int i = 1; i <= 100; i++) {
heap.addArbitrarily(i);
}
heap.optimize();
System.out.print("\nHeap built using optimal method: ");
heap.print(10);
System.out.println();
System.out.print("Number of swaps: ");
System.out.println(heap.getSwaps());
for (int i = 0; i < 10; i++) {
heap.remove();
}
System.out.print("Heap after 10 removals: ");
heap.print(10);
System.out.println();
break;
default: System.out.print("Invalid selection.");
}
}
}
Heap.java
//A heap implemented using an array
import java.lang.Math;
public class Heap {
private final int array[];
private int end;
private int swaps;
//size must be provided to produce the array
public Heap(int size) {
array = new int[size];
end = 0; //last element + 1
swaps = 0;
}
//adds an item directly into the next slot, checking for
duplicates, optimize must be called when complete
public boolean addArbitrarily(int num) {
if (duplicate(num) || end > array.length - 1)
return false;
else {
array[end] = num;
end++;
return true;
}
}
//reheaps the tree after a series of arbitrary adds
public void optimize() {
//find the bottom rung
int j = end - 1;
int level;
level = (int) Math.ceil(Math.log((double) (j + 1)) /
Math.log(2.0)); //the the level of j
int i = j - 1;
int newLevel;
newLevel = (int) Math.ceil(Math.log((double) (i + 1)) /
Math.log(2.0)); // the level of the previous node
while (newLevel == level) { //find the left most node on this
level
i--;
newLevel = (int) Math.ceil(Math.log((double) (i + 1)) /
Math.log(2.0));
} //now i to j is the last level, i - 1 is the end of the next
level up
while (i != 0) {
for (int k = i; k <= j; k += 2) {
if (k + 1 > j) { //only one child
if (array[k] > array[(k - 1) / 2]) {
//swap
int temp = array[k];
array[k] = array[(k - 1) / 2];
array[(k - 1) / 2] = temp;
swaps++;
shiftDown(k);
}
}
else { //two children
if (array[k] > array[k + 1] && array[k] > array[(k -
1) / 2]) {
//swap
int temp = array[k];
array[k] = array[(k - 1) / 2];
array[(k - 1) / 2] = temp;
swaps++;
shiftDown(k);
}
else if (array[k + 1] > array[k] && array[k + 1] >
array[(k - 1) / 2]) {
//swap
int temp = array[k + 1];
array[k + 1] = array[(k - 1) / 2];
array[(k - 1) / 2] = temp;
swaps++;
shiftDown(k + 1);
}
}
}
//move to the next level
j = i - 1;
i = (i - 1) / 2;
}
}
//helper function for optomize to move down swapped nodes
private void shiftDown(int m) {
//have to do bounds checking before accessing the array
while (m != end - 1 && ( (2 * m + 1 < end &&
array[m] < array[2 * m + 1]) || (2 * m + 2 < end &&
array[m] < array[2 * m + 2]) ) ) {
if (2 * m + 2 >= end || array[2 * m + 1] > array[2 * m + 2])
{
//swap
int temp = array[2 * m + 1];
array[2 * m + 1] = array[m];
array[m] = temp;
swaps++;
m = 2 * m + 1;
}
else {
//swap
int temp = array[2 * m + 2];
array[2 * m + 2] = array[m];
array[m] = temp;
swaps++;
m = 2 * m + 2;
}
}
}
//add with swapping
public boolean add(int num) {
if (duplicate(num) || end > array.length - 1)
return false;
int i = end;
array[i] = num;
int parent = (i - 1) / 2;
while (i != 0 && num > array[parent]) {
array[i] = array[parent]; //swap
array[parent] = num;
swaps++;
i = parent;
parent = (i - 1) / 2;
}
end++;
return true;
}
//efficient check for duplicates
public boolean duplicate(int num) {
for (int i = 0; i < end; i++) {
if (num > array[i])
return false;
else if (num == array[i])
return true;
}
return false;
}
//remove the root node and shift the new one down
public void remove() {
array[0] = array[end - 1];
int i = 0;
shiftDown(0);
end--;
}
//returns the number of swaps made by this heap
public int getSwaps() {
return swaps;
}
//prints the first n values of the array
public void print(int n) {
int i;
for (i = 0; i < n && i < end; i++) {
System.out.print(array[i]);
if (i != end- 1)
System.out.print(',');
}
if (i != end)
System.out.print("...");
}
}
/*Output*/

Using Java In this project, you are going to build a max-heap. You will use an...
Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...
In C++ language, need a full executable program!! Build a templated max heap using a linked implementation. Insert 100 unique random int’s into the heap. Display the heap. Then, delete the first 50 int’s that were inserted. Display the heap. Keep track of those first 50 int’s using an array or a vector. Display the heap in such a manner that the viewer is convinced that it is a heap. Now, repeat these actions but using an array implementation. Comparing...
java language
In this question you are working with a max heap of integers, where the integer represents a priority, with larger integers being higher priority. Write a method that will test if a given heap array is correctly ordered (i.e. write a method that tests if an array is a valid heap). The method should accept (i) an array of integers (the heap), and (ii) the current number of items in the heap, and should return a boolean.
Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an array and be prepared to grow the array. The array implementation will probably be more efficient. Next, write three java sorting methods: a) One should be the heapsort algorithm. b) the second should sort the array by inserting all the elements from the array into a heap defined by the MaxHeap class, and then removing all the items from the heap and putting them...
1. In Lab 4, you developed a program to build a Max Heap, and then Heap Sort. Update the program by adding two additional functions: (a) AddData(A, N, V) where V is the new value added. (b) Delete a data Delete by giving the index of the data position Make sure to display the array after calling each of the function. 2. Write a program to implement Binary Search Algorithm, which will return the index of the data searched (V)....
Using C++, data structures, C++ STL, inputs and expected
outputs are shown below.
Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...
Write a program in Java to implement the max-priority queue using max-heap data structure. Implement the max-heap data structure using an integer array of 10 cells. (Do not use Java in-built PriorityQueue class.) [In a max-heap, the root node and the intermediate node vales are always greater than their children.] First, take 10 integer values from the user and insert them in the max-priority queue. Then print the elements of the queue. After that, delete two elements from the queue...
How do I write this in the c++ language?
1. (10 points) Write a program to implement Heapsort. The input should be an array of size at least 15. Have the user enter the values or you can specify your own array (unsorted). The output should be the final sorted array AND print out the values in the max heap (just the heap, NOT the full array) after each MAX-HEAPIFY (after BUILD- MAX-HEAP i.e. don't print output in BUILD-MAX-HEAP) in...
Discrete Math
Max Heap After you build your heap, you then compare the values of the vertices, in the following way: If a child of a parent node is larger than the parent node, switch their positions. Using the list above, the following operations would take place: The parent of value 4 would swap with child of value 8. 1. 8 4 Now the parent value of 4 would swap places with the child value of 6. 2. 6 2...
Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...