Exemplo n.º 1
0
        public static LeftistHeap heapify(int[] A)
        {
            Queue <Node> queue = new Queue <Node>();

            for (int i = 0; i < A.Length; i++)
            {
                queue.Enqueue(new Node(A[i]));
            }

            int size = queue.Count;

            while (queue.Count > 1)
            {
                for (int i = 0; i < size; i++)
                {
                    if (queue.Count == 1)
                    {
                        return(new LeftistHeap(queue.Dequeue()));
                    }

                    Node n1 = queue.Dequeue();
                    Node n2 = queue.Dequeue();

                    queue.Enqueue(LeftistHeap.merge(n1, n2));
                }
            }

            return(new LeftistHeap(queue.Dequeue()));
        }
Exemplo n.º 2
0
        public static int[] sort(ref int[] A)
        {
            LeftistHeap heap = heapify(A);

            for (int i = 0; i < A.Length; i++)
            {
                A[i] = heap.extract_min();
            }
            return(A);
        }
Exemplo n.º 3
0
 public void merge(LeftistHeap h)
 {
     root   = merge(root, h.root);
     h.root = null;
 }
Exemplo n.º 4
0
 public void merge(LeftistHeap h)
 {
     root = merge(root, h.root);
     h.root = null;
 }