[ 6, 22, 30, 37, 63, 48, 42, 76 ]
根节点是第一个元素,6。它的子节点是22和30.如果我们看在这些值的数组索引之间的关系中,对于索引i,子项为2 * i + 1和2 * i + 2.同样,索引0处的元素是索引1和2处的这两个子项的父项。通常,我们可以在任何索引处找到节点的父节点,其中包含以下内容:(i - 1)/ 2.当二叉树增长到任意大小时,这些模式将成立。最后,我们可以稍微调整一下,通过跳过数组中的第一个元素,使这个算法更容易。这样做会为给定索引i处的任何元素创建以下关系:示例数组表示形式: [ null, 6, 22, 30, 37, 63, 48, 42, 76 ]
元素的左子项:i * 2元素的右子项:i * 2 + 1一个元素的父元素:i / 2一旦你绕过数学运算,使用数组表示非常有用,因为使用这个算法可以快速确定节点位置,因为你不需要内存使用量减少维护对子节点的引用。说明:这里我们将创建一个最大堆。首先创建一个insert方法,将元素添加到堆中。在插入期间,始终保持堆属性非常重要。对于最大堆,这意味着根元素应始终在树中具有最大值,并且所有父节点应该大于其子节点。对于堆的数组实现,这通常分三步完成:将新元素添加到数组的末尾。如果元素大于其父元素,请切换它们。继续切换,直到新元素小于其父元素或到达树的根。最后,添加一个print方法,该方法返回已添加到堆中的所有项的数组。