示例#1
0
        /// <summary>
        /// Looks for a node with a matching value in this tree. Returns null if not found
        ///
        /// O(log n)
        /// </summary>
        /// <param name="value">The value of the node to find</param>
        /// <returns>
        /// null if the node was not found
        /// or the node if it was found
        /// </returns>
        public bool Contains(T item)
        {
            BinarySearchTreeNode <T> current = this.Root;

            while (current != null)
            {
                int compareResult = current.Value.CompareTo(item);
                if (compareResult == 0)
                {
                    break;
                }
                else if (compareResult > 0)
                {
                    current = current.Left;
                }
                else
                {
                    current = current.Right;
                }
            }

            return(current != null);
        }
示例#2
0
        /// <summary>
        /// Deletes the node with the given value from the tree and deletes that node. Throws a NodeNotFoundException otherwise
        ///
        /// O(log n)
        /// </summary>
        /// <param name="value">The value to delete from the tree</param>
        /// <returns>The node that was deleted</returns>
        public bool Delete(T value)
        {
            bool result = true;

            BinarySearchTreeNode <T> previous = null;
            BinarySearchTreeNode <T> current  = this.Root;

            while (current != null)
            {
                int compareResult = current.Value.CompareTo(value);
                if (compareResult == 0)
                {
                    break;
                }
                else if (compareResult > 0)
                {
                    previous = current;
                    current  = current.Left;
                }
                else
                {
                    previous = current;
                    current  = current.Right;
                }
            }

            if (current == null)
            {
                result = false;
            }
            else
            {
                this.DeleteNode(current, previous);
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Deletes the node that is passed in
        /// </summary>
        /// <param name="node">The node to delete</param>
        /// <param name="parentNode">the parent of the node we are deleting</param>
        internal void DeleteNode(BinarySearchTreeNode <T> node, BinarySearchTreeNode <T> parentNode)
        {
            if (this.Root.Value.CompareTo(node.Value) == 0)
            {
                if (this.Root.IsLeaf)
                {
                    this.Root = null;
                    --this.Count;
                }
                else if (this.Root.Left == null)
                {
                    this.Root = this.Root.Right;
                    --this.Count;
                }
                else if (this.Root.Right == null)
                {
                    this.Root = this.Root.Left;
                    --this.Count;
                }
                else
                {
                    BinarySearchTreeNode <T> swapNode = this.Root.InOrderPredecessor;

                    T tempValue = swapNode.Value;
                    this.Delete(swapNode.Value);
                    this.Root.Value = tempValue;
                }
            }
            else if (node.IsLeaf)
            {
                if (parentNode != null)
                {
                    if (node.Value.CompareTo(parentNode.Value) < 0)
                    {
                        parentNode.Left = null;
                    }
                    else
                    {
                        parentNode.Right = null;
                    }
                }

                --this.Count;
            }
            else if (node.Right == null)
            {
                if (parentNode != null)
                {
                    if (node.Value.CompareTo(parentNode.Value) < 0)
                    {
                        parentNode.Left = node.Left;
                    }
                    else
                    {
                        parentNode.Right = node.Left;
                    }
                }

                --this.Count;
            }
            else if (node.Left == null)
            {
                if (parentNode != null)
                {
                    if (node.Value.CompareTo(parentNode.Value) < 0)
                    {
                        parentNode.Left = node.Right;
                    }
                    else
                    {
                        parentNode.Right = node.Right;
                    }
                }

                --this.Count;
            }
            else // if both children not null
            {
                BinarySearchTreeNode <T> swapNode = node.InOrderPredecessor;

                T tempValue = swapNode.Value;
                this.Delete(swapNode.Value);
                node.Value = tempValue;
            }
        }
示例#4
0
        /// <summary>
        /// Inserts a new node with the given value in to the tree in order. Throws a ArgumentException if this node was a duplicate of one that already exists in the tree
        /// </summary>
        /// <param name="node">The value to insert</param>
        public void Insert(T value)
        {
            var node = new BinarySearchTreeNode <T>(value);

            this.Insert(node);
        }