s****n 发帖数: 70 | 1 给了这么长一道题,光读就读了半天
Write a Java function, printTree(), which prints a given tree in depth first
format to stdout. Details:
The argument of printTree is a stream of pairs of string values.
Each string found anywhere in the input represents a unique node.
Each item in the stream is a pair indicating a parent/child relationship
in the tree. The first element in the pair is the parent. The second
element in the pair is the child.
Each parent can have many children.
The input list may contain relationship pairs in any order, although:
The order in which the pairs appear in the input list determines the nodes’
order with respect to its siblings.
public static class Edge {
String parent;
String child;
public static Edge of(String parent, String child) { ... }
}
Example input:
List input = newArrayList();
input.add(Edge.of(“cat”, “lion”));
input.add(Edge.of(“mammal”, “cat”));
input.add(Edge.of(“animal”, “fish”));
// Note that the list of nodes is disjoint at this point.
input.add(Edge.of(“animal”, “mammal”));
input.add(Edge.of(“animal”, “bird”));
input.add(Edge.of(“lifeform”, “animal”));
TreePrinter.printTree(input);
Expected output:
lifeform
animal
fish
mammal
cat
lion
bird | l*********8 发帖数: 4642 | 2 谢谢分享
先建树,然后输出。 其实是两道小题
first
【在 s****n 的大作中提到】 : 给了这么长一道题,光读就读了半天 : Write a Java function, printTree(), which prints a given tree in depth first : format to stdout. Details: : The argument of printTree is a stream of pairs of string values. : Each string found anywhere in the input represents a unique node. : Each item in the stream is a pair indicating a parent/child relationship : in the tree. The first element in the pair is the parent. The second : element in the pair is the child. : Each parent can have many children. : The input list may contain relationship pairs in any order, although:
| p*****2 发帖数: 21240 | |
|