二叉搜索树中的前序遍历
Preorder traversing in Binary Search Tree
我 运行 遇到了问题,无法弄清楚为什么我的方法在找到时 return 没有对象?它首先递归遍历树的左侧,然后再遍历右侧。 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;
}
因为您总是 returning null
第一次调用 lookUpCustomer
函数。此外,您没有从 lookUpCustomer
方法的递归调用中保存 returned 值。
解决这个return找到的节点。您可以按如下方式更改代码:
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;
}
我 运行 遇到了问题,无法弄清楚为什么我的方法在找到时 return 没有对象?它首先递归遍历树的左侧,然后再遍历右侧。 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;
}
因为您总是 returning null
第一次调用 lookUpCustomer
函数。此外,您没有从 lookUpCustomer
方法的递归调用中保存 returned 值。
解决这个return找到的节点。您可以按如下方式更改代码:
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;
}