Docsity

CS61B Fall 2006 Midterm Exam II: Problem Set, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CS61B: Fall 2006, Midterm Exam II, Jo na th an Shewchuk
Problem 1. (7 p oints) A Miscella ny
a.(1 poin t) Suppose you impl ement a binar y search met hod and i t t akes 10 microseco nds t o s earch a
1,0 00 ,0 00-in t arra y for a number t ha t tur ns out no t to be in the array . How long do y ou estimat e i t
would take fo r your implemen ta ti on t o search a 1,000,00 0,000, 00 0-int array fo r a number t ha t’s n ot
in the ar ray?
b. (2 poin ts) Ex plai n why (1/(n-10 0))Є O(n). Do n’t just reiterate t he definit ion of big-Oh; give
specific values of c a nd N th at support t he clai m. (No need fo r a co mplet e proo f, th ough.)
c. (2 points) Wh en we remove a n i tem f ro m a 2 -3-4 tr ee, sometimes a node tries to “st eal” a key
fro m a sibling by p erforming a r ota tion. However, t h e remove algorithm we lear ned in class onl y
allows a node t o steal f ro m a n adjacen t sibling. But t here’s no fundamen tal reas on why a n ode
couldn’ t s teal a key from a no nadjacent sibling, excep t that we wanted to keep t he algorithm as
simple as possible.
Show how we could modify the 2- 3-4 tree below so t ha t t he “ 9” n ode has o ne mo re key, the “1 2 3”
node has one fewer key, every oth er node has t he same number o f k eys it had before, a nd the tree is
still a valid 2-3-4 tree con taining t he same ke ys it had before. Th e tri angles at the bot tom are
subtrees, which you canno t restruc ture (but you can change th eir pa ren ts).
d. (2 poin ts) Fill in t he blank. The worst -case running time o f th e following me th od, ex pressed as a
function of the input parameter n, is in (___________________________________________).
Not e th at th e loop do es not use “i - - “!
public sta tic void weirdo(lo ng n) {
Tree234 tr ee = new Tree234() ; //Construct an empty 2-3-4 tree.
for (long i = n; i > 1; i = i / 2) {
tree.inser t(i);
}
}
It’s not quite like the running time of any other algorith m you’ve seen . An explanation of your reasoning
might help you get part marks if your ans wer is wrong.)
Problem 2. (7 points) Binary Trees.
pf3

Partial preview of the text

Download CS61B Fall 2006 Midterm Exam II: Problem Set and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B: Fall 2006, Midterm Exam II, Jonathan Shewchuk Problem 1. (7 points) A Miscellany a.(1 point) Suppose you implement a binary search method and it takes 10 microseconds to search a 1,000,000-int array for a number that turns out not to be in the array. How long do you estimate it would take for your implementation to search a 1,000,000,000,000-int array for a number that’s not in the array? b. (2 points) Explain why (1/(n-100))Є O(n). Don’t just reiterate the definition of big-Oh; give specific values of c and N that support the claim. (No need for a complete proof, though.) c. (2 points) When we remove an item from a 2- 3 - 4 tree, sometimes a node tries to “steal” a key from a sibling by performing a rotation. However, the remove algorithm we learned in class only allows a node to steal from an adjacent sibling. But there’s no fundamental reason why a node couldn’t steal a key from a nonadjacent sibling, except that we wanted to keep the algorithm as simple as possible. Show how we could modify the 2- 3 - 4 tree below so that the “9” node has one more key, the “1 2 3” node has one fewer key, every other node has the same number of keys it had before, and the tree is still a valid 2- 3 - 4 tree containing the same keys it had before. The triangles at the bottom are subtrees, which you cannot restructure (but you can change their parents). d. (2 points) Fill in the blank. The worst-case running time of the following method, expressed as a function of the input parameter n, is in (___________________________________________). Note that the loop does not use “i - - “! public static void weirdo(long n) { Tree234 tree = new Tree234(); //Construct an empty 2 - 3 - 4 tree. for (long i = n; i > 1; i = i / 2) { tree.insert(i); } } It’s not quite like the running time of any other algorithm you’ve seen. An explanation of your reasoning might help you get part marks if your answer is wrong.) Problem 2. (7 points) Binary Trees.

a. (2 points) Draw a tree that is both a binary search tree and a complete binary tree, and whose keys are the integers 1 through 9 (each appearing once). b. (2 points) If you perform the operation remove (5) on a binary search tree, it will obviously delete 5 from the preorder traversal of the tree. For example, if the preorder traversal was 5 2 1 3 4 before the deletion, then it is 2 1 3 4 after the deletion. But can the operation remove(5) ever change the order of keys in a preorder traversal? If “yes,” give an example. If “no,” explain why. c. (3 points) Show the sequence of swaps by which the method bottomUpHeap converts the following array into a heap. Redraw the array once for each swap (in the boxes provided). (Note: We’ve provided room for up to seven swaps, the maximum number possible for eight items. That doesn’t necessarily mean that this particular example takes seven swaps.) d. (0 points) If you could choose to be a tree, what kind of tree would you be? Problem 3. (9 points) Directed Graphs.