2017 Answer
2017 Answer
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
Algorithm TreeAlgOne(v)
if v is external
return (v.value*v.value)
else
return TreeAlgOne (v.left)+ TreeAlgOne(v.right)
B. Algorithm Two
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)
2
{
if (head)
{
if (head->Value == searchFor)
return 1+ Count(head->Next, searchFor);
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
}
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;
}