今treeの構造をString で表現するメソッドを考えているのですが、どうしたらいいのかわかりません。 下のクラスの中のtoIndentedString()なのですが
どのようにString で表現しなくてはいけないのかは決まっていて、
food
meat vegetable
|
|
|
pork, chiken, beef
例えば、food が一番上で そのsubtreeがmeat とvegetable で、meat のsubtree がpork, chiken, beefであるとしたら、String で
food
meat
pork
chicken
beef
vegetable
このように subtreeはroot の次の段落で2空白空けてから表示されないといけません。recursive が使えると思うのですが、全く考えつきません。どうしたらいいのでしょうか?
java
1package week10; 2 3import java.util.*; 4 5/** 6 * Skeleton of the recursive implementation of a general tree. 7 * 8 * @author Michael Albert 9 * @param <T> The type of values stored in the tree. 10 */ 11public class Tree<T> { 12 13 private T rootValue; 14 private List<Tree<T>> children; 15 16 public Tree(T rootValue, List<Tree<T>> children) { 17 this.rootValue = rootValue; 18 this.children = children; 19 } 20 21 public Tree(T rootValue) { 22 this(rootValue, new ArrayList<Tree<T>>()); 23 } 24 25 /** 26 Returns the number of nodes in the tree. 27 @rerturn int the number of nodes in the tree 28 */ 29 public int size() { 30 int count =1; 31 for (Tree<T> child: children){ 32 count += child.size(); 33 } 34 return count; 35 36 37 }// end size 38 39 public int maxDegree() { 40 int max = children.size(); 41 for (Tree<T> child : children){ 42 int n1 = child.maxDegree(); 43 if (n1 > max){ 44 max = n1; 45 46 } 47 } 48 return max; 49 // implement this method 50 51 52 } 53 /** Add a child as a new subtree below the root at the 54 end of the list of children. 55 @param child*/ 56 public void add(Tree<T> child) { 57 children.add(child); 58 // implement this method 59 } 60 61 public Tree<T> find(T value) { 62 if (rootValue.equals(value)) { 63 return this; 64 } 65 for (Tree<T> child : children) { 66 Tree<T> match = child.find(value); 67 if (match != null) { 68 return match; 69 } 70 } 71 return null; 72 } 73 74 public List<T> postOrder() { 75 List<T> list = new ArrayList<>(); 76 for (Tree<T> child: children){ 77 list.addAll(child.postOrder()); 78 } 79 list.add(rootValue); 80 return list; 81 // implement this method 82 } 83 84 public String toString() { 85 if (children.isEmpty()) { 86 return rootValue.toString(); 87 } 88 return rootValue.toString() + ' ' + children.toString(); 89 } 90 91 public String toIndentedString() { 92 93 // implement this method 94 return "Not implemented yet!"; 95 } 96 97 /** A helper method for testing (used by main). Searches tree for 98 * the given target and adds white space separated children to 99 * the tree matching target if there is one. 100 * 101 * @param target the root value to seach for. 102 * @param children a white space separated list of children to add 103 * to the tree whose value matches target. 104 */ 105 private static void addChildren(String target, String children) { 106 Tree<String> parent = tree.find(target); 107 if (parent != null) { 108 for (String child : children.split(" ")) { 109 parent.add(new Tree<>(child)); 110 } 111 } 112 } 113 114 /** A tree instance used for testing. */ 115 private static Tree<String> tree; 116 117 /** 118 * Entry point of the program (used for testing). 119 * 120 * @param args command line arguments are not used. 121 */ 122 public static void main(String[] args) { 123 System.out.println("Creating tree\n-------------"); 124 tree = new Tree<>("food"); 125 System.out.print(tree + "\nsize: " + tree.size()); 126 System.out.println(", max degree: " + tree.maxDegree()); 127 System.out.println("\nAdding children\n----------------"); 128 addChildren("food", "meat fruit vegetable"); 129 System.out.print(tree + "\nsize: " + tree.size()); 130 System.out.println(", max degree: " + tree.maxDegree()); 131 System.out.println("\nAdding deeper children\n----------------------"); 132 addChildren("meat", "chicken beef fish"); 133 addChildren("fish", "salmon cod tuna shark"); 134 addChildren("vegetable", "cabbage"); 135 System.out.print(tree + "\nsize: " + tree.size()); 136 System.out.println(", max degree: " + tree.maxDegree()); 137 System.out.println("\nPostorder\n---------"); 138 System.out.println(tree.postOrder()); 139 System.out.println("\nIndented string\n---------------"); 140 System.out.print(tree.toIndentedString()); 141 } 142 143} 144
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。