Exemple #1
0
        AVLTreeNode <TNode> _right; //правый потомок

        public AVLTreeNode(TNode value, AVLTreeNode <TNode> parent, AVLTree <TNode> tree)
        {
            Value  = value;  //значение в узле
            Parent = parent; //указатель на родительский элемент
            _tree  = tree;   //указатель на дерево
        }
Exemple #2
0
        public bool Remove(T value)
        {
            AVLTreeNode <T> current;

            current = Find(value); // поиск удаляемого значения

            if (current == null)
            {
                return(false);
            }

            AVLTreeNode <T> treeToBalance = current.Parent; //проверка баланса дерева

            Count--;

            // Вариант 1: если удаляемый узел не имеет правого потомка

            if (current.Right == null)      // если нет правого потомка
            {
                if (current.Parent == null) // удаляемый узел является корнем
                {
                    Head = current.Left;    //на место корня перемещаем левый потомок
                    if (Head != null)
                    {
                        Head.Parent = null; // для данного корня удаляем ссылку на родителя
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = current.Left;
                    }
                }
            }

            //Вариант 2. Если правый потомок удаляемого узла, тоже в свою очередь имеет правого потомка, но не имеет левого потомка

            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (current.Parent == null)
                {
                    Head = current.Right;

                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                    else
                    {
                        int result = current.Parent.CompareTo(current.Value);
                        if (result > 0)
                        {
                            current.Parent.Left = current.Right;
                        }
                        else if (result < 0)
                        {
                            current.Parent.Right = current.Right;
                        }
                    }
                }
            }

            // Вариант 3. если правый потомок удалемого узла имеет левого потомка,
            // то ребуется поместить на место удаляемого узла, крайний левый потомок его правого узла

            else
            {
                //нахождение крайнего левого узла для правого потомка текущего элемента
                AVLTreeNode <T> leftmost = current.Right.Left;

                while (leftmost.Left != null)
                {
                    leftmost = leftmost.Left;
                }
                //Родительское левое поддерево становится крайним правым поддеревом
                leftmost.Parent.Left = leftmost.Right;
                //присвоить кайний левый и крайний правый потомки удалемого узла его правому и левому потомкя соответственно

                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;

                if (current.Parent == null)
                {
                    Head = leftmost;
                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = leftmost;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = leftmost;
                    }
                }
            }
            if (treeToBalance != null)
            {
                //treeToBalance.Balance;
            }
            else
            {
                if (Head ! != null)
                {
                    //Head.Balanse();
                }
            }
            return(true);
        }