0% found this document useful (0 votes)
7 views

Data Structure - Coursework

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Structure - Coursework

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DATA STRUCTURE SUBJECT

Practical Work 3 (Dynamic Allocation and Recursion)


Make a program that simulates entries and queries in a family tree. The family tree maintains
the degree of kinship between the ascendants (father/grandfather/great-grandfather...) and their
descendants (son/grandson/great-grandson...). When representing family trees, it is common for
people to be directly connected only to their direct relatives (children or parents). However, it is
possible to reach distant relatives of the family tree by “walking” between generations. The following
figure illustrates a family tree representation.

Figure 1 - Example Family Tree


The most “ancestral” person on this family tree is Valdirene. Valdirene has three children: Carlos, Poliana and
Lucas. Each of these children may or may not have other children. Carlos has only one daughter (Amanda), Poliana
has no children, and Lucas has four children (Valentina, Enzo, Jorge and Maria). Through the family tree it is possible
to calculate the level of kinship between people. Between Paul and Amanda there is only one level of difference on the
tree, because they are son and mother. However, between Paulo and Valdirene there are three levels of difference in
the tree, because they are great-grandson and great-grandmother.

Make a program that discovers the level of relatedness between two people in a family. For
this, a structure (struct) must be created to represent the Person type. The Person type must store
values according to the table below:

Structure name Structure Fields


• name – string of up to 50 characters
• age - whole
People
• children - pointer of type People
• numChildren – whole
The children attribute will be a vector that will store the children (direct descendants) of the
person. As new children are inserted by data entry, the numChildren attribute must be incremented and
the size of the children vector must be increased to fit the new children entered. For that, you must
relocate the children vector using the realloc function, from the stdlib.h library.
The program should work as follows:
1. Initially it must be stored in an integer no the number of people to be registered;

two. Then read the data from these no people. Data reading should be done as
follows:
2.1 each of the no following lines represents a different person;
2.2 Each line has three pieces of information: the person's name, the person's age, and your name.
direct ascendant (father's or mother's name);

2.3 Note that it is not necessary to create an attribute for the “direct ascendant” in the Person struct.
ALTHOUGH, the direct ascendant must have their sons and daughters registered in the “children”
attribute. To find the direct ascendant, use the functionsearch person described in item 5;

2.4 Also note that the number of descendants the Person has is not informed. The ideal
is to reallocate the “children” attribute using the realloc function whenever a new child is
informed by input.

3. After registering people, you must store in an integer m the number of kinship-level
queries to be done;
4. Then read the parameters of the m queries. The reading of queries should be done
as follows:
4.1 Each row represents a different query;
4.2 Each query is represented by a pair of names, the first name being that of the
descendant (son/grandson/great-grandson…) and the second name is that of the ascendant (father/grandfather/great-grandfather…);

4.3 For each query, you must print the level of relatedness between the name pair.
mentioned in the consultation. To do this, use the searchPerson function described below by passing the
parameter printLevel with value 1.

5. Must implement the function recursive "searchPerson (Ascperson, searchname, level,


printLevel);" who discoversrecursively and prints the level of relatedness between the
people mentioned in the queries. If the ascendant is the descendant's father or mother, the
function must print 1. If the ascendant is the grandfather or grandmother, the function must
print 2 and so on. The function call is shown below.

struct Person* searchPerson (struct Person *Ascperson, char searchname[], int level, int printLevel);

The first parameter, “personAsc”, is a pointer to the most distant ancestor to be


considered in the search. When this function is used only to find a person's struct (required for
item 2.3), pass the value 0 to the parameter “imprimeNivel” (informing that this time the level
must not be printed) and pass it to the parameter “Ascperson” the first person entered in data
entry. By default for Moodle test cases, the first person informed in data entry will always be
the most distant ancestor of the family (the equivalent of Valdirene in the example in Figure 1).
This way, the search will consider all the people in the family tree.

The second parameter is the name of the person being searched, as entered in the data entry.
After finding the person you are looking for, the findPerson function should return a pointer to the struct
of the person you are looking for. However, before returning the person found, you must test the value
of the “imprimeNivel” parameter. If “printLevel” is equal to 1, print the level of relationship between the
ascendant and descendant involved in the query. If “printLevel” is equal to zero, don't print the level and
just return the struct of the person found. Thus, the searchPerson function can be
used whenever you have the person's name but don't have the person's struct. In this case, just use the
parameter “imprimeNivel” with a value equal to 0 and the function will search and return the person's struct
without printing other information.

The third parameter is the level of relatedness between the ascendant and descendant
of the query. This level of kinship must be increased throughrecursion as you walk through the
family tree.

The fourth parameter informs whether the relatedness level should be printed or not.
the level of kinshipshould not be printed when you are just looking for a person by the family
tree (for example, to comply with 2.3). And the level of kinshipmust be printed
when executing the queries requested by data entry, as described in item 4.3.
6. Finally, you must free the allocated memory using the “free()” command.

An example of code execution is shown below.

Input: 5
John 50 NULL
Mary 27 John
Joana 6 Mary
Carlos 30 João
Gisele 2 Maria
3
Gisele João
Carlos João
Joana Maria
Exit: two
1
1

In Moodle test cases it is guaranteed that, with the exception of the first Person in the data
entry, all next persons are descendants of someone previously informed. Test cases about uncle or
aunt will not be included. And test cases with two or more people with the same name will not be
included.

You might also like