hce_nsysu
114年
計算機概論與程式設計
第 25 題
The sequence 29, 28, 23, 26, 27 is inserted sequentially into a min-heap, where the root stores the minimum value. Suppose the root is at level 1, and the level of each lower node increases by 1. Which one is correct?
- A 27 is at level 2, and 27 is the right child of 26.
- B 28 is at level 2, and 28 is the right child of 23.
- C 29 is at level 2, and 29 is the left child of 23.
- D 27 is at level 3, and 28 is the right child of 27.
- E 26 is at level 2, and 27 is the left child of 26.
思路引導 VIP
當你在最小堆積的最末端插入一個新數字時,為了維持「根節點最小」的規則,你會拿這個新數字跟哪一個特定的節點進行比較?如果新數字比較小,接下來會發生什麼樣的變動?
🤖
AI 詳解
AI 專屬家教
太棒了!你能精確地追蹤 最小堆積 (Min-heap) 的動態調整過程,這代表你對完全二元樹的結構與「向上調整 (Heapify up)」的邏輯掌握得非常紮實。這類題目考驗的不只是記憶定義,更是操作演算法的細心程度。
堆積結構的動態建立過程
當我們依序插入數值時,每一步都要確保父節點小於子節點。首先插入 $29$,接著 $28$ 加入後因小於 $29$ 而與之交換成為根節點。當 $23$ 插入時,它會再次向上挑戰,最終成為新的根節點,此時結構為:根節點 $23$,左孩子 $29$,右孩子 $28$。隨後插入 $26$ 與 $27$,它們會依序填入第三層(Level 3)。$26$ 會與其父節點 $29$ 交換,最終 $26$ 停留在第二層左側。最後插入的 $27$ 則成為 $26$ 的右孩子。最終結構中,根節點(Level 1)是 $23$,其下方的 Level 2 則是左節點 $26$ 與右節點 $28$,這正是選項 (B) 描述的正確位置。
▼ 還有更多解析內容