0% found this document useful (0 votes)
213 views4 pages

2017 Answer

This document contains a 6 part exam on data structures for an electrical and computer engineering course. It includes short answer and written response questions covering asymptotic analysis, binary trees, queues, stacks, and linked lists. Some questions ask students to analyze time complexities, determine heights of binary trees, describe algorithms that traverse tree nodes, and write pseudocode to search a linked list or count occurrences in a data structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views4 pages

2017 Answer

This document contains a 6 part exam on data structures for an electrical and computer engineering course. It includes short answer and written response questions covering asymptotic analysis, binary trees, queues, stacks, and linked lists. Some questions ask students to analyze time complexities, determine heights of binary trees, describe algorithms that traverse tree nodes, and write pseudocode to search a linked list or count occurrences in a data structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, 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
Data Structures Common Exam (25%) – 2 hours exam

Date: January 4, 2017


Part I: Short Answers
1. Let f(n) = 3n4 + 10n3 + 5n2 and g(n) = 3n4 + 2n2. Write down the best asymptotic ("big-
O") characterization of the following functions: (1 point each)
A. f(n) + g(n)= n4
B. f(n) - g(n)= n3
C. f(n) * g(n)= n8
2. Give the best asymptotic (“big-O") characterization of function. (1 point each)
A. f(n) = 39+n4log(n5)+n3.7+105n4

Ans n4log(n5)

B. f(n) = 56+3n+160.5nlog(n3)+n8log(n5)

Ans 160.5nlog(n3)

3. Let T be a binary tree with 121 internal nodes. What are the maximum and minimum
heights it can have? (2 points)
Ans maximum and minimum heights are 121 and 7 respectively.

4. 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. (2 points each)
A. Algorithm One

Ans, The sum of squared values of External nodes;

Algorithm TreeAlgOne(v)
if v is external
return (v.value*v.value)
else
return TreeAlgOne (v.left)+ TreeAlgOne(v.right)

B. Algorithm Two

Ans, The number of Odd valued nodes;

Algorithm TreeAlgTwo(v)
count=0
if v is external
if v.value%2==0
return 0
else
1
return 1
else
if v.value%2==0
return TreeAlgTwo(v.left)+TreeAlgTwo(v.right)
else
return TreeAlgTwo(v.left)+TreeAlgTwo(v.right)+1

5. Given a 5 element queue Q (from front to tail: 2, 4, 6, 8, 10), and an empty stack S,
remove the elements one-by-one from Q and insert them into S, then remove them one-
by-one from S and re-insert them into Q. Q now looks like (from front to tail). (1 points)
Ans 10,8,6,4,2

6. What is the maximum number of nodes that a binary tree of height 8 can accommodate?
And how many of them are leaf nodes? (2 points)
Ans maximum number of nodes=511
number of leaf nodes =256
Part II: Written Answers with Procedures (Include steps neatly to reach your final answers if
necessary)

1. Write, in pseudocode, an algorithm Check(A, n, m) that takes as input array A storing


integers, its size n, and another integer m. Your algorithm should return true if there are
two elements of A that add up to m, and false otherwise. For example, if A = f5; 4; 7g
then Check(A,3, 12) should return true since A[0]+A[2] = 5+7 = 12, and Check(A, 3, 2)
should return false since no two elements of A add up to 2. You are allowed to use only
O(1) of additional memory. This means that is you can use a few variables, but you
cannot declare arrays or any other data structures whose size depends on n. (4 points)
Ans
For I=0 up to n
For j=0 up to n
If I not equal to j and A[i] +a[j] equals to m
Return true
Return false
2. Write a Count() function that counts the number of times a given integer value occurs in a
linked list of integer values. (3 points)
/*
Given a list and an int, return the number of times that int occurs in the list.
*/
//Use this if you want to answer using C++
int Count(struct node* head, int searchFor)
{
// Your code goes here
}
//Use this if you want to answer using Java
int Count(NodeClass head, int searchFor)
{
// Your code goes here
}
Recursive method
public:int Count(struct Node* head, int searchFor)

2
{

if (head)
{

if (head->Value == searchFor)
return 1+ Count(head->Next, searchFor);

return Count(head->Next, searchFor);


}
return 0;
}
Iterative method
public:int Count1(struct Node* head, int searchFor)
{
if (head)
{
int count = 0;
Node *nodeptr = head;

while (nodeptr)
{
if (nodeptr->Value == searchFor)
count++;
nodeptr = nodeptr->Next;
}
return count;
}
return 0;
}
3. Write a recursive Binary Tree method named count_Even_leaves that returns the number
of even valued leaves in the tree. (4 points)
/*
Given a a binary tree, return the number of even valued leaf nodes in the tree.
*/
// Use this if you want to answer using C++
int countEvenLeaves(TreeNode *rootNode)
{
//Your code goes here
}

// Use this if you want to answer using Java


int countEvenLeaves(TreeNodeClass rootNode)
{
//Your code goes here
}

answer

3
private:int countEvenLeaves(Node *root)
{

if (root)
{
if (!root->Left && !root->Right&&root->Value%2==0)
return 1;

return countEvenLeaves(root->Right)+countEvenLeaves(root->Left);

return 0;
}

You might also like