//Iterative Version public void InsertNode(int val) { AVLNode temp = root; Stack <AVLNode> parentStack = new Stack <AVLNode>(); while (temp != null) { parentStack.Push(temp); if (val < temp.data) { temp = temp.left; } else if (val > temp.data) { temp = temp.right; } else { return; } } temp = new AVLNode(val); if (parentStack.Count != 0) { AVLNode temp1 = parentStack.Pop(); if (val < temp1.data) { temp1.left = temp; } else { temp1.right = temp; } Update(temp1); while (parentStack.Count != 0 && (temp1.bf != -2 && temp1.bf != 2)) { temp1 = parentStack.Pop(); Update(temp1); } if (parentStack.Count != 0) { temp = parentStack.Pop(); if (temp.data > val) { temp.left = Balance(temp1); } else { temp.right = Balance(temp1); } } else { root = Balance(temp1); } } else { root = new AVLNode(val); } }
public AVLTree(int data) { root = new AVLNode(data); }
private AVLNode LeftRightCase(AVLNode node) { node.left = LeftRotation(node.left); return(RightRotation(node)); }
private AVLNode LeftLeftCase(AVLNode node) { return(RightRotation(node)); }
private AVLNode RightLeftCase(AVLNode node) { node.right = RightRotation(node.right); return(LeftRotation(node)); }
private AVLNode RightRightCase(AVLNode node) { return(LeftRotation(node)); }