This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 minheap, 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 minheap is storing 15 items in the arraylist implementation starting with the root being at index 1. Write the sequence of array indices visited in a preorder traversal of the tree. Do the same for inorder traversal. 34. Draw the sequence of binary minheaps 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 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. 37. Suppose 0 < f ( n ) < g ( n ) for all n > 1000. For each of the following, if true, give a very brief justification, if false, give a very brief counterexample. (a) f ( n ) is O ( g ( n )), true or false? (b) g ( n ) f ( n ) is O ( f ( n )), true or false? (c) f ( n ) + g ( n ) is O ( g ( n )), true or false?...
View
Full Document
 Spring '09
 TANNEN
 Algorithms, Data Structures, Big O notation, Analysis of algorithms, Binary heap, Tree traversal, static int foo

Click to edit the document details