【问题标题】:Enumerate all full (labeled) binary tree枚举所有完整(标记)二叉树
【发布时间】:2013-01-31 18:45:42
【问题描述】:

我正在寻找一种实用的算法来枚举所有完全标记的二叉树。

完全二叉树是一棵树,其中所有内部节点的度数为 3,叶子的度数为 1,根的度数为 2。

带标签的树是所有叶子都有唯一标签的树。

例子:

    *
    |\
    | \
    *  *
   /|  |\
  / |  | \
 T  C  D  F

【问题讨论】:

  • 你不是在找树遍历算法吗? en.wikipedia.org/wiki/Tree_traversal
  • 每片叶子的深度都一样吗?
  • @domino Traversal 枚举单个树的节点,而不是一组树。
  • 您想只打印叶子吗?你能举例说明那棵树的输出是什么吗?
  • 没有深度是可变的,叶子的数量是给定的。

标签: python algorithm data-structures catalan


【解决方案1】:

从 cmets 可以清楚地看出,问题是枚举有根无序标记的完整二叉树。如this paper 中所述,带有n 标签的此类树的数量为(2n-3)!!,其中!! 是double factorial function。

以下python程序基于参考论文中的递归证明;我认为代码足够直截了当,可以作为算法的解释:

# A very simple representation for Nodes. Leaves are anything which is not a Node.
class Node(object):
  def __init__(self, left, right):
    self.left = left
    self.right = right

  def __repr__(self):
    return '(%s %s)' % (self.left, self.right)

# Given a tree and a label, yields every possible augmentation of the tree by
# adding a new node with the label as a child "above" some existing Node or Leaf.
def add_leaf(tree, label):
  yield Node(label, tree)
  if isinstance(tree, Node):
    for left in add_leaf(tree.left, label):
      yield Node(left, tree.right)
    for right in add_leaf(tree.right, label):
      yield Node(tree.left, right)

# Given a list of labels, yield each rooted, unordered full binary tree with
# the specified labels.
def enum_unordered(labels):
  if len(labels) == 1:
    yield labels[0]
  else:
    for tree in enum_unordered(labels[1:]):
      for new_tree in add_leaf(tree, labels[0]):
        yield new_tree

对于n == 4,有(2*4 - 3)!! == 5!! == 1 * 3 * 5 == 15 树:

>>> for tree in enum_unordered(("a","b","c","d")): print tree
... 
(a (b (c d)))
((a b) (c d))
(b (a (c d)))
(b ((a c) d))
(b (c (a d)))
(a ((b c) d))
((a (b c)) d)
(((a b) c) d)
((b (a c)) d)
((b c) (a d))
(a (c (b d)))
((a c) (b d))
(c (a (b d)))
(c ((a b) d))
(c (b (a d)))

对该问题的另一种可能解释是,它寻求具有指定标签列表的有根有序完整二叉树的枚举。具有 n 个叶子的此类树的数量由 C<sub>n-1</sub> 给出,来自 Catalan number sequence。

def enum_ordered(labels):
  if len(labels) == 1:
    yield labels[0]
  else:
    for i in range(1, len(labels)):
      for left in enum_ordered(labels[:i]):
        for right in enum_ordered(labels[i:]):
          yield Node(left, right)

对于 5 个标签,我们有 C<sub>5-1</sub> == 14:

>>> for tree in enum_ordered(("a","b","c","d", "e")): print tree
... 
(a (b (c (d e))))
(a (b ((c d) e)))
(a ((b c) (d e)))
(a ((b (c d)) e))
(a (((b c) d) e))
((a b) (c (d e)))
((a b) ((c d) e))
((a (b c)) (d e))
(((a b) c) (d e))
((a (b (c d))) e)
((a ((b c) d)) e)
(((a b) (c d)) e)
(((a (b c)) d) e)
((((a b) c) d) e)

【讨论】:

  • 这看起来是获取所有二叉 搜索 树的好方法。我想知道这是否是 OP 的问题。
  • @templatetypedef:OP 说“完整的二叉树”,这就是我制作的。 BST 具有标记节点和标记叶子,并且不一定是完整的(一个节点只能有一个子节点)。
  • 抱歉.. 但您的代码仅枚举 (A (B C)) ((A B) C) 而不是 ((A C) B)
  • @giggi:如果您想要所有可能的标签序列,请使用标签的每个排列调用枚举。不过,这将是不切实际的许多树。
  • @rici 这不是真的,如果我使用所有排列我会生成很多同构树,例如 (A (B c)) 和 ((B c) A) 或 ((CB) A ) ....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2014-07-05
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多