Пример #1
0
        public FibonacciNode ExtractMin()
        {
            FibonacciNode currentMin = MinNode;

            if (currentMin == null)
            {
                return(null);
            }
            if (currentMin.Child != null)
            {
                List <FibonacciNode> childrenOfMin = Iterate(currentMin.Child);
                foreach (FibonacciNode child in childrenOfMin)
                {
                    MergeWithRootList(child);
                    child.Parent = null;
                }
            }
            RemoveFromRootList(currentMin);
            if (currentMin == currentMin.Right)
            {
                MinNode = RootList = null;
            }
            else
            {
                MinNode = currentMin.Right;
                Consolidate();
            }
            numOfNodes--;
            return(currentMin);
        }
Пример #2
0
        private void Consolidate()
        {
            List <FibonacciNode> helperList = new List <FibonacciNode>(numOfNodes);

            for (int i = 0; i < numOfNodes; i++)
            {
                helperList.Add(null);
            }
            List <FibonacciNode> rootNodes = Iterate(RootList);

            for (int i = 0; i < rootNodes.Count; i++)
            {
                int d = rootNodes[i].Degree;
                while (helperList[d] != null)
                {
                    if (rootNodes[i].Key > helperList[d].Key)
                    {
                        FibonacciNode temp = rootNodes[i];
                        rootNodes[i]  = helperList[d];
                        helperList[d] = temp;
                    }
                    HeapLink(helperList[d], rootNodes[i]);
                    helperList[d] = null;
                    d++;
                }
                helperList[d] = rootNodes[i];
            }
            foreach (FibonacciNode singleNode in helperList)
            {
                if (singleNode != null && singleNode.Key < MinNode.Key)
                {
                    MinNode = singleNode;
                }
            }
        }
Пример #3
0
 private void Cut(FibonacciNode childNode, FibonacciNode parentNode)
 {
     RemoveFromChildList(parentNode, childNode);
     parentNode.Degree--;
     MergeWithRootList(childNode);
     childNode.Parent   = null;
     childNode.IsMarked = false;
 }
Пример #4
0
 private void RemoveFromRootList(FibonacciNode node)
 {
     if (node == RootList)
     {
         RootList = node.Right;
     }
     node.Left.Right = node.Right;
     node.Right.Left = node.Left;
 }
Пример #5
0
 private void HeapLink(FibonacciNode firstNode, FibonacciNode secondNode)
 {
     RemoveFromRootList(firstNode);
     firstNode.Left = firstNode.Right = firstNode;
     MergeWithChildList(secondNode, firstNode);
     secondNode.Degree++;
     firstNode.Parent   = secondNode;
     firstNode.IsMarked = false;
 }
Пример #6
0
 private void MergeWithRootList(FibonacciNode node)
 {
     if (RootList == null)
     {
         RootList = node;
         return;
     }
     node.Right          = RootList.Right;
     node.Left           = RootList;
     RootList.Right.Left = node;
     RootList.Right      = node;
 }
Пример #7
0
        public FibonacciNode Insert(int newKey)
        {
            FibonacciNode newNode = new FibonacciNode(newKey);

            newNode.Left = newNode.Right = newNode;
            MergeWithRootList(newNode);
            if (MinNode == null || newNode.Key < MinNode.Key)
            {
                MinNode = newNode;
            }
            numOfNodes++;
            return(newNode);
        }
Пример #8
0
 private void RemoveFromChildList(FibonacciNode parentNode, FibonacciNode childNode)
 {
     if (parentNode.Child == parentNode.Child.Right)
     {
         parentNode.Child = null;
     }
     else if (parentNode.Child == childNode)
     {
         parentNode.Child       = childNode.Right;
         childNode.Right.Parent = parentNode;
     }
     childNode.Left.Right = childNode.Right;
     childNode.Right.Left = childNode.Left;
 }
Пример #9
0
 private void MergeWithChildList(FibonacciNode parentNode, FibonacciNode node)
 {
     if (parentNode.Child != null)
     {
         node.Right = parentNode.Child.Right;
         node.Left  = parentNode.Child;
         parentNode.Child.Right.Left = node;
         parentNode.Child.Right      = node;
     }
     else
     {
         parentNode.Child = node;
     }
 }
Пример #10
0
        public List <FibonacciNode> Iterate(FibonacciNode head)
        {
            if (head == null)
            {
                return(new List <FibonacciNode>());
            }
            List <FibonacciNode> iteratedNodes = new List <FibonacciNode>();
            FibonacciNode        currentNode, endNode;

            currentNode = endNode = head;
            do
            {
                iteratedNodes.Add(currentNode);
                currentNode = currentNode.Right;
            } while (currentNode != endNode);
            return(iteratedNodes);
        }
Пример #11
0
        private void CascadeCut(FibonacciNode currentNode)
        {
            FibonacciNode parentNode = currentNode.Parent;

            if (parentNode == null)
            {
                return;
            }
            if (parentNode.IsMarked)
            {
                Cut(currentNode, parentNode);
                CascadeCut(parentNode);
            }
            else
            {
                currentNode.IsMarked = true;
            }
        }
Пример #12
0
        public void DecreaseKey(FibonacciNode nodeToChange, int key)
        {
            if (key > nodeToChange.Key)
            {
                return;
            }
            nodeToChange.Key = key;
            FibonacciNode parent = nodeToChange.Parent;

            if (parent != null && nodeToChange.Key < parent.Key)
            {
                Cut(nodeToChange, parent);
                CascadeCut(parent);
            }
            if (nodeToChange.Key < MinNode.Key)
            {
                MinNode = nodeToChange;
            }
        }
Пример #13
0
        public FibonacciHeap Merge(FibonacciHeap heapToMerge)
        {
            FibonacciHeap newHeap = new FibonacciHeap();

            newHeap.RootList = RootList;
            newHeap.MinNode  = MinNode;
            FibonacciNode lastNode = heapToMerge.RootList.Left;

            heapToMerge.RootList.Left   = newHeap.RootList.Left;
            newHeap.RootList.Left.Right = heapToMerge.RootList;
            newHeap.RootList.Left       = lastNode;
            newHeap.RootList.Left.Right = newHeap.RootList;
            if (heapToMerge.MinNode.Key < newHeap.MinNode.Key)
            {
                newHeap.MinNode = heapToMerge.MinNode;
            }
            newHeap.numOfNodes = numOfNodes + heapToMerge.numOfNodes;
            return(newHeap);
        }
Пример #14
0
 public void Delete(FibonacciNode nodeToDelete)
 {
     DecreaseKey(nodeToDelete, int.MinValue);
     ExtractMin();
 }
Пример #15
0
 public FibonacciHeap()
 {
     RootList   = MinNode = null;
     numOfNodes = 0;
 }