Sie sind auf Seite 1von 8

Chapter 10: Non-Linear Data Structures

Test Bank
Multiple Choice Questions:
1) A set is a collection of elements
a) that must consist of integers
b) that cannot contain the value null
c) that has no duplicates
d) that are kept in sorted order
e) that must be Strings
Answer: c. Explanation: The defining property of a set is that it may not have duplicates. A set can contain any kind of
data (including null) and does not need to be kept in sorted order.
2) Which of the following is not a set?
a) {-1, 1, 0}
b) {1, 1, 1}
c) {1, 2, 3}
d) {a, b, c}
e) {3, 2, 1}
Answer: b. Explanation: Answer b is not a set because it contains duplicates.
3) Which of the following is true about the keys in a map?
a) They form a set.
b) There may be duplicate keys as long as they map to different values.
c) They must be integers.
d) There are typically fewer keys than values in a map.
e) They should be mutable.
Answer: a. Explanation: The keys in a map must be unique and they therefore form a set. None of the other choices are
true.
4) Which of the following is an example of a map?
a) A sci-fi novel
b) A car
c) A tree
d) A telephone
e) A dictionary
Answer: e. Explanation: A dictionary maps keys (words) to values (definitions) so it is an example of a map.
5) Which of the following could not be the set of keys for a map?
a) {a, b, c, d, e}
b) {b, e, a, d, c}
c) {a}
d) {a, b, a}
e) {z, y, x}
Answer: d. Explanation: The key in a map form a set and must be unique (have no duplicates, so {a, b, a} could not be
the keys for a map since a appears twice.
6) Suppose a node in a tree is a child. Which of the following must be true?
a) It is the root.
b) It is a leaf.
c) It is a parent.
d) It has a parent.
e) It has a sibling.
Answer: d. Explanation: If a node is a child, then it has a parent. The other answer choices might be true, but they dont
have to be true.
TB 115

TB 116

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test


Bank

7) If a tree only has one node then its height is


a) -1
b) 0
c) 1
d) 2
e) 3
Answer: c. Explanation: A tree with only one node (the root) has height 1.
8) Suppose a binary search tree has n nodes. What is the maximum height the tree could have?
a) n / 2
b) log n
c) n - 1
d) n
e) n2
Answer: d. Explanation: If every node in the tree was on a different level (for example if nodes were inserted into the
binary search tree in sorted order), then the tree would have height n.
9) A tree is a good data structure for organizing what kind of information?
a) linear information
b) hierarchical information
c) unordered information
d) key-value pairs
e) all of the above
Answer: b. Explanation: Because of their hierarchical structure, trees are good for organizing hierarchical information.
10) In a binary tree,
a) each node can have at most two children
b) each node can have at most two parents
c) each node can have at most two siblings
d) there can be at most two roots
e) there can be at most two leaves
Answer: a. Explanation: In a binary tree each node has at most two children. Each node can only have one parent, can
have at most one other sibling, there is only one root, and there can be any number of leaves.
11) A tree would not be a binary tree if
a) it only had one node
b) a leaf had only one parent
c) a leaf had a sibling
d) a node had three children
e) a node had a parent, a child, and a sibling at the same time
Answer: d. Explanation: In a binary tree each node has at most two children, so a tree with a node that has three children
would not be a binary tree.
12) Consider the tree in Figure 10.3 in the book. Which of the following nodes in not a leaf?
a) Biology
b) Zoology
c) Programming Languages
d) Physics
e) Memory Management
Answer: a. Explanation: Choice a, Biology, is the only choice in which the node has a child. The other choices are all
leaves.
13) Consider the binary search tree in Figure 10.9 in the book. Which of the following is an inorder traversal of the
tree?
a) 20 12 43 8 15 35 48 18 31 45 50 17 26 47
b) 20 12 8 15 18 17 43 35 31 26 48 45 47 50

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test Bank

TB 117

c) 8 12 15 17 18 20 26 31 35 43 45 47 48 50
d) 8 17 18 15 12 26 31 35 47 45 50 48 43 20
e) 47 26 17 50 45 31 18 48 35 15 8 43 12 20
Answer: c. Explanation: An inorder traversal of a binary search tree always produces the items in sorted order.
14) Which of the following inorder traversals could not have come from a binary search tree?
a) 1 2 3 4 5 6
b) a b c d e f
c) 9 8 7 6 5
d) 1 3 5 2 4 6
e) 5 6 7 8 9
Answer: d. Explanation: An inorder traversal of a binary search tree always produces the items in sorted order. Choice d
is the only list of items that is not in order. Note that in choice c the numbers are sorted in descending order.
15) When a node is added to a binary search tree, it will be
a) the root
b) a leaf
c) a parent
d) a sibling
e) a child
Answer: b. Explanation: When a node is added to a binary search tree it will always be a leaf. Answer e is not correct
because even though a leaf is usually a child, it is not always. A node added to an empty binary search tree becomes the
root, which is also a leaf in this case, but it is not a child.
16) When deleting a node with two children from a binary search tree, it gets replaced with
a) the root
b) a leaf
c) its left child
d) its right child
e) its inorder successor
Answer: e. Explanation: In order to preserve the ordering of the tree, we replace the node with its inorder successor, that
is, the smallest node in its right subtree.
17) Which ADT below is a heap a good implementation for?
a) stack
b) queue
c) priority queue
d) linked list
e) array
Answer: c. Explanation: In a heap, the smallest element is always at the root. This corresponds well to a priority queue,
since the highest priority item is the one which is removed next and can conveniently be found at the root of a heap.
Priority queues are commonly implemented using heaps.
18) A heap is usually implemented using
a) a stack
b) a list
c) a binary search tree
d) a hashtable
e) a circular queue
Answer: b. Explanation: Heaps are commonly implemented using a list since items can easily be added and deleted that
way. Items are added on the end (and possibly bubbled up) and deleted items are replaced with the item on the end
(which is bubbled up or down).
19) A heap is best envisioned as a
a) tree
b) list
c) table

TB 118

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test


Bank

d) set
e) map
Answer: a. Explanation: A heap is a complete binary tree in which every node has a value less than its children. While a
heap can be implemented using a list, it is difficult to see the parent-child relationships in a list, and a heap is best
envisioned as a tree.
20) When chaining is used to handle collisions in a hashtable, each cell in the hashtable is usually a
a) stack
b) linked list
c) binary search tree
d) second hashtable
e) circular queue
Answer: b. Explanation: With chaining, items that hash to the same cell are all stored in the cell, chained together in a
list. This list is usually implemented as a linked list.
Questions 20-21 refer to a hashtable of size 5 storing integers using the hash function f(x) = (2*x + 1) % 5. Linear
probing is used to handle collisions.
21) Assume the hashtable is empty. The value 18 would be stored in which cell?
a) 0
b) 1
c) 2
d) 3
e) 4
Answer: 2. Explanation: 2*18 + 1 = 37 and 37 % 5 = 2, so 18 is stored in cell 2.
22) Assume cells 0, 2, and 3 already have values in them. The value 18 would be stored in which cell?
a) 0
b) 1
c) 2
d) 3
e) 4
Answer: e. Explanation: 18 hashes to 2, but cell 2 is occupied, so linear probing begins. Cell 3 is also occupied, but cell
4 is free, so 18 is stored there.
23) Which of the following creates a TreeMap and then adds the pair (1, apple) to it?
a) TreeMap t = new TreeMap(1, apple);
b) TreeMap t = new TreeMap();
t.put(new Integer(1));
t.put(apple);
c) TreeMap t = new TreeMap();
t.put(new Integer(1), apple);
d) TreeMap t = new TreeMap(1, apple);
t.put();
e) TreeMap t = new TreeMap();
t.keySet().add(new Integer(1));
t.put(apple);
Answer: c. Explanation: Only answer c correctly uses the put method to add the given pair to the TreeMap.
24) Determining whether a java.util.HashSet contains a particular item can be done in ______ time.
a) O(1)
b) O(log n)
c) O(n)
d) O(nlog n)
e) O(n2)
Answer: a. Explanation: A HashSet is implemented using a hashtable and thus accessing an element or determining that
the element is not there, can be done in constant (O(1)) time.

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test Bank

TB 119

25) When would it be better to use a TreeSet instead of a HashSet?


a) When the data must be accessed quickly.
b) When the data are primitive types.
c) When we know ahead of time exactly how many data items there will be.
d) When the data should be in sorted order.
e) When there are very large amounts of data.
Answer: d. Explanation: The iterator on a TreeSet returns the items in sorted order, whereas HashSet does not. Choice
(a) is not correct because items can be accessed more quickly using HashSet. Choice (b) is not correct because neither
HashSet nor TreeSet can store primitive data directly (and both can store primitive values wrapped in wrapper classes).
Choices (c) and (e) are not relevant.
True/False Questions:
1) They keys in a map must be unique.
Answer: True. Explanation: The keys in a map form a set and there may be no duplicates.
2) The values in a map form a set.
Answer: False. Explanation: It is possible for two different keys to map to the same value, so there may be duplicate
values in a map. Thus, the values cannot be a set.
3) The collection {1, 2, 3, 1, -1} is not a set because sets may not have negative numbers.
Answer: False. Explanation: It is true that the collection given is not a set, but it is because the element 1 is repeated, not
because sets cant have negative numbers (they can).
4) If Set s is a reference to some implementation of the Set interface, then we could add the element 1 to the set by
saying s.add(1);.
Answer: True. Explanation: The add method on the Set interface takes an Object as a parameter, and in versions of Java
prior to Java 5, one could not add a primitive value to a set. Instead, a wrapper class had to be used, as in s.add(new
Integer(1));. In Java 5, with autoboxing, the wrapping of the primitive value occurs automatically, so it is possible to
pass a primitive value to the add method.
5) Suppose s is a reference to some implementation of the Set interface. If we remove an element from s, we can add it
back again later.
Answer: True. Explanation: It is certainly possible to remove an element from a Set and then add it back later.
6) A tree must have a root.
Answer: False. Explanation: The empty tree is a tree with no nodes at all, so it does not have a root.
7) Arithmetic expressions can be encoded in binary trees.
Answer: True. Explanation: Each operand and operator is a node in the tree. The nice thing about encoding arithmetic
expressions in binary trees is that we dont need to use parentheses the order in which the operations should be carried
out is part of the structure of the tree.
8) A node in a tree could be a child, a parent, and a sibling at the same time.
Answer: True. Explanation: A node could be a child and have siblings, and it could also have its own children, making
it a parent as well.
9) A node can be a root and a leaf at the same time.
Answer: True. Explanation: In a one-node tree, the one node is both the root and a leaf.
10) A preorder traversal processes items in the reverse order of a postorder traversal.
Answer: False. Explanation: The root is first in a preorder and last in a postorder traversal, but the other nodes are
usually not mirror images of each other in pre vs. postorder traversals.
11) A binary search tree can also be used for sorting.

TB 120

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test


Bank

Answer: True. Explanation: To sort with a binary search tree, add all the elements to the tree, then traverse it inorder to
produce the elements in sorted order.
12) A binary search tree could be implemented to store any kind of Object.
Answer: False. Explanation: A binary search tree stores items in sorted order, so only objects that can be ordered can be
stored in a binary search tree. So we could store any object that implements the Comparable interface, but not any
Object at all.
13) The iterator method on a TreeSet returns the elements of the set in sorted order.
Answer: True. Explanation: While a Set in general places no constraints on the way elements are ordered, a TreeSet
does store its elements in sorted order, and so the iterator method produces the elements in order.
14) When deleting a leaf from a binary search tree, the leaf must be replaced with its inorder successor.
Answer: False. Explanation: Since a leaf has no children, we can simply delete and be done with it. Only when deleted
node has two children do we replace it with its inorder successor.
15) Recursion can be used to search for an element in a binary search tree.
Answer: True. Explanation: After examining the current node, one can recurse on its right or left child, depending on
whether the element being searched for is less than or greater than the current node.
16) The root node in a heap contains the largest element in the heap.
Answer: False. Explanation: The root contains the smallest element.
17) When a node is added to a heap, it can be either bubbled up or bubbled down.
Answer: False. Explanation: When a node is added to a heap, it is added as a leaf and can bubble up, but cannot bubble
down.
18) In a heap, bubbling up refers to swapping a node with its parent when the parent is larger.
Answer: True. Explanation: Thats what bubbling up is, and it occurs when nodes are added or deleted so that the
structure of the heap is preserved.
19) When a node is deleted from a heap, it is bubbled down until it becomes a leaf, and then removed from the heap.
Answer: False. Explanation: When a node is deleted, it is removed immediately and replaced with another node in the
heap, which then may be bubbled up or down as appropriate.
20) Its easy to find the smallest element in a heap.
Answer: True. Explanation: The smallest element is always at the root, so it is easy to find.
21) The list 3 12 6 25 20 7 18 could be a heap
Answer: True. Explanation: Heaps can be stored as lists where the children of the ith element are the 2i and 2i+1
elements, and the given list satisfies the properties of a heap.
22) Heapsort performs a sort by adding elements to a heap, and then doing a preorder traversal to get them out in sorted
order.
Answer: False. Explanation: Heapsort works by adding the elements to a heap, and then repeatedly removing the root.
23) A collision in a hashtable occurs when two elements hash to the same cell.
Answer: True. Explanation: This is the definition of a collision.
24) When using open addressing techniques, such as linear probing, to handle collisions in a hashtable, the number of
elements that can be stored is limited by the number of cells in the hashtable.
Answer: True. Explanation: Open addressing techniques calculate a new hash value when a collision occurs. Each cell
in the hashtable can still only hold one elements, so the number of elements is limited by the number of cells. This is in
contrast with the chaining method of handling collision in which multiple elements are stored in the same cell,
chained together, usually in a linked list.

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test Bank

TB 121

25) When chaining is used in a hashtable, elements in consecutive cells are chained together in a linkned list to save
memory.
Answer: False. Explanation: Chaining refers to a method of handling collisions in which elements that hash to the same
cell are all stored in that cell (not consecutive cells), chained together, usually in a linked list.
Free-form Questions:
1) Given Set s is a reference to some implementation of the Set interface, and Integer i is a reference to an element
that may or may not be in the set. Write code that will check to see if i is in s and if not, add it to the set.
Answer:
if (!s.contains(i))
s.add(i);
2) Given Set s is a reference to an empty Set, write code that makes s be the set {1, 2, 3, 4}.
Answer:
s.add(new Integer(1));
s.add(new Integer(2));
s.add(new Integer(3));
s.add(new Integer(4));
Note: It is also fine to add the integers directly since autoboxing will cause them to be wrapped in Integer objects.
3) List the nodes in the tree in Figure 10.12 in the book using an inorder traversal.
Answer: 8 12 15 17 18 20 26 31 35 45 47 48 50
4) List the nodes in the tree in Figure 10.12 in the book using a preorder traversal.
Answer: 20 12 8 15 18 17 45 35 31 26 48 47 50
5) List the nodes in the tree in Figure 10.12 in the book using a postorder traversal.
Answer: 8 17 18 15 12 26 31 35 47 50 48 45 20
For questions 6 - 7, use the following class definition of a tree node:
class TreeNode
{
int info;
TreeNode left, right;
}
6) Write code that creates a binary tree containing the numbers 1, 2, and 3, where 1 is the root, 2 is its left child, and 3
is its right child.
Answer:
TreeNode root = new TreeNode();
root.info = 1;
root.left = new TreeNode();
root.left.info = 2;
root.left.left = null;
root.left.right = null;
root.right = new TreeNode();
root.right.info = 3;
root.right.left = null;
root.right.right = null;
7) Suppose node is a reference to a TreeNode in a binary search tree whose left child is to be deleted. Assuming the
left child is a leaf, write code to delete it.

TB 122

Lewis/Loftus/Cocking, 2/e: Chapter 10 Test


Bank

Answer:
node.left = null;
Questions 8-11 refer to the following heap, stored in list form: 3 20 5 25 21 7 30
8) Draw the heap as a tree.
Answer:
3
/
\
20
5
/ \
/ \
25
21 7 30
9) Suppose we add the element 50 to the heap. Show what the list looks like now.
Answer: 3 20 5 25 21 7 30 50
10) Suppose we add the element 1 to the original heap. Show what the list looks like now.
Answer: 1 3 5 20 21 7 30 25
11) Suppose the element 3 is deleted from the original heap. Show what the list looks like now.
Answer: 5 20 7 25 21 30
12) Consider a binary tree in which each node also has a reference to its parent. Write a simple class (data only) for a
node in such a tree.
Answer:
class TreeNode
{
Object info;
TreeNode parent;
TreeNode left, right;
}
13) Consider a hashtable of size SIZE storing Book objects. Linear probing will be used to handle collisions. The
hashCode method on Book has been implemented. Write a private method called rehash that takes as a parameter a
Book that hashed to an occupied cell and returns an empty cell for the Book. The method rehash will go in the
BookHashtable class, and you can use the boolean isOccupied(int cell) method of BookHashtable to determine if a
cell is occupied.
Answer:
// Precondition: the hashtable is not full
private int rehash (Book book)
{
int cell = (book.hashCode() + 1) % SIZE;
while (isOccupied(cell))
cell = (cell+1) % SIZE;
return cell;
}
Questions 14-15 refer to the hash function f(x) = (3x + 4) % 17.
14) What will the following integer values hash to? 0, 1, 17, 200
Answer: 0 -> 4, 1 -> 7, 17 -> 4, 200 -> 9
15) How many cells does the hashtable have?
Answer: 17

Das könnte Ihnen auch gefallen