Пример #1
0
        public void Remove(T value)
        {
            var node = this.Find(this.Root, (x) => x.CompareTo(value) == 0);

            if (node == null)
            {
                throw new ArgumentException("Not such element");
            }

            if (node == this.Root)
            {
                var currNode = node.RightChild;

                if (currNode == null)
                {
                    currNode        = node.LeftChild;
                    currNode.Parent = null;
                    this.Root       = currNode;
                }

                else
                {
                    while (currNode.LeftChild != null)
                    {
                        currNode = currNode.LeftChild;

                        currNode.Parent             = null;
                        currNode.LeftChild          = this.Root.LeftChild;
                        this.Root                   = currNode;
                        this.Root.LeftChild         = this.Root;
                        this.Root.RightChild.Parent = this.Root;
                    }

                    if (currNode.Parent != this.Root)
                    {
                        currNode.Parent.LeftChild = currNode.RightChild;
                        currNode.RightChild       = this.Root.RightChild;
                    }

                    currNode.Parent             = null;
                    currNode.LeftChild          = this.Root.LeftChild;
                    this.Root                   = currNode;
                    this.Root.LeftChild         = this.Root;
                    this.Root.RightChild.Parent = this.Root;
                }



                return;
            }

            if (node.Value.CompareTo(node.Parent.Value) >= 0)
            {
                if (node.RightChild != null)
                {
                    node.Parent.RightChild = node.RightChild;
                    node.RightChild.Parent = node.Parent;

                    var currentNode = node.RightChild.LeftChild;

                    while (currentNode.LeftChild != null)
                    {
                        currentNode = currentNode.LeftChild;
                    }

                    currentNode.LeftChild = node.LeftChild;
                }
                else if (node.LeftChild != null)
                {
                    node.Parent.RightChild = node.LeftChild;
                    node.LeftChild.Parent  = node.Parent;
                }
                else
                {
                    if (node.Value.CompareTo(node.Parent.Value) >= 0)
                    {
                        node.Parent.RightChild = null;
                    }
                    else
                    {
                        node.Parent.LeftChild = null;
                    }
                }
            }

            else
            {
                if (node.RightChild != null)
                {
                    node.Parent.LeftChild  = node.RightChild;
                    node.RightChild.Parent = node.Parent;

                    var currentNode = node.RightChild;

                    while (currentNode.LeftChild != null)
                    {
                        currentNode = currentNode.LeftChild;
                    }

                    currentNode.LeftChild = node.LeftChild;
                }
                else if (node.LeftChild != null)
                {
                    node.Parent.LeftChild = node.LeftChild;
                    node.LeftChild.Parent = node.Parent;
                }
                else
                {
                    if (node.Value.CompareTo(node.Parent.Value) >= 0)
                    {
                        node.Parent.RightChild = null;
                    }
                    else
                    {
                        node.Parent.LeftChild = null;
                    }
                }
            }
        }
Пример #2
0
 public BinaryTree(T root)
 {
     this.Root = new BinaryTreeNode <T>(root, null);
 }