Answered step by step
Verified Expert Solution
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 Heapint mx constructor
maxSize mx;
currentSize;
heapArray new NodemaxSize; create array
public boolean isEmpty
return currentSize;
Insert
public boolean insertint key
ifcurrentSizemaxSize if array is full,
return false; failure
Node newNode new Nodekey; make a new node
heapArraycurrentSize newNode; put it at the end
trickleUpcurrentSize; trickle it up
return true; success
end insert
public void trickleUpint index
int parent index;
Node bottom heapArrayindex;
whileindex && heapArrayparentiData bottom.iData
heapArrayindex heapArrayparent; move node down
index parent; move index up
parent parent;
end while
heapArrayindex bottom;
end trickleUp
Delete
public Node remove delete item with max key
assumes nonempty list
Node root heapArray; save the root
heapArray heapArraycurrentSize; rootlast
trickleDown; trickle down the root
return root; return removed node
public void trickleDownint index
int largerChild;
Node top heapArrayindex; save root
whileindex currentSize while node has at least one child
int leftChild index;
int rightChild leftChild;
find larger child
ifrightChild currentSize &&
heapArrayleftChildiData heapArrayrightChildiData
largerChild rightChild;
else
largerChild leftChild;
top largerChild?
iftopiData heapArraylargerChildiData
break;
shift child up
heapArrayindex heapArraylargerChild;
index largerChild; go down
end while
heapArrayindex top; index root
end trickleDown
public boolean change int index, int newValue
if index index currentSize
return false;
int oldValue heapArrayindexgetKey;
heapArrayindexsetKeynewValue;
ifoldValue newValue
trickleUp index;
else
trickleDownindex;
return true;
end change
public void displayHeap
System,out.printheapArray: ;
forint m; m currentSize; m
ifheapArraymnull
System.out.printheapArraymgetKey;
else
System.out.print;
System.out.println;
heap format
int nBlanks ;
int itemsPerRow ;
int column ;
int j;
while currentSize
if column
for int k; k
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