Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二叉搜索树中的前序遍历_Java_Traversal - Fatal编程技术网

Java 二叉搜索树中的前序遍历

Java 二叉搜索树中的前序遍历,java,traversal,Java,Traversal,我遇到了问题,无法理解为什么我的方法在找到对象时不返回对象?它首先递归遍历树的左侧,然后递归遍历树的右侧。println语句在找到客户但return customer始终为null时打印。我做错了什么 public Customer lookUpCustomer(String lastName, String firstName) { Customer customer; Customer foundCustomer = null; if (left != n

我遇到了问题,无法理解为什么我的方法在找到对象时不返回对象?它首先递归遍历树的左侧,然后递归遍历树的右侧。println语句在找到客户但return customer始终为null时打印。我做错了什么

    public Customer lookUpCustomer(String lastName, String firstName) {

    Customer customer;
    Customer foundCustomer = null;

    if (left != null) {

        customer = (Customer) left.getItem();

        if(customer.getLastName().equals(lastName) && customer.getFirstName().equals(firstName)) {
            System.out.println("Found customer: " + customer.toString());
            return customer;
            //foundCustomer = customer;             
        }
        left.lookUpCustomer(lastName, firstName);
    }
    if (right != null) {

        customer = (Customer) right.getItem();

        if(customer.getLastName().equals(lastName) && 
       customer.getFirstName().equals(firstName)) {
            System.out.println("Found customer: " + customer.toString());
            return customer;
            //foundCustomer = customer;         
        }   
        right.lookUpCustomer(lastName, firstName);
    }

    return null;
}

因为您总是在第一次调用lookUpCustomer的函数时返回null。另外,您没有保存从lookUpCustomer方法的递归调用返回的值

要解决此问题,请返回找到的节点。您可以按以下方式更改代码:

public Customer lookUpCustomer(String lastName, String firstName) {

    Customer customer;
    Customer foundCustomer = null;

    if (left != null) {

        customer = (Customer) left.getItem();

        if(customer.getLastName().equals(lastName) && customer.getFirstName().equals(firstName)) {
            System.out.println("Found customer: " + customer.toString());
            return customer;
            //foundCustomer = customer;             
        }
        foundCustomer = left.lookUpCustomer(lastName, firstName);
    }
    if (foundCustomer==null && right != null) {

        customer = (Customer) right.getItem();

        if(customer.getLastName().equals(lastName) && 
       customer.getFirstName().equals(firstName)) {
            System.out.println("Found customer: " + customer.toString());
            return customer;
            //foundCustomer = customer;         
        }   
        foundCustomer  = right.lookUpCustomer(lastName, firstName);
    }

    return foundCustomer;
}

当递归调用该方法时,不会对任何内容使用返回值。您不认为应该吗?另外,您不认为名称比较应该与当前节点进行比较吗?消除重复代码,并确保根节点中的名称也能进行比较。@Andreas我该怎么做?customer=left.lookUpCustomerlastName,类似的firstName?是,如果不为null,则立即返回。这就是重点,对吗?归还找到的顾客?@Andreas是的,这就是重点。