Skip to content

Commit

Permalink
new question added
Browse files Browse the repository at this point in the history
  • Loading branch information
arorarahul committed Apr 21, 2017
1 parent 1e2f16b commit 12a7de4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions heaps/question2.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void maxHeapify(int arr[],int i, int size){
}

int deleteMax(int arr[], int *size){
if(*size == 0){
if(*size < 1){
return -1;
}
int result = arr[0];
Expand All @@ -51,25 +51,26 @@ int deleteMax(int arr[], int *size){
}

int increaseKey(int arr[], int index, int value, int size){
if(size == 0){
if(size < 1){
return -1;
}
if(value < arr[index]){
return -2;
}
int i=index, temp;
arr[i] = value;

while(i>=1 && arr[(i-1)/2]<arr[i]){
temp = arr[i/2];
arr[i/2] = arr[i];
arr[(i-1)/2] = arr[i];
arr[i]=temp;
i = (i-1)/2;
}
return value;
}

int decreaseKey(int arr[], int index, int value, int size){
if(size == 0){
if(size < 1){
return -1;
}
if(value > arr[index]){
Expand All @@ -90,8 +91,8 @@ int findMax(int arr[],int size){
}

int main(){
int heap[]={9,8,5,3,6,2,1,0}; //max heap
int size = sizeof(heap)/sizeof(heap[0]);
int heap[MAX_VALUE]={9,8,5,3,6,2,1,0}; //max heap
int size = 8;

int step, value, max, result, index;

Expand Down Expand Up @@ -148,22 +149,19 @@ int main(){
}
display(heap,size);
break;
case 5: //@TODO
printf("enter the value to be inserted\n");
case 5: printf("enter the value to be inserted\n");
scanf("%d",&value);
size = size+1;
if(size == MAX_VALUE){
printf("heap memory full\n");
return 0;
}
heap[size-1] = INT_MIN;
printf("new heap size is...%d\n", size);
printf("value...%d\n", size);
result = decreaseKey(heap,size-1,value,size);
result = increaseKey(heap,size-1,value,size);
if(output == -1){
printf("heap is already empty\n");
}else if(output == -2){
printf("value is greater than current value at that index\n");
printf("value is lesser than current value at that index\n");
}else{
printf("updated value is ..%d\n", output);
}
Expand Down

0 comments on commit 12a7de4

Please sign in to comment.