#include <stdio.h>
#include <stdlib.h>
#define height 4
#define MAX (1<<height)
int t[MAX+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 root = t[1];
t[1] = t[sz];
t[sz] = -1;
sz--;
int i = 1;
while (i*2<=sz){
int left=i*2;
int right=i*2+1;
int largest=i;
if(left<=sz&&t[left]>t[largest]){
largest=left;
}
if(right<=sz&&t[right]>t[largest]){
largest=right;
}
if (largest != i){
swap(&t[i],&t[largest]);
i=largest;
}else{
break;
}
}
return root;
}
void pushHeap(int x){
if (sz + 1 >= MAX) {
return;
}
sz++;
t[sz]=x;
int i=sz;
while (i>1&&t[i]>t[i/2]){
swap(&t[i],&t[i/2]);
i=i/2;
}
}
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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAojZGVmaW5lIGhlaWdodCA0CiNkZWZpbmUgTUFYICgxPDxoZWlnaHQpICAKIAppbnQgdFtNQVgrMV07IAppbnQgc3ogPSAwOwogCnZvaWQgc3dhcChpbnQgKngsIGludCAqeSl7CiAgICBpbnQgdG1wID0gKng7CiAgICAqeCA9ICp5OwogICAgKnkgPSB0bXA7Cn0KIAp2b2lkIGluaXRUcmVlKGludCBuKXsKICAgIGludCBpOwogICAgZm9yKGk9MDtpPE1BWDtpKyspewogICAgICAgIHRbaV0gPSAtMTsKICAgIH0KfQogCnZvaWQgcHJpbnRBKCl7CiAgICBpbnQgaTsKICAgIGZvcihpPTE7aTw9c3o7aSsrKSBwcmludGYoIiVkICIsdFtpXSk7CiAgICBwcmludGYoIlxuIik7Cn0KIAp2b2lkIHByaW50VChpbnQgaSl7CiAgICBpbnQgeCA9IGk7CiAgICB3aGlsZSh4LzIhPTApewogICAgICAgIHByaW50ZigiICAiKTsKICAgICAgICB4Lz0yOwogICAgfQogICAgcHJpbnRmKCIlZFxuIix0W2ldKTsKfQogCmludCBnb1AoaW50IGkpewogICAgaWYoaS8yID09IDApIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gaS8yOwp9CiAKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQogCmludCBnb1IoaW50IGkpewogICAgaWYoMippKzEgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaSsxOwp9CiAKdm9pZCBwcmVPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwcmludFQoaSk7CiAgICBwcmVPcmRlcihnb0woaSkpOwogICAgcHJlT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgaW5PcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBpbk9yZGVyKGdvTChpKSk7CiAgICBwcmludFQoaSk7CiAgICBpbk9yZGVyKGdvUihpKSk7Cn0KIAp2b2lkIHBvc3RPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwb3N0T3JkZXIoZ29MKGkpKTsKICAgIHBvc3RPcmRlcihnb1IoaSkpOwogICAgcHJpbnRUKGkpOwp9CiAKdm9pZCBpbnNCVChpbnQgeCl7CiAgICBpbnQgayxpID0gMTsKICAgIGZvcihrPTA7azxoZWlnaHQ7aysrKXsKICAgICAgICBpZih0W2ldPT0tMSl7CiAgICAgICAgICAgIHRbaV0gPSB4OwogICAgICAgICAgICBzeisrOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKHggPCB0W2ldKSBpID0gZ29MKGkpOwogICAgICAgIGVsc2UgaSA9IGdvUihpKTsKICAgIH0KICAgIHByaW50ZigiRXJyb3IgOiB0b28gaGlnaCAtPiAlZFxuIix4KTsKfQogCmludCBwb3BIZWFwKCl7CiAgICBpbnQgcm9vdCA9IHRbMV07ICAKICAgIHRbMV0gPSB0W3N6XTsgIAogICAgdFtzel0gPSAtMTsgIAogICAgc3otLTsgIAogICAgaW50IGkgPSAxOwogICAgd2hpbGUgKGkqMjw9c3opewogICAgICAgIGludCBsZWZ0PWkqMjsKICAgICAgICBpbnQgcmlnaHQ9aSoyKzE7CiAgICAgICAgaW50IGxhcmdlc3Q9aTsKICAgICAgICBpZihsZWZ0PD1zeiYmdFtsZWZ0XT50W2xhcmdlc3RdKXsKICAgICAgICAgICAgbGFyZ2VzdD1sZWZ0OwogICAgICAgIH0KICAgICAgICBpZihyaWdodDw9c3omJnRbcmlnaHRdPnRbbGFyZ2VzdF0pewogICAgICAgICAgICBsYXJnZXN0PXJpZ2h0OwogICAgICAgIH0KICAgICAgICBpZiAobGFyZ2VzdCAhPSBpKXsKICAgICAgICAgICAgc3dhcCgmdFtpXSwmdFtsYXJnZXN0XSk7CiAgICAgICAgICAgIGk9bGFyZ2VzdDsKICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgYnJlYWs7IAogICAgICAgIH0KICAgIH0KICAgIHJldHVybiByb290Owp9Cgogdm9pZCBwdXNoSGVhcChpbnQgeCl7CiAgICBpZiAoc3ogKyAxID49IE1BWCkgewogICAgICAgIHByaW50ZigiSGVhcCBpcyBmdWxsLlxuIik7CiAgICAgICAgcmV0dXJuOyAgCiAgICB9CiAgICBzeisrOyAKICAgIHRbc3pdPXg7CiAgICBpbnQgaT1zejsKICAgIHdoaWxlIChpPjEmJnRbaV0+dFtpLzJdKXsKICAgICAgICBzd2FwKCZ0W2ldLCZ0W2kvMl0pOwogICAgICAgIGk9aS8yOwogICAgfQp9CgppbnQgbWFpbih2b2lkKXsKICAgIGludCBpLHgsbjsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgLy8g5pyo44Gu5Yid5pyf5YyWCiAgICBpbml0VHJlZShuKTsKICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgc2NhbmYoIiVkIiwmdFtpKzFdKTsKICAgIH0KICAgIHN6ID0gbjsKICAgIC8vIOS4remWk+mghuOBp+ihqOekugoJaW5PcmRlcigxKTsKIAoJLy8gcG9wCglpbnQgYT1wb3BIZWFwKCk7CglwcmludGYoInBvcCA6ICVkXG4iLGEpOwoJaW5PcmRlcigxKTsKIAoJYSA9IHBvcEhlYXAoKTsKCXByaW50ZigicG9wIDogJWRcbiIsYSk7CiAKCWluT3JkZXIoMSk7CiAKCXByaW50ZigicHVzaCA6IDEwMFxuIik7CglwdXNoSGVhcCgxMDApOwoJaW5PcmRlcigxKTsKIAoJcHJpbnRmKCJwdXNoIDogMzBcbiIpOwoJcHVzaEhlYXAoMzApOwoJaW5PcmRlcigxKTsKICAgIHJldHVybiAwOwp9