예제 #1
0
        private static void SwapChild(HeapNode <T> H)
        {
            var temp = H.LeftChild;

            H.LeftChild  = H.RightChild;
            H.RightChild = temp;
        }
예제 #2
0
        public static HeapNode <T> Marge(HeapNode <T> H1, HeapNode <T> H2)
        {
            if (H1 == null)
            {
                return(H2);
            }

            if (H2 == null)
            {
                return(H1);
            }

            if (H1.Element.CompareTo(H2.Element) > 0)
            {
                SwapNode(ref H1, ref H2);
            }

            if (H1.LeftChild == null)
            {
                H1.LeftChild = H2;
            }
            else
            {
                H1.RightChild = Marge(H1.RightChild, H2);
                if (H1.LeftChild.NPL < H1.RightChild.NPL)
                {
                    SwapChild(H1);
                }
                H1.NPL = H1.RightChild.NPL + 1;
            }


            return(H1);
        }
예제 #3
0
        private static void SwapNode(ref HeapNode <T> node1, ref HeapNode <T> node2)
        {
            var temp = node1;

            node1 = node2;
            node2 = temp;
        }
예제 #4
0
        public T RemoveMin()
        {
            if (IsEmpty)
            {
                throw new Exception("heap is empty.");
            }
            var min = Root.Element;

            Root = HeapNode <T> .Marge(Root.LeftChild, Root.RightChild);

            return(min);
        }
예제 #5
0
        public void Insert(T element)
        {
            var node = new HeapNode <T>(element);

            Root = HeapNode <T> .Marge(Root, node);
        }
예제 #6
0
 public void MargeWith(LeftHeap <T> other)
 {
     Root = HeapNode <T> .Marge(Root, other.Root);
 }