// RR rotation private void rotateRR() { AVLTreeNode Me = this; AVLTreeNode Left = this.LeftNode; // switch 'Me' and 'Left' and attach 'Me' to the right connector of 'Left' if (Me.Parent != null) Me.Parent.updateMyConnector(Me, Left); Left.Parent = Me.Parent; if (Left.Parent == null) this.tree.root = Left; Me.Parent = Left; Me.LeftNode = null; if (Left.RightNode != null) { // we need to drop a node attached to the place where 'Me' is going to be attached // so we don't lose it. We're going to attach it to 'Me' instead // // Before: Element attached to 'Left' // Going to be: Element attached to 'Me' which is attached to 'Left' // print("[DEBUG] Dropping right node " + Left.RightNode.Name + " caught by " + Me.Name) Left.RightNode.Parent = Me; Me.LeftNode = Left.RightNode; } // finally attach 'Me' Left.RightNode = Me; }
// LL rotation (same as for RR but mirrored) private void rotateLL() { AVLTreeNode Me = this; AVLTreeNode Right = this.RightNode; if (Me.Parent != null) Me.Parent.updateMyConnector(Me, Right); Right.Parent = Me.Parent; if (Right.Parent == null) this.tree.root = Right; Me.Parent = Right; Me.RightNode = null; if (Right.LeftNode != null) { // print("[DEBUG] Dropping left node " + Right.LeftNode.Name + " caught by " + Me.Name) Right.LeftNode.Parent = Me; Me.RightNode = Right.LeftNode; } Right.LeftNode = Me; }