← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2024 · 考試 · 第 2 題

(5 points) A Maximum Binary Tree (abbreviated as MaxBT) is defined as a binary tree that adheres to the max-heap property without the necessity of following the complete binary tree property. In a MaxBT, each node pp consists of a value (p.datap.\text{data}) and two links (p.leftp.\text{left} and p.rightp.\text{right}) pointing to the left and right children, respectively. A link to nil indicates the absence of further children.

The provided pseudocode, although incomplete, implements a recursive function for merging two MaxBTs into a single MaxBT. This function takes the root nodes of the two MaxBTs (root1root1 and root2root2) as input and returns the root node of the merged MaxBT.

1:  function MergeMaxBinaryTrees(root1, root2)
2:      if root1 = nil then return  (a)   end if
3:      if root2 = nil then return  (b)   end if
4:      if root1.data > root2.data then
5:            (c)  ← MergeMaxBinaryTrees(  (d) ,  (e) )
6:          return  (f)
7:      else
8:            (g)  ← MergeMaxBinaryTrees(  (h) ,  (i) )
9:          return  (j)
10:     end if
11: end function

Assuming all the blanks in the above function have been appropriately filled to ensure its proper functioning, please select the correct description(s) below.

  • (A)
    (b) and (f) are the same.
  • (B)
    (c), (d), and (e) are all distinct.
  • (C)
    In the cases of (c) and (g), each has only one correct answer.
  • (D)
    Exchanging the filled content of (h) and (i) leads to a correct implementation.
  • (E)
    The function works correctly even when the two arguments passed into it refer to the same tree.

標準解析

AI 已複查