1

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?

Kainix
  • 1,186
  • 3
  • 21
  • 33
pazl94
  • 31
  • 5
  • Hmm...it is pretty much the same process...if the current node is null then the student could not be found ....if the current node comes after lastname, firstname in alphabetical order then search in the left tree. If it comes before then search in the right tree. Else return the current node. the process of determining ordering is pretty straightforward in java using comparators on String – MS Srikkanth Apr 23 '16 at 18:24

1 Answers1

0

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