Exemplo n.º 1
0
        public void Remove(T data)
        {
            Node <T> parent = null;
            Node <T> left   = null;
            Node <T> right  = null;

            if (Root.Data.Equals(data))
            {
                left  = Root.Left;
                right = Root.Right;

                if (right != null)
                {
                    Root = right;
                    if (left != null)
                    {
                        Root.Add(left);
                    }
                }
                else
                {
                    Root = left;
                }
            }
            else
            {
                parent = GetParent(data, Root);

                if (parent == null)
                {
                    return;
                }

                if (parent.Left.Data.Equals(data))
                {
                    left  = parent.Left.Left;
                    right = parent.Left.Right;

                    if (right != null)
                    {
                        parent.Left = right;

                        if (left != null)
                        {
                            parent.Left.Add(left);
                        }
                    }
                    else
                    {
                        parent.Left = left;
                    }
                }
                else
                {
                    left  = parent.Right.Left;
                    right = parent.Right.Right;

                    if (right != null)
                    {
                        parent.Right = right;

                        if (left != null)
                        {
                            parent.Right.Add(left);
                        }
                    }
                    else
                    {
                        parent.Right = left;
                    }
                }
            }
        }