由文本文件(Java)填充的二进制搜索树的搜索关键字

时间:2018-11-20 02:19:03

标签: java binary-search-tree search-keywords

嗨,我刚开始编程,有一个我希望得到帮助的项目。我应该从文本文件中读取一些名称和电话号码,然后使用它们填充二进制搜索树。文本文件中的每一行(共有5行)都有一个人的名字(名字和姓氏)以及他们的电话号码。然后,我应该使用搜索键查找每个人的名字。以下是说明:

  

编写一个程序,为您提供一种存储和检索电话号码的方法。设计一个提供以下操作的控制台程序:

     

添加:将一个人的姓名和电话号码添加到电话簿中。

     

删除:从电话簿中删除仅给定姓名的姓名和电话号码。

     

查找:仅给出某人的姓名,即可找到该人的电话号码。

     

更改:更改该人的电话号码,并提供该人的姓名和新电话号码。

     

退出:首先将电话簿保存到文本文件中,然后退出应用程序。

     

您可以按照以下步骤进行操作:

     

设计并实现Person类,该类代表一个人的姓名和电话号码。您将在电话簿中存储此类的实例。设计并实现代表电话簿的PhoneBook类。该类应包含一个二进制搜索树作为数据字段。这棵树包含书中的人。添加使用文本文件保存和还原树的方法。设计和实现Menu类,该类提供程序的用户界面。

     

程序应在启动时从文本文件读取数据,并在用户退出程序时将数据保存到文本文件中。

我遇到麻烦的地方是BST。当我尝试填充树时,出现以下错误消息“不兼容的类型:无法将字符串转换为KeyedItem”如何更改KeyedItem类以使其起作用?另外,如果我可以克服KeyedItem问题,那么我在Menu类中编写的代码是否足以填充BST?我知道这里有很多信息;预先感谢您能给我的任何帮助。

这是我的课程:

菜单类:

public static void main(String[]args) throws IOException{

    String read = "";

PhoneBook btree = new PhoneBook(); //creates bst object from phonebook class

    BufferedReader input = new BufferedReader(new FileReader("names.txt"));

        for (int i=0; i<5; i++){



            read = input.readLine(); //reads each lin of text file and stores it in var read




        }

        btree.insert(read); //this is where I get the ERROR message           

}

}

KeyedItem搜索类别:

public abstract class KeyedItem<KT extends Comparable<? super KT>> {

private KT searchKey;

public KeyedItem(KT key){  //constructor
    searchKey = key;
}

电话簿类别:

public class PhoneBook<T extends KeyedItem<KT>, KT extends Comparable<?     super KT>> extends BinaryTreeBasis<T> {

public PhoneBook(){

}
public PhoneBook(T rootItem){
    super(rootItem);
}

public void setRootItem(T newItem)   //I believe this class is not always used
        throws UnsupportedOperationException {
    throw new UnsupportedOperationException();

}

public void insert(T newItem){
    root = insertItem(root, newItem);

}

public T retrieve(KT searchKey){
    return retrieveItem(root, searchKey);
} 

public void delete(KT searchKey) throws TreeException{
    root = deleteItem(root, searchKey);
}

public void delete(T item) throws TreeException{
    root = deleteItem(root, item.getKey());
}

protected TreeNode<T> insertItem(TreeNode<T> tNode, T newItem){
    TreeNode<T> newSubtree;
    if(tNode == null){
        tNode = new TreeNode<T>(newItem, null, null);
        return tNode;
    }

    T nodeItem = tNode.item;

    if(newItem.getKey().compareTo(nodeItem.getKey()) < 0) {
        newSubtree = insertItem(tNode.leftChild, newItem);
        tNode.leftChild = newSubtree;
        return tNode;
    }
    else {
       newSubtree = insertItem(tNode.rightChild, newItem);
       tNode.rightChild = newSubtree;
       return tNode;
    }
}
    protected T retrieveItem (TreeNode<T> tNode, KT searchKey) {

        T treeItem;
        if(tNode == null){
            treeItem = null;
        }
        else {
            T nodeItem = tNode.item;
            if (searchKey.compareTo(nodeItem.getKey()) == 0) {
            treeItem = tNode.item;
        }
        else if (searchKey.compareTo(nodeItem.getKey()) < 0){
            treeItem = retrieveItem(tNode.leftChild, searchKey);
        }   

        else {
            treeItem = retrieveItem(tNode.rightChild, searchKey);
        }  
    }
    return treeItem;    
}

protected TreeNode<T> deleteItem(TreeNode<T> tNode, KT searchKey) {
    TreeNode<T> newSubtree;
    if (tNode == null){
        throw new TreeException("TreeException: Item not found");
    }
    else{
        T nodeItem = tNode.item;
        if (searchKey.compareTo(nodeItem.getKey()) == 0){
            tNode = deleteNode(tNode);
        }
        else if (searchKey.compareTo(nodeItem.getKey()) < 0){
            newSubtree = deleteItem(tNode.leftChild, searchKey);
            tNode.leftChild = newSubtree;
        }
        else {
            newSubtree = deleteItem(tNode.rightChild, searchKey);
            tNode.rightChild = newSubtree;
        }    

        }
    return tNode;
}    

protected TreeNode<T> deleteNode(TreeNode<T> tNode){
    T replacementItem;

    if((tNode.leftChild == null) &&
            (tNode.rightChild == null)){
        return null;
    }

    else if (tNode.leftChild == null){
        return tNode.rightChild;
    }

    else if (tNode.rightChild == null){
        return tNode.leftChild;
    }

    else { 

        replacementItem = findLeftmost(tNode.rightChild);
        tNode.item = replacementItem;
        tNode.rightChild = deleteLeftmost(tNode.rightChild);
        return tNode;
    }
}

protected T findLeftmost(TreeNode<T> tNode){
    if (tNode.leftChild == null) {
        return tNode.item;
    }
    else {
        return findLeftmost(tNode.leftChild);
    }        
}

protected TreeNode<T> deleteLeftmost(TreeNode<T> tNode){
    if (tNode.leftChild == null){
        return tNode.rightChild;
    }
    else{
        tNode.leftChild = deleteLeftmost(tNode.leftChild);
        return tNode;    
    }
}
}
    public KT getKey(){
    return searchKey;
}
}

人员班:

public class Person extends KeyedItem<String>{

private FullName name;
private String phoneNumber;


public Person(String id, FullName name, String phone){ //constructor

    super(id);
    this.name = name;
    phoneNumber = phone;


}

public String toString(){
    return getKey() + " # " + name;
}
}

TreeNode类:

public class TreeNode<T> {

T item;
TreeNode<T> leftChild;
TreeNode<T> rightChild;

public TreeNode(T newItem){ //constructor

    item = newItem;
    leftChild = null;
    rightChild = null;
}

public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right){

    item = newItem;
    leftChild = left;
    rightChild = right;
}

}

BinaryTreeBasis类:

public abstract class BinaryTreeBasis<T> {

protected TreeNode<T> root;

public BinaryTreeBasis(){  //default constructor

    root = null;
}

public BinaryTreeBasis(T rootItem){

    root = new TreeNode<T>(rootItem, null, null);
}

public boolean isEmpty(){

    return root == null;
}

public void makeEmpty(){

    root = null;
}

public T getRootItem() throws TreeException {

    if (root == null){
        throw new TreeException("Tree Exception: Empty Tree");
    }
    else{
        return root.item;
    }
}

public abstract void setRootItem(T newItem);

}

全名类别:

public class FullName implements java.lang.Comparable<Object>{ //change from object?
private String firstName;
private String lastName;

public FullName(String first, String last){
    firstName = first;
    lastName = last;
}
    public int compareTo(Object rhs){

    FullName other = (FullName)rhs;

    if(lastName.compareTo(((FullName)other).lastName)==0){
        return firstName.compareTo(((FullName)other).firstName);
    }
    else {
        return lastName.compareTo(((FullName)other).lastName);
    }
}

}

Tree Iterator类:

public class TreeIterator<T> implements java.util.Iterator<T> {
private BinaryTreeBasis<T> binTree;
private TreeNode<T> currentNode;
private LinkedList <TreeNode<T>> queue;

public TreeIterator(BinaryTreeBasis<T> bTree){
    binTree = bTree;
    currentNode = null;
    queue = new LinkedList <TreeNode<T>>();
}

public boolean hasNext(){
    return !queue.isEmpty();               
}

public T next()
        throws java.util.NoSuchElementException{
    currentNode = queue.remove();
    return currentNode.item;
}

public void remove()
        throws UnsupportedOperationException{
    throw new UnsupportedOperationException();
}

public void setPreorder(){
    queue.clear();
    preorder(binTree.root);
}

public void setInorder(){
    queue.clear();
    inorder(binTree.root);
}

public void setPostorder(){
    queue.clear();
    postorder(binTree.root);
}

private void preorder(TreeNode<T> treeNode){
    if(treeNode != null){
        queue.add(treeNode);
        preorder(treeNode.leftChild);
        preorder(treeNode.rightChild);
    }
}

private void inorder(TreeNode<T> treeNode){
    if(treeNode != null){
        inorder(treeNode.leftChild);
        queue.add(treeNode);
        inorder(treeNode.rightChild);
    }
}

private void postorder(TreeNode<T> treeNode){
    if(treeNode != null){
        postorder(treeNode.leftChild);
        postorder(treeNode.rightChild);
        queue.add(treeNode);
    }
}

}

0 个答案:

没有答案