示例#1
0
        /// <summary>
        ///     union
        /// </summary>
        /// <param name="heap"></param>
        public void Union(MaxFibonacciHeap <K, V> heap)
        {
            MaxFibonacciHeap <K, V> h = Union(this, heap);

            Count   = h.Count;
            maxRoot = h.maxRoot;
        }
示例#2
0
        /// <summary>
        ///     union
        ///     It runs at O(1)
        /// </summary>
        /// <param name="h1"></param>
        /// <param name="h2"></param>
        /// <returns></returns>
        public static MaxFibonacciHeap <K, V> Union(MaxFibonacciHeap <K, V> h1, MaxFibonacciHeap <K, V> h2)
        {
            if (h1 == null || h1.maxRoot == null)
            {
                return(h2);
            }
            if (h2 == null || h2.maxRoot == null)
            {
                return(h1);
            }

            var heap = new MaxFibonacciHeap <K, V>();

            heap.maxRoot = h1.maxRoot;
            if (h1.maxRoot.Key.CompareTo(h2.maxRoot.Key) < 0)
            {
                heap.maxRoot = h2.maxRoot;
            }

            //concatenate the root list of H1 and H2
            FibonacciHeapNode <K, V> h1Node = h1.maxRoot.RightSibling;
            FibonacciHeapNode <K, V> h2Node = h2.maxRoot.RightSibling;

            h1.maxRoot.RightSibling = h2Node;
            h2Node.LeftSibling      = h1.maxRoot;
            h2.maxRoot.RightSibling = h1Node;
            h1Node.LeftSibling      = h2.maxRoot;

            heap.Count = h1.Count + h2.Count;
            return(heap);
        }