1

我怎样才能打开这个树形结构

[1, [2, [3, 4]], [5, [6, [7], 8]]]

1
   2
      3
      4
   5
      6
         7
      8

....进入这个“反向树”结构,它基本上包含从所有叶节点到 1(根)的路径:

[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

结果甚至不必构造为树,以正确顺序排列的四个平面数组也可以。

看起来深度优先搜索可能是一种相关算法,但我无法理解伪代码(incidentEdges() 返回什么?),所以我很困惑。

如果有人可以提供一种 Ruby 方法(或非常容易理解的伪代码)将原始嵌套数组转换为结果数组,我将不胜感激。

这不是家庭作业,而是因为我学习时间太长了......我需要这个来为问题跟踪器中的给定问题以正确的顺序打印依赖关系树。

4

4 回答 4

1

更紧凑的代码:

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
于 2011-03-10T21:56:11.110 回答
1

您可以使用此代码。这不是我最好的代码,但我也在学习 ruby​​ :D (这是一个很好的练习)

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
于 2011-03-10T16:06:53.203 回答
0

为了澄清我在之前对该问题的评论中试图提出的观点,我将展示一些代码。我只使用一个数组作为树,所以必须是空树[root, []](因此是空子的守卫)。

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]]

当然,这既不是您的输入,也不是您想要的结果 ;-) 但这是相同的想法,不是吗?我的观点是,如果数据结构是“逻辑”的,那么代码应该非常简单(并且功能强大,要走一棵树,我们不应该需要命令式算法!)。

于 2011-03-11T18:56:34.533 回答
0

只是在我的脑海中思考,为什么不递归地遍历树,逐步连接节点,当你到达叶子时,以相反的顺序输出节点。这应该给你你想要的 4 个平面阵列。

你的前 2 个叶子数组会像这样演变:

1 - node 
12 - node
123 - leaf - output 321.
12 - pop out
124 - leaf - output 421

新创建

于 2011-03-10T14:19:24.630 回答