質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

1回答

3560閲覧

Tree の構造をString で表現するにはどうしたらいいのでしょうか??

TomofumiKimura

総合スコア65

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2017/05/21 09:20

今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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Treeの要素が、自分が今何層目に位置しているのかということがわかればいい。方法としては
0. 階層情報を要素ごとに持たせる
0. 文字列化する際に、頂上を0階層として、子要素を取り出す際に(自分の階層)+1を引数として渡して文字列化するようなメソッドを作ればいい。

投稿2017/05/21 12:16

swordone

総合スコア20651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問