コード例 #1
0
            public void Add(int value)
            {
                BinarySearchTreeNode newNode = new BinarySearchTreeNode();

                newNode.iData = value;
                BinarySearchTreeNode current = root;
                BinarySearchTreeNode parent  = null;

                while (current != null)
                {
                    if (value == current.iData)
                    {
                        return;
                    }
                    else if (value < current.iData)
                    {
                        parent  = current;
                        current = current.Left;
                    }
                    else
                    {
                        parent  = current;
                        current = current.Right;
                    }
                }
                if (parent == null)
                {
                    root = newNode;
                }
                else
                {
                    if (value < parent.iData)
                    {
                        parent.Left = newNode;
                    }
                    else
                    {
                        parent.Right = newNode;
                    }
                }
            }
コード例 #2
0
            public bool Contains(int value)
            {
                BinarySearchTreeNode current = root;

                while (current != null)
                {
                    if (value == current.iData)
                    {
                        return(true);
                    }
                    else if (value < current.iData)
                    {
                        current = current.Left;
                    }
                    else if (value > current.iData)
                    {
                        current = current.Right;
                    }
                }
                return(false);
            }
コード例 #3
0
            public bool Remove(int value)
            {
                if (root == null)
                {
                    return(false);
                }
                BinarySearchTreeNode current = root;
                BinarySearchTreeNode parent  = null;

                while (value != current.iData)
                {
                    if (value < current.iData)
                    {
                        parent  = current;
                        current = current.Left;
                    }
                    else
                    {
                        parent  = current;
                        current = current.Right;
                    }
                    if (current == null)
                    {
                        return(false);
                    }
                }
                if (current.Right == null)
                {
                    if (parent == null)
                    {
                        root = current.Left;
                    }
                    else
                    {
                        if (parent.iData > current.iData)
                        {
                            parent.Left = current.Left;
                        }
                        else
                        {
                            parent.Right = current.Left;
                        }
                    }
                }
                else if (current.Right.Left == null)
                {
                    current.Right.Left = current.Left;
                    if (parent == null)
                    {
                        root = current.Right;
                    }
                    else
                    {
                        if (parent.iData > current.iData)
                        {
                            parent.Left = current.Right;
                        }
                        else
                        {
                            parent.Right = current.Right;
                        }
                    }
                }
                else
                {
                    BinarySearchTreeNode leftmost = current.Right.Left;
                    BinarySearchTreeNode ImParent = current.Right;

                    while (leftmost.Left != null)
                    {
                        ImParent = leftmost;
                        leftmost = leftmost.Left;
                    }

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

                    if (parent == null)
                    {
                        root = leftmost;
                    }
                    else
                    {
                        if (parent.iData > current.iData)
                        {
                            parent.Left = leftmost;
                        }
                        else
                        {
                            parent.Right = leftmost;
                        }
                    }
                }
                return(true);
            }