This commit is contained in:
krahets
2024-04-11 01:11:20 +08:00
parent a6adc8e20a
commit 739f8a31bb
85 changed files with 1555 additions and 979 deletions

View File

@ -240,9 +240,9 @@ It's worth mentioning that **since leaf nodes have no children, they naturally f
}
/* 元素入堆 */
fun push(value: Int) {
fun push(_val: Int) {
// 添加节点
maxHeap.add(value)
maxHeap.add(_val)
// 从底至顶堆化
siftUp(size() - 1)
}
@ -270,11 +270,11 @@ It's worth mentioning that **since leaf nodes have no children, they naturally f
// 交换根节点与最右叶节点(交换首元素与尾元素)
swap(0, size() - 1)
// 删除节点
val value = maxHeap.removeAt(size() - 1)
val _val = maxHeap.removeAt(size() - 1)
// 从顶至底堆化
siftDown(0)
// 返回堆顶元素
return value
return _val
}
/* 从节点 i 开始,从顶至底堆化 */

View File

@ -1183,9 +1183,9 @@ Given a total of $n$ nodes, the height of the tree is $O(\log n)$. Hence, the lo
```kotlin title="my_heap.kt"
/* 元素入堆 */
fun push(value: Int) {
fun push(_val: Int) {
// 添加节点
maxHeap.add(value)
maxHeap.add(_val)
// 从底至顶堆化
siftUp(size() - 1)
}
@ -1735,11 +1735,11 @@ Similar to the element insertion operation, the time complexity of the top eleme
// 交换根节点与最右叶节点(交换首元素与尾元素)
swap(0, size() - 1)
// 删除节点
val value = maxHeap.removeAt(size() - 1)
val _val = maxHeap.removeAt(size() - 1)
// 从顶至底堆化
siftDown(0)
// 返回堆顶元素
return value
return _val
}
/* 从节点 i 开始,从顶至底堆化 */