Skip to content

Commit 72d73d5

Browse files
committed
new question added
1 parent 9647da6 commit 72d73d5

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ For eg storing the minimum so far in another stack so that each time when a numb
182182
- [Given a max heap, apply different heap operations (find max, delete max,increase key, insert key, decrease key.)](/heaps/question2.c)
183183
- [Write a program for heap sort](/heaps/question3.c)
184184
- [Find a max element in a min-heap](/heaps/question4.c)
185+
- [Build a min-heap and write algo to delete an arbitrary element](/heaps/question5.c)
185186

186187
## Some important concepts to solve algos better
187188

heaps/question5.c

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

nextquestions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ TODO:
4040
- double linked list implementation for question7 stacks and queues to be done
4141
- question 8, 9 implementation for stacks and queues to be done
4242
- Program to find then the stock should be sold in order to gain max profit
43-
- insert key in heap question2 to be done
43+
- insert key in heap question2 to be done
44+
- building min/max heap for duplicate elements

0 commit comments

Comments
 (0)