how will we creat min heap?
Issue , , , ,, ,, , ,,,, , , , , , , , , ,, , , , ,, , ,
@Nitin-Mishra-2380486738834604,
A min heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node.
You can use almost the same logic as max heap but remember the value of parent node is always smaller than the children
sorry i did want to ask about max heap
@Nitin-Mishra-2380486738834604,
In a max heap the parent node is always greater or equal to the values in the children of that node
and what will change in this code
static void up(int ci,int arr1[]) { int pi=(ci-1)/2; if(arr1[ci]>arr1[pi]) { swap(pi,ci,arr1); up(pi,arr1); } } static void swap(int pi,int ci,int arr1[]) { int temp=arr1[ci]; arr1[ci]=arr1[pi]; arr1[pi]=temp; }
is it correct for max heap???
but sir it’s give wrong answer in output
@Nitin-Mishra-2380486738834604,
Please share your complete code. https://ide.codingblocks.com/
select the correct language, copy your code, click on save, share the link
@Nitin-Mishra-2380486738834604,
Use a while loop when you are traversing up and swapping the value. And just change the value of ci.
sir i didn’t understand can you explain this in my code
@Nitin-Mishra-2380486738834604,
Sure but can you share your complete code instead of sharing only the methods??
here is complete code sir. and sorry for late… i had some online that’s why…
reply for my question please
@Nitin-Mishra-2380486738834604,
your code is absolutely correct. Which test case was wrong or where were you submitting?
input: n=6, 1 2 3 4 5 6 ***********output:=6 5 3 4 2 1 *********my output for this code=: 6 4 5 1 3 2
@Nitin-Mishra-2380486738834604,
https://ide.codingblocks.com/s/232060 check this code. you were swapping pi with ci. Instead if was ci with pi.