Question
Write a Java program that can handle three basic operations of 3-ary heap: Insert : IN Decrease-Key: DK Extract-Min: EM Your program (Heap.java) will take
Write a Java program that can handle three basic operations of 3-ary heap:
Insert : IN
Decrease-Key: DK
Extract-Min: EM
Your program (Heap.java) will take input instructions from a file named inputFile.txt. So, you need to read the input file instruction by instruction and thereby perform that operation on the heap. Make sure that the names of all the files are the same as given. The output should print the result of the last Extract-Min (EM) operation with a newline appended to it.
Input File: This is a sample input file. You can use this to test your program. The comments show the state of the array after each instruction.
1 11 # Total number of instructions
2 IN 10 # [10]
3 IN 15 # [10, 15]
4 IN 27 # [10, 15, 27]
5 EM # [15, 27]
6 IN 13 # [13, 27, 15]
7 IN 11 # [11, 27, 15, 13]
8 IN 5 # [5, 11, 15, 13, 27]
9 IN 6 # [5, 6, 15, 13, 27, 11]
10 DK 5 4 # Decrease key of the element at array position 5 to value 4.
[4, 5, 15, 13, 27, 6]
11 IN 3 # [3, 4, 15, 13, 27, 6, 5]
12 EM # Last EM operation
Output:
1 3 # Printing the result of last EM operation
Java class: This class is for reference. These are the basic methods that should be there in the Java class. You can add your own classes on the top of these classes.
public class Heap {
private int array[ ];
private int arraySize; // max array size
private int heapSize; // current heap size
// init method
public Heap(int arraySize){
// initiate your class variables
}
// Private Class methods
private void floatUp (int index);
private void sinkDown (int index);
// Public Class methods
public void insert (int element);
public void decreaseKey (int index, int newElement );
public int extractMin ( );
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started