京都大学 情報学研究科 知能情報学専攻 2017年2月実施 情報学基礎 F-1
Author
祭音Myyura (co-authored with GPT 5.6 SOL)
Description
Q.1
Binary search trees are a widely used data structure in computer science.
-
Give the definition of a binary search tree in 100 words.
-
When searching for the number in a binary search tree that has distinct integers in , which of the following sequences could not be the sequence of nodes visited? Give the reason for your choice(s).
- (a)
- (b)
- (c)
- (d)
- (e)
-
Suppose that the search for a key in a binary search tree ends at a leaf. Let be the keys to the left of the search path, the keys on the search path, and the keys to the right of the search path. Must every , , and satisfy ? If yes, prove it. If not, give a counterexample with the minimum number of nodes and explain it.
Q.2
Finding the -th smallest element in an array of distinct numbers is called Selection.
- Give a Selection algorithm taking time for an unsorted array .
- Give pseudocode for another Selection algorithm based on partitioning that runs in linear time on average, and prove its time complexity.
题目描述
- 回答二叉搜索树相关问题:
- 用不超过 100 词定义二叉搜索树。
- 在由 内不同整数构成的二叉搜索树中查找 ,判断给定的五个序列中哪些不可能是访问节点序列,并说明理由。
- 将一条以叶节点结束的搜索路径左侧、路径上、路径右侧的键分别记为 。判断是否总有 ;若否,给出节点数最少的反例。
- 在含 个不同数的未排序数组 中求第 小元素:
- 给出 时间算法;
- 给出基于划分、平均线性时间的算法伪代码,并证明复杂度。
Kai
Q.1
1.1
A binary search tree is a rooted binary tree whose nodes have distinct keys. For every node , every key in the left subtree of is smaller than , and every key in the right subtree is larger than . Both subtrees satisfy the same property recursively. Consequently, an inorder traversal lists the keys in increasing order.
1.2
During the search, maintain the open interval in which the next visited key must lie. Initially . At a visited key , set if , and set if .
- (a) is impossible. After , the interval is , but the next key is .
- (d) is impossible. After , the interval is , but the next key is .
- (b), (c), and (e) respect the interval at every step and hence can occur.
Thus the answer is
1.3
No. The following six-node tree is a minimum counterexample. The search for follows the marked path .
Choose , , and . Then , so is false.
Both and must be nonempty, requiring at least two off-path nodes. If the path had at most three nodes, it would have at most two branch decisions. To make both side sets nonempty, those decisions would have to be right, left or left, right; in either case every left-side key is below every path key and every right-side key is above every path key. Thus a violation needs at least four path nodes. Hence at least nodes are necessary, and the example is minimum.
Q.2
2.1
Sort by merge sort and return the element at rank :
SELECTION-BY-SORTING(Q, p)
MERGE-SORT(Q)
return Q[p] // indices start at 1
Merge sort takes time, and the final access takes time. Thus the total is .
2.2
Use randomized quickselect.
RANDOMIZED-SELECT(Q, p)
if |Q| = 1
return Q[1]
choose a pivot q uniformly at random from Q
partition Q into L = {x < q} and R = {x > q}
if p = |L| + 1
return q
if p <= |L|
return RANDOMIZED-SELECT(L, p)
return RANDOMIZED-SELECT(R, p - |L| - 1)
One partition costs time. If the pivot has rank , the recursive subproblem has size at most . Therefore
Moreover,
Assuming inductively that for , the recurrence gives
for . Thus the expected running time is . Since partitioning inspects all elements, it is also , hence