/// <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);
        }
示例#2
0
 public BinaryHeap(MaxHeap <T> maxHeap) : this(maxHeap.GetData(), maxHeap.Length, HeapType.MaxHeap)
 {
 }