示例#1
0
        private void button10_Click(object sender, EventArgs e)
        {
            MaxHeap <int> maxheap = new MaxHeap <int>(new KarthicMaxHeapComparer());

            //array 2,4,1,6,3
            //max heap 6,4,3,2,1

            maxheap.Insert(2);
            maxheap.Insert(4);
            maxheap.Insert(1);
            maxheap.Insert(6);
            maxheap.Insert(3);

            maxheap.PopRoot();
            maxheap.PopRoot();

            maxheap.Insert(5);

            StringBuilder sb = new StringBuilder();

            while (maxheap.Size() != 0)
            {
                sb.Append(maxheap.PopRoot()).Append(",");
            }



            string result = sb.ToString();
        }
示例#2
0
 public void InsertRandomElement(int randomvalue, MaxHeap <int> maxheap, MinHeap <int> minheap)
 {
     if (maxheap.Size() == minheap.Size())
     {
         if (minheap.Size() > 0 && randomvalue > minheap.Peek())
         {
             //if the coming random value is greater than the minheap peek
             maxheap.Insert(minheap.PopRoot());
             minheap.Insert(randomvalue);
         }
         else
         {
             maxheap.Insert(randomvalue);
         }
     }
     else
     {
         //if the Size() is not equal means the max heap will be bigger
         if (randomvalue < maxheap.Peek())
         {
             minheap.Insert(maxheap.PopRoot());
             maxheap.Insert(randomvalue);
         }
         else
         {
             minheap.Insert(randomvalue);
         }
     }
 }
示例#3
0
 public double  CalculateMedian(MaxHeap <int> maxheap, MinHeap <int> minheap)
 {
     //check for empty..if maxheap is empty then minheap will also be empty
     if (maxheap.Size() == 0)
     {
         return(0);
     }
     if (maxheap.Size() == minheap.Size())
     {
         //mean there are even number of item..median = (sum of middle two items)/2
         return(Convert.ToDouble((maxheap.Peek() + minheap.Peek()) / 2));
     }
     else
     {
         return(maxheap.Peek());
     }
 }