mock2 - 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
 
 
 b.
Suggest
a
sequence
of
insertion
operations
that
would
create
this
2‐3‐4
tree.
 
 
|3



 







 5|
 
/






|





\
 




|1

 2|



|4|




|6|
 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?
 
 
 
 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.
 
 
 
 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?
 
 
 
 
 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.
 Problem
4
 Suppose
that
while
your
computer
is
sorting
an
array
of
objects,
its

memory
is
struck
by
a
 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
 





 b)
Selection
sort
 






 c)
Mergesort
 





 d)
Quicksort
 
 e)
Radix
sort
 
 
 
 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
termsof
n,
and
show
how
you
got
 it.
 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) { } ...
View Full Document

Ask a homework question - tutors are online