private void Balance() { if (!LowerHeap.IsEmpty && !UpperHeap.IsEmpty && LowerHeap.Peek() > UpperHeap.Peek()) { int lowerHead = LowerHeap.Poll(); int upperHead = UpperHeap.Poll(); LowerHeap.Add(upperHead); UpperHeap.Add(lowerHead); } }
public void Add(int val) { if (Length % 2 == 0) { UpperHeap.Add(val); } else { LowerHeap.Add(val); } Balance(); }