-
Notifications
You must be signed in to change notification settings - Fork 11
/
question5.c
106 lines (93 loc) · 2.04 KB
/
question5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Build a min-heap and write algo to delete an arbitrary element
Time complexity: O(n)
because finding that element will take O(n) time linear search and deleting will take O(logn) time
Total in O(n)
Space complexity: O(1)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(int *arr,int i, int size){
int left = 2*i+1,right=2*i+2,smallest, heapSize = size;
if(left <= heapSize-1 && arr[i] < arr[left]){
smallest = i;
}else{
smallest = left;
}
if(right <= heapSize-1 && arr[right] < arr[smallest]){
smallest = right;
}
if(smallest <= heapSize - 1 && smallest !=i){
swap(&arr[smallest],&arr[i]);
minHeapify(arr,smallest,size);
}
}
void buildMinHeap(int *arr, int size){
int index = floor(size/2);
printf("value of index is...%d\n", index);
for(int i=index; i>=0;i--){
minHeapify(arr,i,size);
}
}
void display(int *arr,int size){
for(int i=0;i<size;i++){
printf("%d\t", arr[i]);
}
printf("\n");
}
int deleteElement(int *arr, int index, int *size){
if(*size < 1){
return -1;
}
int result = arr[index];
int temp = arr[index];
arr[index]=arr[*size-1];
arr[*size-1]=temp;
*size = *size-1;
minHeapify(arr,index,*size);
return result;
}
int findIndex(int *arr, int size, int value){
for(int i=0;i<size;i++){
if(arr[i]==value){
return i;
}
}
return -1;
}
int main(){
int *arr, size, elm, result;
printf("enter the size of the min-heap\n");
scanf("%d",&size);
arr = (int *)malloc(sizeof(int)*size);
for(int i=0; i<size;i++){
printf("enter %d th element\n", i);
scanf("%d",&arr[i]);
}
buildMinHeap(arr,size);
display(arr, size);
while(1){
printf("enter the element to be deleted\n");
scanf("%d",&elm);
int index = findIndex(arr,size,elm);
if(index >= 0){
result = deleteElement(arr,index,&size);
if(result < 0){
printf("underflow\n");
}else{
printf("%d was removed\n", result);
}
printf("updated heap is..\n");
display(arr,size);
}else{
printf("element not found\n");
}
}
return 0;
}