Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Update the code that will perform the followings: Create HeapApp file to Run the code. Study and see the results DON'T Solve it With ChatGBT

Update the code that will perform the followings:
Create HeapApp file to Run the code.
Study and see the results
DON'T Solve it With ChatGBT !!!
public class Heap {
private Node[] heapArray;
private int maxSize;
private int currentSize;
///////////////////////////////////////////
public Heap(int mx)// constructor
{
maxSize= mx;
currentSize=0;
heapArray= new Node[maxSize]; // create array
}
//-----------------------------------------
public boolean isEmpty()
{
return currentSize==0;
}
//////////////////////////////Insert //////////////////////////////////////
public boolean insert(int key)
{
if(currentSize==maxSize)// if array is full,
return false; // failure
Node newNode = new Node(key); // make a new node
heapArray[currentSize]= newNode; // put it at the end
trickleUp(currentSize++); // trickle it up
return true; // success
}// end insert()
///////////////////////////////////////////////////////////////////
public void trickleUp(int index)
{
int parent =(index-1)/2;
Node bottom = heapArray[index];
while(index >0 && heapArray[parent].iData bottom.iData)
{
heapArray[index]= heapArray[parent]; // move node down
index = parent; // move index up
parent =(parent-1)/2;
}// end while
heapArray[index]= bottom;
}// end trickleUp()
//////////////////////Delete ////////////////////////////////////////////////
public Node remove()// delete item with max key
{//(assumes non-empty list)
Node root = heapArray[0]; // save the root
heapArray[0]= heapArray[--currentSize]; // root-last
trickleDown(0); // trickle down the root
return root; // return removed node
}
//////////////////////////////////////////////////////////////////
public void trickleDown(int index)
{
int largerChild;
Node top = heapArray[index]; // save root
while(index currentSize/2)// while node has at least one child
{
int leftChild =2*index+1;
int rightChild = leftChild+1;
// find larger child
if(rightChild currentSize &&
heapArray[leftChild].iData heapArray[rightChild].iData)
largerChild = rightChild;
else
largerChild = leftChild;
// top >= largerChild?
if(top.iData >= heapArray[largerChild].iData)
break;
// shift child up
heapArray[index]= heapArray[largerChild];
index = largerChild; // go down
}// end while
heapArray[index]= top; // index - root
}// end trickleDown()
////////////////////////////////////////////////
public boolean change (int index, int newValue)
{
if (index 0|| index >= currentSize)
return false;
int oldValue = heapArray[index].getKey();
heapArray[index].setKey(newValue);
if(oldValue newValue)
trickleUp (index);
else
trickleDown(index);
return true;
}// end change
/////////////////////////////////////////////////
public void displayHeap()
{
System,out.print("heapArray: ");
for(int m=0; m currentSize; m++)
if(heapArray[m]!=null)
System.out.print(heapArray[m].getKey()+"");
else
System.out.print("--");
System.out.println();
/////heap format
int nBlanks =32;
int itemsPerRow =1;
int column =0;
int j=0;
while (currentSize >0)
{
if (column ==0)
for (int k=0; k
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions