CS 2606 Midterm Exam: Data Structures and Object-Oriented Development II - Prof. William D, Exams of Data Structures and Algorithms

A midterm exam for the cs 2606 data structures and object-oriented development ii course. The exam includes six short-answer questions covering topics such as algorithm complexity, function complexity relationships, and container implementation design. Students are not allowed to use notes or electronic devices during the exam.

Typology: Exams

Pre 2010

Uploaded on 11/09/2008

randyjarrett
randyjarrett 🇺🇸

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2606 Data Structures and OO Devel II Test 1
1
READ THIS NOW!
Print your name in the space provided below.
There are 6 short-answer questions, priced as marked. The maximum score is 100.
Aside from the allowed one-page fact sheet, this is a closed-book, closed-notes examination.
No laptops, calculators, cell phones or other electronic devices may be used during this examination.
You may not discuss this examination with any student who has not taken it.
Failure to adhere to any of these restrictions is an Honor Code violation.
When you have finished, sign the pledge at the bottom of this page and turn in the test and your fact sheet.
Name (Last, First) Solution
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
pf3
pf4
pf5

Partial preview of the text

Download CS 2606 Midterm Exam: Data Structures and Object-Oriented Development II - Prof. William D and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS 2606 Data Structures and OO Devel II Test 1

READ THIS NOW!

  • Print your name in the space provided below.
  • There are 6 short-answer questions, priced as marked. The maximum score is 100.
  • Aside from the allowed one-page fact sheet, this is a closed-book, closed-notes examination.
  • No laptops, calculators, cell phones or other electronic devices may be used during this examination.
  • You may not discuss this examination with any student who has not taken it.
  • Failure to adhere to any of these restrictions is an Honor Code violation.
  • When you have finished, sign the pledge at the bottom of this page and turn in the test and your fact sheet.

Name (Last, First) Solution

printed

Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.

signed

  1. Consider the following algorithm:

for (i = 1; i <= N; i++) { // 1 before, 2 each pass, 1 to exit

for (j = i; j <= N; j++) { // 1 before, 2 each pass, 1 to exit

if ( i * j <= N ) { // 2 each pass less = less + 5; // 2 if done } else { less = less – 1; // 2 if done } } }

a) [14 points] Using the rules given in class for counting operations, find a simplified function T(N) that counts the exact number of operations the algorithm would perform.

From the line-by-line analysis above, the complexity function would be:

( )

( )

( )

1 1

1 1

1 1 1

1

1

2

2

N N

i j N N

i j i N N i

i j j N

i N

i

T N

N i

N i

N N

N N

N N

= =

= = −

= = =

=

=

∑ ∑

∑ ∑

∑ ∑ ∑

b) [6 points] To what big-Θ complexity class does your answer to the previous part belong? (No proof is necessary.)

Obviously, it is Θ ( N^2 ).

  1. [16 points] When designing a container implementation in C++, why is it generally desirable to provide two versions of the search logic, as in the specified BST template interface:

T* const Find(const T& D); const T* const Find(const T& D) const;

Explain carefully why both are needed; it would be useful to provide hypothetical examples of client code to support your explanation.

The second form is necessary so that the client can call Find() in a context in which the container object has been declared as a const object. For example, the client may have created a container object in one function and then

passed it to another function by constant reference:

void Foo(const BST& Tree,... ) {

... ... = Tree.Find(.. .); ... }

Since Tree is const, the function Foo() can only call member functions of Tree that are declared with the const qualifier, and the call shown above would not be allowed unless there were a const version of Find().

On the other hand, the call shown above would only be allowed if the pointer returned by Find() is assigned to a suitably const pointer:

const int* const p = Tree.Find(.. .);

And, in this case, the client cannot use p to make any modifications to the target of p (which may actually be a good thing in most situations). But clearly in some cases, the client will want to call Find() to locate a data object and

then modify that data object in situ. Therefore, we also need the first form of Find() to allows things like:

void DeleteEntry(unsigned int Offset, Location L) {

... LocIdxEntry *p = QTree.Find(.. .); p->DeleteOffset( Offset ); ... }

  1. Consider the AVL tree at right:

a) [8 points] Draw the resulting AVL tree if the value 90 is inserted.

b) [8 points] Draw the resulting AVL tree if the value 70 is inserted (into the original tree).

  1. [16 points] Recall the definition:

Let f and g be non-negative functions of n. Then f is Ο( g)if and only if there exist constants

N > 0 and C > 0 such that, for all n > N, f n( ) ≤ Cg n( ).

Prove the following fact: if f , g and h are non-negative functions of n, and f is Ο( g)and g is Ο( ) h then f is Ο( ) h.

Note: you may NOT use the theorem that states that big-O is transitive.

proof: Suppose that f , g and h are non-negative functions of n, and f is Ο( g)and g is Ο( ) h.

Then from the definition, there exist constants N > 0 and C > 0 such that, for all n > N, f n( ) ≤ Cg n( ).

And, there also exist constants M > 0 and D > 0 such that, for all n > M, f n( ) ≤ Dg n( ).

(Note: there is no reason to suppose that the same constants will apply for both relationships.)

Let R = max( N M, )and let E = CD. Then we have that, for all n > R:

( ) ( ) since

( ) since

( ) since

f n Cg n n R n N

CDh n n R n M

Eh n E CD

Therefore, by definition, we have that f is Ο( ) h.

QED