示例#1
0
        /// <summary>
        /// Tar bort ett värde ur sökträdet.
        /// </summary>
        /// <param name="value">Värdet som ska tas bort.</param>
        public bool Remove(T value)
        {
            // Ifall roten är tom så kan vi ej ta bort något
            if (_root == null)
            {
                return(false);
            }

            // ifall det finns ett vänster- eller högerbarn
            // låt noden hantera borttagandet av värdet
            if (_root.LeftChild != null || _root.RightChild != null)
            {
                _root.Remove(value);
            }
            // ifall det finns en rot men ej några barn-noder
            // kontrollera ifall roten innehåller det sökta värdet
            else if (_root.Value.CompareTo(value) == 0)
            {
                _root = null;
            }
            return(true);
        }
示例#2
0
 /// <summary>
 /// Initierar ett sökträd med en given rot.
 /// </summary>
 public BinarySearchTree(BinarySearchTreeNode <T> root)
 {
     _root = root;
 }
示例#3
0
        public void Remove(T value)
        {
            if (value.CompareTo(Value) == 0)
            {
                //Required for the GUI to work.
                Parent.IsChanged = true;


                //Om noden ej har barn.
                if (_rightChild == null && _leftChild == null)
                {
                    if (IsLeftChild)
                    {
                        _parent._leftChild = null;
                    }
                    else
                    {
                        _parent._rightChild = null;
                    }
                }
                //Om noden har ett högerbarn
                else if (_leftChild == null)
                {
                    if (IsLeftChild)
                    {
                        _parent._leftChild  = _rightChild;
                        _rightChild._parent = _parent;
                    }

                    else
                    {
                        _parent._rightChild = _rightChild;
                        _rightChild._parent = _parent;
                    }
                }
                //Om noden har ett vänsterbarn
                else if (_rightChild == null)
                {
                    if (IsLeftChild)
                    {
                        _parent._leftChild = _leftChild;
                        _leftChild._parent = _parent;
                    }
                    else
                    {
                        _parent._rightChild = _leftChild;
                        _leftChild._parent  = _parent;
                    }
                }

                //Om noden har 2 barn. Då ersätts den borttagna noden med det största värdet i vänster subträd
                else
                {
                    BinarySearchTreeNode <T> largestValue = this._leftChild;
                    while (largestValue._rightChild != null)
                    {
                        largestValue = largestValue._rightChild;
                    }
                    this._leftChild.Remove(largestValue.Value);
                    _value = largestValue.Value;
                }
            }

            //Letar efter noden som skall tas bort
            else if (value.CompareTo(_value) > 0)
            {
                _rightChild.Remove(value);
            }
            else
            {
                _leftChild.Remove(value);
            }
        }
示例#4
0
        public void Remove(T value)
        {
            if (value.CompareTo(Value) == 0)
            {
                Parent.IsChanged = true;

                if (RightChild == null && LeftChild == null)
                {
                    Parent.ParentRemove(value);
                }
                if (RightChild == null && LeftChild != null)
                {
                    LeftChild.Parent = Parent;
                    if (value.CompareTo(Parent.Value) < 0)
                    {
                        Parent.LeftChild = LeftChild;
                    }
                    if (value.CompareTo(Parent.Value) > 0)
                    {
                        Parent.RightChild = LeftChild;
                    }
                }
                if (RightChild != null && LeftChild == null)
                {
                    RightChild.Parent = Parent;
                    if (value.CompareTo(Parent.Value) < 0)
                    {
                        Parent.LeftChild = RightChild;
                    }
                    if (value.CompareTo(Parent.Value) > 0)
                    {
                        Parent.RightChild = RightChild;
                    }
                }
                if (RightChild != null && LeftChild != null)
                {
                    if (RightChild.RightChild == null && RightChild.LeftChild == null)
                    {
                        RightChild.IsChanged = true;
                        RightChild.LeftChild = LeftChild;
                        RightChild.Parent    = Parent;
                        if (value.CompareTo(Parent.Value) < 0)
                        {
                            Parent.LeftChild = RightChild;
                        }

                        if (value.CompareTo(Parent.Value) > 0)
                        {
                            Parent.RightChild = RightChild;
                        }
                    }
                    else
                    {
                        BinarySearchTreeNode <T> tmpChild = RightChild.TraverstLeft();
                        this.Remove(tmpChild.Value);
                        _value = tmpChild.Value;
                    }
                }
            }

            else
            {
                if (value.CompareTo(Value) < 0)
                {
                    if (LeftChild != null)
                    {
                        LeftChild.Remove(value);
                    }
                }
                if (value.CompareTo(Value) >= 0)
                {
                    if (RightChild != null)
                    {
                        RightChild.Remove(value);
                    }
                }
            }
        }
示例#5
0
        /// <summary>
        /// tar bort en nod med angivet värde från trädet
        /// </summary>
        /// <param name="value">värdet på norden som ska tas bort</param>
        public void Remove(T value)
        {
            if (value.CompareTo(Value) == 0) //om infört (eftersökt) värde hittas
            {
                Parent.IsChanged = true;

                if (LeftChild == null && RightChild == null) //om noden är ett löv
                {
                    if (IsRightChild == true)                //om noden är ett högerbarn
                    {
                        Parent.RightChild = null;
                    }
                    else
                    {
                        Parent.LeftChild = null;
                    }
                }

                if (LeftChild == null && RightChild != null) //noden har endast ett högerbarn
                {
                    if (IsRightChild)                        //om noden har högerbarn
                    {
                        Parent.RightChild = RightChild;
                        RightChild.Parent = Parent;
                    }
                    else //om något undantag, dvs vänsterbarn skulle smita förbi
                    {
                        Parent.LeftChild  = RightChild;
                        RightChild.Parent = Parent;
                    }
                }

                if (LeftChild != null && RightChild == null) //om noden har endast ett vänsterbarn
                {
                    if (IsLeftChild)                         //om noden har vänsterbarn
                    {
                        Parent.LeftChild = LeftChild;
                        LeftChild.Parent = Parent;
                    }
                    else //om något undantag, dvs högerbarn, skulle smita förbi
                    {
                        Parent.RightChild = LeftChild;
                        LeftChild.Parent  = Parent;
                    }
                }

                if (LeftChild != null && RightChild != null)   //om noden har två barn, ett höger och ett vänster
                {
                    BinarySearchTreeNode <T> temp = LeftChild; //instantierar temporär variabel av klassen BinarySearchTreeNode som är ett vänsterbarn
                    while (temp.RightChild != null)            //går sålänge den tillfälliga variabeln, som rör sig mellan noder, har ett högerbarn
                    {
                        temp = temp.RightChild;                //ge den temporära variabeln högerbarnets värde
                    }
                    this.Value = temp.Value;                   //ge det aktuella värdet den temporära variabelns värde

                    if (LeftChild != null)
                    {
                        LeftChild.Remove(temp.Value);
                    }
                }
            }

            else if (value.CompareTo(this.Value) > 0)
            {
                RightChild.Remove(value);
            }

            else if (value.CompareTo(this.Value) < 0)
            {
                LeftChild.Remove(value);
            }
        }