2023-01-29 14:03:30 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: my_heap.swift
|
|
|
|
|
* Created Time: 2023-01-28
|
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import utils
|
|
|
|
|
|
2023-02-07 19:05:19 +08:00
|
|
|
|
/* 大顶堆 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
class MaxHeap {
|
|
|
|
|
private var maxHeap: [Int]
|
|
|
|
|
|
2023-02-10 01:04:26 +08:00
|
|
|
|
/* 构造方法,根据输入列表建堆 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
init(nums: [Int]) {
|
|
|
|
|
// 将列表元素原封不动添加进堆
|
|
|
|
|
maxHeap = nums
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 堆化除叶节点以外的其他所有节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
for i in stride(from: parent(i: size() - 1), through: 0, by: -1) {
|
|
|
|
|
siftDown(i: i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 获取左子节点的索引 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
private func left(i: Int) -> Int {
|
|
|
|
|
2 * i + 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 获取右子节点的索引 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
private func right(i: Int) -> Int {
|
|
|
|
|
2 * i + 2
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
|
/* 获取父节点的索引 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
private func parent(i: Int) -> Int {
|
|
|
|
|
(i - 1) / 2 // 向下整除
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 交换元素 */
|
|
|
|
|
private func swap(i: Int, j: Int) {
|
|
|
|
|
maxHeap.swapAt(i, j)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
func size() -> Int {
|
|
|
|
|
maxHeap.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
|
size() == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 访问堆顶元素 */
|
|
|
|
|
func peek() -> Int {
|
|
|
|
|
maxHeap[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
func push(val: Int) {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 添加节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
maxHeap.append(val)
|
|
|
|
|
// 从底至顶堆化
|
|
|
|
|
siftUp(i: size() - 1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 从节点 i 开始,从底至顶堆化 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
private func siftUp(i: Int) {
|
|
|
|
|
var i = i
|
|
|
|
|
while true {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 获取节点 i 的父节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
let p = parent(i: i)
|
2023-08-20 14:51:39 +08:00
|
|
|
|
// 当“越过根节点”或“节点无须修复”时,结束堆化
|
2023-01-29 14:03:30 +08:00
|
|
|
|
if p < 0 || maxHeap[i] <= maxHeap[p] {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 交换两节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
swap(i: i, j: p)
|
|
|
|
|
// 循环向上堆化
|
|
|
|
|
i = p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
|
func pop() -> Int {
|
2023-01-29 14:03:30 +08:00
|
|
|
|
// 判空处理
|
|
|
|
|
if isEmpty() {
|
|
|
|
|
fatalError("堆为空")
|
|
|
|
|
}
|
2023-12-02 06:21:34 +08:00
|
|
|
|
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
2023-01-29 14:03:30 +08:00
|
|
|
|
swap(i: 0, j: size() - 1)
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 删除节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
let val = maxHeap.remove(at: size() - 1)
|
|
|
|
|
// 从顶至底堆化
|
|
|
|
|
siftDown(i: 0)
|
|
|
|
|
// 返回堆顶元素
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
|
/* 从节点 i 开始,从顶至底堆化 */
|
2023-01-29 14:03:30 +08:00
|
|
|
|
private func siftDown(i: Int) {
|
|
|
|
|
var i = i
|
|
|
|
|
while true {
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 判断节点 i, l, r 中值最大的节点,记为 ma
|
2023-01-29 14:03:30 +08:00
|
|
|
|
let l = left(i: i)
|
|
|
|
|
let r = right(i: i)
|
|
|
|
|
var ma = i
|
|
|
|
|
if l < size(), maxHeap[l] > maxHeap[ma] {
|
|
|
|
|
ma = l
|
|
|
|
|
}
|
|
|
|
|
if r < size(), maxHeap[r] > maxHeap[ma] {
|
|
|
|
|
ma = r
|
|
|
|
|
}
|
2023-08-20 14:51:39 +08:00
|
|
|
|
// 若节点 i 最大或索引 l, r 越界,则无须继续堆化,跳出
|
2023-01-29 14:03:30 +08:00
|
|
|
|
if ma == i {
|
|
|
|
|
break
|
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
|
// 交换两节点
|
2023-01-29 14:03:30 +08:00
|
|
|
|
swap(i: i, j: ma)
|
|
|
|
|
// 循环向下堆化
|
|
|
|
|
i = ma
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 打印堆(二叉树) */
|
|
|
|
|
func print() {
|
|
|
|
|
let queue = maxHeap
|
|
|
|
|
PrintUtil.printHeap(queue: queue)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@main
|
|
|
|
|
enum MyHeap {
|
|
|
|
|
/* Driver Code */
|
|
|
|
|
static func main() {
|
|
|
|
|
/* 初始化大顶堆 */
|
|
|
|
|
let maxHeap = MaxHeap(nums: [9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
|
|
|
|
|
print("\n输入列表并建堆后")
|
|
|
|
|
maxHeap.print()
|
|
|
|
|
|
|
|
|
|
/* 获取堆顶元素 */
|
|
|
|
|
var peek = maxHeap.peek()
|
|
|
|
|
print("\n堆顶元素为 \(peek)")
|
|
|
|
|
|
|
|
|
|
/* 元素入堆 */
|
|
|
|
|
let val = 7
|
|
|
|
|
maxHeap.push(val: val)
|
|
|
|
|
print("\n元素 \(val) 入堆后")
|
|
|
|
|
maxHeap.print()
|
|
|
|
|
|
|
|
|
|
/* 堆顶元素出堆 */
|
2023-03-13 22:31:05 +08:00
|
|
|
|
peek = maxHeap.pop()
|
2023-01-29 14:03:30 +08:00
|
|
|
|
print("\n堆顶元素 \(peek) 出堆后")
|
|
|
|
|
maxHeap.print()
|
|
|
|
|
|
|
|
|
|
/* 获取堆大小 */
|
|
|
|
|
let size = maxHeap.size()
|
|
|
|
|
print("\n堆元素数量为 \(size)")
|
|
|
|
|
|
|
|
|
|
/* 判断堆是否为空 */
|
|
|
|
|
let isEmpty = maxHeap.isEmpty()
|
|
|
|
|
print("\n堆是否为空 \(isEmpty)")
|
|
|
|
|
}
|
|
|
|
|
}
|