public void ReBalance() { NodeAVL node = this; while (node != null) { node.UpdateHeight(); if (GetHeight(node.right) >= 2 + GetHeight(node.left)) { if (GetHeight(((NodeAVL)node.right).right) >= GetHeight(((NodeAVL)node.right).left)) { LeftRotate(); } else { ((NodeAVL)node.right).RightRotate(); LeftRotate(); } } else if (GetHeight(node.left) >= 2 + GetHeight(node.right)) { if (GetHeight(((NodeAVL)node.left).left) >= GetHeight(((NodeAVL)node.left).right)) { RightRotate(); } else { ((NodeAVL)node.left).LeftRotate(); RightRotate(); } } node = (NodeAVL)node.parent; } }
public void LeftRotate() { NodeAVL y = (NodeAVL)this.right; y.parent = this.parent; if (((NodeAVL)y.parent).left == this) { ((NodeAVL)y.parent).left = y; } else if (((NodeAVL)y.parent).right == this) { ((NodeAVL)y.parent).right = y; } this.right = y.left; if (this.right != null) { ((NodeAVL)this.right).parent = this; } y.left = this; this.parent = y; UpdateHeight(); y.UpdateHeight(); }
public new void Delete() { NodeAVL node = (NodeAVL)base.Delete(); ((NodeAVL)node.parent).ReBalance(); }
public void Insert(NodeAVL node) { base.Insert(node); node.ReBalance(); }