Understand how min-max heap looks like after after delete-min and delete-max

346 Views Asked by At

I have a specific question about min-max heap related problem in Java.

If you have inputs in this order:

71, 8, 41, 100, 60

Would the tree look like the following?

                   8
           100           41
       70       60

What about after deleteMin and deleteMax?

I am trying to understand what would happen if the max node is somehow smaller than some of the min nodes... If you can help me by explaining it with a tree that would be great :)

1

There are 1 best solutions below

3
On

The process of creating the min heap of this inputs is this:

        71
      8    41
  100  60

And then it looks like this:

     8
  71  41
100 60

And at last, it looks like this:

      8
  60    41
100  71

At this time, every non-leaf node, its value is greater than that of its children's. So, after you delete the min value, the root of the min-heap above would be deleted, and result is this:

     41
  60    100
71

And if you delete the max value, that would be the last element that is deleted. then the result would be this:

     8
  60    41
71

I hope this would help you to understand Heap.