/// <summary> /// build a heap in O(n) /// </summary> /// <param name="source"></param> /// <returns></returns> public static MinHeap <T> BuildMinHeap(T[] source) { MinHeap <T> heap = new MinHeap <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); }
public BinaryHeap(MinHeap <T> minHeap) : this(minHeap.GetData(), minHeap.Length, HeapType.MinHeap) { }