Fall 06 CS 100 Test 2

Fall 06 CS 100 Test 2 - Test 2 Compsci 100 Owen Astrachan...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Test 2: Compsci 100 Owen Astrachan November 14, 2006 Name: Login: Honor code acknowledgment (signature) value grade Problem 1 30 pts. Problem 2 22 pts. Problem 3 18 pts. Problem 4 22 pts. TOTAL: 92 pts. This test has 17 pages, be sure your test has them all. Do NOT spend too much time on one question — remember that this class lasts 75 minutes. 1 Some common recurrences and their solutions. A T(n) = T(n/2) + O(1) O (log n ) B T(n) = T(n/2) + O(n) O ( n ) C T(n) = 2T(n/2) + O(1) O ( n ) D T(n) = 2T(n/2) + O(n) O ( n log n ) E T(n) = T(n-1) + O(1) O ( n ) F T(n) = T(n-1) + O(n) O ( n 2 ) G T(n) = 2T(n-1) + O(1) O (2 n ) In writing code you do not need to worry about specifying the proper import statements . Assume that all libraries and packages we’ve discussed are imported in any code you write. TreeNodes on this test are implemented using the following declaration which is nested inside a class Test in which all methods are written. public static class TreeNode { String info; TreeNode left; TreeNode right; TreeNode(String val, TreeNode lptr, TreeNode rptr) { info = val; left = lptr; right = rptr; } } 2 blank page 3 PROBLEM 1 : ( Oversees, Refugees, Guarantees, Search-trees (30 points) ) The tree below is a search tree. Part A (4 points) What is the pre-order traversal of the tree? (The first value printed is macaque ) macaque monkey baboon chimp tamarin lemur orangutan Part B (2 points) Provide a word (it doesn’t have to be a real word, it must contain at least four letters) that could be inserted as a left-child of monkey so that the tree is still a search tree. Part C (2 points) Show by drawing where a node with gibbon would be inserted into the tree. Part D (4 points) The tree above has a height of four and has three leaves. Draw a search tree with the same values, a height of four, and which has four leaves. Draw the tree on the previous page. continued → 4 Part E (4 points) The method printQ below prints one line for every node in a tree. The first value printed when called with the tree above is the string macaque . What is the complete output? public static void printQ(TreeNode root){ Queue<TreeNode> q = new LinkedList<TreeNode>(); if (root != null){ q.add(root); } while (q.size() != 0){ root = q.remove(); System.out.println(root.info); if (root.left != null) q.add(root.left); if (root.right != null) q.add(root.right); } } Part F (4 points) The method printS below prints one line for every node in a tree. The first value printed when called with the tree above is the string macaque . What is the complete output? public static void printS(TreeNode root){ Stack<TreeNode> st = new Stack<TreeNode>(); if (root != null){ st.push(root); } while (st.size() != 0){ root = st.pop(); System.out.println(root.info); if (root.right != null) st.push(root.right); if (root.left != null) st.push(root.left); } } continued → 5 Part G (4 points) The tree below is formed by copying the shape of the monkey/primate tree and storing in each node the count of how many nodes are in the subtree rooted at the node. This new tree could be created by the codecount of how many nodes are in the subtree rooted at the node....
View Full Document

This note was uploaded on 04/27/2008 for the course COMPSCI 100 taught by Professor Astrachan during the Fall '06 term at Duke.

Page1 / 17

Fall 06 CS 100 Test 2 - Test 2 Compsci 100 Owen Astrachan...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online