예제 #1
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
     {
         _leftChild  = child1;
         _rightChild = child2;
     }
     _nullPathLength = 1 + NullPathLength(_rightChild);
 }
예제 #2
0
 /// <summary>
 /// Merges the given min-heaps into one.
 /// </summary>
 /// <param name="h1">One min-heap.</param>
 /// <param name="h2">The other min-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
     {
         LeftistTree <KeyValuePair <TPriority, TValue> > smaller = h1;
         LeftistTree <KeyValuePair <TPriority, TValue> > larger  = h2;
         if (h2.Data.Key.CompareTo(h1.Data.Key) < 0)
         {
             smaller = h2;
             larger  = h1;
         }
         return(new LeftistTree <KeyValuePair <TPriority, TValue> >(smaller.Data, smaller.LeftChild, Merge(smaller.RightChild, larger)));
     }
 }