0

I'm writing a family tree program and I'm having troubles with realization of degree of kinship.I inputting two names, and program need to output degree of kinship. For example, when you enter - Alison and Anthony, it should turn out that Anthony is great-grandfather, and so for the remaining cases, father / grandfather / great-grandfather. Thanks for any ideas / tips.

#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <stdlib.h>
#include "Windows.h"


using namespace std;

ifstream file;

const int n = 15;

struct family
{
    string member;
    family* l, * r;
}base;

family* tree = NULL;

void init() {
    do {
        file >> base.member;
    } while (!feof);
}

void push(family** t)
{

    if ((*t) == NULL)
    {
        (*t) = new family;
        (*t)->member = base.member;
        //(*t)->year = base.year;
        (*t)->l = (*t)->r = NULL;
        return;
    }
    if (base.member < (*t)->member) push(&(*t)->r);
    else push(&(*t)->l);
}

void Tree_Balance(int n, family** t)
{

    family* now;
    int x, nl, nr;
    now = *t;
    if (n == 0)* t = NULL;
    else
    {
        nl = n / 2; nr = n - nl - 1;
        now = new family;
        (*now).member = base.member;
        //(*now).year = base.year;
        Tree_Balance(nl, &((*now).l));
        Tree_Balance(nr, &((*now).r));
        *t = now;

    }
}

void del_all(family*& t)
{
    if (!t) return;
    del_all(t->l);
    del_all(t->r);
    delete t;
    t = NULL;
}

int count(family* t)
{
    int result;
    if (t == NULL)
    {
        result = 0;
    }

    else if ((t->l == NULL) && (t->r == NULL))
    {
        result = 1;
    }

    else
    {
        result = count(t->l) + count(t->r);
    }

    return result;

}

void Print_Tree(family** tree, int l)
{

    if (*tree != NULL)
    {
        Print_Tree(&((**tree).r), l + 2);
        for (int i = 1; i <= l; i++) cout << "          ";
        cout << (**tree).member  << endl << endl;
        Print_Tree(&((**tree).l), l + 2);
    }
    Tree_Balance(n, tree);
}


int main()
{

    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    int calc;
    file.open("family.txt");

    if (!file)
    {
        cout << " error \n ";
        exit(1);
    }

    for (int i = 0; i < n; ++i) {
        init();
        push(&tree);
    }


    Print_Tree(&tree, 0);
    calc = count(tree);
    cout << endl << "members who have no children: " << calc << endl << endl;
    del_all(tree);
    system("pause");

    return 0;
}

my file:

Anthony Chloe Arthur Cameron Daniela Abraham Davina Daniel Armando Alana Alison  Aiden Aaron Abigale Aarav 

program outputs:


                                                            Aarav

                                        Aaron

                                                            Abigale

                    Abraham

                                                            Aiden

                                        Alana

                                                            Alison

Anthony

                                                            Armando

                                        Arthur

                                                            Cameron

                    Chloe

                                                            Daniel

                                        Daniela

                                                            Davina
hardkaze
  • 1
  • 1
  • You have 2 options. The first one is to keep the height of the current sub-tree like in [AVL_tree](https://en.wikipedia.org/wiki/AVL_tree). Therefore you can say which kinship the person is to another person if it is in the right subtree. The second option is to use recursion or stack, because recursion works like stack, common solution to these kind of problems when you need a child of some node and the parent node. Thats what comes to my mind – vikAy Apr 25 '20 at 09:43
  • My assumption is that you want to find the "path" to both then compare those> Ex: `Daniela=Anthony,Abraham+Chloe,Daniela` `Armando=Anthony,Abraham+Chloe,Daniela+Arthur,Armando` First two are identical so we can ignore them. Tier difference is 1 so son or nephew, Partial match on `Daniela` to `Daniela+Arthur` so son. – DClyde Apr 25 '20 at 17:33

0 Answers0