/** * 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); }
static void Main(string[] args) { AVLtree tree = new AVLtree(); tree.InsertValue(10); tree.InsertValue(9); tree.InsertValue(8); tree.InsertValue(7); tree.InsertValue(6); tree.InsertValue(5); tree.InsertValue(4); tree.InsertValue(3); tree.InsertValue(2); tree.InsertValue(1); Console.WriteLine(tree); }
public void InsertValue(int value) { AVLnode node = new AVLnode(value); this.Root = AVLtree.InsertRecursive(node, this.Root); }