Min Binary Heap
Time limit: 5000ms
Memory limit: 256mb
-------------------------Copy the following code, complete it and submit---------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INF -1
typedef enum {INSERT = 1, DELETE, TOP, ERROR_OP} OP;
struct Heap {
int capacity;
int size;
int* arr;
};
typedef struct Heap* HeapADT;
HeapADT Create(int capacity) {
HeapADT heap = (HeapADT) malloc(sizeof(struct Heap));
heap->capacity = capacity;
heap->size = 0;
heap->arr = (int*) malloc((capacity + 1) * sizeof(int));
return heap;
}
void InsertHeap(HeapADT heap, int value) {
// write your code here
}
void DeleteHeap(HeapADT heap) {
// write your code here
}
// Return the smallest value in the heap
// If the heap is empty, return INF
int Top(HeapADT heap) {
// write your code here
}
OP get_op() {
char str[20];
scanf("%s", str);
if (strcmp(str, "insert") == 0) {
return INSERT;
} else if (strcmp(str, "delete") == 0) {
return DELETE;
} else if (strcmp(str, "top") == 0) {
return TOP;
} else {
return ERROR_OP;
}
}
int main() {
int n, capacity, operation, value;
scanf("%d%d", &capacity, &n);
HeapADT heap = Create(capacity);
for(int i=0; i<n; i++)
{
switch (get_op()) {
case INSERT:
scanf("%d", &value);
InsertHeap(heap, value);
break;
case DELETE:
DeleteHeap(heap);
break;
case TOP:
printf("%d\n", Top(heap));
break;
default:
printf("Error Input\n");
}
}
}
-----------------------------------------End of Code-----------------------------------------
Description:
Implement the min binary heap.
Input:
The first line contains two integers which are capacity and N;
The next N lines, each of which contains an operation: insert x (an Integer) or top or delete
Output:
Each line is the warning or result of Top
Sample Input 1:
3 8
insert 4
insert 3
top
insert 2
insert 1
top
delete
top
Sample Output 1:
3
2
3
Submit