刚才给ebay的印度mm打电话# PhotoGear - 摄影器材
s*n
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
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.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