Javascript 二进制堆-为什么remove函数需要调用bubbleUp方法?

Javascript 二进制堆-为什么remove函数需要调用bubbleUp方法?,javascript,binary,heap,push,Javascript,Binary,Heap,Push,我一直在通过eloquentjavascript网站学习JavaScript,并获得了一些我似乎无法理解的代码: remove: function(node) { var length = this.content.length; // To remove a value, we must search through the array to find // it. for (var i = 0; i < length; i++) { if (

我一直在通过eloquentjavascript网站学习JavaScript,并获得了一些我似乎无法理解的代码:

remove: function(node) {
    var length = this.content.length;
    // To remove a value, we must search through the array to find
    // it.
    for (var i = 0; i < length; i++) {
      if (this.content[i] != node) continue;
      // When it is found, the process seen in 'pop' is repeated
      // to fill up the hole.
      var end = this.content.pop();
      // If the element we popped was the one we needed to remove,
      // we're done.
      if (i == length - 1) break;
      // Otherwise, we replace the removed element with the popped
      // one, and allow it to float up or sink down as appropriate.
      this.content[i] = end;
      this.bubbleUp(i);
      this.sinkDown(i);
      break;
    }
  },
删除:功能(节点){
变量长度=this.content.length;
//要删除一个值,我们必须搜索整个数组以查找
//它。
对于(变量i=0;i
我的问题是-为什么这个函数需要调用bubbleUp()?
如果end是二进制堆上的最后一个值,并且通过push()方法添加到堆上的每个值都调用bubbleUp,那么end值的分数怎么可能低于i处的值呢?我不熟悉数据结构,似乎无法理解这一点。

想象一下这样的最小堆:

       1
   10      2
 18  20  3   4
现在,假设您删除了
20
。您拥有的代码基本上会在其位置上粘贴
4

       1
   10      2
 18   4  3

由于4<10,它必须冒泡以满足最小堆的要求。

当我看到您的示例树时,这很有意义,谢谢。(尽管如果您的示例处理的是最小堆和右侧的低值以及左侧的高值,我会更快地理解答案。)@mbarton:Better?:)我认为对其他有同样问题的人来说可能更容易。谢谢:)