private static TimeSpan testHeap(int[] testData, bool isHeapify) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); MaxHeap <int> maxHeap; if (isHeapify) { maxHeap = new MaxHeap <int>(testData); } else { maxHeap = new MaxHeap <int>(); for (int i = 0; i < testData.Length; i++) { maxHeap.add(testData[i]); } } watch.Stop(); //验证堆的正确性 int[] arr = new int[testData.Length]; for (int i = 0; i < testData.Length; i++) { arr[i] = maxHeap.extractMax(); } for (int i = 1; i < testData.Length; i++) { if (arr[i - 1] < arr[i]) { throw new IndexOutOfRangeException("Error."); } } Console.WriteLine("您的最大堆可以了亲!"); TimeSpan timespan = watch.Elapsed; return(timespan); }
public void enqueue(T element) { maxHeap.add(element); }