Поиск элемента в бинарном дереве

Рейтинг: 0Ответов: 0Опубликовано: 21.03.2015

Добрый день.

Не получается найти элемент в бинарном дереве. Находит только первую вершину и первые боковые, а дальше не идет. Т.е. не перезаписывает почему-то root.student = root.right.student;.

Подскажите, пожалуйста, что не так. Спасибо.

public class MyTree {

    Element root;

public void searchStudent(int number) {

        if (root.student.getID() == number) {       
            System.out.println(root.student);
            return;
        }
        if (root == null) {
            System.out.println("Tree is empty");
            return;
        } else {
            while (root.right != null && root.left != null) {
                if (root.student.getID() == number) {
                    System.out.println(root.student);
                    return;
                }
                if (root.student.getID() < number) {
                    root.student = root.right.student;
                }
                if (root.student.getID() > number) {
                    root.student = root.left.student;
                }

            }
        }
    }
}

class Element{

    Student student;
    Element left;
    Element right;
}

Ответы

Ответов пока нет.