Я пытаюсь реализовать метод splay (Node x) для двоичного дерева поиска. У меня есть методы leftRotation (Node x) и rightRotation (Node x), которые реализованы правильно (по крайней мере, я думаю, что они ...), но когда я пытаюсь реализовать их в методе splay (Node x), он вызывает себя в бесконечная петля. Теперь я знаю, почему это так, но не может понять, как это исправить.Реализация метода splay() для дерева двоичного поиска
Вот метод leftRotation (Node х):
public void leftRotation(Node<E> x) {
if (x.getRightChild() == null) {
return;
}
Node<E> y = x.getRightChild();
x.setRightChild(y.getLeftChild());
if (y.getLeftChild() != null) {
y.getLeftChild().setParent(x);
}
y.setParent(x.getParent());
if (x.getParent() == null) {
root = y;
} else {
if (x == x.getParent().getLeftChild()) {
x.getParent().setLeftChild(y);
} else {
x.getParent().setRightChild(y);
}
}
y.setLeftChild(x);
x.setParent(y);
}
Вот метод rightRotation (Node х):
public void rightRotation(Node<E> x) {
if (x.getLeftChild() == null) {
return;
}
Node<E> y = x.getLeftChild();
x.setRightChild(y.getRightChild());
if (y.getRightChild() != null) {
y.getRightChild().setParent(x);
}
y.setParent(x.getParent());
if (x.getParent() == null) {
root = y;
} else {
if (x == x.getParent().getRightChild()) {
x.getParent().setRightChild(y);
} else {
x.getParent().setLeftChild(y);
}
}
x.setRightChild(x);
x.setParent(y);
}
А вот метод расширяемого (Node х):
public void splay(Node<E> x) {
while (x.getParent() != null) {
if (x.isLeftChild && x.getParent().isLeftChild) {
this.rightRotation(x.getParent());
this.rightRotation(x);
} else if (x.isRightChild && x.getParent().isRightChild) {
this.leftRotation(x.getParent());
this.leftRotation(x);
} else if (x.isLeftChild && x.getParent().isRightChild) {
this.rightRotation(x);
this.leftRotation(x);
} else if (x.isRightChild() && x.getParent().isLeftChild()) {
this.leftRotation(x);
this.rightRotation(x);
} else if (x.isLeftChild && x.getParent() == root) {
this.rightRotation(x);
} else if (x.isRightChild && x.getParent() == root) {
this.leftRotation(x);
}
}
}
Любые идеи о том, как исправить бесконечный цикл? Кажется, что это связано с тем, что он не вырвался из инструкции while (x.getParent()! = Null) в методе splay (Node x), но когда я просмотрел код с помощью отладчика, свойства узел, казалось, менялся, поэтому я не знаю, где это происходит?
setLeftChild (Node LeftChild) метод:
public void setLeftChild(Node<E> leftChild) {
this.leftChild = leftChild;
if (leftChild != null) {
leftChild.setIsRightChild(true);
leftChild.setParent(this);
}
}
Что должен делать 'splay'? На самом деле, что все эти методы должны делать? – Dici
Это двоичное дерево поиска, leftRotation и rightRotation поворачивают узел вверх по дереву к корню, и splay должен использовать эти методы для балансировки дерева. –
В то время как я (и, возможно, другие пользователи SO) пытаюсь понять ваш код, пожалуйста, выполните тестирование двух методов, чтобы убедиться, что они ведут себя так, как вы думаете. Как правило, модульный тест замечательный – Dici