ログインしてさらにmixiを楽しもう

コメントを投稿して情報交換!
更新通知を受け取って、最新情報をゲット!

Javaの課題丸投げコミュの2分探索木を使ったプログラム

  • mixiチェック
  • このエントリーをはてなブックマークに追加
C:\Q1>java PrintSideways MBADEROU
Sideways printing the tree by abcd001:
     U 
  R
     O
M
        E
     D
  B
     A
Inorder traversal: ABDEMORU
Please enter the remove sequence: RMMAGED
The tree after the removing (sideways):
  U
O
  B
Inorder traversal: BOU

と表示されるプログラムの作成と

C:\Q2>java Reconstruct
Binary tree re-constructed by abcd001:
Please enter the inorder sequence: ADHGKLMRUVTW
Please enter the postorder sequence: AHDLKGUVRWTM
Binary tree sideways:
       W
   T
          V
             U
       R
M
          L
       K
   G
          H
       D
          A

Preorder traversal: MGDAHKLTRVUW
Inorder traversal: ADHGKLMRUVTW
Postorder traversal: AHDLKGUVRWTM

という表示されるプログラムを作らなければならないのですが、途方にくれております。
申し訳ないのですが、お時間のある方がいれば助けてください。

コメント(7)

ありゃ、途中までやったんだけど、これってただの2分探索木じゃなくて平衡2分探索木を作る問題?
削除結果がずれたから、間違ってるのかと思ったらそういうことか。

とりあえず時間切れなので、途中までだけど貼っておく。
長すぎると怒られたので途中で切る。

import java.util.ArrayList;
import java.util.List;

public class BinaryTreeNode {
public BinaryTreeNode(char aValue, BinaryTreeNode aLeft, BinaryTreeNode aRight) {
mLeft = aLeft;
mRight = aRight;
mValue = aValue;
}

private BinaryTreeNode mLeft;
private BinaryTreeNode mRight;
private char mValue;

public boolean isLeaf() {
return !(this.hasLeft() || this.hasRight());
}

public boolean hasLeft() {
return mLeft != null;
}

public boolean hasRight() {
return mRight != null;
}

public BinaryTreeNode getLeft() {
return mLeft;
}

public BinaryTreeNode getRight() {
return mRight;
}

public boolean setLeft(BinaryTreeNode aNode) {
if (aNode == null || aNode.getValue() <= this.getValue()) {
mLeft = aNode;
return true;
}

return false;
}

public boolean setRight(BinaryTreeNode aNode) {
if (aNode == null || aNode.getValue() > this.getValue()) {
mRight = aNode;
return true;
}

return false;
}

public char getValue() {
return mValue;
}

// ↓とつながる
// ↑とつながる

public void addNode(char aValue) {
this.addNode(new BinaryTreeNode(aValue, null, null));
}

public void addNode(BinaryTreeNode aNode) {
char tValue = aNode.getValue();
if (tValue <= mValue) {
if (this.hasLeft())
this.getLeft().addNode(aNode);
else
mLeft = aNode;
} else {
if (this.hasRight())
this.getRight().addNode(aNode);
else
mRight = aNode;
}
}

public BinaryTreeNode removeNode(char aValue) {
if (aValue < this.getValue()) {
if (this.hasLeft())
this.setLeft(this.getLeft().removeNode(aValue));

return this;
}

if (aValue > this.getValue()) {
if (this.hasRight())
this.setRight(this.getRight().removeNode(aValue));

return this;
}

return remove();
}

private BinaryTreeNode remove() {
if (this.isLeaf())
return null;

if (this.hasLeft() && !this.hasRight())
return this.getLeft();

if (!this.hasLeft() && this.hasRight())
return this.getRight();

BinaryTreeNode tLeftMax = this.getLeft().getMax();
// このメソッドで削除されるノードは、二分木の性質上tLeftMaxと同一のインスタンスとなる
BinaryTreeNode tLeftTree = this.getLeft().removeNode(tLeftMax.getValue());
tLeftMax.setLeft(tLeftTree);
tLeftMax.setRight(this.getRight());

return tLeftMax;
}

public BinaryTreeNode getMax() {
if (this.hasRight())
return this.getRight().getMax();

return this;
}

public BinaryTreeNode getMin() {
if (this.hasLeft())
return this.getLeft().getMax();

return this;
}

public List<String> toStringList() {
List<String> tResult = new ArrayList<String>();

if (this.hasRight()) {
tResult.addAll(addBlank(this.getRight().toStringList()));
}

tResult.add(String.valueOf(mValue));

if (this.hasLeft()) {
tResult.addAll(addBlank(this.getLeft().toStringList()));
}

return tResult;
}

private List<String> addBlank(List<String> aTarget) {
int tCount = aTarget.size();
for (int i = 0; i < tCount; i++)
aTarget.set(i, " " + aTarget.get(i));

return aTarget;
}

@Override
public String toString() {
return listToString(this.toStringList());
}

private static String listToString(List<String> aList) {
StringBuffer tBuilder = new StringBuffer();
String tNewLine = System.getProperty("line.separator");
for (String tString : aList) {
tBuilder.append(tString).append(tNewLine);
}

return tBuilder.toString();
}
}
public interface TreeTraverser {
public void traverse(BinaryTreeNode aNode);
}
public class InorderTraverser implements TreeTraverser {

@Override
public void traverse(BinaryTreeNode aNode) {
if(aNode.hasLeft())
this.traverse(aNode.getLeft());

System.out.print(aNode.getValue());

if(aNode.hasRight())
this.traverse(aNode.getRight());
}

}

public class PrintSideways {

public static void main(String[] args) {
if(args.length == 0 || args[0].length() == 0)
throw new IllegalArgumentException("適切な引数を渡してください");

BinaryTreeNode tTree = createTree(args[0]);
System.out.println(tTree.toString());
System.out.print("Inorder traversal: ");
new InorderTraverser().traverse(tTree);

System.out.println();

tTree = tTree.removeNode('R');
tTree = tTree.removeNode('M');
tTree = tTree.removeNode('M');
tTree = tTree.removeNode('A');
tTree = tTree.removeNode('G');
tTree = tTree.removeNode('E');
tTree = tTree.removeNode('D');

System.out.println(tTree.toString());
System.out.print("Inorder traversal: ");
new InorderTraverser().traverse(tTree);
}

private static BinaryTreeNode createTree(String aValues){
int tLength = aValues.length();
if(tLength == 0)
return null;

BinaryTreeNode tRoot = new BinaryTreeNode(aValues.charAt(0), null, null);

for(int i = 1; i<tLength; i++){
tRoot.addNode(aValues.charAt(i));
}

return tRoot;
}
}
ここまで。
時間なかったのでコメントが全く無くて申し訳ない。
removeNodeメソッドとか何も考えずに書いたので、何かミスがあるかも。

あとは、誰かに任せた。 暇ができるまで解決なしに残ってたら続き書く(かも)。

ログインすると、みんなのコメントがもっと見れるよ

mixiユーザー
ログインしてコメントしよう!

Javaの課題丸投げ 更新情報

Javaの課題丸投げのメンバーはこんなコミュニティにも参加しています

星印の数は、共通して参加しているメンバーが多いほど増えます。