所以我正在尝试使用Java为AVL树编写RR Rotation和LL Rotation。我已经弄明白了,但是我不知道在哪里修改方法中的高度。我一直试图在这里理解它几天,但我无法弄清楚它在我目前的方法中究竟会在哪里。如果有人可以帮助解释我应该对节点的高度进行修改的确切位置(它使用了左侧高度的getLeft / setLeft和它的正确高度的getRight / setRight),那将非常有用!
/**
* Performs a right shift of the current critical node. (Left rotation)
* @precond Tree is not empty.
* @return a tree with the updated rotation.
**/
public BinaryNodeAVL280<I> shiftLeftRotation(BinaryNodeAVL280<I> critNode)
{
if(this.rootNode() == null)
{
throw new ContainerEmpty280Exception("Cannot rotate an empty AVL tree!");
}
//TempNode becomes right of A (which is C)
BinaryNodeAVL280<I> tempNode = (BinaryNodeAVL280<I>) critNode.rightNode();
//Set the parent of tempNode (A) to be critNode's parent.
tempNode.setParent(critNode.getParent());
//Set critNode (A)'s left Node (which is B) to tempNode's leftNode (C's left which is D)
critNode.setRightNode(tempNode.leftNode());
//Check if the rightNode of A is null.
if(critNode.rightNode() != null)
{
//If not null, set A's right parent (C's parent) to current parent.
((BinaryNodeAVL280<I>) critNode.rightNode()).setParent(critNode);
}
//Set Left node of C (was D) to critNode (A)
tempNode.setLeftNode(critNode);
//Set critNode's (unknown for now) to temp node (C).
critNode.setParent(tempNode);
//If parent of C is not null.
if(tempNode.getParent() != null)
{
//If the right child of tempNode is the same as critNode
if(tempNode.getParent().rightNode() == critNode)
//Set the right child to tempNode.
tempNode.getParent().setRightNode(tempNode);
//If the child of temp node's left side is equal to critNode
else if(tempNode.getParent().leftNode() == critNode)
//Set temp node's left to tempNode(b)
tempNode.getParent().setLeftNode(tempNode);
}
//increment height somehow...
return tempNode;
}