Data Structures Mid Exam_2 (1)
Data Structures Mid Exam_2 (1)
Date 12/12/2014
Part I: Short Answers (4 Marks each)
1. Give the best asymptotic (“big-O") characterization of function
A.
B.
2. Let T be a binary tree with 111 internal nodes. What are the maximum and
minimum heights it can have?
3. Let T be a full binary tree with root r where each node stores an integer value.
Consider the following algorithms. What do the algorithms return when called
with parameter of the root r? Here v.left and v.right give the left and the right
children of node v, respectively, and v.value is the value stored at node v.
A. Algorithm One
Algorithm TreeAlgOne(v)
if v is external
return 0
else
return TreeAlgOne (v.left)+ TreeAlgOne(v.right) + 5 *v.value
B. Algorithm Two
Algorithm TreeAlgTwo(v)
count=0
if v is external
if v.value%2==0
return 1
else
return 0
else
if v.value%2==0
return TreeAlgTwo(v.left)+TreeAlgTwo(v.right) +1
else
return TreeAlgTwo(v.left)+TreeAlgTwo(v.right)
1
int [] values = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
Stack<Integer> s = new Stack<Integer>( );
for (int 1 = 0; i < values.length; i++)
s.push( values[ i ] );
int n = 25;
for (int i = 0; i < 4; i++)
{
n += s.pop( );
}
for (int i = 0; i < 2; i++)
{
n -= s.pop( );
}
cout<< n ;
Part II: Written Answers with Procedures (Include steps neatly to reach your final answers if necessary)
1. Write a Count() function that counts the number of times a given integer value
occurs in a linked list of integer values.
/*
Given a list and an int, return the number of times that int
occurs
in the list.
*/
int Count(struct node* head, int searchFor)
{
// Your code goes here
}
2. Write a RemoveDuplicates() function which takes a list sorted in increasing order
and deletes any duplicate nodes from the list. Ideally, the list should only be
traversed once.
/*
Remove duplicates from a sorted list.
*/
void RemoveDuplicates(struct node* head)
{
// Your code goes here...
}
3. Write a SortedMerge() function that takes two lists, each of which is sorted in
increasing order, and merges the two together into one list which is in increasing
order. SortedMerge() should return the new list. The new list should be made by
splicing together the nodes of the first two lists. There may be many cases to
deal with in your code such as either 'a' or 'b' may be empty. The first nodes
should remain unchanged or unaffected after the function is executed.
/*
Takes two lists sorted in increasing order, and
splices their nodes together to make one big
sorted list which is returned.
*/
struct node* SortedMerge(struct node* a, struct node* b)
{
// your code...
}
2
that takes a Queue and an element and returns true if the Queue contains this
element or false if not. The elements in the Queue must remain in their original
order once this method is complete.
6. Let T be a binary tree where each node stores an integer key that you can
access with v.value. Write a recursive algorithm AddValues(r) that receives as
input the root of the tree r and outputs the sum of all keys at the even levels of
the tree. For example, in the picture below, AddValues(r) should return 10, since
the even levels of this tree contain keys 1; 7; 2. You cannot assume that each
node knows on which level of the tree it is located, but you can store an
additional variable at each node, namely v.var.
Hint: use v.var for computing and storing the levels at each node Then you
are going to assign the v.var values for all nodes before you start to
compute sum of the even level node values.
3
tree
----
j <-- level 0
/ \
f k <-- level 1
/ \ \
a h z <-- level 2
\
d <-- level 3
Hint: A sample code for preorder traversal is shown below to start with and
modify.