Exemplo n.º 1
0
        // Support Methods
        private void BalanceTree()
        {
            data.HeightCalc();
            AVLElement e = data.BFSearch();

            if (e == null)
            {
                return;
            }


            AVLElement l = (AVLElement)e.left;
            AVLElement r = (AVLElement)e.right;

            if (e.BalanceFactor() == -2)
            {   // -- and - (single rotation)
                if (l.BalanceFactor() == -1)
                {
                    e.RightRotation();
                }

                // -- and + (double rotation)
                else if (l.BalanceFactor() == 1)
                {
                    l.LeftRotation();
                    e.RightRotation();
                }
            }

            else if (e.BalanceFactor() == 2)
            {   // ++ and + (single rotation)
                if (r.BalanceFactor() == 1)
                {
                    e.LeftRotation();
                }

                // ++ and - (double rotation)
                else if (r.BalanceFactor() == -1)
                {
                    r.RightRotation();
                    e.LeftRotation();
                }
            }
            data.HeightCalc();
        }
Exemplo n.º 2
0
        public bool Delete(int element)
        {
            // delete from class BinSearchTree
            InfoReturn res = base.Delete(data, element);

            if (res.success == false)
            {
                return(false);
            }

            data = (AVLElement)res.newRoot;
            do
            {
                BalanceTree();
            }while (data.BFSearch() != null);

            return(true);
        }