Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

118 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 587d825a367417b2b2512c8a
title: 将元素插入最大堆
challengeType: 1
videoUrl: ''
dashedName: insert-an-element-into-a-max-heap
---
# --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方法该方法返回已添加到堆中的所有项的数组。
# --hints--
存在MaxHeap数据结构。
```js
assert(
(function () {
var test = false;
if (typeof MaxHeap !== 'undefined') {
test = new MaxHeap();
}
return typeof test == 'object';
})()
);
```
MaxHeap有一个名为insert的方法。
```js
assert(
(function () {
var test = false;
if (typeof MaxHeap !== 'undefined') {
test = new MaxHeap();
} else {
return false;
}
return typeof test.insert == 'function';
})()
);
```
MaxHeap有一个名为print的方法。
```js
assert(
(function () {
var test = false;
if (typeof MaxHeap !== 'undefined') {
test = new MaxHeap();
} else {
return false;
}
return typeof test.print == 'function';
})()
);
```
insert方法根据max heap属性添加元素。
```js
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;
})()
);
```
# --seed--
## --seed-contents--
```js
var MaxHeap = function() {
// Only change code below this line
// Only change code above this line
};
```
# --solutions--
```js
var MaxHeap = function() {
// Only change code below this line
this.heap = [null];
this.insert = (ele) => {
var index = this.heap.length;
var arr = [...this.heap];
arr.push(ele);
while (ele > arr[Math.floor(index / 2)] && index > 1) {
arr[index] = arr[Math.floor(index / 2)];
arr[Math.floor(index / 2)] = ele;
index = arr[Math.floor(index / 2)];
}
this.heap = arr;
}
this.print = () => {
return this.heap.slice(1);
}
// Only change code above this line
};
```