CSE 4510: Lab Assignment #7

Not to be turned in

The Task

You are to write a Java program Ancestor that reads in information about a family tree and upon request prints all the ancestors of a given individual.

Input comes in lines of two forms. The first gives information about a person.

Person;1899;1999;Father;Mother
The first date is the date of birth and the second is the date of death. The third field is the name of the father, and the forth the name of the mother. Any, but the first, field might be absent, but there are always four semicolons. The second form of the input is a query:
Person?
This input calls for the ancestors of the person to be printed. The input lines may come in any order. In particular, queries may come at any place in the input. When the query is read, the ancestors as known upto that point are printed immediately. Then the next input line is read. The behavior of the program is not specified if the input does not conform to these patterns. (This does not mean that all programs are equally good.) You need not detect contradictory information, e.g., different mothers for the same person. In such a case the best approach is probably the one that is easiest: keep only the last information entered as input.

In response to the ancestors query, output comes in lines of the following form:

Person;Father;Mother
A heading is printed before the list of ancestors (see the example). The person may or may not be found in the database already. The output is produced in a depth-first traversal of the ancestors as in the following procedure template.
public void dfs() {
   // Print this person
   if (/* father known */) father.dfs();
   if (/* mother known */) mother.dfs();
}
You may assume that there are no cycles in the ancestor relation. The paternal side is always followed first. Notice that this describes exactly what the output should look like.

In the output, the date of birth and the date of death (if known) are printed with the name. This takes one of the following four forms:

Henry Card               if no dates are known
Henry Card (b. 1893)     if birth date, but not death, is known
Henry Card (d. 1969)     if death date, but not birth, is known
Henry Card (1893-1969)   if both dates are known
Treat the dates as arbitrary strings.

Examples

Fred Card;;;Henry Card;               the father of Fred Card is Henry Card
Fred Card;;;;Lillian Spaulding        the mother of Fred Card is Lillian Spaulding
Henry Card;1893;1969;Williams I. Card;
Henry Card;;;;Mary A. Savage
Williams I. Card;;;Eli Card;
Eli Card;;;Williams Card;
Williams Card;;;Elisha Card;
Lillian Spaulding;;;Hallock Thomas Spaulding;Annie Pendergast
Hallock Thomas Spaulding;;;Thomas Spaulding;Mariah Hooker
Fred Card?                           who are the ancestors of Fred Card?
*** The ancestors of Fred Card ***
Fred Card;Henry Card (1893-1969);Lillian Spaulding
Henry Card (1893-1969);Williams I. Card;Mary A. Savage
Williams I. Card;Eli Card;
Eli Card;Williams Card;
Williams Card;Elisha Card;
Elisha Card;;
Mary A. Savage;;
Lillian Spaulding;Hallock Thomas Spaulding;Annie Pendergast
Hallock Thomas Spaulding;Thomas Spaulding;Mariah Hooker
Thomas Spaulding;;
Mariah Hooker;;
Annie Pendergast;;
Henry Card?                           who are the ancestors of Henry Card?
*** The ancestors of Henry Card (1893-1969) ***
Henry Card (1893-1969);Williams I. Card;Mary A. Savage
Williams I. Card;Eli Card;
Eli Card;Williams Card;
Williams Card;Elisha Card;
Elisha Card;;
Mary A. Savage;;

A hashtable is a good way of organizing an index of persons in this problem. Use the class HashMap. But you will need to design your own data structure for people that handles the the ancestor relation.

Helpful stuff


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Mon Mar 15 09:00:01 EST 1999