0% found this document useful (0 votes)
4 views

Data Structures Mid Exam_2 (1)

The document is an exam paper for the ECEG-4505 Data Structures course at Addis Ababa University, consisting of two parts: short answers and written procedures. Part I includes questions on binary trees, algorithms, stack and queue operations, and code output, while Part II requires students to write functions for counting occurrences in a linked list, removing duplicates, merging sorted lists, and performing breadth-first traversal. The exam is structured to assess students' understanding of data structures and algorithms within a 2.5-hour timeframe.

Uploaded by

natnaelmessay70
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structures Mid Exam_2 (1)

The document is an exam paper for the ECEG-4505 Data Structures course at Addis Ababa University, consisting of two parts: short answers and written procedures. Part I includes questions on binary trees, algorithms, stack and queue operations, and code output, while Part II requires students to write functions for counting occurrences in a linked list, removing duplicates, merging sorted lists, and performing breadth-first traversal. The exam is structured to assess students' understanding of data structures and algorithms within a 2.5-hour timeframe.

Uploaded by

natnaelmessay70
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Addis Ababa University

Addis Ababa Institute of Technology


School of Electrical & Computer Engineering
ECEG-4505: Data Structures Mid Exam (30%) – 2.5 hours exam

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)

4. Given a 5 element stack S (from top to bottom: 2, 4, 6, 8, 10), and an empty


queue Q, remove the elements one-by-one from S and insert them into Q, then
remove them one-by-one from Q and re-insert them into S. S now looks like
(from top to bottom).

5. What is the output of the following code?

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 ;

6. The minimum height of a binary search tree with 79 nodes is _____________

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...
}

4. Write a method of form


public Queue<String> contains(Queue<String> q, String str)

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.

5. Write a recursive BinaryTree method named count_leaves that returns the


number of leaves in the tree.

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.

Figure 1: Figure for Question 2 of part II

7. Breadth-first traversal: Another way is to go through them level-by-level. For


example, each element exists at a certain level (or depth) in the tree as shown
below:

3
tree
----
j <-- level 0
/ \
f k <-- level 1
/ \ \
a h z <-- level 2
\
d <-- level 3

So, if we want to visit the elements level-by-level (and left-to-right, as usual), we


would start at level 0 with j, then go to level 1 for f and k, then go to level 2 for a,
h and z, and finally go to level 3 for d.

This level-by-level traversal is called a breadth-first traversal because we explore


the breadth, i.e., full width of the tree at a given level, before going deeper.

Depending on this description, write a recursive method to implement breadth-


first transversal.

Hint: A sample code for preorder traversal is shown below to start with and
modify.

void IntBinaryTree::displayPreOrder(TreeNode *nodePtr)


{
if (nodePtr)
{
cout << nodePtr->value << endl;
displayPreOrder(nodePtr->left);
displayPreOrder(nodePtr->right);
}
}

You might also like