简体   繁体   中英

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. 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. I created a method which parses the line and extracts just the name from the line in a getName() method. 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.

I am then asked to read in a 20 entry query file full of 20 different names. I have to then compare each name from the query file and search for a Record object with the same name. 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. How do I get the binary search tree to implement the getName() method for searching and sorting?

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. Make the compareTo use attribute name instead of the attribute line for the comparission. Form a quick scan, I havent found the call to setName of the NodeInfo instance. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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