SlideShare a Scribd company logo
1 of 2
Download to read offline
implement the add() method for min-heap
public class Heap {
private ArrayList element;
private comparator comp;
public int patent(int childIndex) {
return (childIndex - 1) / 2;
}
public void add(E element) {
code here
}
}
Solution
publicvoid add(int value) {
if (heapSize == data.length)
thrownewHeapException("Heap's underlying storage is overflow");
else {
heapSize++;
data[heapSize - 1] = value;
shiftUp(heapSize - 1);
}
}
privatevoid shiftUp(int nodeIndex) {
int parentIndex, tmp;
if (nodeIndex != 0) {
parentIndex = getParentIndex(nodeIndex);
if (data[parentIndex] >data[nodeIndex]) {
tmp =data[parentIndex];
data[parentIndex] = data[nodeIndex];
data[nodeIndex] = tmp;
shiftUp(parentIndex);
}
}
}
}

More Related Content

More from footsmart1

Student Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdfStudent Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdf
footsmart1
 
Post your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdfPost your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdf
footsmart1
 
Please fill in the code, which only need to add code in the adju.pdf
Please fill in the code, which only need to add code in the adju.pdfPlease fill in the code, which only need to add code in the adju.pdf
Please fill in the code, which only need to add code in the adju.pdf
footsmart1
 
Linear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdfLinear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdf
footsmart1
 
Many nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdfMany nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdf
footsmart1
 
i need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdfi need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdf
footsmart1
 
How are nuclear pores and porins in the membrane differentSolut.pdf
How are nuclear pores and porins in the membrane differentSolut.pdfHow are nuclear pores and porins in the membrane differentSolut.pdf
How are nuclear pores and porins in the membrane differentSolut.pdf
footsmart1
 
File PrintCoded.java (shown above) contains an incomplete program. T.pdf
File PrintCoded.java (shown above) contains an incomplete program. T.pdfFile PrintCoded.java (shown above) contains an incomplete program. T.pdf
File PrintCoded.java (shown above) contains an incomplete program. T.pdf
footsmart1
 

More from footsmart1 (20)

The three major domains of li.pdf
The three major domains of li.pdfThe three major domains of li.pdf
The three major domains of li.pdf
 
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdfThere are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
There are two major groups of seed plants, gymnosperms (Exercise 24) .pdf
 
The steps in the closing process are (1) close credit balances in re.pdf
The steps in the closing process are (1) close credit balances in re.pdfThe steps in the closing process are (1) close credit balances in re.pdf
The steps in the closing process are (1) close credit balances in re.pdf
 
The positron decay of ^15 O goes directly to the ground state of ^15 .pdf
The positron decay of ^15 O goes directly to the ground state of ^15 .pdfThe positron decay of ^15 O goes directly to the ground state of ^15 .pdf
The positron decay of ^15 O goes directly to the ground state of ^15 .pdf
 
The disease bullous pemphigoid results in the destruction of protein.pdf
The disease bullous pemphigoid results in the destruction of protein.pdfThe disease bullous pemphigoid results in the destruction of protein.pdf
The disease bullous pemphigoid results in the destruction of protein.pdf
 
Student Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdfStudent Outcome f the broad education necessary to understand th.pdf
Student Outcome f the broad education necessary to understand th.pdf
 
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdfQUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
QUESTION 8 1.00000 P Financial intermediaries are different from inve.pdf
 
Post your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdfPost your response to the following focus questionsDescribe a sol.pdf
Post your response to the following focus questionsDescribe a sol.pdf
 
Please fill in the code, which only need to add code in the adju.pdf
Please fill in the code, which only need to add code in the adju.pdfPlease fill in the code, which only need to add code in the adju.pdf
Please fill in the code, which only need to add code in the adju.pdf
 
Linear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdfLinear AlgebraHello. I need help with my practice final. I am not .pdf
Linear AlgebraHello. I need help with my practice final. I am not .pdf
 
Many nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdfMany nations, including the US, already maintain a national DNA data.pdf
Many nations, including the US, already maintain a national DNA data.pdf
 
i need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdfi need the complete answers as soon as possibleSolutionUNI.pdf
i need the complete answers as soon as possibleSolutionUNI.pdf
 
How are nuclear pores and porins in the membrane differentSolut.pdf
How are nuclear pores and porins in the membrane differentSolut.pdfHow are nuclear pores and porins in the membrane differentSolut.pdf
How are nuclear pores and porins in the membrane differentSolut.pdf
 
How many allotropic forms does uranium have Discuss the effect of a.pdf
How many allotropic forms does uranium have Discuss the effect of a.pdfHow many allotropic forms does uranium have Discuss the effect of a.pdf
How many allotropic forms does uranium have Discuss the effect of a.pdf
 
How does C2 compare to various security and encryption methodsa. .pdf
How does C2 compare to various security and encryption methodsa. .pdfHow does C2 compare to various security and encryption methodsa. .pdf
How does C2 compare to various security and encryption methodsa. .pdf
 
Explain homologous recombination between human X and Y chromosomes..pdf
Explain homologous recombination between human X and Y chromosomes..pdfExplain homologous recombination between human X and Y chromosomes..pdf
Explain homologous recombination between human X and Y chromosomes..pdf
 
Explain the differences between a stateless and a stateful firewall..pdf
Explain the differences between a stateless and a stateful firewall..pdfExplain the differences between a stateless and a stateful firewall..pdf
Explain the differences between a stateless and a stateful firewall..pdf
 
By looking at the skeleton of a python, a boa contricor and a legles.pdf
By looking at the skeleton of a python, a boa contricor and a legles.pdfBy looking at the skeleton of a python, a boa contricor and a legles.pdf
By looking at the skeleton of a python, a boa contricor and a legles.pdf
 
Explain what we mean by Ride with vehicles.SolutionRide.pdf
Explain what we mean by Ride with vehicles.SolutionRide.pdfExplain what we mean by Ride with vehicles.SolutionRide.pdf
Explain what we mean by Ride with vehicles.SolutionRide.pdf
 
File PrintCoded.java (shown above) contains an incomplete program. T.pdf
File PrintCoded.java (shown above) contains an incomplete program. T.pdfFile PrintCoded.java (shown above) contains an incomplete program. T.pdf
File PrintCoded.java (shown above) contains an incomplete program. T.pdf
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 

implement the add() method for min-heappublic class Heap {    .pdf

  • 1. implement the add() method for min-heap public class Heap { private ArrayList element; private comparator comp; public int patent(int childIndex) { return (childIndex - 1) / 2; } public void add(E element) { code here } } Solution publicvoid add(int value) { if (heapSize == data.length) thrownewHeapException("Heap's underlying storage is overflow"); else { heapSize++; data[heapSize - 1] = value; shiftUp(heapSize - 1); } } privatevoid shiftUp(int nodeIndex) { int parentIndex, tmp; if (nodeIndex != 0) { parentIndex = getParentIndex(nodeIndex); if (data[parentIndex] >data[nodeIndex]) { tmp =data[parentIndex]; data[parentIndex] = data[nodeIndex]; data[nodeIndex] = tmp;