freeCodeCamp/curriculum/challenges/chinese/08-coding-interview-prep/data-structures/insert-an-element-into-a-ma...

4.1 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d825a367417b2b2512c8a Insert an Element into a Max Heap 1 将元素插入最大堆

Description

现在我们将继续讨论另一个树数据结构,即二进制堆。二进制堆是部分有序的二叉树,它满足堆属性。 heap属性指定父节点和子节点之间的关系。您可能有一个最大堆其中所有父节点都大于或等于其子节点或者最小堆其中反向为真。二进制堆也是完整的二叉树。这意味着树的所有级别都被完全填充如果最后一级被部分填充则从左到右填充。虽然二进制堆可以实现为具有包含左和右引用的节点的树结构但是根据堆属性的部分排序允许我们用数组表示堆。父子关系是我们感兴趣的通过简单的算术我们可以计算任何父节点的子节点和任何子节点的父节点。例如考虑二进制最小堆的数组表示 [ 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方法该方法返回已添加到堆中的所有项的数组。

Instructions

Tests

tests:
  - text: 存在MaxHeap数据结构。
    testString: 'assert((function() { var test = false; if (typeof MaxHeap !== "undefined") { test = new MaxHeap() }; return (typeof test == "object")})(), "The MaxHeap data structure exists.");'
  - text: MaxHeap有一个名为insert的方法。
    testString: 'assert((function() { var test = false; if (typeof MaxHeap !== "undefined") { test = new MaxHeap() } else { return false; }; return (typeof test.insert == "function")})(), "MaxHeap has a method called insert.");'
  - text: MaxHeap有一个名为print的方法。
    testString: 'assert((function() { var test = false; if (typeof MaxHeap !== "undefined") { test = new MaxHeap() } else { return false; }; return (typeof test.print == "function")})(), "MaxHeap has a method called print.");'
  - text: insert方法根据max heap属性添加元素。
    testString: 'assert((function() { var test = false; if (typeof MaxHeap !== "undefined") { test = new MaxHeap() } else { return false; }; test.insert(50); test.insert(100); test.insert(700); test.insert(32); test.insert(51); let result = test.print(); return ((result.length == 5) ? result[0] == 700 : result[1] == 700) })(), "The insert method adds elements according to the max heap property.");'

Challenge Seed

var MaxHeap = function() {
  // change code below this line
  // change code above this line
};

Solution

// solution required