@@ -2,6 +2,7 @@ import Comparator from '../../utils/comparator/Comparator';
2
2
3
3
/**
4
4
* Parent class for Min and Max Heaps.
5
+ * 这里的堆实际上是二叉堆(Binary Heap),其实也可定义k叉堆。
5
6
*/
6
7
export default class Heap {
7
8
/**
@@ -36,7 +37,7 @@ export default class Heap {
36
37
37
38
/**
38
39
* @param {number } childIndex
39
- * @return {number }
40
+ * @return {number } 向下取整
40
41
*/
41
42
getParentIndex ( childIndex ) {
42
43
return Math . floor ( ( childIndex - 1 ) / 2 ) ;
@@ -67,27 +68,27 @@ export default class Heap {
67
68
}
68
69
69
70
/**
70
- * @param {number } parentIndex
71
+ * @param {number } childIndex
71
72
* @return {* }
72
73
*/
73
- leftChild ( parentIndex ) {
74
- return this . heapContainer [ this . getLeftChildIndex ( parentIndex ) ] ;
74
+ parent ( childIndex ) {
75
+ return this . heapContainer [ this . getParentIndex ( childIndex ) ] ;
75
76
}
76
77
77
78
/**
78
79
* @param {number } parentIndex
79
80
* @return {* }
80
81
*/
81
- rightChild ( parentIndex ) {
82
- return this . heapContainer [ this . getRightChildIndex ( parentIndex ) ] ;
82
+ leftChild ( parentIndex ) {
83
+ return this . heapContainer [ this . getLeftChildIndex ( parentIndex ) ] ;
83
84
}
84
85
85
86
/**
86
- * @param {number } childIndex
87
+ * @param {number } parentIndex
87
88
* @return {* }
88
89
*/
89
- parent ( childIndex ) {
90
- return this . heapContainer [ this . getParentIndex ( childIndex ) ] ;
90
+ rightChild ( parentIndex ) {
91
+ return this . heapContainer [ this . getRightChildIndex ( parentIndex ) ] ;
91
92
}
92
93
93
94
/**
0 commit comments