void main()
{
	clrscr();
	int ch=1,item;
	
	while(ch!=0)
	{
		cout<<"\n1.Insert\n2.Delete\n3.Display";
		cout<<"\n0.Exit";
		cout<<"\nEnter Your Choice : ";
		cin>>ch;
		
		switch(ch)
		{
		case 1:
			cout<<"\nEnter Item : ";
			cin>>item;
			max_insert(item);
			disp();
			break;
		case 2:
			max_del();
			break;
		case 3:
			disp();
			break;
		default:
			break;
		}
		
	}
}
Esempio n. 2
0
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    
    int i,n,tmp,*max_heap,*min_heap,max_size=0,min_size=0;

    scanf("%d",&n);
    max_heap = malloc(sizeof(int) * n);
    min_heap = malloc(sizeof(int) * n);

    for (i=0;i<n;i++) {
        scanf("%d",&tmp);
        if (tmp < min_heap[0]) {
            //printf("max insert %d\n",tmp);
            max_insert(tmp,max_heap,max_size++);
        } else {
            //printf("min insert %d\n",tmp);
            min_insert(tmp,min_heap,min_size++);
        }

        //print_heap("max",max_heap,max_size);
        //print_heap("min",min_heap,min_size);

        if (i % 2 == 1) {
            if (min_size > max_size) {
                // swap min root to max heap
                //printf("move %d from min to max\n",min_heap[0]);
                tmp = min_heap[0];
                min_heap[0] = min_heap[min_size-1];
                min_heapdown(min_heap,--min_size,0);
                max_insert(tmp,max_heap,max_size++);
            } else if (max_size > min_size) {
                // swap max root to min heap
                //printf("move %d from max to min\n",max_heap[0]);
                tmp = max_heap[0];
                max_heap[0] = max_heap[max_size-1];
                max_heapdown(max_heap,--max_size,0);
                min_insert(tmp,min_heap,min_size++);
            }
        }

        print_heap("max",max_heap,max_size);
        print_heap("min",min_heap,min_size);

        if (i % 2 == 1) {
            // median is mean of min and max 0th elements
            printf("%.1f\n",(double)(min_heap[0]+max_heap[0])/2);
        } else {
            // median is 0th of largest heap
            if (max_size > min_size) {
                printf("%.1f\n",(double)max_heap[0]);
            } else {
                printf("%.1f\n",(double)min_heap[0]);
            }
        }
    }

    free(max_heap);
    free(min_heap);

    return 0;
}
Esempio n. 3
0
//Insertion in Median Heap
int insert_med(Heap H,int n,int x)
{
    
    int med = median(H,n);
    int j;
    
    if(x<=med)
    {
        if(H.size_max == n - H.size_min-1)
        {
            //Deletes root from max heap and inserts in the min heap
            //Inserts new element in the max heap

            int del = delete_max(H,n);
            H.size_max--;

            max_insert(H,x,n);
            H.size_max++;

            min_insert(H,del,n);
            H.size_min--;
            return 2;
        }
        else
        {
            //Inserts new element in the max heap
            max_insert(H,x,n);
            H.size_max++;
           
            return 1;
        }    
    }    

    else
    {
        if(H.size_max == n - H.size_min-1)
        {
            //Inserts new element in the min heap
            min_insert(H,x,n);
            H.size_min--;
          
            return 2;
        }
        else
        {
            //Deletes root from min heap and inserts in the max heap
            //Inserts new element in the min heap
            int del = delete_min(H,n);
            H.size_min++;

            max_insert(H,del,n);
            H.size_max++;

            min_insert(H,x,n);
            H.size_min--;
  
            return 1;
        }    
    }    


}