Trees and heaps
แนะนำใหม่ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| binary search tree/ˈbaɪnəri sɜːtʃ triː/ | ต้นไม้ค้นหาแบบสองทาง |
| heap/hiːp/ | ฮีป |
| min-heap/mɪn hiːp/ | ฮีปต่ำสุด |
| 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$. So a balanced tree of $n$ nodes has height $\lfloor \log_2 n \rfloor$, and every walk from the root to a leaf pays that height.
- 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 balanced BST of $1000$ keys has height $\lfloor \log_2 1000 \rfloor = 9$, so search, insert and delete take about $10$ 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.
- Balanced height is $\lfloor \log_2 n \rfloor$; sorted inserts 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 |
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้