Created
October 29, 2012 15:45
-
-
Save DimaSamodurov/3974281 to your computer and use it in GitHub Desktop.
Build hash style tree from array of nodes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Got from: http://stackoverflow.com/questions/8028211/tree-to-array-algorithm-ruby | |
def build_tree(i, edges) | |
list = {} | |
out_nodes = edges.select {|e| e[0] == i}.map {|e| e[1]}.uniq | |
out_nodes.each {|n| list[n] = build_tree(n, edges)} | |
list | |
end | |
edges = [[1,2],[1,6],[1,9],[2,3],[3,10],[4,7]] | |
puts build_tree(1, edges) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment