Get path from every leaf node to root in a tree structure
How can I turn this tree structure
[1, [2, [3, 4]], [5, [6, [7], 8]]]
1
2
3
4
5
6
7
8
.... into this "reversed tree" structure, which basically contains the paths from all the leaf nodes to 1 (the root):
[8, [5, [1]], 7, [6, [5, [1]]], 4, [2, [1]], 3, [2, [1]]]
8
5
1
7
6
5
1
4
2
1
3
2
1
The result wouldn’t even have to be structured as a tree, four flat arrays in the correct order would also be fine.
It looks like Depth-first search might be a r开发者_运维问答elevant algorithm, but I can’t understand the pseudocode (what does incidentEdges() return?), so I’m pretty stuck.
If someone could offer a Ruby method (or really easy to understand pseudocode) to convert the original nested array into the result array, I would be infinitely grateful.
And this is not a homework assignment, rather it is the result of it being too long since I’ve studied... I need this to print a dependency tree in the proper order for a given issue in an issue tracker.
A bit more compact code:
tree = [1, [2, [3, 4]], [5, [6, [7], 8]]]
def find_reverse_leaf_paths(nodes, prefix = [], paths = [])
leafs = []
nodes.each do |node|
if node.is_a?(Numeric)
leafs.push(node)
else
prefix.push(leafs.pop) unless leafs.empty?
leafs.clear
find_reverse_leaf_paths(node, prefix, paths)
end
end
leafs.each do |leaf|
paths.push(prefix + [leaf])
end
prefix.pop unless leafs.empty?
paths.map { |path| path.reverse }.reverse
end
puts find_reverse_leaf_paths(tree).inspect
You can use this code. It's not my best code, but I'm learning ruby too :D (it was a good exercise)
a = [1, [2, [3, 4]], [5, [6, [7], 8]]]
class Node
attr_reader :value
attr_reader :parent
attr_reader :children
def initialize(value, parent)
@value = value
@parent = parent
@parent.add_child self unless parent == nil
@children = []
end
def add_child(child)
@children << child
end
def print_node(ident)
Range.new(0,ident).each {print ' '}
print @value.to_s
print "\n"
children.each { |child| child.print_node (ident+4) }
end
end
class Tree
def self.from_array(array)
process array, nil
end
def self.process(array, parent)
node = nil
array.each do |array_item|
if array_item.is_a? Numeric
node = Node.new(array_item, parent)
else
process(array_item, node)
end
end
node
end
def self.print_paths_to_root node
if node.children.empty?
puts print_path_to_root(node)
else
node.children.each do |child|
print_paths_to_root child
end
end
end
def self.print_path_to_root node
if node != nil
node.value.to_s + ' ' + print_path_to_root(node.parent)
else
""
end
end
end
puts 'TREE'
root = Tree.from_array a
root.print_node 0
puts "\n\n\n"
puts 'PATH TO ROOT'
Tree.print_paths_to_root root
Just thinking off the top of my head, why not recusively traverse the tree progressively concatenating the nodes, and when you reach a leaf output the nodes in reverse order. This should give you the 4 flat arrays you wanted.
your first 2 leaf-arrays would evolve like this:
1 - node
12 - node
123 - leaf - output 321.
12 - pop out
124 - leaf - output 421
NWS
To clarify the point I was trying to make in my previous comments to the question, I'll show some code. I use just an Array as tree, so the empty Tree must [root, []]
(hence the guard for empty children).
class Array
def paths
root, children = self
return [root] if children.empty?
children.map do |child|
(child.is_a?(Array) ? child.paths : [[child]]).map do |tail|
[root] + tail
end
end.flatten(1)
end
end
tree = [1, [[2, [3, 4]], [5, [[6, [7]], 8]]]]
p tree.paths
# [[1, 2, 3], [1, 2, 4], [1, 5, 6, 7], [1, 5, 8]]
Granted, this is neither the input you had nor the the result you wanted ;-) but it's the same idea, isn't it? My point is that if the data structure is "logic", the code should be pretty straighforward (and functional, to walk a tree we shouldn't need an imperative algorithm!).
精彩评论