简体   繁体   English

如何在Java中生成树?

[英]How do I generate trees in Java?

So how do I tackle this problem? 那么我该如何解决这个问题呢? I need to a program that reads a positive integer n from standard input and writes to standard output a representation of all distinct rooted, ordered, labeled trees on the set {1,2,3....n} of vertices. 我需要一个程序,该程序从标准输入中读取一个正整数n并将标准{set,3,... n}组顶点上所有不同的生根,有序,标记树的表示写入标准输出。

For the output, I need to use the following linear textual representation L(t) of a tree t : 对于输出,我需要使用树t的以下线性文本表示形式L(t)

 If t is empty then L(t) = ().
 If t has root n and children, in sibling order, C = (c1; c2; : : : ; ck), then
 L(t) = (n, (L(c1), L(c2), : : :, L(ck)))
 where, L(ci) denotes the linear textual representation of the subtree rooted at
  child ci. There is a single space after each comma in the representation.

The output should contain the representation of one tree on each line and should be sorted by lexicographic order on the linear representations viewed as strings. 输出应在每一行上包含一棵树的表示形式,并应在按字符串表示的线性表示形式上按字典顺序排序。 The output should contain nothing else (such as spurious newline characters, prompts, or informative messages). 输出中不应包含任何其他内容(例如虚假的换行符,提示或信息性消息)。 Sample inputs and outputs for n = 1; n = 1时的样本输入和输出; 2; 2; appear below. 出现在下面。

enter code here

Input: 1
Output:
(1, ())
Input: 2
Output:
(1, ((2, ()))) 
(2, ((1, ())))

enter code here

Any help will be largely appreciated. 任何帮助将不胜感激。 I just need to be steered to a direction. 我只需要被引导到一个方向。 Right now, I'm completely stumped :( 现在,我完全被困住了:(

You can generate trees recursively. 您可以递归生成树。 Start with a root. 从根开始。 The root can have 0, 1, 2 ... (m - 1) children, where m is the number of vertices you have left to place. 根可以有0、1、2 ...(m-1)个孩子,其中m是您要放置的顶点数。 Start by placing (m - 1) vertices under the root, and then go down all the way to 0. You'll "place" these vertices recursively, so placing a vertex as child under the root means calling the same method again, but the maximum number of children will be a bit less this time. 首先在根下放置(m-1)个顶点,然后一直向下到0。您将递归“放置”这些顶点,因此将顶点作为子顶点放置在根下意味着再次调用相同的方法,但是这次最大的孩子人数会少一些。

You'll get two stopping criteria for the recursion: 您将获得两个递归的停止条件:

  • You've placed all N vertices. 您已放置所有N个顶点。 You need to output the current tree then with your yet-to-define L(t) function, then backtrack to try different trees. 您需要输出当前树,然后使用尚未定义的L(t)函数,然后回溯以尝试其他树。
  • The algorithm gave all leaf vertices a degree of 0 and you haven't placed n vertices yet. 该算法将所有叶顶点的阶数设置为0,而您尚未放置n个顶点。 Don't output this tree since it is invalid, but backtrack. 由于此树无效,因此不输出,而是回溯。

The algorithm is finished after it tries to give the root node 0 children. 尝试为根节点提供0个子节点后,该算法完成。

As for the output L(t) function, it seems to suffice to do a depth-first tree traversal. 至于输出L(t)函数,似乎足以进行深度优先的树遍历。 Recursive is easiest to program (as this seems to be a practical assignment of some kind, that's probably what they want you to do). 递归最容易编程(因为这似乎是某种实际的任务,这可能就是他们想要您做的)。 If you need speed, look for a non-recursive depth-first tree traversal algorithm on Wikipedia. 如果需要速度,请在Wikipedia上寻找一种非递归的深度优先树遍历算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM