refactor: Replace 结点 with 节点 (#452)

* Replace 结点 with 节点
Update the footnotes in the figures

* Update mindmap

* Reduce the size of the mindmap.png
This commit is contained in:
Yudong Jin
2023-04-09 04:32:17 +08:00
committed by GitHub
parent 3f4e32b2b0
commit 1c8b7ef559
395 changed files with 2056 additions and 2056 deletions

View File

@@ -14,23 +14,23 @@ class MaxHeap {
constructor(nums) {
// 将列表元素原封不动添加进堆
this.#maxHeap = nums === undefined ? [] : [...nums];
// 堆化除叶点以外的其他所有
// 堆化除叶点以外的其他所有
for (let i = this.#parent(this.size() - 1); i >= 0; i--) {
this.#siftDown(i);
}
}
/* 获取左子点索引 */
/* 获取左子点索引 */
#left(i) {
return 2 * i + 1;
}
/* 获取右子点索引 */
/* 获取右子点索引 */
#right(i) {
return 2 * i + 2;
}
/* 获取父点索引 */
/* 获取父点索引 */
#parent(i) {
return Math.floor((i - 1) / 2); // 向下整除
}
@@ -61,20 +61,20 @@ class MaxHeap {
/* 元素入堆 */
push(val) {
// 添加
// 添加
this.#maxHeap.push(val);
// 从底至顶堆化
this.#siftUp(this.size() - 1);
}
/* 从点 i 开始,从底至顶堆化 */
/* 从点 i 开始,从底至顶堆化 */
#siftUp(i) {
while (true) {
// 获取点 i 的父
// 获取点 i 的父
const p = this.#parent(i);
// 当“越过根点”或“点无需修复”时,结束堆化
// 当“越过根点”或“点无需修复”时,结束堆化
if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;
// 交换两
// 交换两
this.#swap(i, p);
// 循环向上堆化
i = p;
@@ -85,9 +85,9 @@ class MaxHeap {
pop() {
// 判空处理
if (this.isEmpty()) throw new Error("堆为空");
// 交换根点与最右叶点(即交换首元素与尾元素)
// 交换根点与最右叶点(即交换首元素与尾元素)
this.#swap(0, this.size() - 1);
// 删除
// 删除
const val = this.#maxHeap.pop();
// 从顶至底堆化
this.#siftDown(0);
@@ -95,18 +95,18 @@ class MaxHeap {
return val;
}
/* 从点 i 开始,从顶至底堆化 */
/* 从点 i 开始,从顶至底堆化 */
#siftDown(i) {
while (true) {
// 判断点 i, l, r 中值最大的点,记为 ma
// 判断点 i, l, r 中值最大的点,记为 ma
const l = this.#left(i),
r = this.#right(i);
let ma = i;
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
// 若点 i 最大或索引 l, r 越界,则无需继续堆化,跳出
if (ma == i) break;
// 交换两
// 交换两
this.#swap(i, ma);
// 循环向下堆化
i = ma;