Пример #1
0
        public void RemoveChild(FibNode node)
        {
            if (node.parent != this)
            {
                Console.WriteLine("Cannot remove child from a node that is not its parent");
            }

            if (node.IsSingle())
            {
                if (child != node)
                {
                    Console.WriteLine("Cannot remove a node that is not a child");
                }

                child = null;
            }
            else
            {
                if (child == node)
                {
                    child = node.next;
                }
                node.Remove();
            }

            node.parent = null;
            node.mark   = false;
            degree--;
        }
Пример #2
0
        public FibNode RemoveMinimum()
        {
            if (minNode == null)
            {
                Console.WriteLine("Boi I can't remove from an empty heap");
            }
            else
            {
                FibNode removeNode = minNode;
                count--;

                //Assign all old root children as new roots
                if (minNode.child != null)
                {
                    FibNode c = minNode.child;

                    while (true)
                    {
                        c.parent = null;
                        c        = c.next;
                        if (c == minNode.child)
                        {
                            break;
                        }
                    }

                    minNode.child = null;
                    minNode.Insert(c);
                }

                //Check if the last key has been removed
                if (minNode.next == minNode)
                {
                    if (count != 0)
                    {
                        //Expected 0 keys
                    }

                    minNode = null;
                    return(removeNode);
                }

                int       logsize     = 100;
                FibNode[] degreeRoots = new FibNode[logsize];
                maxDegree = 0;
                FibNode currentPointer = minNode.next;

                while (true)
                {
                    int     currentDegree = currentPointer.degree;
                    FibNode current       = currentPointer;
                    currentPointer = currentPointer.next;
                    while (degreeRoots[currentDegree] != null)
                    {
                        FibNode other = degreeRoots[currentDegree];
                        if (current.key > other.key)
                        {
                            FibNode temp = other;
                            other   = current;
                            current = temp;
                        }

                        other.Remove();
                        current.AddChild(other);
                        degreeRoots[currentDegree] = null;
                        currentDegree++;
                    }

                    degreeRoots[currentDegree] = current;
                    if (currentPointer == minNode)
                    {
                        break;
                    }
                }

                //Remove current root and find new minnode
                minNode = null;
                var newMaxDegree = 0;

                for (int d = 0; d < logsize; d++)
                {
                    if (degreeRoots[d] != null)
                    {
                        degreeRoots[d].next = degreeRoots[d].previous = degreeRoots[d];
                        InsertNode(degreeRoots[d]);
                        if (d > newMaxDegree)
                        {
                            newMaxDegree = d;
                        }
                    }
                }

                maxDegree = newMaxDegree;
                return(removeNode);
            }

            return(null);
        }