简体   繁体   English

在delete-min和delete-max之后,了解最小最大堆的外观

[英]Understand how min-max heap looks like after after delete-min and delete-max

I have a specific question about min-max heap related problem in Java. 我有一个有关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 ? deleteMindeleteMax呢?

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 :) 我试图了解如果最大节点比某些最小节点小一些会发生什么...如果您可以通过用一棵很棒的树解释它来帮助我:)

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: 因此,删除最小值后,上面的min-heap的根将被删除,结果是:

     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. 希望这可以帮助您了解堆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM