8000 add comment for heapjs · trekhleb/javascript-algorithms@21a3539 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21a3539

Browse files
committed
add comment for heapjs
1 parent dc1047d commit 21a3539

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/data-structures/heap/Heap.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export default class Heap {
99
* @param {Function} [comparatorFunction]
1010
*/
1111
constructor(comparatorFunction) {
12+
// ES6新增,用来检测是否由构造函数调用
13+
// Heap是个抽象类,不能直接实例,需要通过MinHeap/MaxHeap实例化
1214
if (new.target === Heap) {
1315
throw new TypeError('Cannot construct Heap instance directly');
1416
}
@@ -19,7 +21,7 @@ export default class Heap {
1921
}
2022

2123
/**
22-
* @param {number} parentIndex
24+
* @param {number} parentIndex 根据Index获取左子节点索引
2325
* @return {number}
2426
*/
2527
getLeftChildIndex(parentIndex) {
@@ -35,7 +37,7 @@ export default class Heap {
3537
}
3638

3739
/**
38-
* @param {number} childIndex
40+
* @param {number} childIndex 根据Index获取父节点索引
3941
* @return {number}
4042
*/
4143
getParentIndex(childIndex) {
@@ -98,9 +100,14 @@ export default class Heap {
98100
const tmp = this.heapContainer[indexTwo];
99101
this.heapContainer[indexTwo] = this.heapContainer[indexOne];
100102
this.heapContainer[indexOne] = tmp;
103+
104+
// 可以这样交换
105+
// [this.heapContainer[indexTwo], this.heapContainer[indexOne]] =
106+
// [this.heapContainer[indexOne], this.heapContainer[indexTwo]]
101107
}
102108

103109
/**
110+
* 获取根节点元素
104111
* @return {*}
105112
*/
106113
peek() {
@@ -112,7 +119,8 @@ export default class Heap {
112119
}
113120

114121
/**
115-
* @return {*}
122+
* Sort关键过程,交换头尾两个元素值,移除头元素,并重新递归构建新Heap
123+
* @return {*} 返回头元素,此时头元素为一个极值
116124
*/
117125
poll() {
118126
if (this.heapContainer.length === 0) {
@@ -127,6 +135,8 @@ export default class Heap {
127135

128136
// Move the last element from the end to the head.
129137
this.heapContainer[0] = this.heapContainer.pop();
138+
139+
// 元素减少后,重新递归构建新Heap
130140
this.heapifyDown();
131141

132142
return item;
@@ -218,14 +228,18 @@ export default class Heap {
218228
}
219229

220230
/**
231+
* 从下向上重新建堆
221232
* @param {number} [customStartIndex]
222233
*/
223234
heapifyUp(customStartIndex) {
235+
// 在add时,默认从新增元素开始,向上重新建堆,这样可以保证下次ADD前,前面的元素排序
236+
// 已经基本OK,只需考虑当前元素
224237
// Take the last element (last in array or the bottom left in a tree)
225238
// in the heap container and lift it up until it is in the correct
226239
// order with respect to its parent element.
227240
let currentIndex = customStartIndex || this.heapContainer.length - 1;
228241

242+
// 不断的和父元素进行比较,如果大小关系不满足Heap条件,就进行交换
229243
while (
230244
this.hasParent(currentIndex)
231245
&& !this.pairIsInCorrectOrder(this.parent(currentIndex), this.heapContainer[currentIndex])
@@ -236,16 +250,21 @@ export default class Heap {
236250
}
237251

238252
/**
253+
* 从上向下重新建堆
239254
* @param {number} [customStartIndex]
240255
*/
241256
heapifyDown(customStartIndex = 0) {
242257
// Compare the parent element to its children and swap parent with the appropriate
243258
// child (smallest child for MinHeap, largest child for MaxHeap).
244259
// Do the same for next children after swap.
260+
// 当使用Heap排序时,顶部元素被赋值为尾元素,此时,需要从顶向下重新建堆
245261
let currentIndex = customStartIndex;
246262
let nextIndex = null;
247263

248264
while (this.hasLeftChild(currentIndex)) {
265+
// 与二叉树不同,Heap结构中,左右节点值的关系是不固定的
266+
// 左节点可能小于右节点,也可能大于右节点,只能保证左右节点和父节点的值大于关系固定
267+
// 取左右节点中,较大(For MaxHeap)或较小(For MinHeap)的值与父节点进行交换
249268
if (
250269
this.hasRightChild(currentIndex)
251270
&& this.pairIsInCorrectOrder(this.rightChild(currentIndex), this.leftChild(currentIndex))
@@ -255,6 +274,8 @@ export default class Heap {
255274
nextIndex = this.getLeftChildIndex(currentIndex);
256275
}
257276

277+
// 除去最新的变换节点,比较顶节点,其他节点之间的关系是在之前的过程就调整好的
278+
// 因此,若最近两个节点关系正确,则可以认为该节点的子树关系是OK的,可以结束
258279
if (this.pairIsInCorrectOrder(
259280
this.heapContainer[currentIndex],
260281
this.heapContainer[nextIndex],

0 commit comments

Comments
 (0)
0