This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Quiz 1 Solutions Problem 4 [2D points]
Consider the following a. Label. the following binary tree with numbers from. the set {5, 2‘2. 9, 14, 13. 11 B}
so that it is a legal binary search tree. Answer: b. Label each node in the ﬁgure above with r or 'o denoting the colors RED
and BLACK, respectively, so that the tree is a legal redblack tree. Answer: {See above. Note: There‘s only one legal labeling.) :2. Make the left child of the root be the root by performing a single rotation.
Draw the binary search tree that results, and label your tree with the keys
from part Is it possible to label the nodes with colors so that the tree
is a redblack tree? Justify.r your answer. Answer: Problem 5 [21'] points] Consider a set .5' of it 2 2 distinct numbers given in unsorted order. Each of
the following four problem parts asks you to give an algorithm to determine
two distinct numbers I and y in the set .5' that satisfy a stated condition.
In as i'etv.r words as possible, describe your algorithm and justify its running time. To keep your answers brief, use algorithm from lectures and the book
as subroutines. a. In Din} time, determine my E S such that
II m e[ 2 I'm  2i
for all to, e E 3.
Answer: 1, y are the two farthest apart.
Find min, max, each in Din) time.
h. In ﬂinlg n] time, determine 1,1; E .S‘ such that s 91 y and
II  el 5 he  2
for all m, z E 5 such that in 5c 2. Answer: 1.3; are the two closest together. First sort {with heapsort or merge sort) — Obi}; n}: then. scan the sortec'.
numbers for the two adjacent numbers with the minimum difference 
Din}. Total: 0(nlg n} c. In Din] expected time, determine I, y E .S' such that r+y=3. where Z is given, or determine that no two such numbers exist.
{Hint y = 3—2.} Answer: First put all the numbers into a hash table of size n.
Expectsti hashing time: nOﬂ] = Din}, assuming simple uniform hashing
and collisions resolved by chaining, for example. Then for each .1: E 5, look for Z — :t in the hash table. It takes at most r.
lookups to either ﬁnd one or tr;r them all. Expected lockup time: {n}  Gil} = Din]
Expected total time. Gin] ti. In D[n] tinrer determine any E S such that 1
.n— *5 (muz— min 2)
I yl _ n 1 :es‘ 1E5 ' r'.e., determine any two numbers that are at least as close together as the
average distance between ooneeoutive numbers in the sorted order.
[Hintr Use divide and conquer.) Answer: The idea: Repeatedly split the set in half by partitioning around
the median and ret:ursi'.'el‘_i.r look in. the half with the smaller average dis
' tanoe. That half must have a pair oi" numbers at least as close together as
the original average distance. since {see hint for problem 3d] for an}.r set of
numbers. at least one number must be 5 the average. When the recursion
gets down to just 2 numbers, these are the anewer. Note one subtle point:
The median must be included in both sides of the partition. so that it. wﬂi
be in a subset with each of its neighbors: otherwise if the answer includes that median. it might not be found. Algorithm: Input is in array :1. Deﬁne aoemgedistoneebe] = ﬁ (mania z — mint.“ e]. where n = iengtMA].
Takes Eli's} to compute {to ﬁnd min and max). Answer is PIXEL1.], where Finntaii if lengthiA] = 2
then return A
Find median .1: of A in linear time
Partition At around a: into A1 and A: in linear time
and include 3 in both A1 and air;
Calculate auemgetiisteneeMﬂ and onemgedistoneeMﬂ in linear time
1:: averagedistance needn't reenrnpute rnart[:‘l1]I = Ininiﬁigl = I
if enemyedisteneeEAﬂ If. onemgee'istaneehiiﬁ
then Finnish)
else PINDMﬂ Running time: T{n} = Tfnﬂ) + Elﬁn) = Ei[n] {roaster theorem case 3) ...
View Full
Document
 Spring '09
 STAFF
 Algorithms

Click to edit the document details