简体   繁体   中英

Find node in Binary search Tree in java


I have Binary Search Tree containing student records , consisting of ID and first and last name of the student , age and email and phone number .

All students will be stored in a Binary Search Tree based off of their student ID number, and it will be guaranteed that this ordering of students (by ID) will be the exact same as the alphabetical ordering by last name and then first name.

I make method to find node by the student ID .

This is the code

private Unfstudent findNode(Unfstudent student, int id) {

    if (student == null) 
        return null;
    }


    if (id < student.getID()) {
        return findNode(student.getLeft(), id);
    }

    else if (id > student.getID()) { 
        return findNode(student.getRight(), id);
    } 

    else {
        return student; 
        }

   }

I want to make method to find node by the student first name and last name .

Can anyone help me?

It is the same, just use the lexicographically string comparation in order to compare the strings.

So you will just be doing this

//some code
if (name.compareTo(student.name) == -1) {
    return findNode(student.getLeft(), name);
}
//some code

For more info check String Comparison in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM