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 to build a
heap.
• Implement the remove method of a max-heap.
• Read a sequence of integers from an input file.
o “data.txt”: This file contains 100 integers (no duplicates, and
positive numbers). Each line is an integer.
• Perform heap operations and Write the results into an output
file.
o Create a max-heap using the sequential insertions, for those 100
integers.
o Output the first 10 integers of your array, into the output
file
o Output the number of swaps performed, into the output file
o Perform 10 removals on the heap
o Output the first 10 integers of the resulting array, into the
output file
o Create a max-heap using the optimal method, for those 100
integers
o Output the first 10 integers of your array, into the output
file
o Output the number of swaps performed, into the output file
o Perform 10 removals on the heap
o Output the first 10 integers in the resulting array, into the
output file
The output file should use the format as shown below:
=====================================================================
Heap built using sequential insertions:
100,94,99,77,93,98,61,68,76,84,...
Number of swaps in the heap creation: 480
Heap after 10 removals: 90,89,62,77,88,53,61,68,76,84,...
Heap built using optimal method:
100,95,99,79,94,98,63,71,78,87,...
Number of swaps in the heap creation: 96
Heap after 10 removals: 90,89,63,79,88,55,62,71,78,87,...
=====================================================================
Please note that the numbers in the above format are not the actual
figures. It is just an example.
This project will be graded based on the quality of your program.
Java interface and generic data types are NOT required in this
project, but bonus points will be considered for those who use Java
interface and generic data type.
1. Source codes
2. Input file (just the given “data.txt”)
3. Output file
data.txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Heap.java:
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Heap { private int size; private int[] arr; public Heap(){ } //buidHeap method that works in O(n) time public void buildHeap(int[] ar){ this.size = ar.length; this.arr = new int[size]; for(int i=0;i<ar.length;i++) this.arr[i] = ar[i]; for(int i=(this.arr.length)/2;i>=0;i--){ this.heapify(i); } } //max-heapify method public void heapify(int i){ int largest = i; if(((2*i)+1)<size && this.arr[(2*i)+1]>this.arr[largest]) largest = ((2*i)+1); if(((2*i)+2)<size && this.arr[(2*i)+2]>this.arr[largest]) largest = ((2*i)+2); if(largest != i){ int temp = this.arr[i]; this.arr[i] = this.arr[largest]; this.arr[largest] = temp; this.heapify(largest); } } public void insert(int num){ } //method to remove max element from max-heap public int remove(){ if(this.size<1) return -1; int temp = this.arr[0]; this.arr[0] = this.arr[size-1]; this.arr[size-1] = temp; size--; return this.arr[size]; } //method to print elements of max heap in output file public void printHeap(String filename){ try { File file = new File(filename); FileWriter writer = new FileWriter(file); for (int i = 0; i < this.arr.length; i++) writer.write(this.arr[i] + "\n"); writer.close(); }catch (IOException e){ System.out.println("Error while writing"); } } }
Main.java:
import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class main { public static void main(String[] args) { try { ArrayList<Integer> readArray = new ArrayList<Integer>(); File file = new File("input.txt"); Scanner reader = new Scanner(file); while (reader.hasNextInt()) { readArray.add(reader.nextInt()); } int[] arr = new int[readArray.size()]; for (int i = 0; i < readArray.size(); i++) arr[i] = readArray.get(i); Heap h = new Heap(); h.buildHeap(arr); h.printHeap("output.txt"); }catch (IOException e){ System.out.println("Error while reading"); } } }
INPUT FILE "input.txt":
OUTPUT FILE "output.txt":
Write a Java program, In this project, you are going to build a max-heap using array...
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....
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 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...
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...
Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance
Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...
*Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...
Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...
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)....