I have built a family tree program which thus far allows you to add relationships to nodes (mother, father, partner & siblings). This allows the tree to be built. well at least the links to be made.
My problem is I have to represent the tree textually. This is proving to be very hard. I thought of allowing to display the ancestors and descendants for a particular node but this proves to be difficult, especially when there are more than two generations for displaying ancestors (great grandparents and above).
To make things more clear I thought of using a file tree structure but got into a mess when displaying for great grandparents.
I can provide the source if anyone wants to see it.
What I mean is using a file structure starts of with a pair of people: e.g.
GRANDAD & GRANDMA
---DAD ------ME ------SISTER ---AUNT ------COUSIN ------COUSINThis would be for showing my ancestors on my dads side (inclusive of me). My question is what happens if both my grandparents have parents? How would I represent such relationships with the above structure? I would have to show my great grand-parents but there would be two sets! One for my grandma and one for my grandad. The root has been split into two if you see what I mean....
I though about limiting the 'show ancestors' function to show only 2 generations above. Which means that a user can traverse through the tree and indentify his/her g开发者_如何学运维reat grandparents by selecting their parent nodes and displaying their ancestors - but this seems laborious and not very intuitive.
The following method may do what you want. It will print a tree in a tabbed hierarchical structure:
public void printTree(Node root, int tabLevel, PrintStream out){
for(int i =0;i < tabLevel; i++) out.print("\t");
out.println(root)
for(Node n: root.getChildren())
printTree(n, tabLevel + 1, out);
}
You would call it like so:
printTree(rootNode, 0, System.out);
This doesn't take into account all of the the complexities of a family tree, but it should provide a good place to start.
精彩评论