51 Replies - 2978 Views - Last Post: 25 July 2012 - 02:12 PM
#1
Family tree using Tree
Posted 16 July 2012 - 03:36 PM
Replies To: Family tree using Tree
#2
Re: Family tree using Tree
Posted 16 July 2012 - 05:12 PM
Redefine the nodes as not people but that can also be relationships..
Aka.. a node contains a relationship of people but not so much the person themselves..
#3
Re: Family tree using Tree
Posted 16 July 2012 - 05:19 PM
This post has been edited by Skydiver: 16 July 2012 - 05:19 PM
#4
Re: Family tree using Tree
Posted 16 July 2012 - 07:49 PM
#ifndef TREEOBJECTS_H
#define TREEOBJECTS_H
//simple class to represent a binary tree node
template <class Obj>
class Node
{
public:
Node(Obj data):left(NULL), right(NULL)
{
this->data = data;
}
Node(): left(NULL), right(NULL){} //default constructor
Obj data;
Node<Obj> *left, *right;
};
//class template of a simple BST
template<class Obj>
class BST
{
public:
//default constructor
BST()
{
this->size = 0;
this->root = NULL;
}
//return the smallest element in the BST
Obj findMin()
{
if( this->root != NULL)
{
Node<Obj> * p = this->root;
while(p->left != NULL)
{ //go to the left most
p = p->left;
}
return p->data;
}
else
{ //empty BST => throw exception
cout<<"\nCan't access an empty BST\n";
exit(0);
}
}
//insert a node to the tree
void insert(Obj data)
{
//create a node with the given data
if(this->root == NULL)
{
this->root = new Node<Obj>(data);
}
else
{
insert(this->root, data);
}
this->size++; //increment the list count
}
//insert a node given a pointer to a tree node
void insert(Node<Obj> *node, Obj & data)
{
if(node != NULL)
{
if(node->data > data)
{
if(node->left != NULL)
{
insert(node->left, data); //insert to the left
}
else
{
node->left = new Node<Obj>(data);
}
}
else if(node->data < data)
{
if(node->right != NULL)
{
insert(node->right, data); //insert to the right
}
else
{
node->right = new Node<Obj>(data);
}
}
else
{ //duplication
this->size--;
}
}
}
//print all the elements starting with the root based on the given type
// 0 = inorder, 1 = preorder, 2= post order
void print(int type)
{
if(this->root != NULL)
{
switch(type)
{
case 0:
printInOrder(this->root);
break;
case 1:
printPreOrder(this->root);
break;
case 2:
printPostOrder(this->root);
break;
}
}
}
//print the tree inorder
void printInOrder(Node<Obj> *node)
{
if( node != NULL)
{
printInOrder(node->left);
cout<< " " << node->data;
printInOrder(node->right);
}
}
//print the tree pre-order
void printPreOrder(Node<Obj> *node)
{
// put implementation here
}
//print the tree pre-order
void printPostOrder(Node<Obj> *node)
{
// put implementation here
}
//delete the tree from a given root
void deleteAll(Node<Obj> *node)
{
if(node == NULL)
return;
deleteAll(node->left);
deleteAll(node->right);
delete node;
}
//destructor
~BST()
{
if(this->root != NULL)
{
deleteAll(this->root);
}
}
private:
int size; // the number of nodes in the tree
Node<Obj> *root; //root of the tree
};
#endif
Here is the cpp file that is going to be an .obj file when compiled: #include "treeObjects.h" #include <string> #include <cstring> #include <iomanip> #include <iostream> using namespace std; int //What goes here? ::BST::findMin
I have no idea what to do there. If I can figure out how to get just one of the member functions working in the implementation file, it will be great. However, that is only one of the problems. I don't see how it is possible to do that thing with the spouse in a Binary search tree, here is why:
Actually I don't even know how I am going to represent the thing here, so ignore above. The graph theory is unhelpful because it doesn't actually explain whether I can do something in C++ or not (such as having a root node to the side of the graph, which connects to a child, which is already connected to another parent which eventually connects to the main root node; I don't even think this would be a tree anymore actually would it?). Don't know. I asked the teacher but he was extremely unhelpful, just saying again that you look to see if the children have multiple parents which then indicates spouse. How?
#5
Re: Family tree using Tree
Posted 16 July 2012 - 08:49 PM
The family tree programs I have come across have a list of persons with id numbers. Your person data could contain id's of spouses, parents and children.
Edit: I suppose each person has two parents!
Making Family Tree in C++
This post has been edited by #define: 16 July 2012 - 09:33 PM
#6
Re: Family tree using Tree
Posted 16 July 2012 - 10:06 PM
#7
Re: Family tree using Tree
Posted 16 July 2012 - 10:18 PM
I tend to agree with the OP. I'm just not seeing a way of using a just a binary tree to represent a family tree. If somebody can hint how it is possible, that would be great.
#8
Re: Family tree using Tree
Posted 16 July 2012 - 10:41 PM
Quote
In C++ when dealing with templates both the implementation and definition must be in the same compilation unit. Which usually means in the same file.
If you use a "family" as the search condition a tree seems to be applicable. Each "family" consists of the parents and all their children. These children can also be the root of their own "family".
Jim
#9
Re: Family tree using Tree
Posted 17 July 2012 - 05:21 PM
#10
Re: Family tree using Tree
Posted 17 July 2012 - 11:25 PM
However that system still does not account for marriage relationships. It also only cares about paths back to your ancestors roots. It doesn't matters if you married a Kennedy, your kids are still children of an Estonian serf.
To complicate matters, how do you handle adoptions within an extended family? Do you move that sibling or keep them where they are?
Also how do you handle relatives that are distantly related because they know the primary root, but they don't know the intermediate generations?
So please, can somebody please describe how to use a binary tree to represent a family tree suitable for genealogy? I feel that the page that I linked to that ostensibly was using a binary tree for genealogy failed utterly.
#11
Re: Family tree using Tree
Posted 18 July 2012 - 07:34 AM
Quote
It is possible for a person to have multiple wives or husbands, each marriage/relationship is an family type event. I do agree things can get complicated. For example my grandfather remarried after my grandmother died. The strange part about this is that he married my great aunt, the sister of his dead wife. Since this second wife also had children are these children your uncles/aunts or are they cousins? My father always considered her as his aunt, not a step mother, this may have been because this marriage lasted less than a year before my grandfather died and my father was already an adult at the time of this marriage. Also if this new marriage would have produced children where would they fit?
Quote
Adoptions are considered a family event. The adopted child still retains his/her birth parents but now also gains the adopted parents.
Quote
There are several ways of dealing with this, one is to add placeholders for the missing generations. Then as your research reveals the missing information you modify these placeholders. The other method is that until you have the connecting information these individual families are separate and unrelated. As you fill in the gaps the separate "families" will merge at the proper point. The finding of this missing information is the key to creating your trees. I myself have this exact problem. I am aware of distant cousins but have not until recently been able to determine exactly how these separate families were actually connected. I was able to finally "connect the dots" because I found one of these cousins living with his grandparents and his grandparents were one of the "missing links". Before this I had been unable to determine their names, but with this new information I was able to link these two separate trees.
Jim
#12
Re: Family tree using Tree
Posted 18 July 2012 - 07:43 AM
jayburn00, on 16 July 2012 - 09:49 PM, said:
Family trees are not binary, nor are they search trees.
They are not binary: Nodes in a binary tree may have at most two child nodes. No such restriction exists on a family, although I understand Chinese computer scientists are doing interesting research on the unary search tree.
They are not search trees: A binary search tree organizes linear information by sort order: you know whether to branch left or right by comparing your target to the current node - less means left. The information contained in a binary search tree is typically experienced in real life as a flat list, like a series of dictionary entries or contacts in a phone book.
A family tree represents an actual hierarchy, so it can't be a search tree, and there is no restriction on the number of descendents allowed to a particular node. Either you've misunderstood the assignment, or your professor has.
This post has been edited by jon.kiparsky: 18 July 2012 - 07:49 AM
#13
Re: Family tree using Tree
Posted 18 July 2012 - 12:26 PM
#14
Re: Family tree using Tree
Posted 18 July 2012 - 12:42 PM
Requirements: modify and complete the BST we have partially finished in the class accordingly and use it for the following program:
1. Load a data file: load file ancestry.dat with into your tree. The file has the following format:
parent(X,
parent(X,B,C)
parent(John Doe, Mary Jane)
child(B,X)
child(Joe, Mary Jane)
child(B,X,Y)
male(X)
female(Y)
….
This is how to interpret the data
+ parent(X,
+ child(B,X) and child(B,X,Y) mean B is a child of X and B is a child of Y
+ All data are case-insensitive.
+ Do not store duplicate nodes
2. Your program should the provide the user with the following options
a. Print the tree in level-order from root to leaves (hint: a stack can help)
b. Add new entry – the given entry will be in the same format as the file
c. Confirm
d. Search for children, ancestors, descendants, or siblings from any node (obtain the node data from the user)
e. Exit – asks user whether if they want to save the tree back to the file or not – then acts accordingly.
The “confirm” option, worth 50 points, will take in a relation and answer true or false. The following relations must be implemented: ancestor(X,Y), decedent(X,Y), wife(X,Y), and husband(X,Y).
I think the Parent(X,Y) could be read as a function. However, I have never made a function that passed more than one parameter before (actually I probably have done 1 where it passed 2, but very long ago and can't remember how). I think also that the functions could somehow do inserts into the tree. Oh, and operate on the assumption that it is not a binary search tree despite what the assignment says. How can one simply a general tree that one is able to run a comparison of children and parents of each node? Ignore the smiley faces, those are supposed to be parentheses and something else.
This post has been edited by jayburn00: 18 July 2012 - 12:44 PM
#15
Re: Family tree using Tree
Posted 18 July 2012 - 12:44 PM
|
|

New Topic/Question
Reply


MultiQuote









|