Exemple #1
0
        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);
            }
        }
Exemple #2
0
        public void Add(int val)
        {
            if (Length % 2 == 0)
            {
                UpperHeap.Add(val);
            }
            else
            {
                LowerHeap.Add(val);
            }

            Balance();
        }