Ejemplo n.º 1
0
        /// <summary>
        /// build a heap in O(n)
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static MaxHeap <T> BuildMaxHeap(T[] source)
        {
            MaxHeap <T> heap = new MaxHeap <T>();

            if (source == null || source.Length == 0)
            {
                heap.heapSize = 0;
                return(heap);
            }
            if (source.Length == 1)
            {
                heap.arr[0]   = source[0];
                heap.heapSize = source.Length;
                return(heap);
            }
            heap.heapSize = source.Length;
            heap.arr      = source;

            for (int i = heap.FirstLeaf() - 1; i >= 0; i--)
            {
                heap.Sink(i);
            }
            return(heap);
        }
Ejemplo n.º 2
0
 public BinaryHeap(MaxHeap <T> maxHeap) : this(maxHeap.GetData(), maxHeap.Length, HeapType.MaxHeap)
 {
 }