gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:45:39 24 4
gpt4 key购买 nike

我需要实现一个使用字符串节点的二叉树。我可以插入名称,并且在大多数情况下,当我 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;
}

}
}

最佳答案

您的解决方案遗漏了忽略大小写的相等值。您应该在

中将“<”更改为“<=”
...
else if(pName.compareToIgnoreCase(pRoot.getWord()) < 0){
...
}

关于java - 使用字符串的二叉树,不插入所有字符串。有些不见了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com