使用字符串的二叉树,不插入所有字符串。有些人不见了

时间:2015-10-18 05:51:24

标签: java data-structures binary-tree binary-search-tree

我需要实现一个使用字符串节点的二叉树。我可以插入名称,在大多数情况下,当我displayAll时,我注意到有些名字丢失了。我不确定是什么导致了这个问题。任何帮助或建议将不胜感激。我需要在树中插入名称并在需要时显示所有名称。我仍然是Java和数据结构的新手,所以请原谅任何新手的错误。

B树

public class BTree {

private BTNode root;

    public void setRoot(BTNode pRoot){
        root = pRoot;
    }

    public BTNode getRoot(){
        return root;
    }

    public BTNode addOne(BTNode pRoot, String pName){
        if(pRoot == null){
            BTNode temp = new BTNode(pName);
            pRoot = temp;
            temp.setWord(pName);
        }else if(pName.compareToIgnoreCase(pRoot.getWord()) < 0){
            pRoot.setLeft(addOne(pRoot.getLeft(), pName));
        }else if(pName.compareToIgnoreCase(root.getWord()) > 0){
            pRoot.setRight(addOne(pRoot.getRight(), pName));
        }
        return pRoot;
    }

    public void displayAll(BTNode current){
        if(current != null){
            displayAll(current.getLeft());
            System.out.println(current.getWord());
            displayAll(current.getRight());
        }
    }

    public BTNode BTSearch(BTNode pRoot, String pName){
        BTNode found = null;
        if(pRoot == null){
            found = null;
        }else{
            if(pName.equalsIgnoreCase(pRoot.getWord())){
                found = pRoot;
            }
            else if(pName.compareToIgnoreCase(pRoot.getWord()) < 0){
                found = BTSearch(pRoot.getLeft(), pName);
            }else{
                found = BTSearch(pRoot.getRight(), pName);
            }
        }return found;
    }
}

BTNode

public class BTNode {

private String word;
private BTNode rPoint, lPoint;

public BTNode(String pWord){
    word = pWord;
    rPoint = null;
    lPoint = null;
}

public void setWord(String pWord){
    word = pWord;
}

public String getWord(){
    return word;
}

public void setRight(BTNode pRight){
    rPoint = pRight;
}

public BTNode getRight(){
    return rPoint;
}

public void setLeft(BTNode pLeft){
    lPoint = pLeft;
}

public BTNode getLeft(){
    return lPoint;
  }
}

BTreeUser

import java.util.Scanner;

public class BTreeUser {

public static void main(String []args){

    int select = 0;
    BTree tree = new BTree();

    do{
        dispMenu();
        select = getSelection();
        proChoice(select, tree);
    }while(select != 0);

}


    public static void dispMenu(){
        System.out.println("\n|*******************************|");
        System.out.println("|-------------------------------|");
        System.out.println("|************Welcome************|");
        System.out.println("|                               |");
        System.out.println("|  Press [1] to add an entry    |");
        System.out.println("|                               |");
        System.out.println("|  Press [2]|to search          |");
        System.out.println("|                               |");
        System.out.println("|  Press [3] to display all     |");
        System.out.println("|                               |");
        System.out.println("|  Press [0] to exit            |");
        System.out.println("|                               |");
        System.out.println("|Make selection and press[ENTER]|");
        System.out.println("|-------------------------------|");
        System.out.println("|*******************************|\n");
    }

    public static int getSelection(){
        Scanner input = new Scanner(System.in);
        int selection = input.nextInt();
        return selection;
    }

    public static String inputWord(int select){
        Scanner input = new Scanner(System.in);
        String lName = null;
        if(select == 1){
        System.out.println("Please input word now: ");
        lName = input.nextLine();
    }else if(select == 2){
        System.out.println("Please input word to search for now: ");
        lName = input.nextLine();

        }
        return lName;
    }

    public static  void proChoice(int select, BTree tree){
        String pName;
        switch(select){
        case 1: pName = inputWord(select);
                tree.setRoot(tree.addOne(tree.getRoot(), pName));
                break;
        case 2: pName = inputWord(select);
                tree.BTSearch(tree.getRoot(), pName);
                break;
        case 3: tree.displayAll(tree.getRoot());
                break;
        case 0: System.out.println("Thank you, come again...");
                break;
        }

    }
}

1 个答案:

答案 0 :(得分:2)

您的解决方案错过了忽略大小写的相同值。你应该改变&#39;&lt;&#39;到&#39;&lt; =&#39;在

...
else if(pName.compareToIgnoreCase(pRoot.getWord()) < 0){
...
}