Remove Min Function in A heap

I tried to implement a function removeMin() in a min heap, which should return the minimum element present in a heap.

The code that I have written is here:

Expected output is 4 10 15 17 21 67 100.

However, the problem gives segmentation fault.

Please help

your min heap is like this, issue is with your removeMin function, i am not sure about the logic you are using.

What do you mean by “min heap is like this” ?

check the code i have attached.

Sir, the code that you have attached prints
4 10 15 100 17 21 67

which is not the expected output, as we know that in a min heap, the lowest element will be removed first.

check thiis

Sir output of removeMin is still not right, it should be in the increasing order i.e.4 10 15 17 21 67 100

no bro, min heap means, that parent node is less then the value of child node, so

this output means your tree is like this

        4
      /   \
    10    15
    / \   /  \
100   17 21  67

yes, that is what i am saying, check the logic once. you are close to achieve it.

Sir could you please do it? I am not able to figure it out. I recently started learning heaps.

the logic is not correct in your removeMin heap function, it’s working like this

current array -> 4 10 15 100 17 21 67 
swapped indexs of 0 and 6removed 6th element 4
4
current array -> 67 10 15 100 17 21 
swapped indexs of 0 and 5removed 5th element 67
67
current array -> 21 10 15 100 17 
swapped indexs of 0 and 4removed 4th element 21
21
current array -> 17 10 15 100 
swapped indexs of 0 and 3removed 3th element 17
17
current array -> 100 10 15 
swapped indexs of 0 and 2removed 2th element 100
100
current array -> 15 10 
swapped indexs of 0 and 1removed 1th element 15
15
current array -> 10 
swapped indexs of 0 and 0removed 0th element 10
10

Okay sir, thanks a lot for this. Now I think I will be able to find out the mistake.

just a tip, whenever you find stuck in programming, take help of cout statements. it will always help you in debugging. All the best

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.