1. 1. Here is an array with exactly 15 elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Suppose that we are doing a serial search for an element. Circle any elements that will be found by
examining two or fewer numbers from the array.
2. Here is an array with exactly 15 elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Suppose that we are doing a binary search for an element. Circle any elements that will be found by
examining two or fewer numbers from the array.
3. Implement the body of the following function using a binary search of the array. You do not need
to check the precondition.
public static boolean has42(int[ ] data, int start, int end)
// Precondition: The elements data[start]...data[end] are sorted from smallest
// to largest. This array segment might be empty (indicated by end being less
// than start).
// Postcondition: A true return value indicates that the number 42 appears in
// data[start]...data[end]. A false return value indicates that 42 doesn’t
// appear.
4. Draw a hash table with open addressing and a size of 9. Use the hash function "k%9". Insert the
keys: 5, 29, 20, 0, 27 and 18 into your table (in that order).
5. Suppose you are building an open address hash table with double hashing. The hash table
capacity is n, so that the valid hash table indexes range from 0 to n. Fill in the blanks:
In order to ensure that every array position is examined, the value returned by the second
hash function must be ________________________ with respect to n.
One way to ensure this good behavior is to make n be _______________, and have the
return value of the second hash function range from _________ to _________ (including the
end points).
6. You are writing code for the remove method of a chained hash table. Fill in the blanks in this
pseudocode with the two missing statements. You may use pseudocode yourself, or write actual
Java code:
public void Table remove(Object key)
{
2. ChainedHashNode cursor;
int i;
1. i = key.hashCode( );
2. Make cursor refer to the node that contains an item with the given key
(or set it to null if there is no such node).
3. if (cursor != null)
{
3a. _____________________________________________________________
3b. _____________________________________________________________
}
7. Draw a hash table with chaining and a size of 9. Use the hash function "k%9" to insert the keys 5,
29, 20, 0, and 18 into your table.
8. Suppose that I have the following private instance variables for an open-address hash table:
public class Table
{
privateintmanyItems;
9.private Object[ ] keys;
private Object[ ] data;
privateboolean[ ] hasBeenUsed;
...
The hash table uses open addressing with linear probing. A location [i] of the table that has NEVER
been used will have hasBeenUsed[i] set to false. Other locations have hasBeenUsed[i] set to true.
Complete the implementation of the following method of the Table class. There is no need to check
the precondition, but your code must be as efficient as possible.
publicbooleancontainsKey(Object key)
3. // Postcondition: If key occurs as a key of a record in the table, then
// the method returns true; otherwise the method returns false.
10. Suppose that an open-address hash table has a capacity of 811 and it contains 81 elements.
What is the table's load factor? (An appoximation is fine.)
11. I plan to put 1000 items in a hash table, and I want the average number of accesses in a
successful search to be about 2.0.
A. About how big should the array be if I use open addressing with linear probing? NOTE: For a load
factor of A, the average number of accesses is generally ½(1+1/(1-A)).
12. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the FIRST iteration of the large loop in a selection sort (sorting from smallest to
largest).
13. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the FIRST iteration of the large loop in an insertion sort (sorting from smallest
to largest). This iteration has shifted at least one item in the array!
14. Suppose that you are writing a program that has this selectionsort static method available:
voidselectionsort(int[ ] data, int first, int n);
Your program also has an integer array called x, with 10 elements. Write two method activations:
The first activation uses selectionsort to sort all of x; the second call uses selectionsort to sort
x[3]..x[9].
15. Describe a case where quicksort will result in quadratic behavior.
16. Here is an array which has just been partitioned by the first step of quicksort:
3, 0, 2, 4, 5, 8, 7, 6, 9
Which of these elements could be the pivot? (There may be more than one possibility!)
17. Give a concise accurate description of a good way for quicksort to choose a pivot element. Your
approach should be better than "use the entry at location [0]".
18. Give a concise accurate description of a good way for quicksort to improve its performance by
using insertionsort.
19. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
4. Suppose we partition this array using quicksort's partition function and using 5 for the pivot. Draw
the resulting array after the partition finishes.
20. Here is an array of ten integers:
5 3 8 9 1 7 0 2 6 4
Draw this array after the TWO recursive calls of merge sort are completed, and before the final
merge step has occured.
21. Implement the following static method:
private static void merge(int[ ] data, int first, int n1, int n2);
// Precondition: data has at least n1+n2 components starting at
// data[first]. The first n1 elements (from data[first] to
// data[first+n1-1]) are sorted from smallest to largest, and the last
// n2 elements (from data[first+n1] to data[first+n1+n2-1]) are also
// sorted from smallest to largest.
// Postcondition: Starting at data[first, n1+n2 elements of data have been
// rearranged to be sorted from smallest to largest.
22. Write two or three clear sentences to describe how a heapsort works.
23. Fill in the following table for the times to sort an array of n items. Use only big-O notation, and
do not have any extraneous constants in your expressions.
Worst Case Average Case
Binary search of a sorted array
Insertion sort
Merge sort
Quick sort without "median of three" pivot
selection
Quick sort with "median of three" pivot
selection
Selection sort
Heap sort
5. 24. Here is a small binary tree:
14
/
2 11
/ /
1 3 10 30
/ /
7 40
Circle all the leaves. Put a square box around the root. Draw a star around each ancestor of
the node that contains 10. Put a big X through every descendant of the node the contains 10.
25. Draw a full binary tree with at least 6 nodes.
26. Draw a complete binary tree with exactly six nodes. Put a different value in each node.
Then draw an array with six components and show where each of the six node values would
be placed in the array (using the usual array representation of a complete binary tree).
27. Write the instance variables for a new class that could be used for a node in a tree where:
(1) Each node contains int data, (2) Each node has up to four children, and (3) Each node also
has a reference to its parent. Store the references to the children in an array of four
components.
28. Draw a binary taxonomy tree that can be used for these four animals: Rabbit, Horse,
Whale, Snake.
29. Using the BTNode class from, write a new static method of the BTNode class to meet the
following specification. No recursion is needed.
public static void subswap(BTNode root)
// Precondition: Root is the root reference of a binary tree.
// Postcondition: If root is non-null, then the original left subtree below
// this root has been moved and is now the right subtree, and the original
// right subtree is now the left subtree.
// Example original tree: Example new tree:
// 1 1
// / /
// 2 3 3 2
// / /
// 4 5 4 5
30. Redo the previous problem as a new non-static BTNode method.
31. Using the BTNode class from, write a new static method of the BTNode class to meet the
following specification.
public static intmanyNodes(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the number of nodes in the tree.
// NOTES: The empty tree has 0 nodes, and a tree with just a root has
// 1 node.
32. Using the BTNode class, write a new static method of the BTNode class to meet the
following specification.
public static inttreeDepth(BTNode root)
// Precondition: root_ptr is the root reference of a binary tree.
// Postcondition: The return value is the depth of the binary tree.
// NOTES: The empty tree has a depth of -1 and a tree with just a root
// has a depth of 0.
33. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
34. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static int count42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
6. // NOT NECESSARILY a search tree).
// Postcondition: The return value indicates how many times 42 appears
// in the tree. NOTE: If the tree is empty, the method returns zero.
35. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static boolean has42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value indicates whether 42 appears somewhere
// in the tree. NOTE: If the tree is empty, the method returns false.
36. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static boolean all42(IntBTNode root)
// Precondition: root is the root reference of a binary tree (but
// NOT NECESSARILY a search tree).
// Postcondition: The return value is true if every node in the tree
// contains 42. NOTE: If the tree is empty, the method returns true.
37. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification.
public static int sum(IntBTNode root)
// Precondition: root is the root reference of a binary tree.
// Postcondition: The return value is the sum of all the data in all the
nodes.
// NOTES: The return value for the empty tree is zero.
38. Suppose that we want to create a binary search tree where each node contains information
of some data type. What additional factor is required for the data type?
39. Suppose that a binary search tree contains the number 42 at a node with two children.
Write two or three clear sentences to describe the process required to delete the 42 from the
tree.
40. Suppose IntBTNode is a BTNodewith integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int count42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: The return value indicates how many times 42 appears
// in the tree.
41. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
public static int max(BTNode root)
// Precondition: root is the root reference of a nonempty binary SEARCH
// tree.
// Postcondition: The return value is the largest value in the tree.
42. Suppose IntBTNode is a BTNode with integer data. Write a new static method of the
IntBTNode class to meet the following specification. Make the method as efficient as
possible (do not visit nodes unnecessarily).
void insert42(BTNode root)
// Precondition: root is the root reference of a binary SEARCH tree.
// Postcondition: One copy of the number 42 has been added to the binary
// search tree.
43. What is the importance of the stopping case in recursive methods?
7. 44. Write a recursive method that has one parameter which is an int value called x. The method
prints x asterisks, followed by x exclamation points. Do NOT use any loops. Do NOT use any variables
other than x.
45. For the tree shown below, determine
a. Which node is the root
b. Which nodes are leaves
c. The tree’s depth
d. The result of preorder, postorder, inorder, and level-order traversals
46.For each node shown on the tree below:
a. Name the parent node
b. List the children
c. List the siblings
d. Compute the height
e. Compute the depth
f. Compute the size
47.What is the output of the following method for the
tree shown below?
1 public static <AnyType> void mysteryPrint(BinaryNode<AnyType> t )
2 {
3 if( t != null )
4 {
5 System.out.println(t.getElement( ) );
6 mysteryPrint(t.getLeft( ) );
7 System.out.println(t.getElement( ) );
8 mysteryPrint(t.getRight( ) );
9 System.out.println(t.getElement( ) );
10 }
11 }
48.Show the stack operations when an inorder and preorder traversal is
applied to the tree.