简体   繁体   English

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

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

I am asked to write an application "PrintIt" to load a data file of format "51850 Kianna Squares, Terre Haute|552.531.3674|Gislason Kenna" into a BST and then traverse the BST and print out the telephone listing in order of name. 我被要求编写一个应用程序“ PrintIt”,将格式为“ 51850 Kianna Squares,Terre Haute | 552.531.3674 | Gislason Kenna”的数据文件加载到BST中,然后遍历BST并按名称顺序打印出电话清单。 I was given the code for a generic BST and created my own Record data type which stores the full line in its current format. 我得到了通用BST的代码,并创建了自己的Record数据类型,该数据类型以其当前格式存储了整行。 I created a method which parses the line and extracts just the name from the line in a getName() method. 我创建了一个方法来解析该行,并在getName()方法中仅从该行中提取名称。 I am having trouble figuring out how to get my BST to use the getName() method so that each node is inserted by comparing the names and printed out in alphabetical order of the name. 我在弄清楚如何使我的BST使用getName()方法时遇到了麻烦,以便通过比较名称来插入每个节点并按名称的字母顺序打印出来。

I am then asked to read in a 20 entry query file full of 20 different names. 然后,我被要求读入20个条目的查询文件,其中包含20个不同的名称。 I have to then compare each name from the query file and search for a Record object with the same name. 然后,我必须比较查询文件中的每个名称,并搜索具有相同名称的Record对象。 The record object holds the full line in the original format of "51850 Kianna Squares, Terre Haute|552.531.3674|Gislason Kenna" so I know I must compare each name to the getName() method of the record object, but again am having trouble. 记录对象以“ 51850 Kianna Squares,Terre Haute | 552.531.3674 | Gislason Kenna”的原始格式保留了整行,因此我知道我必须将每个名称与记录对象的getName()方法进行比较,但又有麻烦。 How do I get the binary search tree to implement the getName() method for searching and sorting? 如何获取二进制搜索树以实现用于搜索和排序的getName()方法?

My Record class: 我的记录班:

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;
}

}

My binary search tree class: 我的二叉搜索树类:

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);
}

My binary tree node class: 我的二叉树节点类:

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;
}
}

My binary tree class: 我的二叉树类:

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());
    }
}
}

and finally my main method so far: 最后是我到目前为止的主要方法:

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();

}
}

Your BST is calling the NodeInfo's compareTo method for searching and sorting which in turn uses the line attribute to compare two entries. 您的BST正在调用NodeInfo的compareTo方法进行搜索和排序,该方法进而使用line属性比较两个条目。 Make the compareTo use attribute name instead of the attribute line for the comparission. 使compareTo使用属性名称而不是属性行进行比较。 Form a quick scan, I havent found the call to setName of the NodeInfo instance. 快速扫描后,我还没有找到对NodeInfo实例的setName的调用。 Thus you either add that to the scanning part or rely on a call to getName in the compareTo method instead of using the name attribue. 因此,您可以将其添加到扫描部分,或者依靠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