阿摩線上測驗 登入

109年 - 109國立臺灣大學_碩士班招生考試_資訊工程學研究所:資料結構與演算法(A)#106053

科目:台大◆資工◆資料結構與演算法(A) | 年份:109年 | 選擇題數:0 | 申論題數:21

試卷資訊

所屬科目:台大◆資工◆資料結構與演算法(A)

選擇題 (0)

申論題 (21)

5. (3 points) We will follow the notations from the previous question. We can build a heap from an array of n keys by the following algorithun. A heapify operation for a binary minimum heap combines two heaps and a root into a new heap. For ease of discussion we assume that the array is indexed from l to n, and the left/right child of a node with array index i have array indices 2: and 2i + 1 respectively. We first set each key of the second half of the aray (indexed from n/2 to n) as r:/2 heaps, each has one node. Then we combine two of them and their parent into a heap of three nodes, and so on. If the root is smaller than the roots of both subtrees then we stop. Otherwise we exchange the root with the smaller root from the subtrees, and repeatedly heapify the subtree where the root has just been replaced. Next we heapify the key at n/2 - 1 and all the way back to the root (at index 1). The final result will be a binary minimum heap of n nodes.
1. The minimum number of key comparisons is θ(n).
 2. The maximum number of key comparisons is θ(n).
3. Since each heapify on a key may compare it with all the keys down to a leaf, its number of comparisons is no more than the height of the heap, which is O(logn). Also we have O(n) keys to heapify, so the total number of key comparisons is O(n log n).
4. The number of key comparisons is in the same order as the sum of (original, before heapified) levels (as defined in the previous question, the distance to the root) of all keys.
5. The number of key comparisons is mazimized if and only if the largest n/2 keys are in the first half of the array and in decreasing order.
9. (3 points) A binary search tree is a tree where every n node has at most two children. For ease of discussion we assume that every y node has a distinct key. In addition, all keys in the left subtree are smaller than the key of root, and all keys in the right subtree are larger than the key of the root. A tree node is a leaf if it does not have any children. The height of the tree is the length of the longest path from the root to a leaf. The successor of a node z is the node right after a in increasing order, and the predecessor of a node z is the node right before z in increasing order.
 1. We can locate the smallest key in a binary search tree in O(h) time in the worst case, where h is the height of the tree.
 2. The height of a binary search tree of n nodes is O(logn.).
3. We can locate the Largest key in a binary search tree in O(logn) time in the worst case, where n is the number of the nodes in the tree.
 4. Assume that a node z has two children. We can remove z while still mainteining the sorted order by locating its successor y, connecting y's parent directly to the only child of y (if there is one, otherwise make it null), which effectively removes the node y from the tree, and replacing the key of z with that of y. 5. Assume that a node z has two children. We can remove z z while still maintaining the sorted order by locating its predecessor y, connecting y's parent directly to the only chid of y (iF there is one, otherwise make it null), which effectively removes the node y from the tree, and replacing the key of z with that of y.