Last Day Quiz Practice Questions for C Sc 227: Binary Search Trees and Ordered Maps, Quizzes of Computer Science

Practice questions for the last day quiz of c sc 227, focusing on binary search trees and ordered maps. Students are required to identify binary search trees, draw pictures, implement methods, and analyze code output.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-2j9-2
koofers-user-2j9-2 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Last Day Quiz Practice Questions SL _______________ Name ____________________
1. Is this a Binary Search Tree (yes or no)? _______
M
R
G
G
X
H
2. Is this a Binary Search Tree (yes or no)? _______
M
H
R
X
G
Q
3. What potential benefit is there to using a binary search tree rather than an array to store the same
collection of elements?
4. Draw the a picture of the binary search tree that results from the following code that uses your
OrderedMap class:
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);
5. Add a public method named maxKey to OrderedMap that returns a reference to the object with the
maximum value. Write two different implementations of a private helper method. One method must have an
iterative solution. The other method will have a recursive solution. The code shown to the left must generate
the output shown to the right. Assume that when this code runs all methods are in your class OrderedMap.
Remember that the OrderedMap class has an instance variable named root to reference the root of the BST
in OrderedMap and it uses MapNode objects to store objects. You may assume the key has the compareTo
method (it implements Comparable).
OrderedMap<Double, String> doubleTree =
new OrderedMap<Double, String>();
System.out.println("\nMax key is " + doubleTree.maxKey());
OrderedMap<Integer, String> intTree =
new <Integer, String> OrderedMap();
intTree.put(5, "A");
intTree.put(8, "B");
intTree.put(-3, "C");
intTree.put(4, "D");
System.out.println("\nMax key is " + intTree.maxKey());
Output
Max key is null
Max key is 8
pf2

Partial preview of the text

Download Last Day Quiz Practice Questions for C Sc 227: Binary Search Trees and Ordered Maps and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Last Day Quiz Practice Questions SL _______________ Name ____________________

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

M

G R

G H X

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

M

R H

X Q G

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

collection of elements?

4. Draw the a picture of the binary search tree that results from the following code that uses your

OrderedMap class:

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);

5. Add a public method named maxKey to OrderedMap that returns a reference to the object with the

maximum value. Write two different implementations of a private helper method. One method must have an

iterative solution. The other method will have a recursive solution. The code shown to the left must generate

the output shown to the right. Assume that when this code runs all methods are in your class OrderedMap.

Remember that the OrderedMap class has an instance variable named root to reference the root of the BST

in OrderedMap and it uses MapNode objects to store objects. You may assume the key has the compareTo

method (it implements Comparable).

OrderedMap<Double, String> doubleTree = new OrderedMap<Double, String>(); System.out.println("\nMax key is " + doubleTree.maxKey() ); OrderedMap<Integer, String> intTree = new <Integer, String> OrderedMap(); intTree.put(5, "A"); intTree.put(8, "B"); intTree.put(-3, "C"); intTree.put(4, "D"); System.out.println("\nMax key is " + intTree.maxKey() );

Output

Max key is null Max key is 8

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

b) Write the public and private methods using a recursive solution.

6. Write the output generated by the following code:

Map<String, BankAccount> tm = new TreeMap<String, BankAccount>(); tm.put("M", new BankAccount("Michel", 111.11)); tm.put("G", new BankAccount("Georgie", 222.22)); tm.put("S", new BankAccount("Sam", 333.33)); tm.put("P", new BankAccount("Pei", 333.33)); tm.put("Z", new BankAccount("Zac", 333.33)); Set keys = tm.keySet(); for (String str : keys) System.out.print(str + " "); Collection accounts = tm.values(); for (BankAccount next : accounts) System.out.println(next.getID());

7. Write the code the will print the toString version of the value associated with the key "G" in the previous

question. The output should be Georgie $222.