I have to change the structure of AVL tree, presented in the book "Data Structures and Algorithms in Java" by Michael T. Goodrich, to reverse the order of elements.
Alternatively I change the items after they are inserted with this code
public void change(BinarySearchTree<Integer,String> t,Position<Entry<Integer, String>> v){
boolean changed=false;
if (t.hasLeft(v)){
change(t,t.left(v));
t.swapElements(t.left(v), t.sibling(t.left(v)));
changed=true;
}
if (t.hasRight(v)){
change(t,t.right(v));
if (!changed)
t.swapElements(t.right(v), t.sibling(t.right(v)));
}
}
but it don't work
in other classes:
public void swapElements(Position<E> v, Position<E> w) throws InvalidPositionException {
BTPosition<E> vv = checkPosition(v);
BTPosition<E> ww = checkPosition(w);
E temp = w.getElement();
ww.setElement(v.getElement());
vv.setElement(temp);
}
Quote
public Position<E> sibling(Position<E> v) throws InvalidPositionException, BoundaryViolationException {
BTPosition<E> vv = checkPosition(v);
BTPosition<E> parentPos = vv.getParent();
if (parentPos != null) {
BTPosition<E> sibPos;
BTPosition<E> leftPos = parentPos.getLeft();
if (leftPos == vv) {
sibPos = parentPos.getRight();
}
else {
sibPos = parentPos.getLeft();
}
if (sibPos != null) {
return sibPos;
}
}
throw new BoundaryViolationException("No sibling");
}
BTPosition<E> vv = checkPosition(v);
BTPosition<E> parentPos = vv.getParent();
if (parentPos != null) {
BTPosition<E> sibPos;
BTPosition<E> leftPos = parentPos.getLeft();
if (leftPos == vv) {
sibPos = parentPos.getRight();
}
else {
sibPos = parentPos.getLeft();
}
if (sibPos != null) {
return sibPos;
}
}
throw new BoundaryViolationException("No sibling");
}


Sign In
Create Account

Back to top









