DOC PREVIEW
UMD CMSC 132 - Midterm #2 Key

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC132 Summer 2007Midterm #2 KEYFirst Name: _______________________Last Name: _______________________Student ID: _______________________Section time ___________ TA: __________________________General Rules (Read):- This exam is closed book and closed notes.- If you have a question, please raise your hand.- Total point value is 100 points. - Answer True/False questions by circling the True or False at the end of the question.- Answer essay questions concisely using 1 or 2 sentences. Longer answers are not necessary and arediscouraged.- WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit).1. (20 pts) Heaps/TreesUse the following binary tree/heap to answer the questions that follow.1Grader Use Only:#1 (20)#2 (10)#3 (12)#4 (12)#5 (10)#6 (36)Total (100)a. What is the big O notation for heapsort?Answer: O(nlog(n))b. Heaps are balanced trees. True/False.Answer: Truec. A heap is a binary search tree. True/False.Answer: Falsed. List the order nodes are traversed in an depth-search traversal of the treeAnswer: 8,9,11,13,10,12,14  One possible traversale. List the order nodes are traversed in a breadth-first traversal of the treeAnswer: 8, 9, 10, 11, 13, 12, 14  One possible traversalf. (4 pts) Draw the heap that would result from inserting 5 in the above heap.g. (4 pts) Draw the heap that would result by deleting 8 from the original heap.891113101214589131012141122. (10 pts) Huffmana. (2 pts) Use the above Huffman tree to encode the string “BD”. Show the resulting code.Answer: 00110b. (2 pts) Use the above Huffman tree to decode the code “0111”. Show the resulting string.Answer: CEc. (6 pts) Create a Huffman tree for the symbols M, P, R, and T which have the following frequencies:M : 4 P : 5 R : 8 T : 7You don’t need to assign 0’s and 1’sAnswer:3. (12 pts) Hashinga. (2 pts) Using hashing we can achieve a complexity of O(1) during the retrieval of an element. 91114131012CED11101000BAM PTR1110003True/False. Answer: Trueb. (2 pts) What is the load factor?i. Number of entries in the hash table divided by table sizeii. Maximum number of entries that can be placed in the tableiii. The load generated by the multiplicative congruency methodiv. None of the above.Answer: ic. (2 pts) The hashCode method for a class always returns the number 14. Is this a valid hashCodemethod for the class? True/False.Answer: Trued. (2 pts) Hashing is efficient for a load factor that is less than:i. 99%ii. 90%iii. 80%iv. None of the above.Answer: iie. (4 pts) Mentioned two properties of a good hash function? Notice we are not asking about the properties of the Java hashCode method.Answer: Any two valid ones. Possible ones:1. Cheap to compute2. Good distribution of values4. (16 pts) Dijkstras’ AlgorithmRun Dijkstra’s shortest path algorithm on the previous graph using B as the start vertex. Showthe entries in the following table after adding the first 3 nodes (B & 2 other nodes) to the set Sof processed nodes (as defined by Djikstra’s algorithm). Keep in mind that after adding a nodeto the set S you must adjust the cost/predecessor of the appropriate successor nodes. Answer:A B C D E FAFDBEC214574224LowestCost 3 0 ∞ 2 5 5Predecessor D none none B A B5. (10 pts) GUIsConsider the following Java Swing code that uses a JSlider object. A JSlider object let’s the user graphically select a value by sliding a knob. The following is a snapshot of a slider: The stateChanged method is called when we move the knob.public class JSliderPanel extends JPanel {private JSlider jSlider;public JSliderPanel() {add(jSlider=new JSlider(JSlider.HORIZONTAL));jSlider.addChangeListener(new Handler());}class Handler implements ChangeListener {public void stateChanged(ChangeEvent e) {System.out.println("Slider action");}}}a. (2 pts) In the above code, which class represents the view?Answer: JSliderPanelb. (2 pts) In the above code, which class represents the controller?Answer: Handlerc. (6 pts) Write a new JSliderPanel constructor that uses an anonymous inner class to implement the same ChangeListener for the JSlider. Answer: (Code that should appear in // YOUR CODE HERE)jSlider.addChangeListener(new ChangeListener() {public void stateChanged(ChangeEvent e) {System.out.println("Slider action");}});public class JSliderPanel extends JPanel {private JSlider jSlider;public JSliderPanel() {add(jSlider=new JSlider(JSlider.HORIZONTAL));// YOUR CODE HERE}}6. (36 pts) Binary Treesa. (2 pts) We generate a balanced binary search tree when elements are inserted in sorted order. True/False5Answer: Falseb. The following Java class definition for a binary search tree will be used to answer the questionsbelow. Feel free to add an auxiliary method during the implementation of any method. Non-recursive solutions will receive no credit.public class BinarySearchTree <E> {class Node<T> { T data; Node <T> left, right; public Node(T data) {this.data = data;left = right = null; }}Node <E> root; // Tree Root// YOU MUST IMPLEMENT A DEFAULT CONSTRUCTORpublic int numberOfLeaves() {// YOU MUST IMPLEMENT THIS METHOD}public ArrayList<E> decreasingOrder() {// YOU MUST IMPLEMENT THIS METHOD}}i. (2 pts)Constructor – Define a default constructor that creates an empty binary search tree. Answer: public BinarySearchTree() {root = null;}ii. (4 pts) isEmpty – Returns true if the tree is empty and false otherwise. Answer: public boolean isEmpty() {if (root == null)return true;return false;}iii. (14 pts) numberOfLeaves – Implement a method named numberOfLeaves . The methodreturns the number of leave nodes present in the tree. Answer:public int numberOfLeaves() {return numberOfLeavesAux(root);}private int numberOfLeavesAux(Node<E> currRoot) {6if (currRoot != null) {if (currRoot.left == null && currRoot.right == null)return 1;else if (currRoot.left == null)return numberOfLeavesAux(currRoot.right);else if (currRoot.right == null)return numberOfLeavesAux(currRoot.left);elsereturn numberOfLeavesAux(currRoot.left) + numberOfLeavesAux(currRoot.right);}return 0; }iv. (14 pts) decreasingOrder – Implement a method named decreasingOrder . It returns anarray with the data elements of the tree in decreasing order. public ArrayList<E> decreasingOrder() {ArrayList<E> result = new ArrayList<E>();decreasingOrderAux(root, result);return result;}private void decreasingOrderAux(Node<E> currRoot, ArrayList<E> result) {if (currRoot != null) {if (currRoot.right != null)


View Full Document

UMD CMSC 132 - Midterm #2 Key

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download Midterm #2 Key
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm #2 Key and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm #2 Key 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?