Ejemplo n.º 1
0
 public void Add(T data)
 {
     lock (_synchRoot)
     {
         bool dataAdded = true;
         if (_count == 0)
         {
             _root = new LinkedAvlTreeNode <T>(data);
         }
         else
         {
             _root.AddData(data, out _root, out dataAdded);
         }
         if (dataAdded)
         {
             _count++;
         }
     }
 }
Ejemplo n.º 2
0
        private bool AddChildRight(T data, out LinkedAvlTreeNode <T> node, out bool added)
        {
            bool subTreeHeightIncreased = false;

            if (_right == null)
            {
                LinkedAvlTreeNode <T> child = new LinkedAvlTreeNode <T>(data);
                _right          = child;
                child._previous = this;
                child._next     = this._next;
                if (this._next != null)
                {
                    this._next._previous = child;
                }
                this._next = child;
                added      = true;
                if (_balance == 0)
                {
                    _balance = 1;
                }
                else
                {
                    System.Diagnostics.Debug.Assert(_balance == -1);
                    _balance = 0;
                }
                node = this;
                return(_balance != 0);
            }
            else
            {
                subTreeHeightIncreased = _right.AddData(data, out _right, out added);
            }
            if (!subTreeHeightIncreased)
            {
                node = this;
                return(false);
            }
            // the subtree height has changed!
            if (_balance < 0) // the tree is balanced in a way that it has more children that are smaller
            {
                _balance = 0; // now the tree is balanced
                node     = this;
                return(false);
            }
            else if (_balance == 0)
            {
                _balance = 1; // the tree is now unbalanced but still ok
                node     = this;
                return(true);
            }
            else //we have to reestablish the order criterion of the tree
            {
                if (_right._balance == 1)  // single rotation
                {
                    RotateRight(out node);
                    node._balance = 0;
                    _balance      = 0;
                }
                else                        // double rotation
                {
                    DoubleRotateRight(out node);
                    AdjustBalance(node);
                }
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// add a child whose value is smaller than the current data item
        /// </summary>
        /// <param name="child"></param>
        private bool AddChildLeft(T data, out LinkedAvlTreeNode <T> node, out bool added)
        {
            bool subTreeHeightIncreased = false;

            if (_left == null)  // there is no smaller subtree!
            {
                LinkedAvlTreeNode <T> child = new LinkedAvlTreeNode <T>(data);
                _left           = child;
                child._next     = this;
                child._previous = this._previous;
                if (this._previous != null)
                {
                    this._previous._next = child;
                }
                this._previous = child;
                node           = this;
                added          = true;
                if (_balance == 0)  // if the tree was balanced before, it is now longer on the left side
                {
                    _balance = -1;
                }
                else // if it was longer on the right side!
                {
                    System.Diagnostics.Debug.Assert(_balance == 1);
                    _balance = 0;
                }

                return(_balance != 0);
            }
            else
            {
                subTreeHeightIncreased = _left.AddData(data, out _left, out added);
            }
            if (!subTreeHeightIncreased)
            {
                node = this;
                return(false);
            }
            // the subtree height has changed!
            if (_balance > 0) // the tree is balanced in a way that it has more children that are bigger
            {
                _balance = 0; // now the tree is balanced
                node     = this;
                return(false);
            }
            else if (_balance == 0)
            {
                _balance = -1; // the tree is now unbalanced but still ok
                node     = this;
                return(true);
            }
            else //we have to reestablish the order criterion of the tree
            {
                if (_left._balance == -1)  // single rotation
                {
                    _left._balance = 0;
                    _balance       = 0;
                    RotateLeft(out node);
                }
                else                        // double rotation
                {
                    DoubleRotateLeft(out node);
                    AdjustBalance(node);
                }
                return(false);
            }
        }