data structure for Family tree
Is it homework?
Even if it's called “Tree”, It's a bad structure : imagine two brother who marry two sisters.
A general graph structure would be the best (a tree being a specific form of a graph). The edge would carry the relationship. Then you can run a path finding algorithm (like good old dijkstra) only on edges which represent blood relationship.
And boost::graph is a very good library.
Which data structure to use to implement family tree in ruby?
I could figure out a way to get minimum relations using RubyTree
itself. RubyTree has inbuilt methods like parent, siblings, children etc and also we can pass content to the nodes.
So I used these to get what I want. For example for creating a spouse I created a child node to the root node and passed a hash like {relation: spouse}
in the content. In this way I am able to apply logic by putting conditions on this hash and get the relations that I want
example:
tina = Tree::TreeNode.new('Tina', {gender: 'female', relation: 'root'})
mike = tina << Tree::TreeNode.new('Mike', {gender: 'male', relation: 'spouse'})
sofi = tina << Tree::TreeNode.new('sofi', {gender: 'female', relation: 'child'})
...... #add all children and their children like this
puts "--------siblings of sofi--------"
siblings_of_sofi.each do |sib|
if sib.content[:relation] == 'child'
puts sib.name
end
end
# assume tina has 4 sons and one of them is bob and alice is daughter of bob.
puts "--------alice paternal uncles--------"
puts alice.parent.name
puts alice.parent.content
if alice.parent.content[:relation] == 'spouse'
father = alice.parent.parent #as per the question child should be added through mother only, therefore alice is added as a child to bob's wife and bob's wife is added as a child to bob as {relation: spouse}
uncles = father.siblings
uncles.each do |uncle|
puts uncle.name if uncle.content[:gender] == 'male' && uncle.content[:relation] == 'child'
end
end
Related Topics
Image - Upload Not Responding, No Access to $_Files
How to Correctly Link PHP-Fpm and Nginx Docker Containers
Better Way to Check Variable for Null or Empty String
What Does New Self(); Mean in PHP
How to Output in Cli During Execution of PHP Unit Tests
Accessing JSON Array After JSON_Decode/Multidimensional Array
Passing Variables from PHP to JavaScript
Simplexml Get Attributes with a Namespace
Url Rewriting-Code for Rewriting
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce
Domxpath - Get Href Attribute and Text Value of an a Element
Retrieve Data from Db and Display It in Table in PHP .. See This Code Whats Wrong with It