10 Problems with Solution - Data Structures and File Management | CS 2604, Exams of Computer Science

Midterm w/ answers B Material Type: Exam; Professor: McQuain; Class: Data Structures and File Mgmt; Subject: Computer Science; University: Virginia Polytechnic Institute And State University; Term: Spring 2005;

Typology: Exams

Pre 2010

Uploaded on 10/04/2006

koofers-user-yzc
koofers-user-yzc 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2604 Data Structures Midterm Spring 2005
Form B 1
V
I
R
G
I
N
I
A
P
O
L
Y
T
E
C
H
N
I
C
I
N
S
T
I
T
U
T
E
A
N
D
S
T
A
T
E
U
N
I
V
E
R
S
I
T
Y
U
T
P
R
O
S
I
M
Instructions:
Print your name in the space provided below.
This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No
calculators or other computing devices may be used.
Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly
indicate that and label the continuation with the question number.
If you want partial credit, justify your answers, even when justification is not explicitly required.
There are 10 questions, priced as marked. The maximum score is 100.
When you have completed the test, sign the pledge at the bottom of this page and turn in the test.
Note that either failing to return this test, or discussing its content with a student who has not taken it is a
violation of the Honor Code.
Do not start the test until instructed to do so!
Name Solution
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
pf3
pf4
pf5
pf8

Partial preview of the text

Download 10 Problems with Solution - Data Structures and File Management | CS 2604 and more Exams Computer Science in PDF only on Docsity!

V

IR

G

IN

IA

PO

LYTECHNIC IN ST IT U TE

A N D ST A (^) TE (^) UNIVE

RS

IT

Y U (^) TPROSIM

Instructions:

  • Print your name in the space provided below.
  • This examination is closed book and closed notes, aside from the permitted one-page formula sheet. No calculators or other computing devices may be used.
  • Answer each question in the space provided. If you need to continue an answer onto the back of a page, clearly indicate that and label the continuation with the question number.
  • If you want partial credit, justify your answers, even when justification is not explicitly required.
  • There are 10 questions, priced as marked. The maximum score is 100.
  • When you have completed the test, sign the pledge at the bottom of this page and turn in the test.
  • Note that either failing to return this test, or discussing its content with a student who has not taken it is a violation of the Honor Code.

Do not start the test until instructed to do so!

Name Solution

printed

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

signed

  1. [6 points] Complete the statement of the theorem below:

Theorem Suppose that f and g are functions with domain [1, ∞) and that: C

g n

f n

n

lim

Then: (a) if 0 < C < ∞ then f(n) is Θ ( g(n) ).

(b) if C = 0 then f(n) is Ο( g(n) ).

(c) if C = ∞ then f(n) is Ω( g(n) ).

  1. [8 points] Use the theorem you completed above to either prove the statement below is true, or that it is false. (Be sure to state your conclusion.)

f ( n )= n^2 + 3 n^2 log n isΩ^ (^ n^4 )

First we need to find the relevant limit:

ln 3

0 lim

1 3 log

lim

3 log

lim

3 log

lim 422

2

4

2

4

2 2

= +^ =

→∞ →∞ →∞ →∞ n

n

n

n

n n

n n

n

n

n

n n n

n n n n

So, from the theorem, the statement is false.

  1. [10 points] Referring to the binary tree shown below, write down the values in the order they would be visited if an enumeration was done using:

a) a preorder traversal b) an inorder traversal

For each of the questions 5 and 6, start with the following BST:

  1. [8 points] Draw the resulting BST if 40 is inserted.
  2. [8 points] Draw the resulting BST if 50 is deleted.

For questions 7 and 8, assume the following template declarations for an implementation of a doubly-linked list:

// DNodeT.h //

... template class DNodeT { public: T Element; DNodeT* Prev; DNodeT* Next;

// irrelevant members not shown };

...

// DListT.h //

... template class DListT { private: DNodeT* Head, Tail; // pointers to first and last data nodes, if any DNodeT* Fore, Aft; // pointers to leading and trailing sentinels

public: // irrelevant members not shown iterator begin(); // return iterator to first data node (or end()) iterator end(); // one-past-end const_iterator begin() const; // return const_iterator objects similarly const_iterator end() const; ~DListT(); // destroy all dynamic content of the list };

...

  1. [8 points] Write an implementation of the destructor for the DListT template. You may not call any other member functions of the template in your solution.

template DListT::~DListT() {

DNodeT* Current = Head;

while ( Current != Aft ) {

Head = Head->Next; delete Current; Current = Head; }

delete Fore; delete Aft; }

For question 9, assume the following template declarations for an implementation of a binary tree:

template class BinNodeT { public: T Element; BinNodeT* Left; BinNodeT* Right; // irrelevant members not shown };

template class BinTreeT { protected: BinNodeT* Root; // irrelevant members not shown

public: // irrelevant members not shown };

  1. [10 points] Write an implementation for the new BinTreeT member function below. Your implementation should not

need to call any other template member functions, except for any helper member functions you may wish to write.

// PostOrderRepMax makes a postorder traversal of the binary tree. When it // "visits" an internal node, it replaces the value stored there with the larger // of the of the values stored in the children of that node. Leaf nodes are // unchanged. template void BinTreeT::PostOrderRepMax(std::ostream& Out) const {

RepMaxHelper( Root ); }

template void BinTreeT::RepMaxHelper(BinNodeT* sRoot) const {

if ( sRoot == NULL ) return;

if ( sRoot->Left == NULL && sRoot->Right == NULL ) return;

RepMaxHelper( sRoot->Left );

RepMaxHelper( sRoot->Right );

if ( sRoot->Left == NULL ) sRoot->Element = sRoot->Right->Element; else if ( sRoot->Right == NULL ) sRoot->Element = sRoot->Left->Element; else { if ( sRoot->Left->Element >= sRoot->Right->Element ) sRoot->Element = sRoot->Right->Element; else sRoot->Element = sRoot->Left->Element; } }

  1. [10 points] Let T be a binary tree, and number the levels of the tree starting at zero, so we would say the root node is in

level 0. Prove: for all n ≥ 0, the maximum number of nodes T can have in level n is equal to 2n^.

proof: Let T be a binary tree. Level 0 contains, at most, the root node, so the maximum number of nodes that can occur in level 0 would indeed be 1 (which is 2^0 ).

Assume that for some integer k ≥ 0, the maximum number of nodes in level k is 2 k.

Consider level k+1 of T. Each node in level k+1 must be the child of a node in level k. Each node in a binary tree can have no more than 2 children. Therefore, the maximum number of nodes in level k+1 is exactly twice the number of nodes in level k. Since the latter is 2 k, the maximum number of nodes T can have in level k+1 would be 2 × 2 k^ = 2 k+^.