public AvlNode(int _value, AvlNode _parrent) { value = _value; times = 1; parrent = _parrent; if (_parrent == null) balance = 0; //else // balance += (balance > 0 ? -1 : 1); }
private AvlNode RotateRightLeft(AvlNode node) { AvlNode right = RotateRight(node); return RotateLeft(node); //AvlNode right = node.Right; //AvlNode rightLeft = right.Left; //AvlNode parent = node.Parent; //AvlNode rightLeftLeft = rightLeft.Left; //AvlNode rightLeftRight = rightLeft.Right; //rightLeft.Parent = parent; //node.Right = rightLeftLeft; //right.Left = rightLeftRight; //rightLeft.Right = right; //rightLeft.Left = node; //right.Parent = rightLeft; //node.Parent = rightLeft; //if (rightLeftLeft != null) // rightLeftLeft.Parent = node; //if (rightLeftRight != null) // rightLeftRight.Parent = right; //if (node == root) // root = rightLeft; //else if (parent.Right == node) // parent.Right = rightLeft; //else // parent.Left = rightLeft; //if (rightLeft.Balance == 1) //{ // node.Balance = 0; // right.Balance = -1; //} //else if (rightLeft.Balance == 0) //{ // node.Balance = 0; // right.Balance = 0; //} //else //{ // node.Balance = 1; // right.Balance = 0; //} //rightLeft.Balance = 0; //return rightLeft; }
private AvlNode RotateRight(AvlNode node) { AvlNode left = node.Left; AvlNode leftRight = (left != null ? left.Right : null); AvlNode parent = node.Parent; left.Parent = (left != null ? parent : null); left.Right = (left != null ? node : null); node.Left = leftRight; node.Parent = left; if (leftRight != null) leftRight.Parent = node; if (node == root) root = left; else if (parent.Left == node) parent.Left = left; else parent.Right = left; left.Balance--; node.Balance = -left.Balance; return left; }
public bool Delete(int _value) { AvlNode node = root; while (node != null) { if (_value.CompareTo(node.Value) < 0) { node = node.Left; } else if (_value.CompareTo(node.Value) > 0) { node = node.Right; } else { if(node.Times > 1) { node.Times += -1; return true; } AvlNode left = node.Left; AvlNode right = node.Right; if (left == null) { if (right == null) { if (node == root) { root = null; } else { AvlNode parent = node.Parent; if (parent.Left == node) { parent.Left = null; DeleteBalance(parent, -1); } else { parent.Right = null; DeleteBalance(parent, 1); } } } else { Replace(node, right); DeleteBalance(node, 0); } } else if (right == null) { Replace(node, left); DeleteBalance(node, 0); } else { AvlNode successor = right; if (successor.Left == null) { AvlNode parent = node.Parent; successor.Parent = parent; successor.Left = left; successor.Balance = node.Balance; if (left != null) left.Parent = successor; if (node == root) { root = successor; } else { if (parent.Left == node) parent.Left = successor; else parent.Right = successor; } DeleteBalance(successor, 1); } else { while (successor.Left != null) successor = successor.Left; AvlNode parent = node.Parent; AvlNode successorParent = successor.Parent; AvlNode successorRight = successor.Right; if (successorParent.Left == successor) successorParent.Left = successorRight; else successorParent.Right = successorRight; if (successorRight != null) successorRight.Parent = successorParent; successor.Parent = parent; successor.Left = left; successor.Balance = node.Balance; successor.Right = right; right.Parent = successor; if (left != null) left.Parent = successor; if (node == root) { root = successor; } else { if (parent.Left == node) parent.Left = successor; else parent.Right = successor; } DeleteBalance(successorParent, -1); } } return true; } } return false; }
private AvlNode RotateLeft(AvlNode node) { AvlNode right = node.Right; AvlNode rightLeft = right.Left; AvlNode parent = node.Parent; right.Parent = parent; right.Left = node; node.Right = rightLeft; node.Parent = right; if (rightLeft != null) rightLeft.Parent = node; if (node == root) root = right; else if (parent.Right == node) parent.Right = right; else parent.Left = right; right.Balance++; node.Balance = -right.Balance; return right; }
private void InsertBalance(AvlNode node, int balance) { while (node != null) { balance = (node.Balance += balance); if (balance == 0) { return; } else if (balance == 2) { if (node.Left.Balance == 1) { RotateRight(node); } else { RotateLeftRight(node); } return; } else if (balance == -2) { if (node.Right.Balance == -1) { RotateLeft(node); } else { RotateRightLeft(node); } return; } AvlNode parent = node.Parent; if (parent != null) { balance += parent.Left == node ? 1 : -1; } node = parent; } }
private void DeleteBalance(AvlNode node, int balance) { while (node != null) { balance = (node.Balance += balance); if (balance >= 2) { if (node.Left.Balance >= 0) { node = RotateRight(node); if (node.Balance == -1) return; } else node = RotateLeftRight(node); } else if (balance <= -2) { if (node.Right.Balance <= 0) { node = RotateLeft(node); if (node.Balance == 1) return; } else node = RotateRightLeft(node); } else if (balance != 0) return; AvlNode parent = node.Parent; if (parent != null) balance = parent.Left == node ? -1 : 1; node = parent; } }
public void Clear() { root = null; }
private int CountRecusive(AvlNode node) { int elements = 0; if (node != null) elements = 1 + CountRecusive(node.Left) + CountRecusive(node.Right); return elements; }
private static void Replace(AvlNode target, AvlNode source) { AvlNode left = source.Left; AvlNode right = source.Right; target.Balance = source.Balance; target.Value = source.Value; target.Value = source.Value; target.Left = left; target.Right = right; if (left != null) left.Parent = target; if (right != null) right.Parent = target; }
/** * Print all values that lie between min and max in order (inclusive) */ public void PrintInRange(AvlNode _node, int _min, int _max) { if (_node == null) return; if (_node.Value > _min) PrintInRange(_node.Left, _min, _max); if (_node.Value >= _min && _node.Value <= _max) Console.Write(_node.Value + ", "); if (_node.Value < _max) PrintInRange(_node.Right, _min, _max); }
/** * Print all the values in the tree in order */ public void Print(AvlNode node) { if (node == null) return; Print(node.Left); Console.Write(node.Value + " "); Print(node.Right); }
public AvlNode Min(AvlNode node) { while (node != null) { if (node.Left == null) { return node; } node = node.Left; } return new AvlNode(-1, null); }
public AvlNode Max(AvlNode node) { while (node != null) { if (node.Right == null) { return node; } node = node.Right; } return new AvlNode(-1, null); }
public void Insert(int value) { if (root == null) { root = new AvlNode(_value: value, _parrent: null); } else { AvlNode node = root; while (node != null) { int compare = value.CompareTo(node.Value); if (compare < 0) { AvlNode left = node.Left; if (left == null) { node.Left = new AvlNode(_value: value, _parrent: node); InsertBalance(node, 1); return; } else { node = left; } } else if (compare > 0) { AvlNode right = node.Right; if (right == null) { node.Right = new AvlNode(_value: value, _parrent: node); InsertBalance(node, -1); return; } else { node = right; } } else { // value allready exists node.Times += 1; return; } } } }
/** * Returns how many levels deep the deepest level in the tree is * (the empty tree is 0 levels deep, the tree with only one root node is 1 deep) * @return */ public int Depth(AvlNode node) { if (node == null) { return 0; } else { // compute the depth of each subtree int lDepth = Depth(node.Left); int rDepth = Depth(node.Right); // use the larger one if (lDepth > rDepth) return (lDepth + 1); else return (rDepth + 1); } }