卷別
資料結構與演算法
選擇國立臺灣大學 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 and two BSTs and , in which for all nodes and , the objective is to design a function that integrates the input into a unified BST with the nodes . AVL and Red-Black trees are specific types of BSTs. Given the constraint that , , and must share the same type, they are required to be either all BSTs, all AVL trees, or all Red-Black trees.
The function is implemented with the steps: (1) selecting a subtree from either or (a subtree may be empty, a portion, or the entire tree), (2) detaching , (3) attaching under the node , (4) attaching the other tree (which does not contain ) under , and (5) re-attaching the tree rooted at to the former parent of . The modified tree is returned as . If has no former parent, the tree rooted at is simply returned as .
Almost all steps except (1) have been finalized — there could be multiple ways to choose in (1), so the tree returned may not be unique. Unlike AVL or Red-Black trees, does not perform re-coloring and re-balancing. Consider the following arguments passed into . All nil nodes (considered as leaves in red-black trees) are omitted for brevity.
- : root , with left child and right child .
- .
- : root ; , ; , ; ; ; , ; ; , ; .
Please select the correct description(s) below.
- (A)If no additional balance is required for and , i.e., both and are BSTs, there are 6 possible tree shapes that could be returned from .
- (B)If both and are AVL trees as observed, among all possible tree shapes returning from , there is a unique tree that is an AVL tree.
- (C)There are multiple ways to assign colors to the nodes in to make a red-black tree.
- (D)There exists a color assignment to all nodes in such that: (1) both and are red-black trees, (2) a red-black tree can be returned from , and (3) the node with the key 4 is BLACK.
- (E)There exists a unique color assignment to all nodes in such that: (1) both and are red-black trees, (2) a red-black tree can be returned from , and (3) the node with the key 1 is BLACK.