示例#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);
         }
     }
 }
        public int FindthekthLargestElementInMatrix(int[,] matrix, int k)
        {
            MaxHeap <MatrixElement> myheap = new MaxHeap <MatrixElement>(new MaxMatrixElementComparer());

            //add last row to max heap//all col in first row
            int lastrow = matrix.GetLength(1) - 1;

            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                myheap.Insert(new MatrixElement(matrix[lastrow, col], lastrow, col));
            }

            // to find kth smallest..iterate k times
            for (int i = 1; i < k; i++)
            {
                //get the min
                MatrixElement maxelement = myheap.Peek();
                //Find the next element in the next row of the same colmumn
                //check the next row is not ourside bounds
                if (maxelement.Row - 1 >= 0)
                {
                    MatrixElement nextelement = new MatrixElement(matrix[maxelement.Row - 1, maxelement.Col], maxelement.Row - 1, maxelement.Col);

                    //we can update the heap root element and then call minheapify of the root
                    //my heap fon't prov that so
                    myheap.PopRoot();
                    myheap.Insert(nextelement); //this will make sure to have the smallest
                }
                else
                {
                    myheap.PopRoot();
                }
            }

            return(myheap.Peek().Data);
        }
        //Logic:
        //Create maxheap to store max value of the window
        //note: maxheap size doesn't matter
        //Insert first 3 items (window size)
        //loop the other item
        //the heap item has value and index
        //for each item added we insert into the output
        // Generally we should delete the items that are not in the window  eg 3,2,4,5, 6 consider (3,2,4) and then (2,4,5) and then (5,6 etc)
        //but here since we know the heap max we delete the item only if the max heap index is outside the window
        //If the last max heap is outside the current window we remove the peek and then check untill the maxheap index is within the current window
        //Note: here the heap size doesn't matter we may have heap of size 10 for windows of 3 eg input  0,1,2,3,4,5,6,7,8,9,10

        public int[] MaxofSlidingWindowUsingHeap(int[] input, int w)
        {
            int[]          output      = new int[input.Length];
            int            outputindex = 0;
            MaxHeap <Pair> maxheap     = new MaxHeap <Pair>(new KarthicMaxHeapPairComparer());

            //0,1,2
            for (int i = 0; i < w; i++)
            {
                maxheap.Insert(new Pair(input[i], i));
            }
            //we already insert 0 to w-1
            for (int j = w; j < input.Length; j++)
            {
                //previous window peek
                Pair max = maxheap.Peek();
                output[outputindex] = max.Value;
                outputindex++;
                //j - w make sure that only the elements that are lesser than this window should be popped up if it has the peek value
                //update: check whether the max.Index is inside the current window
                //If it is outside the current window then pop and get the next max
                //while (max.Index + w <= j ) this condition is also same
                while (max.Index <= j - w)
                {
                    maxheap.PopRoot();
                    max = maxheap.Peek();
                }

                maxheap.Insert(new Pair(input[j], j));
            }


            Pair max1 = maxheap.Peek();

            output[outputindex] = max1.Value;
            outputindex++;


            Array.Resize <int>(ref output, outputindex);

            return(output);
        }