Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CS61B Midterm #2, Spring 1995: Problem Solutions, Exams of Data Structures and Algorithms

Solutions to selected problems from the cs61b midterm #2 exam held in spring 1995. The problems cover various topics in computer science, including heaps, hash tables, red-black trees, and threaded trees. Students are encouraged to study these solutions to better understand the concepts.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashidhar_p43
shashidhar_p43 🇮🇳

4.5

(53)

80 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS61B, Spring 1995
Midterm #2
Professor P. N. Hilfinger
Problem #1
Tell whether each of the following is true or false. In each case, give a brief explanation where possible; for
false entries, this should take the form of a counter-example. Assume that comparisons always take constant
time.
a. If all items in a heap are distinct, the second-smallest item in a heap has no children (assuming the heap is
ordered with the largest item at the top).
b. Suppose that an ordinary binary tree (not a search tree) obeys the heap property - that the value of any node
is at least as large as that of any of its children. Then inserting a new item into this tree while maintaining the
heap property requires time O(lg n), where n is the number of nodes in the tree.
c. Insertion sort always rquires Θ(n2) time to sort n elements.
d. Quicksort always requires Ω(nlg n) time to sort n elements.
e. Suppose that RB is a red-black tree and that M comparisons are required to discover that a key, x, is not
present in RB. Then if y also is not present in RB, it will require at least M/2 comparisons to discover this.
Problem #2
Assume that h(k, n) is a hash function that takes a key k and integer n and produces a number in the range
[0...n-1]. Assume that K1, K2, ... is an infinite sequence of keys, and that h distributes these keys "evenly."
More precisely, assume that for any M > 0, the number of keys k {K1, ... KM} such that h(k, n) = p is M/n ±
1, for each p [0...n - 1]. Answer the following, giving brief explanations for each answer.
a. Suppose that you are to input a value M and then insert K1, ..., KM into a hash table. How can you arrange
that each insertion runs in worst-case time O(1)?
b. Suppose that M is not explicitly input, and that instead you read keys K1, K2, ..., inserting them into a hash
table as you go, until you encounter some marker indicating the end of the data. Assume that you try to
manage the hash table so as to minimize the time required to do all the insertions. What is the (asymptotic)
worst-case time for inserting Km (that is, the mth item you insert) as a function of m? Why?
c. Under the same assumptions as (b), what is the total worst-case asymptotic time for doing all M insertions?
Why?
Problem #3
A threaded tree is essentially a tree whose nodes are additionally linked together into a list such that
following the list gives an inorder traversal of the tree. Consider the following representation.
class TTree {
public:
CS61B, Midterm #2, Spring 1995
CS61B, Spring 1995 Midterm #2 Professor P. N. Hilfinger 1
pf3

Partial preview of the text

Download CS61B Midterm #2, Spring 1995: Problem Solutions and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS61B, Spring 1995

Midterm

Professor P. N. Hilfinger

Problem

Tell whether each of the following is true or false. In each case, give a brief explanation where possible; for false entries, this should take the form of a counter-example. Assume that comparisons always take constant time.

a. If all items in a heap are distinct, the second-smallest item in a heap has no children (assuming the heap is ordered with the largest item at the top).

b. Suppose that an ordinary binary tree (not a search tree) obeys the heap property - that the value of any node is at least as large as that of any of its children. Then inserting a new item into this tree while maintaining the heap property requires time O(lg n) , where n is the number of nodes in the tree.

c. Insertion sort always rquires Θ(n^2 ) time to sort n elements.

d. Quicksort always requires Ω(nlg n) time to sort n elements.

e. Suppose that RB is a red-black tree and that M comparisons are required to discover that a key, x , is not present in RB. Then if y also is not present in RB , it will require at least M /2 comparisons to discover this.

Problem

Assume that h(k, n) is a hash function that takes a key k and integer n and produces a number in the range [0...n-1]. Assume that K 1 , K 2 , ... is an infinite sequence of keys, and that h distributes these keys "evenly." More precisely, assume that for any M > 0, the number of keys k ∈{ K 1 , ... KM } such that h(k, n) = p is M/n ± 1, for each p ∈[0... n - 1]. Answer the following, giving brief explanations for each answer.

a. Suppose that you are to input a value M and then insert K 1 , ..., KM into a hash table. How can you arrange that each insertion runs in worst-case time O(1)?

b. Suppose that M is not explicitly input, and that instead you read keys K 1 , K 2 , ... , inserting them into a hash table as you go, until you encounter some marker indicating the end of the data. Assume that you try to manage the hash table so as to minimize the time required to do all the insertions. What is the (asymptotic) worst-case time for inserting Km (that is, the mth^ item you insert) as a function of m? Why?

c. Under the same assumptions as (b), what is the total worst-case asymptotic time for doing all M insertions? Why?

Problem

A threaded tree is essentially a tree whose nodes are additionally linked together into a list such that following the list gives an inorder traversal of the tree. Consider the following representation.

class TTree { public:

CS61B, Spring 1995 Midterm #2 Professor P. N. Hilfinger 1

TTree(KeyType k, TTree* L, TTree* R) : key(k), left(L), right(R), next(NULL) { }

KeyType key; TTree* left; TTree* right; TTree* next; };

Fill in the following function body to make the comment correct. You may define any additional functions you need.

/* Set the 'next' fields in the tree with root T, so as to link them / / into an inorder list. Return a pointer to the first node in / / this inorder list (or NULL if there is none.) / TTree linkTree(TTree* T) { // FILL IN HERE

Problem

When the program below is executed, it prints exactly the following three lines:

The answer is 42 What is the question?

Define the classes Action, IntAction, and StrAction so that the program behaves like this.

class Action // FILL IN

class IntAction // FILL IN

};

class StrAction // FILL IN

};

typedef List&#60Action*> AList;

main() { AList* L = new Alist( new StrAction("The answer is"), new AList( new IntAction(42), new AList("What is the question?", NULL))); while (L != NULL) {

Problem #3 2