Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the.

Similar presentations


Presentation on theme: "Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the."— Presentation transcript:

1 Data Structures -3 st exam- 授課教師 : 李錫智 教授

2 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the height of the tree. ANS: Calculate the height to be 1 + max(the height of the left subtree, the height of the right subtree) recursively.

3 2. [10] Please answer the following questions: (a) [5] What is the minimum height of a binary tree with n nodes? Prove it. Note that no credit will be given for an answer without a sound proof. (b) [5] What is the maximum height of a binary tree with n nodes?

4 Ans: (a) (b) 當所有節點都成一直線時, Tree 會有最大 高度,即 N

5 3. [10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using quicksort. Note that the first element of the underlying sequence is used as the pivot. Draw the array after the execution of each partition.

6 ANS: – 原始資料 : 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. 1: 8, 3, 17, 22, 32, 7, 12, 42, 47, 74 2: 7, 3, 8, 22, 32, 17, 12, 42, 47, 74 3: 3, 7, 8, 22, 32, 17, 12, 42, 47, 74 4: 3, 7, 8, 17, 12, 22, 32, 42, 47, 74 5: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 6: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 紅色代表此次的 pivot ,綠色代表已經排到 正確的位置

7 4.[5] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using mergesort. Draw the array after each merge step. ANS:

8 5.[10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using insertion sort. Draw the array after each insertion. ANS: 1: 42, 3, 17, 22, 32, 7, 12, 74, 47, 8 2: 3, 42, 17, 22, 32, 7, 12, 74, 47, 8 3: 3, 17, 42, 22, 32, 7, 12, 74, 47, 8 4: 3, 17, 22, 42, 32, 7, 12, 74, 47, 8 5: 3, 17, 22, 32, 42, 7, 12, 74, 47, 8 6: 3, 7, 17, 22, 32, 42, 12, 74, 47, 8 7: 3, 7, 12, 17, 22, 32, 42, 74, 47, 8 8: 3, 7, 12, 17, 22, 32, 42, 74, 47, 8 9: 3, 7, 12, 17, 22, 32, 42, 47, 74, 8 10: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74

9 6.Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using bubble sort. Draw the array after each pass. Ans: Pass 1: 3, 17, 22, 32, 7, 12, 42, 47, 8, 74 Pass 2: 3, 17, 22, 7, 12, 32, 42, 8, 47, 74 Pass 3: 3, 17, 7, 12, 22, 32, 8, 42, 47, 74 Pass 4: 3, 7, 12, 17, 22, 8, 32, 42, 47, 74 Pass 5: 3, 7, 12, 17, 8, 22, 32, 42, 47, 74 Pass 6: 3, 7, 12, 8, 17, 22, 32, 42, 47, 74 Pass 7: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 8: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 9: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74 Pass 10: 3, 7, 8, 12, 17, 22, 32, 42, 47, 74

10 7. [10] Consider an array A containing 10 integers 42, 3, 17, 22, 32, 7, 12, 74, 47, 8. Please sort the integers in ascending order using selection sort. Draw the array after each swap. Ans: 42,3,17,22,32,7,12,74,47,8 42,3,17,22,32,7,12,8,47,74 8,3,17,22,32,7,12,42,47,74 8,3,17,22,12,7,32,42,47,74 8,3,17,7,12,22,32,42,47,74 8,3,12,7,17,22,32,42,47,74 8,3,7,12,17,22,32,42,47,74 7,3,8,12,17,22,32,42,47,74 3,7,8,12,17,22,32,42,47,74

11 8.[10] Consider the integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order. (a) [5] Please use these numbers to create a binary search tree and draw the final tree. (b) [5] Please delete 41 from the tree and show the resulting binary search tree. Note that the replacement should be the least of the numbers equal to or greater than the deleted element.

12 ANS: (a) 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67

13 ANS: (b)

14 9. [15] Consider the binary tree shown in Figure 1. Please display the nodes in preorder, inorder, and postorder, respectively. Ans: Preorder: 44,18,20,61,50,46,54,78,90 Inorder: 20,18,44,46,50,54,61,90,78 Postorder: 20,18,46,54,50,90,78,61,44

15 10. Explain why the worst time complexity of quicksort is O(n 2 ). Ans: In each sequence, the smallest(or largest) item is chosen as the pivot, then the sequence can not be divided into two partition.

16 11.Consider Figure 2. This is a minheap tree. If we want to delete the root. Please describe the process and show the final heap tree. Ans: 10 2515 30 262029 2515 302620 15 2529 302620 15 2520 302629 Delete Root

17 12.Consider Figure 2. This is a minheap tree. If we want to insert 21. Please describe the process and show the final heap tree. Ans: 10 2515 30262029 10 2515 30262029 21 10 2515 21262029 30 10 2115 25262029 30 Insert 21

18 13. Consider an array A containing 12 integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67. (a) [10] Please convert A into a maxheap. Draw the final array. (b) [5] Please describe how you sort the integers in ascending order from the maxheap obtained in (a). Ans: (a) 94, 75, 70, 65, 68, 67, 25, 23, 29, 41, 30, 37 (b) Each time we delete the root, place it in front of the output sequence, and then adjust the tree to be a maxheap again. Do 12 times, and we will have a sorted output sequence.


Download ppt "Data Structures -3 st exam- 授課教師 : 李錫智 教授. 1. [5] Assume we have a binary tree which is implemented in a pointer-based scheme. Describe how to know the."

Similar presentations


Ads by Google