v22.0310-003: Homework 3
Due date: Wed Oct 20.
This homework has a programming part and
a written part. The programming part should
be submitted as usual by email.
The written part must be handed in either
after the lectures on Wednesday, OR to the TA during
his office hours on Wednesay. [Please do not
just stuff in our mail boxes without asking first]
Look here for additional information
after the original posting of
assignment:
-
Problem 3.4 (page 71) in Text.
[parts (a), (b) and (c)]
-
Problem 3.10 (page 73) in Text.
[relating n! and n log n]
-
Let us insert the following keys (in this order) into
an initially empty binary search tree:
9, 7, 2, 8, 12, 5, 1, 16, 10, 13. 11
-
(a) Please show the binary search tree after each insertion.
-
(b) Let T be the final tree in (a).
List the keys in a pre-order traversal of T.
-
(c) Repeat (b) but for an in-order traversal.
-
(d) Repeat (b) but for a post-order traversal.
-
(e) Draw the tree after deleting node 12 from T.
-
Suppose A is an array initially containing the keys
in the previous question (9, 7, 2, ..., 11).
Apply the
buildheap()
method (page 156, Figure 5.24)
to array A.
-
(a)
Show the MaxHeap after each application of
the
siftdown
method.
NOTE: draw the MaxHeap as a complete binary tree, not
as an array.
-
(b)
Suppose we apply the method
removemax()
to the result of (a). Show the resulting MaxHeap.
-
Programming Question.
Download and read the following "abstract" java program,
myNode.java.
It is "abstract" because we omit the
various bodies for methods and putting dummy
bodies where we could not. [But you can call
"javac" on it without any error.]
We want you to implement the
merge sort
algorithm
(discussed in the first lecture
of class, but see p.242, section 8.5).
To write your own myNode.java
program,
first remove all occurences of the
key word "abstract" from the sample program,
then fill in the appropriate code. Remove also any
dummy code. The main
method has
already been written for you.
Here is a sample input file, input.0.
-
RECOMMENDED EXTRA PROBLEMS:
Do not hand in, as they will not
be graded. But we will give solutions and discuss
this. Please try them yourself first!
-
(a) 2n = O(n)
-
(b) n! = O(2n) is false.
To show this, show that for any constant C> 0,
if n is large enough, then
n! > C 2n
-
(c) 3n = O(2n)
is false.
-
(d)
Consider the following property: at each node u of a binary
tree we store a key. Moreover,
u_L.Key < u.Key < u_R.Key
Is this a binary search tree? Give counter example.
-
(e)
In chapter 5.3,
Schaffer said that if we eliminate pointers
from leaves in full binary trees then
the overhead fraction is p/(p+d).
If the binary tree is not necessarily full,
we can still achieve this overhead fraction.
How? Explain your reasoning.