practice_final

# practice_final - 0 AVL Trees a Underneath each of the...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0 AVL Trees a. Underneath each of the diagrams below, indicate which of the following kinds of structures the diagram might reasonably represent by writing the corresponding letters: If the diagram represents a tree, indicate its height if not indicate why it is not a tree. A diagram A. Tree may represent more than one of B. Binary search tree the choices | write all the letters that apply. In determining if an C. Tree with AVL property ordering property holds, consider D. AVL tree the items at the nodes to be their own keys and consider strings to be ordered alphabetically. i) Madison / \ Cincinnati Milwaukee / \ \ Chicago Detroit Minneapolis \ St. Louis ii) (21) / \ (5) (13) / \ / (3) (2) (8) \ / (1) iii) Madison / \ Milwaukee Chicago / \ / \ Minneapolis St. Louis Detroit Columbus / Cincinnati 0 Consider the following AVL tree: (13) / \ (3) (21) / \ (2) (5) Suppose we insert a node with key (8) into this tree. b. Show the tree resulting from the standard binary tree insertion of the key 8 into this tree. c. Show at which point this tree violates the AVL tree property | speci cally, indicate the critical node. d. State which kind of rotation is required to restore the AVL tree property. e. Show the tree resulting from that rotation. 1 1 Graphs Consider the following graph: E B F D G A C a. Starting with node B, list one possible depth- rst traversal of the graph. b. Starting with node A, list one possible breadth- rst traversal of the graph. 2 Below is an adjacency matrix representing a directed graph: 0 1 2 3 4 5 6 7 0 false true false true true false false false 1 false false true true false false false false 2 false false false true false false false true 3 false false false false false false false false 4 false false false true false false false false 5 true true true false false false true true 6 true false false false false false false false 7 false false false true true false true false c. Draw the corresponding graph. d. Recall, a reachability matrix for a directed graph is an boolean matrix such that entry ( ) is true if and only if there is a path from node to node . (Assume that is not automatically reachable from itself.) Show the reachability matrix for this graph. n n i j i j i 3 2 Hashing Suppose we are inserting strings into a hash table of size 9. Suppose we have two hash functions, hash0 and hash1 . Their values for certain strings are indicated by the table below: string hash0 hash1 \Mo" 2 6 \Adam" 4 1 \Chloe" 5 4 \Fred" 4 5 \Bartholomew" 3 2 \Cheryl" 6 3 \Rebecca" 7 7 \Reggie" 6 8 a. Fill in the hash table below assuming that we are using open-address, linear-probing style hashing, given that the table starts as it appears below, the hash function is hash0 , and the sequence of insertions is: insert(``Fred'') insert(``Chloe'') insert(``Adam'') insert(``Rebecca'') insert(``Reggie'') 0 1 2 3 \Bartholomew" 4 5 6 7 8 4 b. Fill in the hash table below assuming that we are using open-address, double hashing, given that the table starts as it appears below, the main hash function is hash0 , the double hash function is hash1 , and the sequence of insertions is the same as above. 0 1 2 3 \Bartholomew" 4 5 6 7 8 c. Fill in the hash table below assuming that we are using chained hashing, given that the table starts as it appears below, the main hash function is hash0 , the double hash function is hash1 , and the sequence of insertions is the same as above. 0 1 2 3 7;! \Bartholomew" 4 5 6 7 8 d. Is hash0 a monotone hash function? Brie y state why or why not. e. Is hash1 a monotone hash function? Brie y state why or why not. 5 3 Sorting Recall our item-key pair struct: template <class Item, class Key> struct ikPair Item it Key key f g Consider the following sorting algorithm which sorts an array of item-key pairs based on their keys: template <class Item, class Key> void bubbleSort(ikPair<Item,Key> A f , size_t n) for (size_t i = n i > 0 i--) for (size_t j = 0 j+1 < i j++) if (A j+1].key <= A j].key) swap(A j+1], A j]) displayArray(A,n) f g g where we can assume, given an array A of item-key pairs of size n, displayArray(A, outputs each item-key pair in the array in the order in which the pairs occurs in the array (i.e. A 0], A 1], ..., A n-1]), separated by commas as in the following example: n) f``foo'',1g, f``bar'',2g, f``bash'',5g, f``bogus'',3g] a. Show the result of displayArray(A,n) after each iteration of the outer loop during a call to bubbleSort(A,4), given that A is an array of four elements of type ikPair<string,int> with initial contents: f``bread'',4g, f``cheese'',1g, f``apples'',7g, f``spinach'',0g] 6 A sort over item-key pairs is said to be stable if, for any two item-key pairs with the same key, the sort preserves the original order of those pairs. For example, if A is: f3.14,1g, f2.71,2g, f1.41,1g] a stable sort will preserve the fact that the pair with item 1.41 comes after the pair with item 3.14: f3.14,1g, f1.41,1g, f2.71,2g] b. The bubbleSort function given above is not stable. Either brie y explain why it is not stable or illustrate with an example. c. The bubbleSort function can be made stable with a small change. Indicate the change in the code above and brie y explain why it solves the problem. d. What is the worst-case running-time complexity of bubbleSort in terms of -notation? List at least one sort we studied in class that has the same or worse worst-case complexity. O e. What is the best-case running-time complexity of bubbleSort in terms of -notation? List at least one sort we studied in class that has a better best-case complexity. O 7 ...
View Full Document

## This note was uploaded on 03/27/2008 for the course CS 367 taught by Professor Marvinsolomon during the Spring '08 term at University of Wisconsin.

Ask a homework question - tutors are online