Ejemplo n.º 1
0
 public void removechild(HeapNode node)
 {
     if (node.parent != this)
     {
         throw new Exception("Cannot remove child from node that is not it's parent");
     }
     if (node.issingle())
     {
         if (child != node)
         {
             throw new Exception("Cannot remove node that is not a child");
         }
         child = null;
     }
     else
     {
         if (child == node)
         {
             child = node.next;
         }
         node.remove();
     }
     node.parent = null;
     node.mark   = false;
     degree--;
 }
Ejemplo n.º 2
0
        public void removeminimum()
        {
            if (minnode == null)
            {
                throw new Exception("Cannot remove from empty heap");
            }
            count--;
            if (minnode.child != null)
            {
                HeapNode c = minnode.child;
                while (true)
                {
                    c.parent = null;
                    c        = c.next;
                    if (c == minnode.child)
                    {
                        break;
                    }
                }
                minnode.child = null;
                minnode.insert(c);
            }
            if (minnode.next == minnode)
            {
                if (count != 0)
                {
                    throw new Exception("Heap error: Expected 0 keys. Count is " + count);
                }
                minnode = null;
                return;
            }
            int logsize = 100;

            HeapNode[] degreeroots = new HeapNode[logsize];
            maxdegree = 0;
            HeapNode currentpointer = minnode.next;

            while (true)
            {
                int      currentdegree = currentpointer.degree;
                HeapNode current       = currentpointer;
                currentpointer = currentpointer.next;
                while (degreeroots[currentdegree] != null)
                {
                    HeapNode other = degreeroots[currentdegree];
                    if (current.key > other.key)
                    {
                        HeapNode temp = other;
                        other   = current;
                        current = temp;
                    }
                    other.remove();
                    current.addchild(other);
                    degreeroots[currentdegree] = null;
                    currentdegree++;
                }
                degreeroots[currentdegree] = current;
                if (currentpointer == minnode)
                {
                    break;
                }
            }
            minnode = null;
            int newmaxdegree = 0;

            for (int i = 0; i < logsize; i++)
            {
                if (degreeroots[i] != null)
                {
                    degreeroots[i].next = degreeroots[i].previous = degreeroots[i];
                    this._insertnode(degreeroots[i]);
                    if (i > newmaxdegree)
                    {
                        newmaxdegree = i;
                    }
                }
            }
            maxdegree = newmaxdegree;
        }