Esempio n. 1
0
 private void rebalance(ref AvlTreeNode root)
 {
     // LL imbalance
     if (root.BalanceFactor > 1 && root.Value < root.Left.Value)
     {
         rotateRight(ref root);
     }
     // RR imbalance
     if (root.BalanceFactor < -1 && root.Value > root.Right.Value)
     {
         rotateLeft(ref root);
     }
     // LR imbalance
     if (root.BalanceFactor > 1 && root.Value > root.Left.Value)
     {
         rotateLeft(ref root._left);
         rotateRight(ref root);
     }
     // RL imbalance
     if (root.BalanceFactor < -1 && root.Value < root.Right.Value)
     {
         rotateRight(ref root._right);
         rotateLeft(ref root);
     }
 }
Esempio n. 2
0
        private void insert(ref AvlTreeNode root, int value)
        {
            if (root == null)
            {
                root = new AvlTreeNode(value);
            }

            if (value < root.Value)
            {
                insert(ref root._left, value);
            }

            if (value > root.Value)
            {
                insert(ref root._right, value);
            }

            recalculateBalanceFactors(ref root);
            if (!root.IsBalanced())
            {
                rebalance(ref root);
                rebalance(ref root._left);
                rebalance(ref root._right);
            }
            recalculateBalanceFactors(ref root);
        }
Esempio n. 3
0
 private void preOrder(AvlTreeNode root)
 {
     if (root != null)
     {
         Console.Write($"{root.Value} ");
         preOrder(root.Left);
         preOrder(root.Right);
     }
 }
Esempio n. 4
0
 private void rotateLeft(ref AvlTreeNode root)
 {
     if (root.Right != null)
     {
         AvlTreeNode tmp = root.Right;
         root._right = tmp.Left;
         tmp._left   = root;
         root        = tmp;
         recalculateBalanceFactors(ref root);
     }
 }
        public void Balance(TNode node, BinarySearchTree <T, TNode> tree)
        {
            if (node == null || tree == null)
            {
                throw new ArgumentNullException();
            }

            _node = node as AvlTreeNode <T>;
            _tree = tree as BinarySearchTree <T, AvlTreeNode <T> >;

            RotateTree();
        }
Esempio n. 6
0
        private void clear(ref AvlTreeNode root)
        {
            if (root.Left != null)
            {
                clear(ref root._left);
            }

            if (root.Right != null)
            {
                clear(ref root._right);
            }

            root = null;
        }
Esempio n. 7
0
        private AvlTreeNode removeGreatestNode(ref AvlTreeNode root)
        {
            AvlTreeNode current;

            if (root.Right == null)
            {
                current = root;
                root    = root.Right;
                return(current);
            }

            else
            {
                return(removeGreatestNode(ref root._right));
            }
        }
        private void RotateRight(AvlTreeNode <T> node)
        {
            AvlTreeNode <T> oldRoot = node;
            AvlTreeNode <T> newRoot = node.Left;

            oldRoot.Left = newRoot.Right;

            if (oldRoot.Left != null)
            {
                oldRoot.Left.Parent = oldRoot;
            }

            RotateParent(oldRoot, newRoot);

            newRoot.Right  = oldRoot;
            oldRoot.Parent = newRoot;
        }
Esempio n. 9
0
        private void recalculateBalanceFactors(ref AvlTreeNode root)
        {
            if (root == null)
            {
                return;
            }

            root.SetBalanceFactor();

            if (root.Left != null)
            {
                root.Left.SetBalanceFactor();
            }
            if (root.Right != null)
            {
                root.Right.SetBalanceFactor();
            }
        }
Esempio n. 10
0
        private AvlTreeNode search(int value, AvlTreeNode root)
        {
            if (root == null)
            {
                return(null);
            }

            if (value < root.Value)
            {
                search(value, root.Left);
            }

            if (value > root.Value)
            {
                search(value, root.Right);
            }

            return(root);
        }
        private void RotateParent(AvlTreeNode <T> oldRoot, AvlTreeNode <T> newRoot)
        {
            if (oldRoot.Parent == null)
            {
                _tree.Root     = newRoot;
                newRoot.Parent = null;
            }
            else
            {
                if (oldRoot.Parent.Left != null && oldRoot.Parent.Left.Value.Equals(oldRoot.Value))
                {
                    oldRoot.Parent.Left = newRoot;
                }
                else if (oldRoot.Parent.Right != null && oldRoot.Parent.Right.Value.Equals(oldRoot.Value))
                {
                    oldRoot.Parent.Right = newRoot;
                }

                newRoot.Parent = oldRoot.Parent;
                oldRoot.Parent = newRoot;
            }
        }
Esempio n. 12
0
        private void deleteRoot(ref AvlTreeNode root)
        {
            AvlTreeNode current = root;

            if (root.Left == null)
            {
                root = root.Right;
            }

            else if (root.Right == null)
            {
                root = root.Left;
            }

            else
            {
                root        = removeGreatestNode(ref root._left);
                root._left  = current.Left;
                root._right = current.Right;
            }

            recalculateBalanceFactors(ref root);
        }
Esempio n. 13
0
        private void delete(ref AvlTreeNode root, int value)
        {
            if (root == null)
            {
                return;
            }

            else if (value < root.Value)
            {
                delete(ref root._left, value);
            }

            else if (value > root.Value)
            {
                delete(ref root._right, value);
            }

            else
            {
                deleteRoot(ref root);
                recalculateBalanceFactors(ref root);
            }

            if (root != null && !root.IsBalanced())
            {
                rebalance(ref root);
                if (root.Left != null)
                {
                    rebalance(ref root._left);
                }
                if (root.Right != null)
                {
                    rebalance(ref root._right);
                }
            }
            recalculateBalanceFactors(ref root);
        }
 private void RotateRightLeft(AvlTreeNode <T> node)
 {
     RotateLeft(node.Left);
     RotateRight(node);
 }