Ejemplo n.º 1
0
        public void Remove(int value)
        {
            if (this.rootNode == null)
            {
                return;
            }
            var        currentNode = this.rootNode;
            BinaryNode parentNode  = new BinaryNode();

            while (currentNode != null)
            {
                if (value < currentNode.Value)
                {
                    parentNode  = currentNode;
                    currentNode = currentNode.LeftNode;
                }
                else if (value > currentNode.Value)
                {
                    parentNode  = currentNode;
                    currentNode = currentNode.RightNode;
                }
                else if (currentNode.Value == value)
                {
                    if (currentNode.RightNode == null)
                    {
                        if (parentNode == null)
                        {
                            this.rootNode = currentNode.LeftNode;
                        }
                        else
                        {
                            if (currentNode.Value < parentNode.Value)
                            {
                                parentNode.LeftNode = currentNode.LeftNode;
                            }
                            else if (currentNode.Value > parentNode.Value)
                            {
                                parentNode.RightNode = currentNode.LeftNode;
                            }
                        }
                    }
                    else if (currentNode.RightNode.LeftNode == null)
                    {
                        if (parentNode == null)
                        {
                            this.rootNode = currentNode.LeftNode;
                        }
                        else
                        {
                            currentNode.RightNode.LeftNode = currentNode.LeftNode;
                            if (currentNode.Value < parentNode.Value)
                            {
                                parentNode.LeftNode = currentNode.RightNode;
                            }
                            else if (currentNode.Value > parentNode.Value)
                            {
                                parentNode.RightNode = currentNode.RightNode;
                            }
                        }
                    }
                    else
                    {
                        var leftmost       = currentNode.RightNode.LeftNode;
                        var leftmostParent = currentNode.RightNode;
                        while (leftmost.LeftNode != null)
                        {
                            leftmostParent = leftmost;
                            leftmost       = leftmost.LeftNode;
                        }
                        leftmostParent.LeftNode = leftmost.RightNode;
                        leftmost.LeftNode       = currentNode.LeftNode;
                        leftmost.RightNode      = currentNode.RightNode;

                        if (parentNode == null)
                        {
                            this.rootNode = leftmost;
                        }
                        else
                        {
                            if (currentNode.Value < parentNode.Value)
                            {
                                parentNode.LeftNode = leftmost;
                            }
                            else if (currentNode.Value > parentNode.Value)
                            {
                                parentNode.RightNode = leftmost;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 public BinarySearchTree()
 {
     this.rootNode = null;
 }