Пример #1
0
 /// <summary>
 /// Removes an element with maximum priority.
 /// </summary>
 /// <returns>The element removed.</returns>
 public TValue RemoveMaximumPriority()
 {
     if (_elements == null)
     {
         throw new InvalidOperationException();
     }
     else
     {
         TValue tempValue = _elements.Data.Value;
         _count--;
         _elements = Merge(_elements.LeftChild, _elements.RightChild);
         return(tempValue);
     }
 }
Пример #2
0
 /// <summary>
 /// Constructs a LeftistTree with the given data, and children.
 /// </summary>
 /// <param name="data">The data stored in the node.</param>
 /// <param name="child1">One child.</param>
 /// <param name="child2">The other child.</param>
 public LeftistTree(T data, LeftistTree <T> child1, LeftistTree <T> child2)
 {
     _data = data;
     if (NullPathLength(child1) < NullPathLength(child2))
     {
         _rightChild = child1;
         _leftChild  = child2;
     }
     else
     {
         _rightChild = child2;
         _leftChild  = child1;
     }
     _nullPathLength = NullPathLength(_rightChild) + 1;
 }
Пример #3
0
 /// <summary>
 /// Merges the given heaps into one.
 /// </summary>
 /// <param name="h1">One heap.</param>
 /// <param name="h2">The other heap.</param>
 /// <returns></returns>
 private static LeftistTree <KeyValuePair <TPriority, TValue> > Merge(LeftistTree <KeyValuePair <TPriority, TValue> > h1,
                                                                      LeftistTree <KeyValuePair <TPriority, TValue> > h2)
 {
     if (h1 == null)
     {
         return(h2);
     }
     else if (h2 == null)
     {
         return(h1);
     }
     else
     {
         int compare = h1.Data.Key.CompareTo(h2.Data.Key);
         if (compare > 0)
         {
             return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h1.Data, h1.LeftChild, Merge(h1.RightChild, h2)));
         }
         else
         {
             return(new LeftistTree <KeyValuePair <TPriority, TValue> >(h2.Data, h2.LeftChild, Merge(h2.RightChild, h1)));
         }
     }
 }