Vous êtes sur la page 1sur 3

# CST 370 Spring 2017

## Final Exam (Take-home projects)

Name: ___________________________________
Ashley Wallace

awalace@csumb.edu
Email: ___________________________________

Important Note:

• There are two projects, each worth 50 points, total 100 points
• This is take-home exam, open book, open notes, and open computer,
but you MUST work independently
Project #2 (50 points). Find the first common ancestor of two nodes in a
binary search tree. For the following example, the common ancestor of node “30”
and “100” is node “80”, the common ancestor of node “40” and “60” is node “60”.

Avoid storing additional nodes in a data structure. Each node of the binary search
tree having two pointers: one to the left child and the other to the right child, but
there is no pointer to the parent node. This is the same data structure as defined in
the codes BST.h and BST.cpp.

a) (20 points) Design the algorithm and describe it in Pseudo code, including
necessary explanations so that the logic of your algorithm can be understood.

int firstCommonAncestor(right, left) //public function that returns an int, takes node data from user
{
BinNode node ← firstCommonAncestor(myRoot, right, left) //use the overloaded private function
return node.data //return the data value from the returned node
}

## BinNode firstCommonAncestor(BinNode node, right, left)

{
If tree is empty, return null //check for bad input at start

## If right & left are less than node data

firstCommonAncestor(node→left, right, left) //run recursively going left
if right & left are greater than node data
firstCommonAncestor(node→right, right, left) //run recursively going right
else
return node //else the path for right and left diverge here and we have found the first common
ancestor
}

b) (20 points) Implement your algorithm in C++ and test your implementation
with a tester/driver. In the tester, you can use the function “insert” in
BST.cpp to create a sample Binary Search Tree (either from user input or
hard coded). Then, run a few tests with different pairs of nodes to verify that
your implementation can generate the right ancestor. Again, your
implementation should handle special cases properly.

c) (5 points) Give the running time of your algorithm in big O notation (briefly
explain your reasoning). You may not get the credit for this part if your
program running time is very inefficient.
i Time complexity for this depends the most on what kind of tree is being searched. On a
balanced tree the time complexity is O(log n) because with each turn of the recursive
formula the pool of possible results is being reduced by 2, so n/2, then n/4, then n/8 …
n/2^k where k is the common ancestor. As with the previous question this becomes k=
log2n or O(log n).

If the the tree is unbalanced the worst case scenario is O(n). Assuming the tree contains
nothing but left or right side entries i.e. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, then the algorithm
would search until it finds either one of the two user entered nodes to find (right & left).
Then the worst case scenario is n-2 if the two entries were the bottom most nodes, i.e.
as in the 10-1 example above 1 & 2. Because in Big O notation constant numbers are
dropped, the time complexity is then O(n).