static void Main(string[] args) { MaxHeap<int> maxHeap = new MaxHeap<int>(); maxHeap.Insert(100); maxHeap.Insert(33); maxHeap.Insert(66); }
private IEnumerable<Point> GetSkyLine(IEnumerable<Building> buildings) { var buildingsArray = buildings.ToArray(); var leftPoints = new Queue<Point>(buildingsArray.Select(item => new Point(item.Left, item.Height))); var rightPoints = new Queue<Point>(buildingsArray.Select(item => new Point(item.Right, item.Height)).OrderBy(item => item.X)); var heights = new MaxHeap<int>(int.MinValue); heights.Insert(0); int height = 0; while (leftPoints.Count != 0 || rightPoints.Count != 0) { bool takeLeftPoint = leftPoints.Count != 0 && leftPoints.Peek().X < rightPoints.Peek().X; int x; if (takeLeftPoint) { Point point = leftPoints.Dequeue(); x = point.X; heights.Insert(point.Y); } else { Point point = rightPoints.Dequeue(); x = point.X; heights.Remove(point.Y); } int maxHeight = heights.Maximum(); if (maxHeight != height) { height = maxHeight; yield return new Point(x, height); } } }
public void TestMinMaxHeap() { // given int[] a = { 1, 4, 6, 8, 10, 2, 5, 3, 0, 7 }; int[] sortedArray = { 0, 1, 2, 3, 4, 5 , 6 , 7, 8, 10 }; MaxHeap<int> maxh = new MaxHeap<int>(); MinHeap<int> minh = new MinHeap<int>(); // when for (int i = 0; i < a.Length; i++) { maxh.Push(a[i]); minh.Push(a[i]); } // then for (int i = 0; i < a.Length; i++) { int v = minh.Pop(); Assert.AreEqual(v, sortedArray[i]); } for (int i = a.Length - 1; i >= 0 ; i--) { int v = maxh.Pop(); Assert.AreEqual(v, sortedArray[i]); } }
public void RightTest() { Heap<int> heap = new MaxHeap<int>(this.testInts, true); Assert.Throws<IndexOutOfRangeException>(() => heap.GetRight(-1)); Assert.Throws<IndexOutOfRangeException>(() => heap.GetRight(this.count)); }
public void GetTest() { Heap<int> heap = new MaxHeap<int>(this.testInts, true); Assert.Throws<IndexOutOfRangeException>(() => heap[-1]); Assert.Throws<IndexOutOfRangeException>(() => heap[this.count]); }
public void ClearTest() { Heap<int> heap = new MaxHeap<int>(this.testInts, true); ((ICollection<int>)heap).Clear(); Assert.Throws<IndexOutOfRangeException>(() => heap[0]); Assert.True(heap.ExtractRoot() == 0); }
public void TestPop() { int[] heapArr = { 1, 2, 3, 4, 5, 6 }; MaxHeap <int> pq = new MaxHeap <int>(heapArr); Assert.AreEqual(6, pq.Pop()); Assert.AreEqual(5, pq.Pop()); Assert.AreEqual(4, pq.Peek()); }
public void MaxHeap_AddRange_CountOfTen() { List<int> testNumbers = GetTestNumbers(); HeapBase<int> heap = new MaxHeap<int>(); heap.AddRange(testNumbers); Assert.AreEqual(10, heap.Count); }
// 6 public static void D_ConstructingTheArray() { int t; string s = Console.ReadLine().Trim(); t = int.Parse(s); for (int j = 0; j < t; j++) { s = Console.ReadLine().Trim(); int n = int.Parse(s); int[] a = new int[n]; if (n == 1) { Console.WriteLine(1); continue; } MaxHeap <Interval> heap = new MaxHeap <Interval>(new IntervalComparer()); heap.Add(new Interval { l = 0, r = n - 1, len = n }); int counter = 1; while (heap.Count > 0) { Interval v = heap.ExtractDominating(); if (v.len < 1) { continue; } int ind = (v.l + v.r) / 2; a[ind] = counter; counter++; if (v.len > 1) { var v1 = new Interval { l = v.l, r = ind - 1 }; var v2 = new Interval { l = ind + 1, r = v.r }; v1.len = v1.r - v1.l + 1; v2.len = v2.r - v2.l + 1; heap.Add(v1); heap.Add(v2); } } Console.WriteLine(PrintList(a)); } }
private static MaxHeap <int> GetMaxHeapUpToX(int maxVal) { var p = new MaxHeap <int>(); for (int i = 1; i <= maxVal; i++) { p.Insert(i); } return(p); }
public void MaxHeap_Ctor_Throws_Error_For_Invalid_Arguments(int capacity) { var ctorEx = Assert.Throws <DdnDfException>(() => { var _ = new MaxHeap <int>(capacity); }); Assert.IsNotNull(ctorEx); Assert.IsTrue(ctorEx.ErrorCode.Equals(DdnDfErrorCode.ValueLessThanThreshold)); }
public void Insert_ThreeItem_HeapStructureIsCorrect() { MaxHeap <int> heap = new MaxHeap <int>(); heap.Insert(2); heap.Insert(1); heap.Insert(3); Assert.That(heap.ToArray(), Is.EqualTo(new [] { 3, 1, 2 })); }
public void TryGetValueShouldReturnFalse() { var heap = new MaxHeap <int, int>(); heap.Add(3, 5); int val; Assert.IsFalse(heap.TryGetValue(0, out val)); }
public void EnumeratorTest() { int[] collection = { 3, 1, 9, 15, 2 }; var heap = new MaxHeap <int>(collection); heap.Add(5); heap.ExtractMax(); Assert.IsTrue(heap.SetEquals(new[] { 1, 2, 3, 5, 9 })); }
public void TestEmptyHeap() { MaxHeap heap = new MaxHeap(0); Assert.True(heap.IsEmpty); Assert.True(heap.IsFull); Assert.Throws <HeapEmptyException>(() => heap.GetRoot()); Assert.Throws <HeapEmptyException>(() => heap.PopRoot()); Assert.Throws <HeapException>(() => heap.Add(1)); }
public void TestAdd_Performace_WorstCase() { MaxHeap <int> maxHeap = new MaxHeap <int>(); int items = 500000; for (int i = 0; i < items; i++) { maxHeap.Add(i); } }
public static void EntryPoint() { int[] input = { 3, 10, 9, 4, 399, 1, 49 }; //int[] input = { 1, 1, 1, 1, 1 }; MaxHeap heap = new MaxHeap(input); heap.SortByASC(); Console.WriteLine(heap.ToString()); }
public void ExtractMaxTest() { const int N = 100; var maxHeap = new MaxHeap<int>(Enumerable.Range(0, N)); for (int i = 0; i < N; i++) { maxHeap.ExtractMax().Should().Be(N - i - 1); } }
private void AddTest() { MaxHeap maxHeap = new MaxHeap(100); maxHeap.Add(4); maxHeap.Add(9); maxHeap.Add(5); Assert.AreEqual(3, maxHeap.Count); Assert.AreEqual() }
public void Insert01() { var person1 = new Person { Age = 1 }; var person2 = new Person { Age = 1 }; var maxheap = new MaxHeap<Person, int>(2, x => x.Age); maxheap.Insert(person1); maxheap.Insert(person2); Assert.AreEqual(person1, maxheap.Peek()); Assert.AreNotEqual(person2, maxheap.Peek()); }
// Use Prim's algorithm to generate a MST of edges. private IEnumerable <Edge> TrimEdges(ICollection <int>[] adjacency, IList <Room> rooms) { // Comparator for MapVertex is defined to give negated, so this is actually a minheap MaxHeap <MapVertex> pq = new MaxHeap <MapVertex>(rooms.Count); var(firstX, firstY) = rooms[0].Center; pq.Add(new MapVertex(0, firstX, firstY, 0)); bool[] inMst = new bool[rooms.Count]; double[] weight = new double[rooms.Count]; int[] parent = new int[rooms.Count]; for (int i = 0; i < rooms.Count; i++) { weight[i] = double.MaxValue; parent[i] = -1; } while (pq.Count > 0) { MapVertex min = pq.PopMax(); inMst[min.ID] = true; foreach (int neighborID in adjacency[min.ID]) { if (inMst[neighborID]) { continue; } var(neighborX, neighborY) = rooms[neighborID].Center; double newWeight = Distance.EuclideanDistanceSquared(min.X, min.Y, neighborX, neighborY); if (weight[neighborID] > newWeight) { weight[neighborID] = newWeight; pq.Add(new MapVertex(neighborID, neighborX, neighborY, newWeight)); parent[neighborID] = min.ID; } } } ICollection <Edge> graph = new HashSet <Edge>(); for (int i = 0; i < rooms.Count; i++) { if (parent[i] != -1) { graph.Add(new Edge(i, parent[i])); } } return(graph); }
/// <summary> /// Find median of 2 sorted arrays balancing heaps /// </summary> /// <param name="nums"></param> /// <returns></returns> public static double FindMedianSortedArrays(int[] nums1, int[] nums2) { var minHeap = new MinHeap <int>(); var maxHeap = new MaxHeap <int>(); // An integer from the array is first added to the minheap. int i = 0, j = 0; for (; i < nums1.Length && j < nums2.Length;) { int nextNum; if (nums1[i] > nums2[j]) { nextNum = nums2[j]; j++; } else { nextNum = nums1[i]; i++; } BalanceHeaps(minHeap, maxHeap, nextNum); } while (i < nums1.Length) { BalanceHeaps(minHeap, maxHeap, nums1[i]); i++; } while (j < nums2.Length) { BalanceHeaps(minHeap, maxHeap, nums2[j]); j++; } // In the end, the median is found by using the peek element from min-heap and peek element from the max-heap double median; if (minHeap.Count() == maxHeap.Count()) { median = (minHeap.Peek() + maxHeap.Peek()) / 2.0; } else if (minHeap.Count() > maxHeap.Count()) { median = minHeap.Peek(); } else { median = maxHeap.Peek(); } return(median); }
protected MaxHeap <T> GenericheapFactory(int count) { var heap = new MaxHeap <T>(); var seed = count * 34; for (var i = 0; i < count; i++) { heap.Push(CreateT(seed++)); } return(heap); }
public void TopElementInHeapsCanBeRead() { MinHeap <int> minHeap = new MinHeap <int>(IntComparer); MaxHeap <int> maxHeap = new MaxHeap <int>(IntComparer); int elem = 1; minHeap.Add(elem); Assert.AreEqual(elem, minHeap.Peek()); maxHeap.Add(elem); Assert.AreEqual(elem, maxHeap.Peek()); }
/// <summary> /// 弹出队头 /// </summary> public void Pop() { if (this.heapLength == 0) { throw new OverflowException("队列为空时不能出队"); } // 维护堆的属性 heapLength--; this.swap(0, heapLength); MaxHeap <T> .heapAdjustFromTop(this.buffer, 0, this.heapLength); }
public int PopMax(params int[] nums) { var sut = new MaxHeap(); foreach (var num in nums) { sut.Add(num); } return(sut.PopMax()); }
public void Max_Heap_Push_Test_1() { IBinaryHeap heap = new MaxHeap(); heap.Push(8); heap.Push(10); heap.Push(11); heap.Pop().Should().Be(11); heap.Pop().Should().Be(10); heap.Pop().Should().Be(8); }
private MaxHeap <int> ConstructMaxHeap(int[] target) { var maxHeap = new MaxHeap <int>(); foreach (var item in target) { maxHeap.Add(item); } return(maxHeap); }
public static void Main() { var stack = new Stack <int> (); int[][] buildings = { new int[] { 2, 92, 10 }, new int[] { 3, 7, 15 }, new int[] { 5, 12, 12 }, new int[] { 15, 20, 10 }, new int[] { 19, 24, 8 } }; MaxHeap <int> maxHeap = new MaxHeap <int> (); maxHeap.Push(0); maxHeap.Push(20); maxHeap.Push(10); maxHeap.Push(40); maxHeap.Push(60); var list = new List <int> (); Console.WriteLine("The 1st:"); foreach (var item in maxHeap) { Console.Write("{0} ", item); } Console.WriteLine(); maxHeap.Pop(); maxHeap.Pop(); maxHeap.Pop(); Console.WriteLine("The 2nd:"); list = maxHeap.ToAscendingList(); foreach (var item in list) { Console.Write("{0} ", item); } Console.WriteLine(); var array = new MinHeap <Node> (); //List<int> array = new List<int>(); for (int i = 0; i < 30; i++) { array.Push(new Node(i, i, i)); } int count = 0; foreach (Node item in array) { Console.WriteLine(item.val + ", count:" + ++count); } }
public void TestExtractAndCount() { var maxHeap = new MaxHeap<string>(); maxHeap.Add("Ivan"); maxHeap.Add("George"); var expected = "Pesho"; maxHeap.Add(expected); var actual = maxHeap.ExtractDominating(); Assert.AreEqual(expected, actual); Assert.AreEqual(2, maxHeap.Count); }
public void MaxHeap_ExtractRoot_ReturnsMax() { List<int> testNumbers = GetTestNumbers(); MaxHeap<int> heap = new MaxHeap<int>(); heap.AddRange(testNumbers); int expected = testNumbers.Max(); int max = heap.ExtractRoot(); Assert.AreEqual(expected, max); }
public void TestInsert() { var list = new List <int> { 1, 9, 3, 6, 5, 2, 7, 11, 9 }; var maxHeap = new MaxHeap(); list.ForEach(elem => maxHeap.Add(elem)); Assert.AreEqual(11, maxHeap.peekMax()); }
public void MaxHeapKeepFirst() { MaxHeap <int> max = new MaxHeap <int>(); for (int i = MAX_VALUE; i >= 1; i--) { max.Add(i); Assert.AreEqual <int>(MAX_VALUE, max.Peek()); Assert.AreEqual <int>(MAX_VALUE + 1 - i, max.Count); } }
private MaxHeap <int> getExampleHeap() { MaxHeap <int> h = new MaxHeap <int>(); h.Add(4); h.Add(5); h.Add(3); h.Add(1); h.Add(2); return(h); }
public void GetMaximum_AddThreeItem_ReturnsRightMaxValue() { MaxHeap <int> heap = new MaxHeap <int>(); heap.Insert(2); heap.Insert(1); heap.Insert(3); Assert.That(heap.GetMaximum(), Is.EqualTo(3)); Assert.That(heap.ToArray(), Is.EqualTo(new[] { 2, 1 })); }
private static void TestMaxHeap() { BaseHeap <double> maxHeap = new MaxHeap <double>(); foreach (var value in heapVaues) { maxHeap.AddValue(value); } Console.WriteLine(maxHeap); }
public void TestPeek_NonEmptyHeap() { MaxHeap <int> maxHeap = new MaxHeap <int>(); maxHeap.Add(5); maxHeap.Add(90); maxHeap.Add(15); maxHeap.Add(89); Assert.AreEqual(90, maxHeap.Peek()); }
/// <summary> /// Sort data using a heap sort /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data">Data to be sorted</param> public static IList <T> HeapSortAscending <T>(this IList <T> data) { IHeap <T> heap = new MaxHeap <T>(data); for (int i = heap.Count - 1; i > 0; i--) { heap.Pop(); } return(heap.ToList()); }
public void GetMaxTest() { var heap = new MaxHeap <int, int>(); heap.Add(42, -1); heap.Add(5, 6); var max = heap.Max; Assert.AreEqual(5, max.Key); Assert.AreEqual(6, max.Value); }
public void MaxHeapSortTest() { List <int> list = Utility.GenerateRandomList(10000000, 0, 10000000); MaxHeap <int> maxheap = new MaxHeap <int>(list); for (int i = list.Count - 1; i >= 0; i--) { list[i] = maxheap.ExtractMax(); } Assert.IsTrue(Utility.IsSorted(list), "The list is not sorted."); }
public static void Main(string[] args) { int[] container = new int[] { 1, 9, 2, 6, 7, 3, 4, 0, 5, 8 }; MaxHeap heap = new MaxHeap(container); heap.heapSort(); for (int i = 0; i < heap.size; i++) { Console.WriteLine(heap.heap[i]); } }
public void TestHeapBySortingInts() { var minHeap = new MinHeap<int>(new[] { 9, 8, 4, 1, 6, 2, 7, 4, 1, 2 }); AssertHeapSortInts(minHeap, minHeap.OrderBy(i => i).ToArray()); minHeap = new MinHeap<int> { 7, 5, 1, 6, 3, 2, 4, 1, 2, 1, 3, 4, 7 }; AssertHeapSortInts(minHeap, minHeap.OrderBy(i => i).ToArray()); var maxHeap = new MaxHeap<int>(new[] { 1, 5, 3, 2, 7, 56, 3, 1, 23, 5, 2, 1 }); AssertHeapSortInts(maxHeap, maxHeap.OrderBy(d => -d).ToArray()); maxHeap = new MaxHeap<int> { 2, 6, 1, 3, 56, 1, 4, 7, 8, 23, 4, 5, 7, 34, 1, 4 }; AssertHeapSortInts(maxHeap, maxHeap.OrderBy(d => -d).ToArray()); }
public void HeapWorks() { var heap = new MaxHeap<int>(); heap.Add(5); heap.Add(6); heap.Add(4); heap.Add(1); heap.Add(-1); Assert.AreEqual(6, heap.Remove()); Assert.AreEqual(5, heap.Remove()); Assert.AreEqual(4, heap.Remove()); Assert.AreEqual(1, heap.Remove()); Assert.AreEqual(-1, heap.Remove()); }
public void ExtractRootTest() { Heap<int> heap = new MaxHeap<int>(this.testInts, true); int[] sortedInts = new int[this.count]; this.testInts.CopyTo(sortedInts, 0); Array.Sort(sortedInts); for (int i = this.count - 1; 0 <= i; i--) { Assert.True(sortedInts[i] == heap.ExtractRoot()); } Assert.True(heap.ExtractRoot() == 0); }
public PotentialField( NavigationField navigationField, Target target) { Size = navigationField.fieldSize; NavigationField = navigationField; Target = target; Potentials = new FloatField(navigationField.fieldSize); Flows = new Vector2Field(navigationField.fieldSize); _heap = new MaxHeap<CellPotentialHeapEntry> { RemoveAction = CellPotentialHeapEntry.ReturnCellCostHeapEntry }; }
public static IEnumerable<int> GetMedians(IEnumerable<int> sequence) { var enumerator = sequence.GetEnumerator(); var smallestNumbers = new MaxHeap<int>(); var largestNumbers = new MinHeap<int>(); if (enumerator.MoveNext()) { var number = enumerator.Current; smallestNumbers.Add(number); yield return smallestNumbers.Max; } while (enumerator.MoveNext()) { var number = enumerator.Current; if (smallestNumbers.Count <= largestNumbers.Count) { if (number > largestNumbers.Min) { var min = largestNumbers.ExtractMin(); smallestNumbers.Add(min); largestNumbers.Add(number); } else { smallestNumbers.Add(number); } } else { if (number < smallestNumbers.Max) { var max = smallestNumbers.ExtractMax(); largestNumbers.Add(max); smallestNumbers.Add(number); } else { largestNumbers.Add(number); } } yield return smallestNumbers.Max; } }
public void Delete01() { var minheap = new MinHeap<int>(2); Assert.AreEqual(0, minheap.Count()); minheap.Insert(1); minheap.Insert(0); Assert.Throws<InvalidOperationException>(() => { minheap.Insert(-1); }); Assert.AreEqual(0, minheap.Delete()); Assert.AreEqual(1, minheap.Count()); var maxheap = new MaxHeap<int>(2); Assert.AreEqual(0, maxheap.Count()); maxheap.Insert(0); maxheap.Insert(1); Assert.Throws<InvalidOperationException>(() => { maxheap.Insert(-1); }); Assert.AreEqual(1, maxheap.Delete()); Assert.AreEqual(1, maxheap.Count()); }
public void Displace01() { var minheap = new MinHeap<int>(2); Assert.AreEqual(0, minheap.Count()); minheap.Insert(5); minheap.Insert(4); Assert.AreEqual(4, minheap.Displace(6)); Assert.AreEqual(2, minheap.Count()); Assert.AreEqual(5, minheap.Peek()); var maxheap = new MaxHeap<int>(2); Assert.AreEqual(0, maxheap.Count()); maxheap.Insert(3); maxheap.Insert(4); Assert.AreEqual(4, maxheap.Displace(2)); Assert.AreEqual(2, maxheap.Count()); Assert.AreEqual(3, maxheap.Peek()); }
public void BuildMinHeap() { List<int> items = new List<int>() { 11,28,314,3,156,561,401,359,271 }; var minHeap = new MinHeap<int>(items); Assert.AreEqual(3, minHeap.GetTop()); var itemRemoved = minHeap.ExtractDominating(); Assert.AreEqual(3, itemRemoved); Assert.AreEqual(11, minHeap.GetTop()); var maxHeap = new MaxHeap<int>(items); Assert.AreEqual(561, maxHeap.GetTop()); itemRemoved = maxHeap.ExtractDominating(); Assert.AreEqual(561, itemRemoved); Assert.AreEqual(401, maxHeap.GetTop()); }
static void Main(string[] args) { int[] array = { 10, 35, 7, 11, 15, 21, 3, 9, 23, 71, 2, 0, 6 }; //int[] array = { 6, 5, 3, 1, 8, 7, 2, 4 }; MaxHeap<int> heap = new MaxHeap<int>(array); foreach (int item in heap) { Console.WriteLine(item); } Console.WriteLine("========================="); heap.Sort(); foreach (int item in heap) { Console.WriteLine(item); } Console.WriteLine("====================="); heap.Remove(21); foreach (int item in heap) { Console.WriteLine(item); } }
public void ContainsTest() { ICollection<int> heap = new MaxHeap<int>(this.testInts, true); for (int i = this.count - 1; 0 <= i; i--) { Assert.True(heap.Contains(this.testInts[i])); } Assert.False(heap.Contains(-1)); }
public void Peek01() { var minheap = new MinHeap<int>(2); Assert.Throws<InvalidOperationException>(() => { minheap.Peek(); }); minheap.Insert(0); Assert.AreEqual(0, minheap.Peek()); var maxheap = new MaxHeap<int>(2); Assert.Throws<InvalidOperationException>(() => { maxheap.Peek(); }); maxheap.Insert(0); Assert.AreEqual(0, maxheap.Peek()); }
private static void TestMaxSort(MaxHeap<int> heap, string error) { for (int index = 0; index < heap.Count - 1; index++) { Debug.Assert(heap[index] <= heap[index + 1], error); } }
public void GetEnumeratorTest() { MaxHeap<int> heap = new MaxHeap<int>(this.testInts, true); int i = 0; foreach (int item in (ICollection<int>)heap) { Assert.True(item == heap[i++]); } }
public void Insert02() { var minheap = new MinHeap<int>(3); Assert.AreEqual(0, minheap.Count()); minheap.Insert(0); Assert.AreEqual(1, minheap.Count()); minheap.Insert(0); Assert.AreEqual(2, minheap.Count()); minheap.Insert(2); Assert.AreEqual(3, minheap.Count()); var maxheap = new MaxHeap<int>(3); Assert.AreEqual(0, maxheap.Count()); maxheap.Insert(0); Assert.AreEqual(1, maxheap.Count()); maxheap.Insert(0); Assert.AreEqual(2, maxheap.Count()); maxheap.Insert(1); Assert.AreEqual(3, maxheap.Count()); }
public void CopyToTest() { ICollection<int> heap = new MaxHeap<int>(this.testInts, true); int[] copy = new int[this.count]; heap.CopyTo(copy, 0); for (int i = 0; i < this.count; i++) { Assert.True(copy[i] == ((MaxHeap<int>)heap)[i]); } Assert.Throws<ArgumentNullException>(() => heap.CopyTo(null, 0)); Assert.Throws<ArgumentOutOfRangeException>(() => heap.CopyTo(copy, -1)); Assert.Throws<ArgumentException>(() => heap.CopyTo(copy, 1)); }
public void MaxHeap_Sorts_NegativeAndPositiveIntegers_HighToLow() { MaxHeap<int> integerMaxHeap = new MaxHeap<int>(); integerMaxHeap.Push(150); integerMaxHeap.Push(2); integerMaxHeap.Push(50); integerMaxHeap.Push(1); integerMaxHeap.Push(0); integerMaxHeap.Push(-150); integerMaxHeap.Push(-2); integerMaxHeap.Push(-50); integerMaxHeap.Push(-1); integerMaxHeap.Push(0); Assert.IsTrue(ValidateIntMaxHeapSort(integerMaxHeap)); }
public void MaxHeap_Sorts_EqualIntegers_HighToLow() { MaxHeap<int> integerMaxHeap = new MaxHeap<int>(); integerMaxHeap.Push(150); integerMaxHeap.Push(50); integerMaxHeap.Push(50); integerMaxHeap.Push(1); integerMaxHeap.Push(0); Assert.IsTrue(ValidateIntMaxHeapSort(integerMaxHeap)); }
/// <summary> /// Pops elements off heap and validates elements in increasing value. /// </summary> /// <param name="integerMinHeap">Integer MinHeap</param> /// <returns>true if sorted</returns> public bool ValidateIntMaxHeapSort(MaxHeap<int> integerMinHeap) { while (integerMinHeap.Size > 0) { int higher = integerMinHeap.Pop(); if (higher < integerMinHeap.Peek()) return false; } return true; }
private static void TestMinMaxHeap() { int[] a = { 1, 4, 6, 8, 10, 2, 5, 3, 0, 7 }; MaxHeap<int> maxh = new MaxHeap<int>(); MinHeap<int> minh = new MinHeap<int>(); for (int i = 0; i < a.Length; i++) { maxh.Push(a[i]); minh.Push(a[i]); } DisplayArray(maxh.ToArray()); DisplaySeparator(); for (int i = 0; i < a.Length; i++) { int v = maxh.Pop(); System.Console.Write("Pop {0} : ", v); DisplayArray(maxh.ToArray()); System.Console.WriteLine(""); } DisplayArray(maxh.ToArray()); DisplaySeparator(); for (int i = 0; i < a.Length; i++) { int v = minh.Pop(); System.Console.Write("Pop {0} : ", v); DisplayArray(minh.ToArray()); System.Console.WriteLine(""); } DisplayArray(minh.ToArray()); DisplaySeparator(); }