예제 #1
0
        public void Remove(T value)
        {
            if (Head == null)
            {
                return;
            }
            Node <T> removalNode = Find(value);

            if (removalNode.ChildCount() == 0)
            {
                if (removalNode.IsRightChild() == Node <T> .ChildType.Right)
                {
                    removalNode.Parent.Right = null;
                    removalNode = null;
                }
                else if (removalNode.IsRightChild() == Node <T> .ChildType.Left)
                {
                    removalNode.Parent.Left = null;
                    removalNode             = null;
                }
                else
                {
                    removalNode = null;
                    return;
                }
                Count--;
                return;
            }
            else if (removalNode.ChildCount() == 1)
            {
                if (removalNode.IsRightChild() == Node <T> .ChildType.Right)
                {
                    removalNode.Child().Parent = removalNode.Parent;
                    removalNode.Parent.Right = removalNode.Child();
                }
                else if (removalNode.IsRightChild() == Node <T> .ChildType.Left)
                {
                    removalNode.Child().Parent = removalNode.Parent;
                    removalNode.Parent.Left = removalNode.Child();
                }
            }
            else if (removalNode.ChildCount() == 2)
            {
                Node <T> temp = removalNode.Left;
                temp = max(temp);
                T val = temp.Value;
                Remove(val);
                removalNode.Value = val;
            }
        }
        public void DeleteNode(Node <T> NodeToDelete)
        {
            //No children -> Just Delete
            if (NodeToDelete.HasNoChildren())
            {
                if (NodeToDelete.Equals(Root))
                {
                    Root = null;
                    return;
                }

                Traverse((Node <T> node) =>
                {
                    if (node.Left != null)
                    {
                        if (node.Left.Equals(NodeToDelete))
                        {
                            node.Left = null;
                        }
                    }
                    if (node.Right != null)
                    {
                        if (node.Right.Equals(NodeToDelete))
                        {
                            node.Right = null;
                        }
                    }
                }, TraversalType.PostOrder);
                return;
            }

            //Exactly one child -> Swap
            if (NodeToDelete.HasOneChild())
            {
                if (NodeToDelete.Equals(Root))
                {
                    Root = Root.Child();
                    return;
                }

                Traverse((Node <T> node) =>
                {
                    if (node.Left != null)
                    {
                        if (node.Left.Equals(NodeToDelete))
                        {
                            node.Left = NodeToDelete.Child();
                        }
                    }
                    if (node.Right != null)
                    {
                        if (node.Right.Equals(NodeToDelete))
                        {
                            node.Right = NodeToDelete.Child();
                        }
                    }
                }, TraversalType.PostOrder);

                return;
            }

            //Find Minimum of Right sub-Tree, Copy Value, Delete Minimum
            if (NodeToDelete.HasTwoChildren())
            {
                Node <T> SuccessorNode = MinValue(NodeToDelete.Right);
                NodeToDelete.Value = SuccessorNode.Value;
                DeleteNode(SuccessorNode);

                return;
            }
        }