This lab has you lay out a family tree so that it can be displayed by a graphical viewer. It is designed to give you practice with:
You are going to write two programs, one called print_family_tree that reads an input file that contains a family tree and pretty prints the family tree to the console, and one called family_tree that reads an input file that contains a family tree, calculates the graphical coordinates for each node in the tree, and outputs the graphical coordinates to a user-specified file. You may then pass the output-file as a command line argument to /home/bvz/cs140/labs/lab9/display_tree. display_tree will read the coordinates that you produce and graphically display the family tree. You will write print_family_tree first, and then modify it to create family_tree. Sample invocations of your programs might be:
print_family_tree family1 family_tree family1 family1_output
The input to your program will consist of lines of the form:
parent-name child1-name child2-name ... childk-nameThe first line of the input file represents the root of the family tree. For example:
Mary Jane Jill Emily Howard Jill Joe William Eddie Tom Hank Nancy Howard Tom James Ellen Katie Jane Tommy Jennifer SusanMary is the root of the this tree. To simplify the problem, you may assume that the children names are unique. Note that a child may appear as a parent later in the input. You should not assume that children always appear after their parents. They may appear before their parents as well. For example, Tom's parent line appears on line 3, but you do not find out that Tom's parent is Howard until line 4. Children that do not have a parent line are assumed to be leaf nodes in the family tree.
Start by writing print_family_tree. It should use the fields library to read the input lines and construct a family tree. You cannot make any assumptions in advance about how many children a person might have, although one you read a person's parent line, you can use an inputstruct's NF member to determine how many children a person has. Once you have constructed the family tree, you should print it out so that the person at the root of tree is printed first, then each of the person's children's subtrees is recursively printed, with the children indented 4 spaces. For example, given the above file, your output should look like:
Mary Jane Tommy Jennifer Susan Jill Joe William Eddie Emily Howard Tom Hank Nancy James Ellen KatieOnce print_family_tree.c works correctly, you should copy it to family_tree.c and modify it so that it computes a graphical layout for the family tree using a two step procedure as follows:
For each person in your family tree you should output the person's name, the person's x and y coordinates, and the names of the person's children. Each person should have their own separate line of output. You should use a pre-order traversal to print your tree. A sample output for the above input file would be:
Mary 0 0 Jane Jill Emily Howard Jane -31 1 Tommy Jennifer Susan Tommy -40 2 Jennifer -32 2 Susan -24 2 Jill -9 1 Joe William Eddie Joe -17 2 William -11 2 Eddie -4 2 Emily 4 1 Howard 24 1 Tom James Ellen Katie Tom 13 2 Hank Nancy Hank 10 3 Nancy 16 3 James 22 2 Ellen 28 2 Katie 34 2
You should perform the following error checks:
You may assume that names are unique in the sense that no name appears more than once in a family tree (note that a name may appear as both a parent and a child but a name will not appear more than once as a child).
Make sure that you check for special situations, such as a parent having only a single child or a parent's name requiring more space than the space required by its children.
The following homework will be discussed at the beginning of lab and should help acquaint you with the problem that you will be asked to solve:
Roughly speaking, here are the steps that you will need to implement in order to implement this program:
Mary Jane Jill Emily Howard Jill Joe William EddieSo after you have read the first two lines, you will have list elements for Mary, Jane, Jill, Emily, Howard, Joe, William, and Eddie. Each of these list elements will point to a tree struct that you should have created for the corresponding individual. When you create a tree struct for an individual, you may not be able to completely fill in its information. For example, when you create the tree struct for Mary, you will be able to completely fill in her children information. However, when you create the tree struct for Jane, you will not be able to fill in her children information. You will have to wait until later in the program when you read her parent line (or you may never read a parent line for Jane if she does not have children).
For each of the parent's children you should also determine whether or not a tree struct already exists for the child. If a tree struct already exists, then you should retrieve the tree struct and make the parent point to this child. For example, in the above input, when the parent line for Howard is read (line 4), a tree struct already exists for Tom, who is one of Howard's sons (Tom is first seen on line 3). Hence you would retrieve the tree struct for Tom from the dllist and add a child link from Howard to Tom. If a tree struct does not yet exist, then you should create a tree struct for the child and then make the parent point to this child. In the above input, none of Mary's children yet exists when Mary's line is read, and thus you would create a tree struct for each of Mary's children, add them to your dllist, and then add links from Mary to each of them.
When you complete your lab, answer the following two questions and place them in a file called answers.txt. Please just use a text editor like vi or notepad to answer these questions.
You should submit your homeowrk when the TA asks for it at the beginning of lab. You will submit the following files to the TAs via the submit script: