Data Structures (3rd Exam)
1. [5] Determine whether or not the arrays A = [62, 40, 58, 26, 30, 57, 50, 16, 15], B = [56, 38, 55, 46, 16, 53, 48, 39, 15] are maxheaps. Explain your decisions.
1.A = [62, 40, 58, 26, 30, 57, 50, 16, 15]. Yes. A node >= its daughters
1.B = [56, 38, 55, 46, 16, 53, 48, 39, 15]. No. Node 38 < node 46
2.[10] Suppose you are given an array A = [13, 38, 57, 33, 27, 31, 46, 35, 26, 59]. Please adjust the elements to make A become a maxheap. Show the result.
2.A = [13, 38, 57, 33, 27, 31, 46, 35, 26, 59].
3.[10] Suppose you are given the following elements: 13, 38, 57, 33, 27, 31, 46, 35, 26, 59. Please create a maxheap by enqueueing the elements one by one in the order shown. Show the maxheap in an array.
3.13, 38, 57, 33, 27, 31, 46, 35, 26, 59.
4.[5] Suppose you are given an array A = [13, 38, 57, 33, 27, 31, 46, 35, 26, 59]. Please adjust the elements to make A become a minheap. Show the result.
4.A = [13, 38, 57, 33, 27, 31, 46, 35, 26, 59].
5.[5] Suppose you are given the following elements: 13, 38, 57, 33, 27, 31, 46, 35, 26, 59. Please create a minheap by enqueueing the elements one by one in the order shown. Show the minheap in an array.
5.13, 38, 57, 33, 27, 31, 46, 35, 26, 59.
6.[10] Suppose we use the hash function h(k) = k%13 to insert the following keys 37, 30, 41, 24, 17, 20, 27, 15, 28, 26 into the hash table with the chaining method. Assume that the hash table has 13 positions. Note that a key is inserted as the first element of the underlying chain. Please show your result.
6.37, 30, 41, 24, 17, 20, 27, 15, 28, 26.
7.[10] Consider the function given below: 1bool search( Node *ptr, Bird & bird, Bird dove ) 2{ 3 if ( ptr == NULL ) 4 return false; 5 if ( ptr->info == dove ) { 6 bird = ptr->info; 7 return true; 8 } 9 return search( ptr->next, bird, dove ); 10}
7.which is called for a linked list (where start points to the first node) using the lines of code: if ( search( start, bird, dove ) ) cout << “search successful” << endl; Please answer the following questions: a.In what condition will the function return false? b.In what condition will the function return true? c.What if there were no base case for this function? d.Suppose there are n elements in the linked list. What is the best-case time complexity for this function? e.Suppose there are n elements in the linked list. What is the worst-case time complexity for this function?
7. a. can’t find the desired information. b. Find the desired node. c. Runtime error : ptr = NULL. d. O(1) e. O(n)
8.[10] Suppose we have the following program: 1 for each j, from 1 to (length (A) – 1) 2 {temp = A[ j ]; 3 i = j – 1; 4 while i is greater than -1 and A[ i ] is greater than temp 5 {A[ i + 1 ] = A[ i ]; 6 i--;} 7 A[ i + 1] = temp} Let A = [60, 10, 50, 80, 30, 20, 40, 70]. Please show A after each iteration of the for loop.
8.A = [60, 10, 50, 80, 30, 20, 40, 70].
9.[10] Let A = [60, 10, 50, 80, 30, 20, 40, 70]. Please use heapsort to sort the elements in A In ascending order. Please draw A after the initial stage and each iteration. Please use maxheap.
9.A = [60, 10, 50, 80, 30, 20, 40, 70].
10.[10] Let A = [60, 10, 50, 80, 30, 20, 40, 70]. Please use mergesort to sort the elements in A in ascending order. Please draw the result after the merge of each level.
10.A = [60, 10, 50, 80, 30, 20, 40, 70].
11.[5] Suppose we have the following program: 1 for each j, from 1 to (length (A) – 1) 2 {temp = A[ j ]; 3 i = j – 1; 4 while i is greater than -1 and A[ i ] is greater than temp 5 {A[ i + 1 ] = A[ i ]; 6 i--;} 7 A[ i + 1] = temp} Let B = [80, 50, 30, 40, 90, 60, 20, 70]. Please show A after each iteration of the for loop.
11. B = [80, 50, 30, 40, 90, 60, 20, 70].
12.[5] Let B = [80, 50, 30, 40, 90, 60, 20, 70]. Please use heapsort to sort the elements in A In ascending order. Please draw A after the initial stage and each iteration. Please use maxheap.
12.B = [80, 50, 30, 40, 90, 60, 20, 70].
13.[5] Let B = [80, 50, 30, 40, 90, 60, 20, 70]. Please use mergesort to sort the elements in A in ascending order. Please draw the result after the merge of each level.
13.Let B = [80, 50, 30, 40, 90, 60, 20, 70].