Ejemplo n.º 1
0
    public void Enqueue(T item)
    {
        var heap = new PairingHeap <T>(item);

        top = PairingHeap <T> .Merge(top, heap, compare);

        size++;
    }
Ejemplo n.º 2
0
 void MergePairs(List <PairingHeap <T> > l)
 {
     if (l.Count == 0)
     {
         return;
     }
     else if (l.Count == 1)
     {
         if (root == null)
         {
             root = l[0].root;
         }
         else
         {
             root.item     = l[0].root.item;
             root.priority = l[0].root.priority;
         }
         children = l[0].children;
     }
     else
     {
         PairingHeap <T> aux = new PairingHeap <T>();
         aux.Merge(l[0]);
         aux.Merge(l[1]);
         PairingHeap <T> aux2 = new PairingHeap <T>();
         aux2.MergePairs(l.GetRange(2, l.Count - 2));
         aux.Merge(aux2);
         if (root == null)
         {
             root = aux.root;
         }
         else
         {
             root.item     = aux.root.item;
             root.priority = aux.root.priority;
         }
         children = aux.children;
     }
 }
Ejemplo n.º 3
0
    bool UpdateKeyInternal(T item, int newPrio, PairingHeap <T> originalHeap)
    {
        bool equalsRoot = item.Equals(root.item);

        if (equalsRoot && (newPrio < root.priority))
        {
            root.priority = newPrio;
            return(true);
        }
        bool stop = false;

        for (int i = 0; !stop && i < children.Count; ++i)
        {
            equalsRoot = children[i].root.item.Equals(item);
            if (equalsRoot)
            {
                if (newPrio >= children[i].root.priority)
                {
                    return(true);
                }
                PairingHeap <T> child = children[i];
                child.root.priority = newPrio;
                if (child.root.priority < root.priority)
                {
                    children.Remove(child);
                    originalHeap.Merge(child);
                }
                return(true);
            }
            else
            {
                stop = children[i].UpdateKeyInternal(item, newPrio, originalHeap);
            }
        }
        return(false);
    }