// heap-ki
#include <stdio.h>
#include <stdlib.h>
#define height 4
#define MAX (1<<height) //ビットシフト演算 2^height と同じ
int t[MAX+1]; //配列外アクセス防止のためのダミーで+1
int sz = 0;
void swap(int *x, int *y){
int tmp = *x;
*x = *y;
*y = tmp;
}
void initTree(int n){
int i;
for(i=0;i<MAX;i++){
t[i] = -1;
}
}
void printA(){
int i;
for(i
=1;i
<=sz
;i
++) printf("%d ",t
[i
]); }
void printT(int i){
int x = i;
while(x/2!=0){
x/=2;
}
}
int goP(int i){
if(i/2 == 0) return 0;
else return i/2;
}
int goL(int i){
if(2*i >= MAX) return 0;
else return 2*i;
}
int goR(int i){
if(2*i+1 >= MAX) return 0;
else return 2*i+1;
}
void preOrder(int i){
if(t[i] == -1) return;
printT(i);
preOrder(goL(i));
preOrder(goR(i));
}
void inOrder(int i){
if(t[i] == -1) return;
inOrder(goL(i));
printT(i);
inOrder(goR(i));
}
void postOrder(int i){
if(t[i] == -1) return;
postOrder(goL(i));
postOrder(goR(i));
printT(i);
}
void insBT(int x){
int k,i = 1;
for(k=0;k<height;k++){
if(t[i]==-1){
t[i] = x;
sz++;
return;
}
if(x < t[i]) i = goL(i);
else i = goR(i);
}
printf("Error : too high -> %d\n",x
); }
int popHeap(){
if(sz == 0) return -1;
int root = t[1];
t[1] = t[sz];
t[sz] = -1;
sz--;
int i = 1;
while(goL(i) <= sz){
int largerChild = goL(i);
if(goR(i) <= sz && t[goR(i)] > t[goL(i)]) largerChild = goR(i);
if(t[i] >= t[largerChild]) break;
swap(&t[i], &t[largerChild]);
i = largerChild;
}
return root;
}
void pushHeap(int x){
if(sz >= MAX - 1) return;
sz++;
t[sz] = x;
int i = sz;
while(goP(i) != 0 && t[i] > t[goP(i)]){
swap(&t[i], &t[goP(i)]);
i = goP(i);
}
}
int main(void){
int i,x,n;
// 木の初期化
initTree(n);
for(i=0;i<n;i++){
}
sz = n;
// 中間順で表示
inOrder(1);
// pop
int a=popHeap();
inOrder(1);
a = popHeap();
inOrder(1);
pushHeap(100);
inOrder(1);
pushHeap(30);
inOrder(1);
return 0;
}
Ly8gaGVhcC1raQogCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiAKI2RlZmluZSBoZWlnaHQgNAojZGVmaW5lIE1BWCAoMTw8aGVpZ2h0KSAgLy/jg5Pjg4Pjg4jjgrfjg5Xjg4jmvJTnrpcgMl5oZWlnaHQg44Go5ZCM44GYCiAKaW50IHRbTUFYKzFdOyAvL+mFjeWIl+WkluOCouOCr+OCu+OCuemYsuatouOBruOBn+OCgeOBruODgOODn+ODvOOBp++8i++8kQppbnQgc3ogPSAwOwogCnZvaWQgc3dhcChpbnQgKngsIGludCAqeSl7CiAgICBpbnQgdG1wID0gKng7CiAgICAqeCA9ICp5OwogICAgKnkgPSB0bXA7Cn0KIAp2b2lkIGluaXRUcmVlKGludCBuKXsKICAgIGludCBpOwogICAgZm9yKGk9MDtpPE1BWDtpKyspewogICAgICAgIHRbaV0gPSAtMTsKICAgIH0KfQogCnZvaWQgcHJpbnRBKCl7CiAgICBpbnQgaTsKICAgIGZvcihpPTE7aTw9c3o7aSsrKSBwcmludGYoIiVkICIsdFtpXSk7CiAgICBwcmludGYoIlxuIik7Cn0KIAp2b2lkIHByaW50VChpbnQgaSl7CiAgICBpbnQgeCA9IGk7CiAgICB3aGlsZSh4LzIhPTApewogICAgICAgIHByaW50ZigiICAiKTsKICAgICAgICB4Lz0yOwogICAgfQogICAgcHJpbnRmKCIlZFxuIix0W2ldKTsKfQogCmludCBnb1AoaW50IGkpewogICAgaWYoaS8yID09IDApIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gaS8yOwp9CiAKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQogCmludCBnb1IoaW50IGkpewogICAgaWYoMippKzEgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaSsxOwp9CiAKdm9pZCBwcmVPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwcmludFQoaSk7CiAgICBwcmVPcmRlcihnb0woaSkpOwogICAgcHJlT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgaW5PcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBpbk9yZGVyKGdvTChpKSk7CiAgICBwcmludFQoaSk7CiAgICBpbk9yZGVyKGdvUihpKSk7Cn0KIAp2b2lkIHBvc3RPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwb3N0T3JkZXIoZ29MKGkpKTsKICAgIHBvc3RPcmRlcihnb1IoaSkpOwogICAgcHJpbnRUKGkpOwp9CiAKdm9pZCBpbnNCVChpbnQgeCl7CiAgICBpbnQgayxpID0gMTsKICAgIGZvcihrPTA7azxoZWlnaHQ7aysrKXsKICAgICAgICBpZih0W2ldPT0tMSl7CiAgICAgICAgICAgIHRbaV0gPSB4OwogICAgICAgICAgICBzeisrOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKHggPCB0W2ldKSBpID0gZ29MKGkpOwogICAgICAgIGVsc2UgaSA9IGdvUihpKTsKICAgIH0KICAgIHByaW50ZigiRXJyb3IgOiB0b28gaGlnaCAtPiAlZFxuIix4KTsKfQogCgppbnQgcG9wSGVhcCgpewogICAgaWYoc3ogPT0gMCkgcmV0dXJuIC0xOyAKICAgIGludCByb290ID0gdFsxXTsgCiAgICB0WzFdID0gdFtzel07IAogICAgdFtzel0gPSAtMTsgCiAgICBzei0tOyAKICAgIGludCBpID0gMTsKICAgIHdoaWxlKGdvTChpKSA8PSBzeil7IAogICAgICAgIGludCBsYXJnZXJDaGlsZCA9IGdvTChpKTsKICAgICAgICBpZihnb1IoaSkgPD0gc3ogJiYgdFtnb1IoaSldID4gdFtnb0woaSldKSBsYXJnZXJDaGlsZCA9I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