




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





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:
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
n
f ( n )= n^2 + 3 n^2 log n isΩ^ (^ n^4 )
First we need to find the relevant limit:
2
4
2
4
2 2
n n n n
So, from the theorem, the statement is false.
a) a preorder traversal b) an inorder traversal
For each of the questions 5 and 6, start with the following BST:
For questions 7 and 8, assume the following template declarations for an implementation of a doubly-linked list:
// DNodeT.h //
... template
// irrelevant members not shown };
...
// DListT.h //
... template
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 };
...
template
DNodeT
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
template
public: // irrelevant members not shown };
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
RepMaxHelper( Root ); }
template
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; } }
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+^.