Ejemplo n.º 1
0
        AvlNode <T> RemoveMin(out AvlNode <T> min)
        {
            if (IsEmpty)
            {
                min = Empty;
                return(Empty);
            }
            if (_left.IsEmpty)
            {
                //We are the minimum:
                min = this;
                return(_right);
            }
            //Go down:
            var newLt   = _left.RemoveMin(out min);
            var newRoot = NewOrMutate(Value, newLt, _right);

            return(newRoot.FixRootBalance());
        }
Ejemplo n.º 2
0
        public AvlNode <T> SetItem(int index, T val)
        {
            AvlNode <T> newLt = _left;
            AvlNode <T> newGt = _right;

            if (index < _left._count)
            {
                newLt = _left.SetItem(index, val);
            }
            else if (index > _left._count)
            {
                newGt = _right.SetItem(index - _left._count - 1, val);
            }
            else
            {
                return(NewOrMutate(val, newLt, newGt));
            }
            return(NewOrMutate(Value, newLt, newGt));
        }
Ejemplo n.º 3
0
 AvlNode <T> RemoveMax(out AvlNode <T> max)
 {
     if (IsEmpty)
     {
         max = Empty;
         return(Empty);
     }
     if (_right.IsEmpty)
     {
         //We are the max:
         max = this;
         return(_left);
     }
     else
     {
         //Go down:
         var newGt   = _right.RemoveMax(out max);
         var newRoot = NewOrMutate(Value, _left, newGt);
         return(newRoot.FixRootBalance());
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Return a new tree with the key-value pair inserted
        /// If the key is already present, it replaces the value
        /// This operation is O(Log N) where N is the number of keys
        /// </summary>
        public AvlNode <T> InsertIntoNew(int index, T val)
        {
            if (IsEmpty)
            {
                return(new AvlNode <T>(val));
            }

            AvlNode <T> newLt = _left;
            AvlNode <T> newGt = _right;

            if (index <= _left._count)
            {
                newLt = _left.InsertIntoNew(index, val);
            }
            else
            {
                newGt = _right.InsertIntoNew(index - _left._count - 1, val);
            }

            var newRoot = NewOrMutate(Value, newLt, newGt);

            return(newRoot.FixRootBalance());
        }
Ejemplo n.º 5
0
 public virtual AvlNode <T> NewOrMutate(T newValue, AvlNode <T> newLeft, AvlNode <T> newRight)
 {
     return(new AvlNode <T>(newValue, newLeft, newRight));
 }
Ejemplo n.º 6
0
 public AvlNode()
 {
     _right = Empty;
     _left  = Empty;
 }