Questions 1-7 are to be submitted to the drop box and question 8 committed to your repository. Late days may be used/gained for written assignments - for example, if either part (dropbox or repo) is late, you will be charged a late day. If both parts are early, you can gain a late day
(20 points) Weiss Exercise 5.26 [5.20]. In order to make the calculations simpler, you may assume that N = 2k - 1 for some integer k ≥ 0, and that all elements are equally likely to be the one we are searching for. Note that the problem only deals with successful searches (where the item we are looking for is actually in the array).
Hint: For each i, count how many of the array elements will be found after binary search examines exactly i array elements. (For example, the middle element is the only one to be found after examining one array element, and there are two elements that can be found after two probes into the array.) Use a summation to help you find the average.
Potentially useful fact: Depending on how you work this problem, you may find the following useful:
(15 points) Suppose we execute this pseudocode:
String in = "ABCD"; String out = ""; Stack stack = new empty stack; while ( in != "" || stack is not empty) randomly choose ONE of the following two operations: - remove the first character from in and push it on stack, or - pop stack and add the character that was popped to the end of out.
Note that this process will always result in some permutation of
ABCD
in the output. Recall the "railroad diagram" discussion from
the Session 10 PowerPoint Slides.
ABCD
that are NOT possible final values of
out
.
ABCD
are possible final values for
out
?
(10 points) It is useful to have a unique representation of binary trees that can be written compactly. For simplicity, we assume that each node of the tree will contain one
Character
. We represent a tree by a pair of strings, which I call
chars
and
children
. The
chars
is a
pre-order
listing of the contents of the nodes. The
children
string tells about the
children
of the corresponding node from chars as follows:
2 | The node has two children. |
0 | The node has no children. |
L | The node only has a left child. |
R | The node only has a right child. |
For example:
chars = "+a*-bcd"; children = "2022000";
represents the tree in Weiss Figure 18.11 (a)
chars = "7215349"; children = "220LR00";
represents the tree in Weiss Figure 19.4 (a).
What are the values of
chars
and
children
for the tree below?
For a different tree we have:
chars = "THEQUICKBROWN"; children = "2R220002R0RL0";
Show the order of the nodes in an in-order traversal of the tree.
(10 points)
THEQUICKLAZYFOX
is the pre-order traversal of a tree (one character per node).
QIUCEHLAZKFYTXO
is the in-order traversal of the same tree.
What is the order of the nodes in the level-order traversal of this tree?
(10 points) If
tree
is a
BinaryTree
with
N
nodes, and
itr
is a
PostOrder
iterator for that tree, suppose that we run the following code:
for (itr.first(); itr.isValid(); itr.advance()) { System.out.println(itr.retrieve()); }
Is the running time for this code Θ(log N), Θ(N), Θ(N log N) or Θ(N2)?
Explain your answer.
Be sure that your explanation accounts for how the postorder iterator’s stack is used during the execution of the code. The code for the PostOrder class can be found in Section 18.4 (spanning three different pages).
Notes:
These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.
java.util.Map
to complete this problem.
Weiss exercise 6.2 [6.2]. You can just write the code for part (a) out by hand if you wish, but I recommend getting it working in Eclipse and printing out your code. Here is some code that you can use to test your method if you wish:
public static void main(String[] args) { Collection<Collection<String>> a = new ArrayList<Collection<String>>(); String[][] arrays = {{"abc", "def", "ghi"}, {"xyz", "uvw", "abc", "abc"}, {"a", "ab", "abc", "xyz", "abc"}}; for (String[] sArray : arrays){ Collection<String> a1 = new ArrayList<String>(); for (String s: sArray) a1.add(s); a.add(a1); } System.out.println(count(a, "abc")); }