二叉树Java中的搜索函数

二叉树Java中的搜索函数,java,search,tree,binary-tree,binary-search-tree,Java,Search,Tree,Binary Tree,Binary Search Tree,我正在为一个我一直在做的项目创建一个二叉树,在这个项目中,我按名字将人插入一个二叉树(树遍历每个字符,以确定插入时哪个更大)。是否有一种方法可以让我的树在树中搜索,以找到与程序名称匹配的人。这是到目前为止我的代码 lass Node { private String person; private Node left; private Node right; public Node(String person) { this.person = person; left = nu

我正在为一个我一直在做的项目创建一个二叉树,在这个项目中,我按名字将人插入一个二叉树(树遍历每个字符,以确定插入时哪个更大)。是否有一种方法可以让我的树在树中搜索,以找到与程序名称匹配的人。这是到目前为止我的代码

lass Node {
private String person;
private Node left;
private Node right;

public Node(String person) {
    this.person = person;
    left = null;
    right = null;
}

//setters
protected void setLeft(Node left) {
    this.left = left;
}

protected void setRight(Node right) {
    this.right = right;
}

//getters
protected String getPerson() {
    return person;
}

protected Node getLeft() {
    return left;
}

protected Node getRight() {
    return right;
}
}

public class BinaryTree {
private static Node root; 

public BinaryTree() {
    root = null;
}

public void insert(String person) {
    root = insert(person, root);
}

//Check if node is leaf
public static boolean isLeaf() {
    if(root.getLeft() == null && root.getRight() == null)
        return false;
    else
        return true;
}

// Search tree for entered value

public static void searchTree(String search, Node tNode) {
// Not sure what to put into the part to make the method search through people in the tree
    }

    private Node insert(String person, Node tree) {
    if(tree == null)
        tree = new Node(person);
    else {
        int count = 1;
        int x = 0;
        while(person.toLowerCase().charAt(x) == tree.getPerson().toLowerCase().charAt(x) && count != tree.getPerson().length()) {
            count = count + 1;
            x = x + 1;
        }
        if(person.toLowerCase().charAt(x) != tree.getPerson().toLowerCase().charAt(x)) {
            if(person.toLowerCase().charAt(x) < tree.getPerson().toLowerCase().charAt(x))
                tree.setLeft(insert(person, tree.getLeft()));
            else
                tree.setRight(insert(person, tree.getRight()));
        } else {
            tree.setRight(insert(person, tree.getRight()));
        }
    }
    return tree;

}
lass节点{
私人串人;
私有节点左;
私有节点权;
公共节点(字符串人){
这个人=人;
左=空;
右=空;
}
//二传手
受保护的void setLeft(节点左侧){
this.left=左;
}
受保护的void setRight(节点右侧){
这个。右=右;
}
//吸气剂
受保护的字符串getPerson(){
返回人;
}
受保护的节点getLeft(){
左转;
}
受保护的节点getRight(){
返还权;
}
}
公共类二叉树{
私有静态节点根;
公共二叉树(){
root=null;
}
公共空白插入(字符串人){
根=插入(人,根);
}
//检查节点是否为叶
公共静态布尔isLeaf(){
if(root.getLeft()==null&&root.getRight()==null)
返回false;
其他的
返回true;
}
//搜索树以查找输入的值
公共静态void searchTree(字符串搜索,节点tNode){
//不确定要在部件中放入什么以使方法在树中的人员中搜索
}
私有节点插入(字符串人、节点树){
if(tree==null)
树=新节点(人);
否则{
整数计数=1;
int x=0;
而(person.toLowerCase().charAt(x)=tree.getPerson().toLowerCase().charAt(x)和&count!=tree.getPerson().length()){
计数=计数+1;
x=x+1;
}
if(person.toLowerCase().charAt(x)!=tree.getPerson().toLowerCase().charAt(x)){
if(person.toLowerCase().charAt(x)

您能建议我如何创建一个方法来搜索树吗?我建议您执行以下步骤。这些步骤将为您提供一个开始

  • 从root开始,使用compareTignoreCase()将要搜索的名称与root进行比较
  • 根据结果向左或向右移动
  • 继续,直到任一节点变为null或找到匹配项

  • 如果您试图实现二元搜索树,则需要更改setter中的代码,以确定是将人员添加到左侧节点还是右侧节点(通过按字典顺序比较字符串)每次调用这些方法时。如果树没有排序,则必须搜索每个节点。排序后,您将能够在logn时间内搜索。

    我同意@maus,每次都需要调用此方法。其中的insert方法在添加它们时会在列表中按顺序对它们进行排序,那么搜索也会起作用吗?我认为这也是easier使用compareTo或compareToIgnoreCase。不,搜索不起作用,但现在我正在使用compareTo函数,它运行得很好。让我知道它是如何运行的,我认为你走对了方向。