Skip to content

Instantly share code, notes, and snippets.

@attomos
Created November 30, 2012 18:45
Show Gist options
  • Save attomos/4177672 to your computer and use it in GitHub Desktop.
Save attomos/4177672 to your computer and use it in GitHub Desktop.
Excerpt from my Family Tree
/**
* Prints all of ancestors of the given `full_name`, `level` is used
* for indicates the level of ancestors.
*/
void FamilyLinkedList::PrintAncestors(string full_name, int level) {
FamilyNode *person = Get(full_name);
// base case
if (person == NULL || full_name.compare(kNotIdentified) == 0) {
return;
}
FamilyNode *node = head_;
string prefix = "grand ";
int i;
// level - 1 because 1 level (grand father is not great-grand father)
for (i = 0; i < level - 1; i++) {
prefix = "great-" + prefix;
}
if (level == 0) {
prefix = "";
}
// printf("L %d\n", level);
// Only father and mother is look up based on the member variable not the
// node in linked list.
if (person->father().compare(kNotIdentified) != 0) {
printf("%s, %sfather\n", person->father().c_str(), prefix.c_str());
PrintAncestors(person->father(), level + 1);
}
if (person->mother().compare(kNotIdentified) != 0) {
printf("%s, %smother\n", person->mother().c_str(), prefix.c_str());
PrintAncestors(person->mother(), level + 1);
}
}
/**
* Prints all of descendants of the given `full_name`, `level` is used
* for indicates the level of descendants.
*/
void FamilyLinkedList::PrintDescendants(string full_name, int level) {
FamilyNode *person = Get(full_name);
FamilyNode *node = head_;
int counter = 0;
string prefix = "grand ";
int i;
// level -1 because 1 level is grand daughter or grand son
for (i = 0; i < level - 1; i++) {
prefix = "great-" + prefix;
}
if (level == 0) {
prefix = "";
}
while (node != NULL) {
if (EqualsIgnoreCase(node->father(), full_name) ||
EqualsIgnoreCase(node->mother(), full_name)) {
if (EqualsIgnoreCase(node->sex(), kFemale)) {
printf("%s, %sdaughter\n", node->full_name().c_str(),
prefix.c_str());
} else {
printf("%s, %sson\n", node->full_name().c_str(),
prefix.c_str());
}
// print descendants recursively
PrintDescendants(node->full_name(), level + 1);
}
node = node->next;
}
}
/**
* Prints siblings of `full_name`.
*/
void FamilyLinkedList::PrintSiblings(string full_name) {
FamilyNode *person = Get(full_name);
FamilyNode *node = head_;
while (node != NULL) {
// Common father or mother
if ((EqualsIgnoreCase(node->father(), person->father()) ||
EqualsIgnoreCase(node->mother(), person->mother())) &&
!EqualsIgnoreCase(node->full_name(), person->full_name())) {
if (EqualsIgnoreCase(node->sex(), kFemale)) {
if (node->age() == person->age()) {
printf("%s, sister [same age %d]\n",
node->full_name().c_str(),
person->age());
} else if (node->age() > person->age()) {
printf("%s, elder sister\n",
node->full_name().c_str());
} else {
printf("%s, younger sister\n",
node->full_name().c_str());
}
} else {
if (node->age() == person->age()) {
printf("%s, brother [same age %d]\n",
node->full_name().c_str(),
person->age());
} else if (node->age() > person->age()) {
printf("%s, elder brother\n",
node->full_name().c_str());
} else {
printf("%s, younger brother\n",
node->full_name().c_str());
}
}
}
node = node->next;
}
}
/**
* Prints all of the relative of `full_name`.
* The person full_name=`full_name` must exists in the linked list.
*/
void FamilyLinkedList::PrintRelativesOf(string full_name) {
printf("\n%s's relatives:\n\n", full_name.c_str());
PrintAncestors(full_name, 0);
PrintDescendants(full_name, 0);
PrintSiblings(full_name);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment