AI EducademyAIEducademy
🌳

AI基础

🌱
AI 种子

从零开始

🌿
AI 萌芽

打好基础

🌳
AI 枝干

付诸实践

🏕️
AI 树冠

深入探索

🌲
AI 森林

精通AI

🔨

AI精通

✏️
AI 草图

从零开始

🪨
AI 雕刻

打好基础

⚒️
AI 匠心

付诸实践

💎
AI 打磨

深入探索

🏆
AI 杰作

精通AI

🚀

职业准备

🚀
面试发射台

开启你的旅程

🌟
行为面试精通

掌握软技能

💻
技术面试

通过编程轮次

🤖
AI与ML面试

ML面试精通

🏆
Offer与未来

拿下最好的Offer

查看所有学习计划→

实验室

已加载 7 个实验
🧠神经网络游乐场🤖AI 还是人类?💬提示实验室🎨图像生成器😊情感分析器💡聊天机器人构建器⚖️伦理模拟器
🎯模拟面试进入实验室→
学习旅程博客
🎯
关于

让AI教育触达每一个人、每一个角落

❓
常见问题

Common questions answered

✉️
Contact

Get in touch with us

⭐
Open Source

在 GitHub 上公开构建

立即开始
AI EducademyAIEducademy

MIT 许可证。开源项目

学习

  • 学习计划
  • 课程
  • 实验室

社区

  • GitHub
  • 参与贡献
  • 行为准则
  • 关于
  • 常见问题

支持

  • 请我喝杯咖啡 ☕
  • 服务条款
  • 隐私政策
  • 联系我们
AI & 工程学习计划›✏️ AI 草图›课程›堆与优先队列
⛰️
AI 草图 • 中级⏱️ 18 分钟阅读

堆与优先队列

What Is a Heap?

A heap is a complete binary tree where every parent satisfies the heap property - either always smaller (min-heap) or always larger (max-heap) than its children. "Complete" means every level is full except possibly the last, which fills left to right.

       1            Min-heap
      / \
     3   5
    / \
   7   4

Because it's complete, we store it in a flat array - no pointers needed. For index i: left child = 2i + 1, right child = 2i + 2, parent = (i - 1) // 2.

Min-Heap vs Max-Heap

| Property | Min-Heap | Max-Heap | |----------|----------|----------| | Root | Smallest element | Largest element | | Parent ≤ Children? | ✅ Yes | ❌ No (≥) | | Extract gives | Minimum | Maximum |

Python's heapq is a min-heap by default. For a max-heap, negate values on insert and negate again on extract.

Insert and Extract - Step by Step

Insert - push to end, then bubble up (swap with parent while smaller):

import heapq
h = [1, 3, 5, 7]
heapq.heappush(h, 2)  # h becomes [1, 2, 5, 7, 3]

Extract min - swap root with last element, pop last, then sift down (swap with smaller child):

smallest = heapq.heappop(h)  # returns 1

Both operations run in O(log n) because the tree height is log n.

Diagram showing insert bubble-up and extract sift-down on a min-heap
Insert bubbles up; extract-min sifts down.

Heapify - Building a Heap in O(n)

Calling heapq.heapify(arr) converts a list into a heap in O(n), not O(n log n). It works bottom-up, sifting down each non-leaf node. This is one of the most surprising complexity results in CS.

第 6 课,共 10 课已完成 0%
←树与图可视化

Discussion

Sign in to join the discussion

建议修改本课内容
in-place
🤯
Building a heap is O(n), not O(n log n). Most nodes are near the bottom and barely need to sift down - the maths works out to linear time.

The Priority Queue Abstraction

A priority queue lets you always access the highest-priority item first. Heaps are the go-to implementation because both insert and extract are O(log n). Use one any time you need "give me the best item so far."

Pattern: Top-K Problems

"Find the K largest elements in an unsorted array."

Keep a min-heap of size K. For each element, push it; if the heap exceeds K, pop the smallest. What remains are the K largest.

import heapq
def top_k(nums, k):
    return heapq.nlargest(k, nums)

Time: O(n log k) - much better than sorting when k ≪ n.

🧠小测验

You need the 5 largest scores from 1 million entries. What heap type and size?

Pattern: Merge K Sorted Lists

Push the head of each list into a min-heap. Pop the smallest, then push the next element from that same list. Repeat until all lists are exhausted. Time: O(N log K) where N is total elements.

Pattern: Median from Data Stream

Maintain two heaps: a max-heap for the lower half and a min-heap for the upper half. Balance their sizes so they differ by at most 1. The median is always at one or both roots.

Lower half (max-heap): [1, 2, 3]  ← max = 3
Upper half (min-heap): [4, 5, 6]  ← min = 4
Median = (3 + 4) / 2 = 3.5
🧠小测验

In the two-heap median approach, where does a new element go first?

Scheduling Problems

Heaps shine in scheduling: meeting rooms (track earliest end-time), task scheduling with cooldowns, or CPU job queues. The key insight is that a heap efficiently tracks "what finishes next."

🤔
Think about it:If you need both the minimum AND maximum efficiently, a single heap won't do. What data structure combination would you use?

When to Use Heap vs Sorted Array vs BST

| Need | Best choice | Why | |------|-------------|-----| | Repeated min/max extraction | Heap | O(log n) insert + extract | | Static data, one-time sort | Sorted array | O(n log n) once, O(1) access | | Range queries + order stats | Balanced BST | O(log n) for everything | | Top-K from a stream | Heap of size K | O(n log k) total |

🧠小测验

Which operation is O(1) in a min-heap?

Complexity Cheat Sheet

| Operation | Time | |-----------|------| | Insert | O(log n) | | Extract min/max | O(log n) | | Peek min/max | O(1) | | Heapify | O(n) | | Top-K from n items | O(n log k) |

🤔
Think about it:Why can't you do binary search on a heap even though it's stored in an array? Think about what sorted order the heap property actually guarantees.

📚 Further Reading

  • NeetCode - Heap / Priority Queue - visual problem roadmap with heap problems grouped
  • Tech Interview Handbook - Heap - concise patterns and tips
  • Python heapq docs - official module reference with examples