mock2solutions - Problem
1



Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Problem
1

 a.
Suggest
a
sequence
of
insertion
operations
that
would
create
this
binary
search
tree.
 









4
 








/
\

 






2


8
 





/
\
 



1



2
 There
are
a
number
of
solutions
to
this.
All
of
the
following
would
work:
 4
2
8
1
2,
4
8
2
1
2,
4
8
2
2
1,
4
2
1
2
8,
4
2
2
1
8
 Generally,
the
parent
needs
to
be
inserted
before
the
two
children
but
the
two
children
can
be
 inserted
in
any
order.

 If
you
convert
this
tree
to
a
directed
graph
(where
the
direction
of
the
edge
is
from
the
parent
to
 the
children)
then
running
topological
sort
would
give
you
the
above
sequences.
 
 b.
Suggest
a
sequence
of
insertion
operations
that
would
create
this
2‐3‐4
tree.
 
 
|3



 







 5|
 
/






|





\
 




|1

 2|



|4|




|6|
 One
solution:
5
2
6
3
1
4
 Another
solution:
5
4
6
3
1
2
 Problem
2

 If
a
2‐3‐4
tree
has
depth
h
(that
is,
the
(empty)
leaves
are
at
distance
h
from
the
root),
what
is
 the
maximum
number
of
comparisons
done
in
the
corresponding
red‐black
tree
to
find
whether
 a
certain
key
is
present
in
the
tree?
 Each
2‐3‐4
tree
node
corresponds
to
a
binary
search
tree
of
depth
2:
 |2
4
6|


=>

4
 




















/\
 
 




2


6
 hence
the
depth
of
a
2‐3‐4
tree
of
depth
h
is
2h.
 Question
3
 In
Project
2,
you
had
to
sort
the
names
of
files
and
folders
in
alphabetical
order.
For
this
 question,
assume
that
files
and
folders
within
the
same
parent
folder
are
stored
as
an
array
of
 Strings.
Also
assume
no
two
files
or
folders
have
the
same
name.

 a)
Suppose
the
files
and
folders
are
initially
unsorted,
which
algorithm
would
you
pick
to
sort
 this
array
of
Strings?
Justify
your
answer
by
characterizing
the
time
complexity
and
space
 complexity
of
this
algorithm.
 Assuming
file
and
folder
names
are
not
too
long
use
Radix
sort
with
radix
26.
Linear
time
time
 and
space
complexity.
 b)
Now,
you
add
a
new
folder
to
the
file
system.
Assuming
there
is
enough
space
in
the
array
(ie,
 it's
not
full),

a
new
String
representing
the
folder
is

appended
to
the
end
of
your
already
sorted
 array.
What
algorithm
would
you
pick
to
key
the
array
sorted?
Is
it
different
from
your
answer
in
 part
a?
If
so,
why?
If
not,
why
not?
 Insertion
sort
to
take
advantage
of
the
fact
that
the
array
is
almost
sorted.
 
 d)
Given
that
the
File
System
executes
the
sorting
algorithm
from
b)
after
every
single
addition,
 starting
from
an
empty
File
System,
give
a
theta
bound
on
the
total
running
time
of
inserting
n
 files
or
folders
assuming
the
worst‐case
ordering
of
the
input.
Describe
how
you
arrived
at
this
 answer.
 Worst
case
ordering
is
when
the
files
and
folders
are
inserted
in
reverse
sorted
order
(i.e.
largest
 to
smallest),
insertion
sort
takes
theta(n2).
 Problem
4
 Suppose
that
while
your
computer
is
sorting
an
array
of
objects,
its

memory
is
struck
bya
 cosmic
ray
that
changes
exactly
one
of
the
keys
to
something
completely
different.

For
each
of
 the
following
sorting
algorithms,
what
is
the
worst‐case
possibility?

For
each,
answer
 [x]
the
final
array
won't
even
be
close
to
sorted,

 [y]
the
final
array
will
have
just
one
or
two
keys
out
of
place,
or

 [z]
the
final
array
will
consist
of
two
separate
sorted
subsets,
one
following
the
other,
plus
 perhaps
one
or
two
additional
keys
out
of
place.
 a)

Insertion
sort
 




z.

 In
the
middle
of
insertion
sort,
the
last
element
of
the
sorted
portion
of
the
array
is
zapped
to
 be
smaller
than
all
the
elements
in
the
unsorted
portion
of
the
array.

 b)
Selection
sort
 





y
 Since
selection
sort
always
picks
a
the
minimum
element
among
the
unsorted
portion
of
the
 array
zapping
any
element
will
incorrectly
place
one
key.

 c)
Mergesort
 




z
 After
the
sublists
are
sorted
and
before
the
merging
starts,
the
front
of
one
list
is
zapped
to
be
 larger
than
all
elements
in
the
second
list.
The
second
list
will
be
output
first,
followed
by
the
 zapped
element
and
the
remaining
elements
in
the
first
list.

 d)
Quicksort
 



z

 After
the
pivot
is
moved
back
to
it’s
correct
position
after
the
partitioning
step,
the
pivot
is
 zapped
to
something
else.
Quicksort
will
recursively
sort
the
left
and
right
sublists
but
they
are
 separated
by
a
key
that
may
not
be
in
the
right
position.

 e)
Radix
sort
 



y
 Since
radix
sort
is
not
comparison
based,
changing
on
element
will
after
only
that
element.

 
 Problem
5
 Consider
a
hash
table
with
n
cells,
each
of
which
contains
a
pointer
to
an
unsorted
linked
list
of
 words
that
hash
to
that
cell.
Suppose
25,000
words
are
to
be
added
to
the
hash
structure,
and
 the
hash
function
is
good
enough
to
distribute
them
evenly
throughout
the
table
(that
is,
all
the
 n
lists
contain
the
same
number
of
words).
How
many
comparisons,
on
the
average,
are
needed
 to
access
a
word
in
the
hash
structure?
Give
your
answer
in
terms
of
n,
and
show
how
you
got
 it.
 
 Number
of
elements
per
bucket
or
the
length
of
the
word
list
in
each
buck
=
25000/n.
On
 average
the
word
to
be
accessed
is
this
list
is
in
the
middle,
so
that’s
25000/n
/2
=
12500/n.
 Problem
6
 Consider
the
following
class
implementing
a
binary
search
tree,
write
a
method
elementAt(k)
 that
returns
the
kth
key
when
all
the
keys
are
sorted
from
smallest
to
largest.
 class TreeNode { public TreeNode left; public TreeNode right; public int value; } 
 class Tree { TreeNode root; private int count; public TreeNode elementAt(int k) { count = k; return helper(root); } private TreeNode helper(TreeNode node) { if(node == null) return null; TreeNode temp = helper(node.left); if(temp != null) return temp; count--; if(count == 0) return this; return help(node.right); }} ...
View Full Document

Ask a homework question - tutors are online