Pagini recente » Cod sursa (job #507320) | Cod sursa (job #2002797) | Cod sursa (job #341149) | Cod sursa (job #1990428) | Cod sursa (job #593571)
Cod sursa(job #593571)
#define _CRT_SECURE_NO_DEPRECATE
#include <stdlib.h>
#include <stdio.h>
struct heap_el {int key; struct heap_el *left,*right; int deleted;};
struct list_el {void* data; struct list_el *next;};
struct heap {int num; heap_el *first; heap_el *list[200000];};
heap_el * h_add(heap * p, int key);
void h_wrt_min(heap *p);
void h_del_x(heap *p, int x);
heap static zz,*z=&zz;
FILE static *fi,*fo;
int main()
{
fi = fopen("heapuri.in","r");
fo = fopen("heapuri.out","w");
int a,b,n;
fscanf(fi,"%d",&n);
while (n--)
{
fscanf(fi,"%d",&a);
if (a == 1)
{
fscanf(fi,"%d",&b);
h_add(z,b);
}
if (a == 2)
{
fscanf(fi,"%d",&b);
h_del_x(z,b);
}
if (a == 3)
{
h_wrt_min(z);
}
}
fclose(fi);
fclose(fo);
return 0;
}
heap_el * h_add(heap * p, int key)
{
if (p == NULL) return NULL;
heap_el *n = (heap_el*) malloc(sizeof(heap_el));
n->key = key;
n->left = NULL;
n->right = NULL;
n->deleted = 0;
p->list[p->num]=n;
if (p->first == NULL)
{
p->first = n;
//p->pare[p->num] = NULL;
}
else
{
heap_el *i = p->first;
while (1)
{
if (key<i->key)
if (i->left == NULL)
{
i->left = n;
//p->pare[p->num] = i;
break;
}
else i=i->left;
else
if (i->right == NULL)
{
i->right = n;
//p->pare[p->num] = i;
break;
}
else i=i->right;
}
}
p->num++;
return n;
}
heap_el *minimum = NULL;
heap_el *ino(heap_el *p)
{
if ((p != NULL) && (minimum == NULL))
{
ino(p->left);
if (! (p->deleted))
{
minimum = p;
}
else ino(p->right);
}
return minimum;
}
void h_wrt_min(heap *p)
{
heap_el *q,*w,*valid = NULL;
q = p->first;
minimum=NULL;
w = ino(q);
fprintf(fo,"%d\n",w->key);
}
void h_del_x(heap *p, int x)
{
x--;
p->list[x]->deleted = 1;
}