// 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(){
int k=t[1];
t[1]=t[sz];
t[sz]=-1;
sz--;
int i=1;
while(i<=sz){
int l, r, fox;
l=goL(i);
r=goR(i);
fox=i;
if(l<sz && t[l]>t[fox]){
fox=l;
}
if(r<sz && t[r]>t[fox]){
fox=r;
}
if(fox!=i){
swap(&t[i], &t[fox]);
i=fox;
}
else break;
}
return k;
}
//末尾に要素を追加する
//アップヒープ
void pushHeap(int x){
int i=++sz;
t[i]=x;
while(i>1 && 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;
}
Ly8gaGVhcC1raQoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgaGVpZ2h0IDQKI2RlZmluZSBNQVggKDE8PGhlaWdodCkgIC8v44OT44OD44OI44K344OV44OI5ryU566XIDJeaGVpZ2h0IOOBqOWQjOOBmAoKaW50IHRbTUFYKzFdOyAvL+mFjeWIl+WkluOCouOCr+OCu+OCuemYsuatouOBruOBn+OCgeOBruODgOODn+ODvOOBp++8i++8kQppbnQgc3ogPSAwOwoKdm9pZCBzd2FwKGludCAqeCwgaW50ICp5KXsKICAgIGludCB0bXAgPSAqeDsKICAgICp4ID0gKnk7CiAgICAqeSA9IHRtcDsKfQoKdm9pZCBpbml0VHJlZShpbnQgbil7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7aTxNQVg7aSsrKXsKICAgICAgICB0W2ldID0gLTE7CiAgICB9Cn0KCnZvaWQgcHJpbnRBKCl7CiAgICBpbnQgaTsKICAgIGZvcihpPTE7aTw9c3o7aSsrKSBwcmludGYoIiVkICIsdFtpXSk7CiAgICBwcmludGYoIlxuIik7Cn0KCnZvaWQgcHJpbnRUKGludCBpKXsKICAgIGludCB4ID0gaTsKICAgIHdoaWxlKHgvMiE9MCl7CiAgICAgICAgcHJpbnRmKCIgICIpOwogICAgICAgIHgvPTI7CiAgICB9CiAgICBwcmludGYoIiVkXG4iLHRbaV0pOwp9CgppbnQgZ29QKGludCBpKXsKICAgIGlmKGkvMiA9PSAwKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIGkvMjsKfQoKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQoKaW50IGdvUihpbnQgaSl7CiAgICBpZigyKmkrMSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippKzE7Cn0KCnZvaWQgcHJlT3JkZXIoaW50IGkpewogICAgaWYodFtpXSA9PSAtMSkgcmV0dXJuOwogICAgcHJpbnRUKGkpOwogICAgcHJlT3JkZXIoZ29MKGkpKTsKICAgIHByZU9yZGVyKGdvUihpKSk7Cn0KCnZvaWQgaW5PcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBpbk9yZGVyKGdvTChpKSk7CiAgICBwcmludFQoaSk7CiAgICBpbk9yZGVyKGdvUihpKSk7Cn0KCnZvaWQgcG9zdE9yZGVyKGludCBpKXsKICAgIGlmKHRbaV0gPT0gLTEpIHJldHVybjsKICAgIHBvc3RPcmRlcihnb0woaSkpOwogICAgcG9zdE9yZGVyKGdvUihpKSk7CiAgICBwcmludFQoaSk7Cn0KCnZvaWQgaW5zQlQoaW50IHgpewogICAgaW50IGssaSA9IDE7CiAgICBmb3Ioaz0wO2s8aGVpZ2h0O2srKyl7CiAgICAgICAgaWYodFtpXT09LTEpewogICAgICAgICAgICB0W2ldID0geDsKICAgICAgICAgICAgc3orKzsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZih4IDwgdFtpXSkgaSA9IGdvTChpKTsKICAgICAgICBlbHNlIGkgPSBnb1IoaSk7CiAgICB9CiAgICBwcmludGYoIkVycm9yIDogdG9vIGhpZ2ggLT4gJWRcbiIseCk7Cn0KCi8v5YWI6aCt44Gu6KaB57Sg44KS5Y+W44KK5Ye644GZCi8v44OA44Km44Oz44OS44O844OXCmludCBwb3BIZWFwKCl7CglpbnQgaz10WzFdOwoJdFsxXT10W3N6XTsKCXRbc3pdPS0xOwoJc3otLTsKCQoJaW50IGk9MTsKCXdoaWxlKGk8PXN6KXsKCQlpbnQgbCwgciwgZm94OwoJCWw9Z29MKGkpOwoJCXI9Z29SKGkpOwoJCWZveD1pOwoJCQoJCWlmKGw8c3ogJiYgdFtsXT50W2ZveF0pew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