Jump to content

change the order of an AVL tree. extremely urgent

- - - - -

  • Please log in to reply
No replies to this topic

#1
Jstar

Jstar

    Newbie

  • Members
  • Pip
  • 4 posts
Hello,
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");
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users