Expression trees, B and B* trees, red-black trees, quad trees, PQ trees; trees play a significant role in many domains of computer science. Sometimes the name of a problem may indicate that trees are used when they are not, as in the Artificial Intelligence planning problem traditionally called the Monkey and Bananas problem. Sometimes trees may be used in a problem whose name gives no indication that trees are involved, as in the Huffman code.
This problem involves determining how pairs of people who may be part of a ``family tree'' are related.
Given a sequence of child-parent pairs, where a pair consists of the child's name followed by the (single) parent's name, and a list of query pairs also expressed as two names, you are to write a program to determine whether the query pairs are related. If the names comprising a query pair are related the program should determine what the relationship is. Consider academic advisees and advisors as exemplars of such a single parent genealogy (we assume a single advisor, i.e., no co-advisors).
In this problem the child-parent pair p q denotes that p is the child of q. In determining relationships between names we use the following definitions:
p is a 0-descendent of q (respectively 0-ancestor) if and only if the child-parent pair p q (respectively q,p ) appears in the input sequence of child-parent pairs.
p is a k-descendent of q (respectively k-ancestor) if and only if the child-parent pair p r (respectively q r) appears in the input sequence and r is a (k-1)-descendent of q (respectively p is a (k-1)-ancestor of r).
For the purposes of this problem the relationship between a person p and a person q is expressed as exactly one of the following four relations:
1 child -- grand child, great grand child, great great grand child, etc.
By definition p is the ``child'' of q if and only if the pair p q appears in the input sequence of child-parent pairs (i.e., p is a 0-descendent of q); p is the ``grand child'' of q if and only if p is a 1-descendent of q; and
if and only if p is an (n+1)-descendent of q.
2 parent -- grand parent, great grand parent, great great grand parent, etc.
By definition p is the ``parent'' of q if and only if the pair appears in the input sequence of child-parent pairs p q (i.e., p is a 0-ancestor of q); p is the ``grand parent'' of q if and only if p is a 1-ancestor of q; and
if and only if p is an (n+1)-ancestor of q.
3 cousin -- 0 th cousin, 1 st cousin, 2 nd cousin, etc.; cousins may be once removed, twice removed, three times removed, etc.
By definition p and q are ``cousins'' if and only if they are related (i.e., there is a path from p to q in the implicit undirected parent-child tree). Let r represent the least common ancestor of p and q (i.e., no descendent of r is an ancestor of both p and q), where p is an m-descendent of r and q is an n-descendent of r.
Then, by definition, cousins p and q are `k th` cousins'' if and only if k=min(n,m)
, and, also by definition, p and q are ``cousins removed j times'' if and only if j=|n-m|.
4 sibling -- 0 th cousins removed 0 times are ``siblings'' (they have the same parent).