Hi everyone,
I'm trying develop a code to read a file containing records of INDIVIDUAL ID, FATHER ID and MOTHER ID, and re-code the file using multiple trees. At this point I'm not concerning to re-code the IDs and I would like to build the multiple tree.
The idea is:
A) Read each line from the genealogy file and connect the individuals by theirs fathers or mothers.
B) If the individual doesn't have father or mother, I will set father and mother as NULL. Tree disconnected from the others.
C) If the individual has father or mother (Or if the individual is father or mother), I need to connect him to respective node.
Below, I typed a simple example and my first code. I don't know how to manipulate the pointers to build the multiple tree. I was planning to retrieve information from the vectors and build the tree. Could anyone help me?
Thanks a lot!
File:
1 0 0
2 0 0
3 14 20
14 999 6
20 10 1000
30 999 7
Multiple Tree:
1) NULL(F) NULL(M)
1(I)
2) NULL(F) NULL(M)
2(I)
3) [999(F) 6(M)] [10(F) 1000(M)]
[14(I)(F)] [20(I)(M)] // 14 is son of 999 and 6 and father of 3
3(I)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <ctype.h>
using namespace std;
struct TreeGen *get_AniGeneal(void); /* Prototype for input function */
char set_ancestry(struct TreeGen *pmember1, struct TreeGen *pmember2);
char related (struct Family *pmember1, struct Family *pmember2);
void getGeneal(ifstream &myfileGenealOriginal,ofstream &myfileLog, ofstream &myfileOut,vector<int> &vetorGenealOrigIndiv,vector<int> &vetorGenealOrigFather,vector<int> &vetorGenealOrigMother);
void printIntV(ofstream &myfileLog,vector<int>& a);
void buildTree(M &top, int indiv, int father, int mother);
typedef struct STreeGen /* Family structure declaration */
{
int indiv;
int father;
int mother;
struct STreeGen *pFather; /* Pointer to father structure */
struct STreeGen *pMother; /* Pointer to mother structure */
} Tree;
typedef Tree *M;
int main ()
{
vector<int> vetorGenealOrigIndiv;
vector<int> vetorGenealOrigFather;
vector<int> vetorGenealOrigMother;
ifstream myfileGenealOriginal;
ofstream myfileOut; // OUTPUT END FILE
ofstream myfileLog;
myfileGenealOriginal.open("ped.txt");
myfileLog.open("log.txt");
myfileOut.open("out.txt");
getGeneal(myfileGenealOriginal,myfileLog, myfileOut,vetorGenealOrigIndiv,vetorGenealOrigFather,vetorGenealOrigMother);
return 0;
}
void getGeneal(ifstream &myfileGenealOriginal,ofstream &myfileLog, ofstream &myfileOut,vector<int> &vetorGenealOrigIndiv,vector<int> &vetorGenealOrigFather,vector<int> &vetorGenealOrigMother)
{
string lineGenealOrig = "";
int indivV = 0;
int fatherV =0;
int motherV = 0;
if ( myfileGenealOriginal.is_open() )
{
while(myfileGenealOriginal >> lineGenealOrig)
{
istringstream (lineGenealOrig) >> indivV;
vetorGenealOrigIndiv.push_back(indivV);
myfileGenealOriginal >> lineGenealOrig;
istringstream (lineGenealOrig) >> fatherV;
vetorGenealOrigFather.push_back(fatherV);
myfileGenealOriginal >> lineGenealOrig;
istringstream (lineGenealOrig) >> motherV;
vetorGenealOrigMother.push_back(motherV);
}
}
else
{
cout << "Unable to open file Original File" << endl;
}
myfileLog << "\nPrinting ID: " << endl;
printIntV(myfileLog,vetorGenealOrigIndiv);
myfileLog << "\nPrinting Father ID: " << endl;
printIntV(myfileLog,vetorGenealOrigFather);
myfileLog << "\nPrinting Mother ID: " << endl;
printIntV(myfileLog,vetorGenealOrigMother);
}
void printIntV(ofstream &myfileLog,vector<int>& a)
{
for(int i=0; i<a.size(); ++i)
myfileLog << a[i] << "\n";
}
void buildTree(M &top, int indiv, int father, int mother)
{
M NOaux;
NOaux = top; // PRESERVAR "TOPO" INTACTO
if(NOaux == NULL)
{
NOaux = new(Tree);
NOaux->indiv = indiv;
NOaux->father = father;
NOaux->mother = mother;
top = NOaux;
}
else
{
}
}