CSE 2100 Data Structures and Introduction to Algorithms...! In Java!! Instructor: Fei Wang! Mid-Term Exam CSE2100 DS & Algorithms 1
1. True or False (20%=2%x10)! (1) O(n) is O(n^2) (2) The height h of a binary search tree of size n is O(log n) (3) In an AVL tree, the depths of any two external nodes differ by at most 1 (4) Splay tree is balanced (5) A stack follows the first-in-first-out rule (6) A heap is a binary search tree (7) An AVL tree is a heap (8) In a circular doubly linked list with 10 nodes, we will need to change 4 links if we want to delete a node other than the head node (9) Inserting an element to the beginning of an array (that is A[0] element) is more difficult than inserting an element to the beginning of a linked list (10) The worst-case height of a binary search tree is O(log n) CSE2100 DS & Algorithms 2
15 9 20 5 12 15 30 4 7 16 40 6 6 CSE2100 DS & Algorithms 3
2. (10%) Draw a binary tree with 10 nodes numbered from 1 to 10 for which the preorder and post- order traversals are 5, 2, 1, 3, 4, 7, 6, 8, 9, 10, respectively 1, 4, 3, 2, 6, 10, 9, 8, 7, 5. Is there more than one tree satisfying these properties? CSE2100 DS & Algorithms 4
3. (10%) Describe a linear-time algorithm that lists the nodes of a binary tree in level-order: first list the root, then the nodes of depth 1, followed by nodes of depth 2, and so on. Nodes at the same depth should be listed in left-to-right order. This can be achieved by using a FIFO queue Q. We start by inserting the root of the tree in Q, then, while Q is not empty we extract the node at the front of the queue and insert its children (in left-to-right order) at the end of the queue. CSE2100 DS & Algorithms 5
4. (10%) Draw the 11-entry hash table that results from using the hash function (3i+1) mod 11, to hash the keys 12,44,13,88,23,94,11,39,20,16 and 5, assuming collisions are handled by (1) chaining and (2) linear probing. 0 1 2 3 4 5 6 7 8 9 10 44 12 16 20 13 94 88 23 39 11 0 1 2 3 4 5 6 7 8 9 10 44 88 11 12 23 20 13 94 39 16 CSE2100 DS & Algorithms 6
5. (20%) In the following questions, consider the list of numbers: 62, 31, 70, 91, 25, 11, 9, 61, 73, 6. a). Show the result of inserting the numbers in the list in the same order specified above into an initially empty minimum heap. Note that you need to show how the heap looks like after each number is inserted. (5%) 25 9 6 31 70 31 11 9 11 61 62 70 25 61 31 70 25 82 62 91 91 73 62 CSE2100 DS & Algorithms 7
b) Show the result of inserting the numbers in the list in the same order specified above into an initially empty binary search tree. Note that you need to show how the binary search tree looks like after each number is inserted. (5%) 62 62 62 31 70 31 70 31 70 61 25 91 61 25 91 25 91 11 11 73 9 9 6 CSE2100 DS & Algorithms 8
c) Use the binary search tree you created in question 2. What are the two possible binary search trees after 62 is deleted? (5%) 61 70 31 70 31 91 25 91 25 61 73 11 73 11 9 9 6 6 CSE2100 DS & Algorithms 9
d) Explain how you can utilize a heap to sort the list of numbers in descending order. (3%) Let n be the number of elements in the list. What is the time complexity of your sorting algorithm? (2%) (1) step1: Delete the root from the min heap and put it into stacka. O(1) step2: Restructure the min heap. O(log n) step3: Go step1 until the min heap is empty. ntimes step4: Pop items from stacka.(the result is in descending order.) O(n) (2) O(n log n) CSE2100 DS & Algorithms 10
6. (10%) Given an AVL tree shown below, please draw the process on how such AVL tree will adjust after we insert 40 and 10. 15 9 20 12 30 CSE2100 DS & Algorithms 11
15 9 20 12 30 40 CSE2100 DS & Algorithms 12
15 9 30 12 20 40 CSE2100 DS & Algorithms 13
15 9 30 12 20 40 10 CSE2100 DS & Algorithms 14
15 9 30 12 20 40 10 CSE2100 DS & Algorithms 15
15 9 30 10 20 40 12 CSE2100 DS & Algorithms 16
15 10 30 9 20 12 40 CSE2100 DS & Algorithms 17
7. (10%) For a tree T, let ni denote the number of its internal nodes, ne denote the number of its external nodes. Show that if every internal node in T has exactly C children, then ne=(c-1)ni+1 CSE2100 DS & Algorithms 18
For ni = 0, then ne = 1. For ni = 1, then ne = C-1 +1 = C. Assume the ne equation holds true for k < k, i.e., for any ni = k < k, ne = (C-1)nI +1. Now consider ni = k. Then, ne = (C-1)(k 1) + 1 + C-1. That is, the number of external nodes is equal to the number of external nodes for a tree with k 1 internal nodes plus C (we added an internal node which must have C children) minus 1 (in creating the new internal node, we made an external node into an internal node). Thus, ne = (C-1)k+1. CSE2100 DS & Algorithms 19
8. (10%) The Tower of Hanoi is a mathematical game. The game consists of 3 rods, and n disks of different sizes which can slide onto any rod. The puzzle starts with the disks stack in ascending order of size on rod number 1 (the smallest disk is on the top). The objective of the game is to move the entire stack to rod number 3, obeying the following rules: a). Only one disk can be moved at a time. b). Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. c). No disk may be placed on top of a smaller disk. In this problem, please write an algorithm to output a series of moves which solve the Tower of Hanoi problem. The input and output are as follows: Input: one single number which represents the number of disks on the rod Output: Each line of output consists of a pair (i, j), which represents a move from rod number i to rod number j. The sequence of moves should move the entire stack from rod number 1 to rod number 3. Example: Input: 3; Output: (1,3) (1,2) (3,2) (1,3) (2,1) (2,3) (1,3) CSE2100 DS & Algorithms 20
hanoi(from, tmp, to, n){ if(n==1){ move(from,to); return; } hanoi(from, to, tmp, n-1); System.out.println("("+from.toString()+","+to.toString()+")") hanoi(tmp, from, to, n-1); return; }! CSE2100 DS & Algorithms 21
9. (10% Extra Credit) Let A and B be two sets of integer keys stored at the internal nodes of two AVL trees, T A and T B, respectively. Suppose that all keys are distinct. We say that A separates B if there is a triplet of keys x<y<z,such that x and z are in B and y is in A. Design an algorithm that takes as input T A andt B and determines whether or not A separates B. Describe the algorithm in English or pseudo-code, including any additional data structures. For full credit the algorithm must have worst-case time complexity of O(log n), where n denotes the total number of keys in A and B. CSE2100 DS & Algorithms 22
Without loss of generality we can assume that x and z are the minimum, respectively the maximum keys of B, and we have to decide in O(log n) time if A contains a key y between them. We can do so by searching x=min(b) in T A. Let u the node at which the search stops (u is either an internal node storing key x or an external node). Find the internal node v following u in inorder using (possibly twice) the inordernext() method from problem 3. If the key stored at v is smaller than z=max(b), then it can be used as y, proving that A separates B. Otherwise no key of A is between x and z, and A does not separate B. The overall running time is O(log n) since the heights of AVL trees T A and T B are O(log n), and thus computing x=min(b) and z=max(b), searching for x in T A, and the calls to inordernext() take O(log n) time each. CSE2100 DS & Algorithms 23
CSE 2100 Data Structures and Introduction to Algorithms...! In Java!! Instructor: Fei Wang! Sorting CSE2100 DS & Algorithms 24
Merging The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x 1 x 2 x m ) and Y(y 1 y 2 y n ) the resulting list is Z(z 1 z 2 z m+n ) Example: L 1 = { 3 8 9 } L 2 = { 1 5 7 } merge(l 1, L 2 ) = { 1 3 5 7 8 9 } CSE2100 DS & Algorithms 25
Merging X: 3 10 23 54 Y: 1 5 25 75 Result: CSE2100 DS & Algorithms 26
Merging 3 10 23 54 5 25 75 X: Y: Result: 1 CSE2100 DS & Algorithms 27
Merging 10 23 54 5 25 75 X: Y: Result: 1 3 CSE2100 DS & Algorithms 28
Merging 10 23 54 25 75 X: Y: Result: 1 3 5 CSE2100 DS & Algorithms 29
Merging X: Y: 23 54 25 75 Result: 1 3 5 10 CSE2100 DS & Algorithms 30
Merging X: Y: 54 25 75 Result: 1 3 5 10 23 CSE2100 DS & Algorithms 31
Merging X: Y: 54 75 Result: 1 3 5 10 23 25 CSE2100 DS & Algorithms 32
Merging X: Y: 75 Result: 1 3 5 10 23 25 54 CSE2100 DS & Algorithms 33
Merging X: Y: Result: 1 3 5 10 23 25 54 75 CSE2100 DS & Algorithms 34
Divide and Conquer Merging a two lists of one element each is the same as sorting them. Merge sort divides up an unsorted list until the above condition is met and then sorts the divided parts back together in pairs. Specifically this can be done by recursively dividing the unsorted list in half, merge sorting the right side then the left side and then merging the right and left back together. CSE2100 DS & Algorithms 35
Merge Sort Given a list L with a length k: If k == 1 à the list is sorted Else: Merge Sort the left side (1 thru k/2) Merge Sort the right side (k/2+1 thru k) Merge the right side with the left side CSE2100 DS & Algorithms 36
Example 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 37
Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 38
Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 39
Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 CSE2100 DS & Algorithms 40
Example 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 4 0 CSE2100 DS & Algorithms 41
Example 99 6 86 15 58 35 86 0 4 Merge 4 0 CSE2100 DS & Algorithms 42
Example 6 99 15 86 58 35 0 4 86 99 6 86 15 58 35 86 0 4 Merge CSE2100 DS & Algorithms 43
Example 6 15 86 99 0 4 35 58 86 6 99 15 86 58 35 0 4 86 CSE2100 DS & Algorithms 44
Example 0 4 6 15 35 58 86 86 99 6 15 86 99 0 4 35 58 86 CSE2100 DS & Algorithms 45
Example 0 4 6 15 35 58 86 86 99 CSE2100 DS & Algorithms 46
Implementation There are two basic ways to implement merge sort: In Place: Merging is done with only the input array Pro: Requires only the space needed to hold the array Con: Takes longer to merge because if the next element is in the right side then all of the elements must be moved down. Double Storage: Merging is done with a temporary array of the same size as the input array. Pro: Faster than In Place since the temp array holds the resulting array until both left and right sides are merged into the temp array, then the temp array is appended over the input array. Con: The memory requirement is doubled. CSE2100 DS & Algorithms 47
CSE2100 DS & Algorithms 48