public static AVLnode LeftRotation(AVLnode node) { AVLnode root = node.RightChild; AVLnode pivot = root.LeftChild; root.LeftChild = node; node.RightChild = pivot; return(root); }
/** * Main function */ public static AVLnode InsertRecursive(AVLnode node, AVLnode root) { if (root == null) { return(node); } else { if (node.Value < root.Value) { root.LeftChild = AVLtree.InsertRecursive(node, root.LeftChild); } else { root.RightChild = AVLtree.InsertRecursive(node, root.RightChild); } } if (root.BalanceFactor > 1) { // leva stran poddrevesa if (node.Value < root.LeftChild.Value) { root = AVLrotations.RightRotation(root); } else { // desna stran poddrevesa root.RightChild = AVLrotations.LeftRotation(root.RightChild); root = AVLrotations.RightRotation(root); } } else if (root.BalanceFactor < -1) { if (node.Value > root.RightChild.Value) { root = AVLrotations.LeftRotation(root); } else { root.RightChild = AVLrotations.RightRotation(root); root = AVLrotations.LeftRotation(root); } } // set balance factor return(root); }
public AVLtree(AVLnode node) { this.Root = node; }
public void InsertValue(int value) { AVLnode node = new AVLnode(value); this.Root = AVLtree.InsertRecursive(node, this.Root); }
public static int GetBalanceFactor(AVLnode node) { return(node.LeftChild.Height - node.RightChild.Height); }