"PrintIt"というアプリケーションで "51850 Kianna Squares、Terre Haute | 552.531.3674 | Gislason Kenna "をBSTに入力して、BSTをトラバースし、名前順に電話帳をプリントアウトします。私は一般的なBSTのコードを与えられ、現在の形式で完全な行を格納する独自のRecordデータ型を作成しました。私は、行を解析し、getName()メソッドの行から名前だけを抽出するメソッドを作成しました。私はBSTにgetName()メソッドを使用させて、各ノードが名前を比較して挿入され、名前のアルファベット順に表示されるようにする方法を考え出すのに問題があります。自分のデータ型とその方法でデータが入力されたバイナリ検索ツリーを印刷する方法
次に、20種類の名前で構成された20エントリのクエリファイルを読み込むように求められます。次に、クエリファイルの各名前を比較し、同じ名前のレコードオブジェクトを検索する必要があります。レコードオブジェクトは、 "51850 Kianna Squares、Terre Haute | 552.531.3674 | Gislason Kenna"の元の形式でフルラインを保持していますので、各名前をレコードオブジェクトのgetName()メソッドと比較する必要がありますが、トラブル。検索とソートのためのgetName()メソッドを実装するためにバイナリ検索ツリーを取得するにはどうすればよいですか?
マイRecordクラス:
public class NodeInfo implements Comparable<NodeInfo>
{
String line;
String name;
public String getName(String lineTwo)
{
String split = "\\|";
String segments[] = lineTwo.split(split);
String name = segments[segments.length -1];
return name;
}
public void setName(String name)
{
this.name = name;
}
public NodeInfo(String record)
{
this.line = record;
}
public String getLine()
{
return line;
}
public int compareTo(NodeInfo other)
{
String x = this.line;
String y = other.line;
return x.compareTo(y);
}
public String toString()
{
return line;
}
}
私の二分探索木クラス:
public class BinarySearchTree<NodeInfo extends Comparable<? super NodeInfo>> extends BinaryTree<NodeInfo>
{
public void insert (NodeInfo d)
{
if (root == null)
root = new BinaryTreeNode<NodeInfo> (d, null, null);
else
insert (d, root);
}
public void insert (NodeInfo d, BinaryTreeNode<NodeInfo> node)
{
if (d.compareTo (node.data) <= 0)
{
if (node.left == null)
node.left = new BinaryTreeNode<NodeInfo> (d, null, null);
else
insert (d, node.left);
}
else
{
if (node.right == null)
node.right = new BinaryTreeNode<NodeInfo> (d, null, null);
else
insert (d, node.right);
}
}
public BinaryTreeNode<NodeInfo> find (NodeInfo d)
{
if (root == null)
return null;
else
return find (d, root);
}
public BinaryTreeNode<NodeInfo> find (NodeInfo d, BinaryTreeNode<NodeInfo> node)
{
if (d.compareTo (node.data) == 0)
return node;
else if (d.compareTo (node.data) < 0)
return (node.left == null) ? null : find (d, node.left);
else
return (node.right == null) ? null : find (d, node.right);
}
マイバイナリツリーノードクラス:
public class BinaryTreeNode<NodeInfo>
{
NodeInfo data;
BinaryTreeNode<NodeInfo> left;
BinaryTreeNode<NodeInfo> right;
public BinaryTreeNode (NodeInfo d, BinaryTreeNode<NodeInfo> l, BinaryTreeNode<NodeInfo> r)
{
data = d;
left = l;
right = r;
}
BinaryTreeNode<NodeInfo> getLeft()
{
return left;
}
BinaryTreeNode<NodeInfo> getRight()
{
return right;
}
}
マイバイナリツリークラス:
public class BinaryTree<NodeInfo> {
BinaryTreeNode<NodeInfo> root;
public BinaryTree() {
root = null;
}
public void visit(BinaryTreeNode<NodeInfo> node) {
System.out.println(node.data);
}
public void inOrder() {
inOrder(root);
}
public void inOrder(BinaryTreeNode<NodeInfo> node) {
if (node != null) {
inOrder(node.getLeft());
visit(node);
inOrder(node.getRight());
}
}
}
、最終的にはこれまでの私の主な方法:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
Scanner scanOne = new Scanner(System.in);
ArrayList<NodeInfo> holder = new ArrayList<>();
try {
Scanner scan = new Scanner(System.in);
File file = new File("testdata");
scan = new Scanner(file);
while(scan.hasNextLine())
{
String line = scan.nextLine();
NodeInfo info = new NodeInfo(line);
holder.add(info);
}
scan.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
BinarySearchTree<NodeInfo> directory = new BinarySearchTree<>();
for(int i = 0; i < holder.size(); i++)
{
NodeInfo one = holder.get(i);
String line = one.getLine();
directory.insert(one);
}
directory.inOrder();
}
}
重要な点は、あなたのBSTが重要でない*行*によって注文されるべきではなく、*名前*であることです。また、毎回解析するのではなく、最初から3つのフィールドを使用してオブジェクトを設定するとよいでしょう。 – RealSkeptic