我构建了一个二元搜索树,它会根据人们的名字对人进行排序,代码是:
类Person的代码:
package Tree;
public class Person {
private int age;
private String name;
private String gender;
public Person( String name, String gender,int age) {
this.age = age;
this.name = name;
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + ", gender="
+ gender + "]";
}
}
搜索树代码是:
package Tree;
public class BinarySearchPerson {
private boolean empty;
private Person person;
private BinarySearchPerson left;
private BinarySearchPerson right;
public BinarySearchPerson(Person person, BinarySearchPerson left,
BinarySearchPerson right) {
this.empty = false;
this.person = person;
this.left = left;
this.right = right;
}
public BinarySearchPerson() {
this.empty = true;
}
public boolean isEmpty() {
return empty;
}
public Person getPerson() {
if (isEmpty()) {
throw new IllegalStateException(
"Trying to access root of an empty tree");
}
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public BinarySearchPerson getLeft() {
if (isEmpty()) {
throw new IllegalStateException(
"Trying to access subtree of an empty tree");
}
return left;
}
public void setLeft(BinarySearchPerson left) {
this.left = left;
}
/**
* gets the right subtree of this node
*/
public BinarySearchPerson getRight() {
if (isEmpty()) {
throw new IllegalStateException(
"Trying to access subtree of an empty tree");
}
return right;
}
public void setRight(BinarySearchPerson right) {
this.right = right;
}
public static BinarySearchPerson insert(Person person, BinarySearchPerson bt){
int n = person.getName().compareTo(bt.getPerson().getName());
if (n<0){
if(bt.getLeft().isEmpty() == true){
BinarySearchPerson newLeft = new BinarySearchPerson(person,new BinarySearchPerson(),new BinarySearchPerson());
bt.setLeft(newLeft);
return bt;
}
else{
insert(person, bt.getLeft());
return bt;
}
}
if (n>0){
if(bt.getRight().isEmpty() == true){
BinarySearchPerson newRight = new BinarySearchPerson(person,new BinarySearchPerson(),new BinarySearchPerson());
bt.setRight(newRight);
return bt;
}
else{
insert(person, bt.getRight());
return bt;
}
}
else return bt;
}
public static int lookupAge(String name) {
Person person =
if (name.compareTo(person.getName())==0){
return person.getAge();
}
else return -1;
}
}
和我用来测试它的代码:
package Tree;
public class TreeTest {
public static void main(String[] args) {
Person p = new Person("John", "M",32);
Person p1 = new Person("Alex", "M",15);
Person p2 = new Person("Tom", "M",24);
Person p3 = new Person("Bill", "M",23);
Person p4 = new Person ("Jackson", "F", 50);
BinarySearchPerson bt = new BinarySearchPerson(p,new BinarySearchPerson(),new BinarySearchPerson());
BinarySearchPerson bt1 = BinarySearchPerson.insert(p1, bt);
BinarySearchPerson bt2 = BinarySearchPerson.insert(p2, bt1);
BinarySearchPerson bt3 = BinarySearchPerson.insert(p3, bt2);
BinarySearchPerson bt4 = BinarySearchPerson.insert(p4, bt3);
System.out.println("alex".compareTo("john"));
System.out.println(bt4.getLeft().getRight().getRight().getPerson().getName());
}
}
该方法可以正常工作并对名称进行排序。我现在要做的是创建一个新方法
lookupAge(String name);
可用于在树中搜索某人的姓名。我对如何执行此操作有一个粗略的想法,但我正在努力创建此方法的语法。该方法必须能够像:
一样调用bt.lookupAge("Tom");
那应该是24岁,这是汤姆的年龄。
我需要通过树查找tom,但该方法只有参数名称所以我不知道如何引用树bt。我之前从未用过这种方法,只是包含了我需要使用的所有参数。如果它是
我会好的lookupAge(String Name, BinarySearchPerson bt);
但我不能将树作为参数包含在内。
任何帮助都会很棒,谢谢。
答案 0 :(得分:0)
在BinarySearchPerson类中编写您的方法,使其看起来像
public int lookupAge(String name) {...
一旦写完,如果有人按照您的示例bt.lookupAge("Tom")
调用该方法,那么&#39;这个&#39;参数将引用bt
。然后,您将this.person.getName()
与name进行比较,并返回this.person.getAge(),或返回类似this.lookupAge(getRight().getPerson().getName())
的内容