繁体   English   中英

如何打印用我自己的数据类型填充的二进制搜索树及其方法

[英]How to print a binary search tree populated with my own data type and its method

我被要求编写一个应用程序“ PrintIt”,将格式为“ 51850 Kianna Squares,Terre Haute | 552.531.3674 | Gislason Kenna”的数据文件加载到BST中,然后遍历BST并按名称顺序打印出电话清单。 我得到了通用BST的代码,并创建了自己的Record数据类型,该数据类型以其当前格式存储了整行。 我创建了一个方法来解析该行,并在getName()方法中仅从该行中提取名称。 我在弄清楚如何使我的BST使用getName()方法时遇到了麻烦,以便通过比较名称来插入每个节点并按名称的字母顺序打印出来。

然后,我被要求读入20个条目的查询文件,其中包含20个不同的名称。 然后,我必须比较查询文件中的每个名称,并搜索具有相同名称的Record对象。 记录对象以“ 51850 Kianna Squares,Terre Haute | 552.531.3674 | Gislason Kenna”的原始格式保留了整行,因此我知道我必须将每个名称与记录对象的getName()方法进行比较,但又有麻烦。 如何获取二进制搜索树以实现用于搜索和排序的getName()方法?

我的记录班:

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正在调用NodeInfo的compareTo方法进行搜索和排序,该方法进而使用line属性比较两个条目。 使compareTo使用属性名称而不是属性行进行比较。 快速扫描后,我还没有找到对NodeInfo实例的setName的调用。 因此,您可以将其添加到扫描部分,或者依靠compareTo方法中对getName的调用,而不是使用名称属性。

public class NodeInfo implements Comparable<NodeInfo>
{
...

public int compareTo(NodeInfo other)
{
  String x = getName(this.line);
  String y = getName(other.line);
  return x.compareTo(y);
}
...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM