Question: Problem Statement: Implement 3 - ary heapProblem Statement: Implement 3 - ary heap In this programming homework, you are required to write a program in
Problem Statement: Implement ary heapProblem Statement: Implement ary heap
In this programming homework, you are required to write a program in either C or Java that
can perform basic operations on a ary heap. Unlike a binary heap with children per node, a
ary heap has children per node.
A sample code andor guided pseudocode has been provided herewith. This sample code is for
a binary heap. You task is to complete the commented lines with a "recursive version code".
The file also contains two nonrecursive functions written at the bottom. These should be used
just as a guideline for filling up the recursive version. Once your recursive version is working,
you should modify it from binary heap to ary heap. Make sure that you DO NOT CHANGE
the main function.
Required Output:
The output of your code should exactly match the content of the "heapoutput.txt file, with a
newline appended to the last output.
In this programming homework, you are required to write a program in either C or Java that
can perform basic operations on a ary heap. Unlike a binary heap with children per node, a
ary heap has children per node.
A sample code andor guided pseudocode has been provided herewith. This sample code is for
a binary heap. You task is to complete the commented lines with a recursive version code
The file also contains two nonrecursive functions written at the bottom. These should be used
just as a guideline for filling up the recursive version. Once your recursive version is working,
you should modify it from binary heap to ary heap. Make sure that you DO NOT CHANGE
the main function.
Required Output:
The output of your code should exactly match the content of the heapoutput.txt file, with a
newline appended to the last output.
java
sample code andor guided pseudocode for a binary heap
import java.io;
import java.util.;
public class Heap
int A; you may use ArrayList instead
int size ;
int max;
public Heapint max
A new intmax;
this.max max;
int minint i int j
returns index of minimum value within Aij
int minindex i;
forint ki; kj; k
if Ak Aminindex minindex k;
return minindex;
void swapint i int jint temp Ai; Ai Aj; Aj temp;
public int extractmin
if size return ; error
int retval A;
A Asize; put last element at the top
size;
heapify;
return retval;
void heapifyint top
if top size return;
int minChild;
calculate minChild refer below to nonrecursive heapify as a guideline
for this block of code
if minchild and top do not conflict then return
else there is violation of value property so "swap;
recurse with minChild to fix further down
void insertint newkey
if size max return; error you may want to throw an exception
Asize newkey; add new key at the end of the heap
size; heaps size grows by one due to this new arrival
int pos size; set pos to last items location
percolatepos;
void percolateint pos
fixes upwards recursively
if pos return;
calcuate the position of parent of pos
if all fine no conflict above then return
if conflict the swap with parent
recurse continue percolating upwards starting at parent now
void decreasekeyint pos, int newkey
decreases the key at Apos to a new value
if newkey is bigger than Apos nothing to do
else change Apos to newkey
fix value property
public static void mainString args
Heap H new Heap;
for int i ; i ; i
Hinserti;
Hdecreasekey;
for int i ; i ; i
System.out.printlnHextractmin;
nonrecursive version below
void insertnrint newkey
if size max return; error you may want to throw an exception
Asize newkey; add new key at the end of the heap
int pos size;
size;
int parent;
whilepos
parent pos; calcuate the position of parent
if Aparent Apos break;if all fine then break the loop
swapparentpos; if conflict then swap with parent
pos parent;
void heapifynrint top
int minChild;
while top
output:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
