Trees and heaps
導入済み
7:45
データ構造とアルゴリズム
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| binary search tree/ˈbaɪnəri sɜːtʃ triː/ | 二項探索木 |
| heap/hiːp/ | ヒープ |
| min-heap/mɪn hiːp/ | ミニヒープ |
| complete binary tree | |
| Extract-min/ˈekstrækt mɪn/ | the heap operation that removes and returns the smallest key, then restores the heap order |
| Heapsort/ˈhiːpsɔːt/ | a sort that builds a heap and repeatedly removes its top element; O(n log n) and in place |
| rotation/rəʊˈteɪʃn/ | A transformation that turns points through a specified angle about a fixed centre or axis. |
| AVL tree/ˌeɪ viː ˈel triː/ | A self-balancing binary search tree that keeps every node's left and right subtree heights within one of each other by rotating. |
Order rule, complete shape
- A binary search tree 二叉搜索树 keeps the left subtree below the node and the right subtree above it. Inserting $50, 30, 70, 20, 40, 60, 80$ builds a perfect tree of height $2$, and the inorder walk reads $20, 30, 40, 50, 60, 70, 80$: sorted because of how the tree is built.
- A heap 堆 is a complete tree stored in an array: the children of index $i$ are at $2i+1$ and $2i+2$. A min-heap 小顶堆 keeps every parent at or below its children, so the minimum is at index $0$.

In a heap stored as an array, the children of index i sit at —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The complete shape fixes the mapping: left child 2i+1, right child 2i+2.
Height is the cost
- A perfect tree of height $h$ holds $2^{h+1} - 1$ nodes: $7$ at $h = 2$, $15$ at $h = 3$, $31$ at $h = 4$. A complete binary tree 完全二叉树 of $n$ nodes has height $\lfloor \log_2 n \rfloor$; balanced trees have height $O(\log n)$, and their leaves need not all be at the same depth.
- A skewed tree loses this. Inserting the sorted keys $1, 2, 3, 4$ makes a chain of height $3 = n - 1$, so search is $O(n)$. This is why AVL and red-black trees rotate to stay logarithmic.
A perfect tree of height 3 holds —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
2 to the h+1 minus 1: 16 minus 1 is 15.
Walks and sifts
- BST search for $40$: compare with $50$ (go left), $30$ (go right), $40$: found in $3$ comparisons among $7$ nodes. Inserting $35$ puts it to the left of $40$, and the inorder walk is still sorted.
- Build a min-heap 小顶堆 by inserting $5, 3, 8, 1$: $3$ sifts up past $5$; $1$ sifts up past $5$ and then $3$. The array reads $[1, 3, 8, 5]$, with every parent at or below its children.
heapq.heapifybuilds a heap from a whole array in $O(n)$ by sifting down from the middle.
Search costs the height
Insert 50, 30, 70, 20, 40, 60, 80 to get a perfect tree of height 2, then search for 40. The path visits 50, 30, 40, three comparisons for seven nodes.
Inserting 5, 3, 8, 1 into an empty min-heap leaves the array —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
3 sifts over 5; 1 sifts over 5 then 3 — parents stay at or below children.
The heap as a priority queue
- Extract-min 取出最小值 takes the root, moves the last element to index $0$ and sifts it down. From $[1, 3, 8, 5]$: remove $1$, move $5$ to the root to get $[5, 3, 8]$, then swap $5$ with its smaller child $3$: $[3, 5, 8]$. Insert and extract-min each cost $O(\log n)$.
- Heapsort 堆排序 builds a heap in $O(n)$ and then extracts $n$ times: $O(n \log n)$ in place, but not stable. A complete BST of $1000$ keys has height $\lfloor \log_2 1000 \rfloor = 9$, so a search takes at most $10$ node comparisons.
Extract-min on the min-heap $[2, 4, 3, 9, 6]$ leaves the array —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Move 6 to the root: [6, 4, 3, 9]; its smaller child is 3 at index 2, so swap: [3, 4, 6, 9].
What is the height of a perfectly balanced BST holding 4000 keys?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
floor(log2 4000) = 11, since 2^11 = 2048 and 2^12 = 4096.
Put the steps of extract-min on an array min-heap in order.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Moving the last element keeps the tree complete; sifting it down then restores the order invariant in at most the height, $O(\log n)$ swaps.
One rotation keeps it logarithmic
- An AVL tree AVL树 stores each node's balance factor (the height of its left subtree minus the height of its right subtree) and keeps it within $\pm 1$. Insert 1, 2, 3 in that order: node 1 reaches $-2$.
- A left rotation 旋转 makes 2 the root, with children 1 and 3: height 1 instead of 2. Each insert needs at most one single or double rotation, so search stays $O(\log n)$ whatever order the keys arrive in.
Inserting 1, 2, 3 into an empty AVL tree ends with root:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Node 1 reaches balance -2; a left rotation lifts 2 to the root.
Worked example: why heapify is O(n)
- Bottom-up heapify sifts down from the middle of the array. A node can sink only as far as its height, and most nodes are near the bottom.
- For $n = 15$: 4 nodes of height 1, 2 of height 2 and 1 of height 3 sink at most $4 + 4 + 3 = \mathbf{11}$ levels in total, fewer than $n$. In general the sum stays below $n$, so building a heap 堆 takes linear time, not $n \log n$.
Bottom-up heapify on 15 nodes: the most sift-down swaps in total?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
$4 \times 1 + 2 \times 2 + 1 \times 3 = 11$.
Building a heap from an unsorted array needs $\Theta(n \log n)$ time.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Inserting one at a time does; bottom-up heapify is $O(n)$.
Marks that slip away
- Assuming a BST is balanced. Sorted inserts build a chain of height $n - 1$, so search becomes $O(n)$.
- Confusing heap order with BST order. A heap 堆 only promises parent $\le$ children: $[1, 3, 8, 5]$ is a valid heap but not sorted.
- Wrong child indices. In a 0-based array the children of $i$ are $2i + 1$ and $2i + 2$, not $2i$ and $2i + 1$.
Recap
- BST: left $<$ node $<$ right; the inorder walk is sorted; the cost is the height.
- A balanced tree has $O(\log n)$ height; sorted inserts can make an $O(n)$ chain.
- Min-heap in an array: children of $i$ at $2i+1$ and $2i+2$; insert and extract-min 取出最小值 are $O(\log n)$.
| Balanced BST | Min-heap | |
|---|---|---|
| Order rule | left $<$ node $<$ right | parent $\le$ children |
| Find any key | $O(\log n)$ | $O(n)$ |
| Find the minimum | $O(\log n)$ | $O(1)$, at the root |
| Stored as | linked nodes | an array |
何が問題でしたか?この内容はサイトの運営チームだけが確認します。