And give a big theta θ bound for code fragments

Info icon This preview shows pages 10–12. Sign up to view the full content.

View Full Document Right Arrow Icon
and give a Big-Theta (Θ( . . . )) bound. For code fragments whose running time is a constant, do not give a specific number of steps, use symbolic constants c 1 , c 2 , etc. instead. 29. In this problem you NOT allowed to use any of the theorems about Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. Prove that 2 2 n is not O (2 n ). 30. In this problem you NOT allowed to use any of the theorems about Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. Suppose 0 < f ( n ) < g ( n ) for all n > 1000. Suppose also that g ( n ) is O ( n 2 ). Let h ( n ) = f ( n ) p g ( n ). Prove that h ( n ) is O ( n 3 ). 31. Let A be an integer array of length n . Give an O ( n ) algorithm to compute another array B such that B [ i ] = max(Σ i j =0 A [ j ] , Σ n - 1 j = i +1 A [ j ]). You can use pseudocode (a mixture of code and English) to describe the algorithm. (You do not need to justify that your algorithm works, just give it.) 32. For each statement below, decide whether it is true or false. In each case attach a very brief expla- nation of your answer. (a) We can always test if an element occurs in a doubly linked list twice as fast as we can test the same for a singly linked list, true or false? (b) Using a priority queue implemented as a binary min-heap, it is possible to remove the 9th smallest key from the priority queue in time O (log n ), true or false? (c) It is possible to implement priority queues with sorted lists such that removeMin can always be performed in constant time. (d) The first node visited in the inorder traversal of a binary tree is the same as the first node visited in the postorder traversal of the same tree, true or false? 33. A binary min-heap is storing 15 items in the array-list implementation starting with the root being at index 1. Write the sequence of array indices visited in a pre-order traversal of the tree. Do the same for inorder traversal. 34. Draw the sequence of binary min-heaps that results from starting with the empty heap and performing the operations add(6), add(5), add(4), add(3), add(2), add(1), removeMin() in the given order. Circle the final heap. 10
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
35. Recall that a complete binary tree of height h has 2 h +1 - 1 nodes. How many nodes does a complete ternary tree of height h have? 36. Consider the Java code: interface Queue { // Queues of ints! public int peek(); public void enqueue(int x); public int dequeue(); public boolean empty(); } class QueueImpl implements Queue{ // OMMITTED; IT DOESN’T MATTER HOW THEY ARE IMPLEMENTED ... static Queue merge(Queue q1, Queue q2) { if (q1 == null) q1 = new QueueImpl(); if (q2 == null) q2 = new QueueImpl(); Queue rq = new QueueImpl(); // MISSING CODE, YOUR TASK IS TO WRITE THIS return rq; } } It is assumed that in q1 and q2 the integers are in ascending order from front to back. Write the missing code such that the method merge returns a queue containing the integers in both q1 and q2 , again ordered ascendingly from front to back.
Image of page 11
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern