Practice Quiz V - Program Design and Development | C SC 227, Quizzes of Computer Science

Material Type: Quiz; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Summer 2008;

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-e5c-1
koofers-user-e5c-1 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227 Practice Quiz 5 Name ________________________________ 50pts
1. Binary Tree Traversals. Use the following tree to write out the order of each of the three traversals (12pts)
A
C
B
D
E
F
I
G
H
Preorder traversal __________________________
Inorder traversal __________________________
Postorder traversal __________________________
2. Is this a Binary Search Tree (yes or no)? _______ (4pts)
M
R
G
G
X
H
3. Is this a Binary Search Tree (yes or no)? _______ (4pts)
M
H
R
X
G
Q
For the final two questions of this practice quiz, add the methods as if they are in the following
OrderedMap<K,V> class. Remember that OrderedMap has an instance variable named root to reference
the root of the OrderedMap object and it uses the MapNode class.
// This heading ensures that the key implements Comparable so you can use the
// compareTo method on the key. If type K does not, the compiler complains.
public class OrderedMap<K extends Comparable<K>, V> {
private class MapNode {
private K key;
private V value;
private MapNode left;
private MapNode right;
public MapNode(K theKey, V theValue) {
key = theKey;
value = theValue;
left = null;
right = null;
}
} // end class MapNode
private MapNode root;
public OrderedMap() { // Create an empty tree
root = null;
}
// …
}
pf3
pf4

Partial preview of the text

Download Practice Quiz V - Program Design and Development | C SC 227 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227 Practice Quiz 5 Name ________________________________ 50pts

1. Binary Tree Traversals. Use the following tree to write out the order of each of the three traversals (12pts)

A

B C

D E F

G H I

Preorder traversal __________________________

Inorder traversal __________________________

Postorder traversal __________________________

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

M

G R

G H X

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

M

R H

X Q G

For the final two questions of this practice quiz, add the methods as if they are in the following

OrderedMap<K,V> class. Remember that OrderedMap has an instance variable named root to reference

the root of the OrderedMap object and it uses the MapNode class.

// This heading ensures that the key implements Comparable so you can use the // compareTo method on the key. If type K does not, the compiler complains. public class OrderedMap<K extends Comparable, V> { private class MapNode { private K key; private V value; private MapNode left; private MapNode right; public MapNode(K theKey, V theValue) { key = theKey; value = theValue; left = null; right = null; } } // end class MapNode private MapNode root; public OrderedMap() { // Create an empty tree root = null; } // … }

4. To the OrderedMap class, add public method isFull that returns true if the OrderedMap object has a full

binary search tree. A full tree is a tree in which each node has exactly zero or two children.. For example,

the following trees are full. (15pts)

emptyTree M M M / \ /
G R G R / \ /
B K P W

These binary trees are not full:

M M M M

/ \ / \ / \

R R G R G R

/ / \ \

K B K W

public boolean isFull() {

Answers

A

B C

D E F

G H I

Preorder traversal A B D E G C F H I

Inorder traversal D B G E A H F I C

Postorder traversal D G E B H I F C A

2) No

3) No

public boolean isFull() { return isFull(root); } private boolean isFull(MapNode t) { if (t == null ) return true ; else if ((t.left == null && t.right != null ) || (t.left != null && t.right == null )) return false ; else return isFull(t.left) && isFull(t.right); }

public int numberOfKeysGreaterThan(K key) { return keysGreaterThan(key, root); } private int keysGreaterThan(K key, MapNode t) { if (t == null ) return 0; else { int result = 0; if (t.key.compareTo(key) > 0) result = 1; return result + keysGreaterThan(key, t.left)

  • keysGreaterThan(key, t.right); } }