{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Spring 08 CS 100 Test 2

# Spring 08 CS 100 Test 2 - Test 2 Compsci 100 Owen Astrachan...

This preview shows pages 1–5. Sign up to view the full content.

Test 2: Compsci 100 Owen Astrachan April 2, 2008 Name: (2 points) Login: Honor code acknowledgment (signature) value grade Problem 1 32 pts. Problem 2 6 pts. Problem 3 22 pts. Problem 4 20 pts. TOTAL: 80 pts. This test has 12 pages, be sure your test has them all. Do NOT spend too much time on one question — remember that this class lasts 75 minutes. 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. Unless indicated otherwise, here’s the TreeNode class for this test. public static class TreeNode { String info; TreeNode left; TreeNode right; TreeNode(String val, TreeNode lptr, TreeNode rptr) { info = val; left = lptr; right = rptr; } } 1

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

View Full Document
PROBLEM 1 : ( Trees (32 points) ) The questions in this problem will use the tree below. iguana eagle octopus bison hedgehog badger crayfish hyena wallaby Part A (4 points) The inorder traversal of the tree below is: badger, bison, crayﬁsh, eagle, hedgehog, hyena, iguana, octopus, wallaby. What is the post-order traversal? Part B (6 points) Show where the values fox , koala , and zebra would appear if inserted into the search tree above by adding the nodes in the diagram. Part C (4 points) Complete the method below that returns a copy of the tree shown above (in general a copy of any tree). public TreeNode copy(TreeNode root){ if (root == null) // add code return new TreeNode(root.info, copy(root.left), ); // add code } 2
Part D (5 points) Write method oneChildCount that returns the number of nodes in a tree that have one child (e.g., not zero children and not two children). In the tree at the beginning of this problem the value returned would be two since the nodes labeled octopus and hedgehog in the original tree each have one child. public int oneChildCount(TreeNode root) { } Part E (5 points) Implementing a priority queue using a balanced search tree results in add , peek , and remove operations that are all O (log N ) for a N -element priority queue. However, heaps are typically used to implement priority queues because they oﬀer better performance. Explain why heaps are better than balanced binary trees for implementing priority queues (be brief). 3

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

View Full Document
Part F (8 points) The two methods print and printItems below generate the following output: iguana eagle bison badger iguana eagle bison crayfish iguana eagle hedgehog hyena iguana octopus wallaby when invoked with the call print(tree,new LinkedList<String>()); where tree is the root of the tree on the previous page. public static void printItems(LinkedList<String> list){ Queue<String> copy = (Queue<String>) list.clone(); while (copy.size() != 0){ System.out.print(copy.remove()+" "); } System.out.println(); } public static void print(TreeNode root, LinkedList<String> q){ if (root == null) return; q.add(root.info); printItems(q); q.removeLast(); return; } q.add(root.info); print(root.left,q);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

Spring 08 CS 100 Test 2 - Test 2 Compsci 100 Owen Astrachan...

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

View Full Document
Ask a homework question - tutors are online