Practice Final for CSc 227: Expression Trees, Binary Trees, and Ordered Maps, Exams of Computer Science

Practice final questions for csc 227, covering topics such as expression trees, binary trees, and ordered maps. Students are required to write code to implement methods like nodesatlevel, traversals, maxkey, and keysgreaterthan. The document also includes examples and expected outputs.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-la2
koofers-user-la2 🇺🇸

5

(1)

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
C Sc 227 Practice Final SL _______________ Name ______________________________
1. To class ExpressionTree, add method nodesAtLevel that returns the number of nodes at a
particular depth. By definition, the root is at depth 0, its children are at depth 1, and the children of its
children are at depth 2, and so on.
ExpressionTree threeLevels = new ExpressionTree("- + 9 8 ^ 5 4");
// 4
// ^
// 5
//-
// 8
// +
// 9
assertEquals(1, threeLevels.nodesAtLevel(0));
assertEquals(2, threeLevels.nodesAtLevel(1));
assertEquals(4, threeLevels.nodesAtLevel(2));
assertEquals(0, threeLevels.nodesAtLevel(3));
assertEquals(0, threeLevels.nodesAtLevel(4));
public class ExpressionTree {
private class TreeNode {
private String data;
private TreeNode left;
private TreeNode right;
public TreeNode(String theData) {
data = theData;
left = null;
right = null;
}
}
// . . .
public int nodesAtLevel(int level) {
pf3
pf4
pf5

Partial preview of the text

Download Practice Final for CSc 227: Expression Trees, Binary Trees, and Ordered Maps and more Exams Computer Science in PDF only on Docsity!

C Sc 227 Practice Final SL _______________ Name ______________________________

1. To class ExpressionTree, add method nodesAtLevel that returns the number of nodes at a

particular depth. By definition, the root is at depth 0, its children are at depth 1, and the children of its

children are at depth 2, and so on.

ExpressionTree threeLevels = new ExpressionTree("- + 9 8 ^ 5 4"); // 4 // ^ // 5 //- // 8 // + // 9 assertEquals (1, threeLevels.nodesAtLevel(0)); assertEquals (2, threeLevels.nodesAtLevel(1)); assertEquals (4, threeLevels.nodesAtLevel(2)); assertEquals (0, threeLevels.nodesAtLevel(3)); assertEquals (0, threeLevels.nodesAtLevel(4)); public class ExpressionTree { private class TreeNode { private String data; private TreeNode left; private TreeNode right; public TreeNode(String theData) { data = theData; left = null; right = null; } } //... public int nodesAtLevel(int level) {

2. Use the following binary tree to write out each of the three traversals indicated below.

Preorder traversal __________________________________________________

Inorder traversal __________________________________________________

Postorder traversal __________________________________________________

3. Is this a Binary Search Tree (yes or no)? _______

M

G R

G H X

4. Is this a Binary Search Tree (yes or no)? _______

M

R H

X Q G

5a. What potential benefit is there to using a binary search tree rather than an array to store the same

collection of elements?

5b. What would be required of those objects to be stored in a binary search tree?

6. Draw a picture of the data that results from the following code on your OrderedMap class. Just write the

keys, do not write the values.

OrderedMap<String, Integer> aBST = new OrderedMap<String, Integer>(); aBST.put("Matrix", 5); aBST.put("Antz", 3); aBST.put("Bean", 2); aBST.put("Shaft", 2); aBST.put("Scream", 3); aBST.put("Titanic", 4);

b) Write the complete public method with an iterative solution.

8. To the OrderedMap class, add method Set keysGreaterThan(K key) to return the set of keys

in the OrderedMap object that are greater than the argument key. The assertions must pass.

@Test public void testKeyGreater() { OrderedMap<Integer, String> om = new OrderedMap<Integer, String>(); om.put(50, "A"); om.put(60, "B"); om.put(45, "C"); om.put(55, "D"); Set set = om.keysGreaterThan(50); assertTrue (set.contains(55)); assertTrue (set.contains(60)); assertFalse (set.contains(45)); assertFalse (set.contains(50)); }

// Complete this method

public Set keysGreaterThan(K key) {

9. Complete the output that would be generated by this code

ArrayList list = new ArrayList(); list.add(12); list.add(23); list.add(-5); list.add(-5); list.add(12); Collections.sort(list); for (Integer theNextInt : list) { System.out.print(theNextInt + " "); } System.out.println("\n==="); System.out.println(list.contains(-5)); System.out.println(list.contains(9999)); System.out.println(Collections.min(list)); System.out.println(Collections.binarySearch(list, 23)); System.out.println("==="); Collections.reverse(list); Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.print(itr.next() + " "); }

10. Complete intersection so it returns a TreeSet that only has elements contained in both sets.

Set ts1 = new TreeSet(); ts1.add(1); ts1.add(3); ts1.add(2); ts1.add(4); Set ts2 = new TreeSet(); ts2.add(8); ts2.add(2); ts2.add(4); ts2.add(6); ts2.add(0); // The intersection of {1, 2, 3, 4} and {0, 2, 4, 6, 8} should be in result as {2,4} Set result = intersection(ts1, ts2); Iterator itr = result.iterator(); assertEquals(2, (int) itr.next()); assertEquals(4, (int) itr.next()); assertFalse(itr.hasNext()); public Set intersection (Set s1, Set s2) {