← 回題庫

卷別

資料結構與演算法

選擇

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

(10 points) A Binary Search Tree (BST) is comprised of nodes that maintain a specific order among their keys. Given a single node xx and two BSTs LL and RR, in which l.key<x.key<r.keyl.\text{key} < x.\text{key} < r.\text{key} for all nodes l∈Ll \in L and r∈Rr \in R, the objective is to design a Join(L,x,R)\text{Join}(L, x, R) function that integrates the input into a unified BST TT with the nodes L∪{x}∪RL \cup \{x\} \cup R. AVL and Red-Black trees are specific types of BSTs. Given the constraint that TT, LL, and RR must share the same type, they are required to be either all BSTs, all AVL trees, or all Red-Black trees.

The Join(L,x,R)\text{Join}(L, x, R) function is implemented with the steps: (1) selecting a subtree T′T' from either LL or RR (a subtree may be empty, a portion, or the entire tree), (2) detaching T′T', (3) attaching T′T' under the node xx, (4) attaching the other tree (which does not contain T′T') under xx, and (5) re-attaching the tree rooted at xx to the former parent of T′T'. The modified tree is returned as TT. If T′T' has no former parent, the tree rooted at xx is simply returned as TT.

Almost all steps except (1) have been finalized — there could be multiple ways to choose T′T' in (1), so the tree returned may not be unique. Unlike AVL or Red-Black trees, Join(L,x,R)\text{Join}(L, x, R) does not perform re-coloring and re-balancing. Consider the following arguments passed into Join(L2,x2,R2)\text{Join}(L_2, x_2, R_2). All nil nodes (considered as leaves in red-black trees) are omitted for brevity.

  • L2L_2: root 22, with left child 11 and right child 33.
  • x2=4x_2 = 4.
  • R2R_2: root 1010; 10.left=710.\text{left}=7, 10.right=1310.\text{right}=13; 7.left=57.\text{left}=5, 7.right=97.\text{right}=9; 5.right=65.\text{right}=6; 9.left=89.\text{left}=8; 13.left=1113.\text{left}=11, 13.right=1513.\text{right}=15; 11.right=1211.\text{right}=12; 15.left=1415.\text{left}=14, 15.right=1615.\text{right}=16; 16.right=1716.\text{right}=17.

Please select the correct description(s) below.

  • (A)
    If no additional balance is required for L2L_2 and R2R_2, i.e., both L2L_2 and R2R_2 are BSTs, there are 6 possible tree shapes that could be returned from Join(L2,x2,R2)\text{Join}(L_2, x_2, R_2).
  • (B)
    If both L2L_2 and R2R_2 are AVL trees as observed, among all possible tree shapes returning from Join(L2,x2,R2)\text{Join}(L_2, x_2, R_2), there is a unique tree that is an AVL tree.
  • (C)
    There are multiple ways to assign colors to the nodes in R2R_2 to make R2R_2 a red-black tree.
  • (D)
    There exists a color assignment to all nodes in L2∪{x2}∪R2L_2 \cup \{x_2\} \cup R_2 such that: (1) both L2L_2 and R2R_2 are red-black trees, (2) a red-black tree can be returned from Join(L2,x2,R2)\text{Join}(L_2, x_2, R_2), and (3) the node with the key 4 is BLACK.
  • (E)
    There exists a unique color assignment to all nodes in L2∪{x2}∪R2L_2 \cup \{x_2\} \cup R_2 such that: (1) both L2L_2 and R2R_2 are red-black trees, (2) a red-black tree can be returned from Join(L2,x2,R2)\text{Join}(L_2, x_2, R_2), and (3) the node with the key 1 is BLACK.

標準解析

AI 已複查