【发布时间】:2017-05-11 23:15:34
【问题描述】:
我的一个关于树木的大学实验室要求我们编写一个名为toIndentedString() 的方法。说明如下:
另一种将树的内容表示为字符串的方法是使用缩进列表。在这里,我们首先表示根,然后每个 它的子子树,由两个空格缩进(这是应用的 递归)。对于示例树,我们会得到:
food meat chicken beef fish salmon cod tuna shark fruit vegetable cabbage
这是示例树的示意图:
这是我当前的代码:
package week10;
import java.util.*;
/**
* Skeleton of the recursive implementation of a general tree.
*
* @author Michael Albert
* @param <T> The type of values stored in the tree.
*/
public class Tree<T> {
private T rootValue;
private List<Tree<T>> children;
public Tree(T rootValue, List<Tree<T>> children) {
this.rootValue = rootValue;
this.children = children;
}
public Tree(T rootValue) {
this(rootValue, new ArrayList<Tree<T>>());
}
public int size() {
int count = 1;
for (Tree<T> child : children) {
count += child.size();
}
return count;
}
public int maxDegree() {
// Get the number of immediate children
int numChildren = this.children.size();
// Find the max of all children
int maxOfChildren = 0;
for (Tree<T> child : children) {
maxOfChildren = Math.max(maxOfChildren, child.maxDegree());
}
// return the greater of immediate child or max of children
return Math.max(numChildren, maxOfChildren);
}
public void add(Tree<T> child) {
children.add(child);
}
public Tree<T> find(T value) {
if (rootValue.equals(value)) {
return this;
}
for (Tree<T> child : children) {
Tree<T> match = child.find(value);
if (match != null) {
return match;
}
}
return null;
}
public List<T> postOrder() {
ArrayList<T> list = new ArrayList<T>();
for (Tree<T> child : children) {
if (!child.children.isEmpty()) {
child.postOrder();
} else {
//list.add(child);
System.out.println(child);
}
}
return list;
}
public String toString() {
if (children.isEmpty()) {
return rootValue.toString();
}
return rootValue.toString() + ' ' + children.toString();
}
public String toIndentedString() {
// implement this method
return "Not implemented yet!";
}
/** A helper method for testing (used by main). Searches tree for
* the given target and adds white space separated children to
* the tree matching target if there is one.
*
* @param target the root value to seach for.
* @param children a white space separated list of children to add
* to the tree whose value matches target.
*/
private static void addChildren(String target, String children) {
Tree<String> parent = tree.find(target);
if (parent != null) {
for (String child : children.split(" ")) {
parent.add(new Tree<>(child));
}
}
}
/** A tree instance used for testing. */
private static Tree<String> tree;
/**
* Entry point of the program (used for testing).
*
* @param args command line arguments are not used.
*/
public static void main(String[] args) {
System.out.println("Creating tree\n-------------");
tree = new Tree<>("food");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nAdding children\n----------------");
addChildren("food", "meat fruit vegetable");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nAdding deeper children\n----------------------");
addChildren("meat", "chicken beef fish");
addChildren("fish", "salmon cod tuna shark");
addChildren("vegetable", "cabbage");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nPostorder\n---------");
System.out.println(tree.postOrder());
System.out.println("\nIndented string\n---------------");
System.out.print(tree.toIndentedString());
}
}
如何实现此方法,以便递归缩进子树?
【问题讨论】:
-
你在打印带有缩进的树时提到过这个example。
-
您在此处尝试执行的操作与depth-first search 有关。首先尝试实现它,然后跟踪缩进的数量并按照规范打印树。
标签: java string recursion tree