// 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];
sz--;
// ダウンヒープ処理
int i = 1;
while (goL(i) != 0) { // 左の子が存在する間
int left = goL(i);
int right = goR(i);
int larger = left;
// 右の子が存在して、右の方が大きい場合
if (right != 0 && t[right] > t[left]) {
larger = right;
}
// 親が子より小さい場合に交換
if (t[i] < t[larger]) {
swap(&t[i], &t[larger]);
i = larger; // 交換後、子ノードに移動
} else {
break; // ヒープが完成
}
}
return root;
}
void pushHeap(int x){
if (sz >= MAX - 1) {
return;
}
// 末尾に追加
sz++;
t[sz] = x;
// アップヒープ処理
int i = sz;
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;
}
Ly8gaGVhcC1raQogCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+CiAKI2RlZmluZSBoZWlnaHQgNAojZGVmaW5lIE1BWCAoMTw8aGVpZ2h0KSAgLy/jg5Pjg4Pjg4jjgrfjg5Xjg4jmvJTnrpcgMl5oZWlnaHQg44Go5ZCM44GYCiAKaW50IHRbTUFYKzFdOyAvL+mFjeWIl+WkluOCouOCr+OCu+OCuemYsuatouOBruOBn+OCgeOBruODgOODn+ODvOOBp++8i++8kQppbnQgc3ogPSAwOwogCnZvaWQgc3dhcChpbnQgKngsIGludCAqeSl7CiAgICBpbnQgdG1wID0gKng7CiAgICAqeCA9ICp5OwogICAgKnkgPSB0bXA7Cn0KIAp2b2lkIGluaXRUcmVlKGludCBuKXsKICAgIGludCBpOwogICAgZm9yKGk9MDtpPE1BWDtpKyspewogICAgICAgIHRbaV0gPSAtMTsKICAgIH0KfQogCnZvaWQgcHJpbnRBKCl7CiAgICBpbnQgaTsKICAgIGZvcihpPTE7aTw9c3o7aSsrKSBwcmludGYoIiVkICIsdFtpXSk7CiAgICBwcmludGYoIlxuIik7Cn0KIAp2b2lkIHByaW50VChpbnQgaSl7CiAgICBpbnQgeCA9IGk7CiAgICB3aGlsZSh4LzIhPTApewogICAgICAgIHByaW50ZigiICAiKTsKICAgICAgICB4Lz0yOwogICAgfQogICAgcHJpbnRmKCIlZFxuIix0W2ldKTsKfQogCmludCBnb1AoaW50IGkpewogICAgaWYoaS8yID09IDApIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gaS8yOwp9CiAKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQogCmludCBnb1IoaW50IGkpewogICAgaWYoMippKzEgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaSsxOwp9CiAKdm9pZCBwcmVPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwcmludFQoaSk7CiAgICBwcmVPcmRlcihnb0woaSkpOwogICAgcHJlT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgaW5PcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBpbk9yZGVyKGdvTChpKSk7CiAgICBwcmludFQoaSk7CiAgICBpbk9yZGVyKGdvUihpKSk7Cn0KIAp2b2lkIHBvc3RPcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBwb3N0T3JkZXIoZ29MKGkpKTsKICAgIHBvc3RPcmRlcihnb1IoaSkpOwogICAgcHJpbnRUKGkpOwp9CiAKdm9pZCBpbnNCVChpbnQgeCl7CiAgICBpbnQgayxpID0gMTsKICAgIGZvcihrPTA7azxoZWlnaHQ7aysrKXsKICAgICAgICBpZih0W2ldPT0tMSl7CiAgICAgICAgICAgIHRbaV0gPSB4OwogICAgICAgICAgICBzeisrOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKHggPCB0W2ldKSBpID0gZ29MKGkpOwogICAgICAgIGVsc2UgaSA9IGdvUihpKTsKICAgIH0KICAgIHByaW50ZigiRXJyb3IgOiB0b28gaGlnaCAtPiAlZFxuIix4KTsKfQogCi8v5YWI6aCt44Gu6KaB57Sg44KS5Y+W44KK5Ye644GZCi8v44OA44Km44Oz44OS44O844OXCmludCBwb3BIZWFwKCl7CiAgaWYgKHN6ID09IDApIHsKICAgICAgICBwcmludGYoIkhlYXAgaXMgZW1wdHlcbiIpOwogICAgICAgIHJldHVybiAtMTsgIC8vIOODkuODvOODl+OBjOepuuOBruWgtOWQiAogICAgfQogICAgCiAgICBpbnQgcm9vdCA9IHRbMV07ICAvLyDjg5Ljg7zjg5fjga7lhYjpoK3opoHntKDvvIjmnIDlpKflgKTvvIkKICAgIAogICAgLy8g5pyA5b6M44Gu6KaB57Sg44KS5qC544Gr5oyB44Gj44Gm44GP44KLCiAgICB0WzFdID0gdFtzel07CiAgICBzei0tOwogICAgCiAgICAvLyDjg4Djgqbjg7Pjg5Ljg7zjg5flh6bnkIYKICAgIGludCBpID0gMTsKICAgIHdoaWxlIChnb0woaSkgIT0gMCkgeyAgLy8g5bem44Gu5a2Q44GM5a2Y5Zyo44GZ44KL6ZaTCiAgICAgICAgaW50IGxlZnQgPSBnb0woaSk7CiAgICAgICAgaW50IHJpZ2h0ID0gZ29SKGkpOwogICAgICAgIGludCBsYXJnZXIgPSBsZWZ0OwoKICAgICAgICAvLyDlj7Pjga7lrZDjgYzlrZjlnKjjgZfjgabjgIHlj7Pjga7mlrnjgYzlpKfjgY3jgYTloLTlkIgKICAgICAgICBpZiAocmlnaHQgIT0gMCAmJiB0W3JpZ2h0XSA+IHRbbGVmdF0pIHsKICAgICAgICAgICAgbGFyZ2VyID0gcmlnaHQ7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIC8vIOimquOBjOWtkOOCiOOCiuWwj+OBleOBhOWgtOWQiOOBq+S6pOaPmwogICAgICAgIGlmICh0W2ldIDwgdFtsYXJnZXJdKSB7CiAgICAgICAgICAgIHN3YXAoJnRbaV0sICZ0W2xhcmdlcl0pOwogICAgICAgICAgICBpID0gbGFyZ2VyOyAgLy8g5Lqk5o+b5b6M44CB5a2Q44OO44O844OJ44Gr56e75YuVCiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgYnJlYWs7ICAvLyDjg5Ljg7zjg5fjgYzlrozmiJAKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiByb290Owp9CiB2b2lkIHB1c2hIZWFwKGludCB4KXsKaWYgKHN6ID49IE1BWCAtIDEpIHsKICAgICAgICBwcmludGYoIkhlYXAgaXMgZnVsbFxuIik7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgCiAgICAvLyDmnKvlsL7jgavov73liqAKICAgIHN6Kys7CiAgICB0W3N6XSA9IHg7CiAgICAKICAgIC8vIOOCouODg+ODl+ODkuODvOODl+WHpueQhgogICAgaW50IGkgPSBzejsKICAgIHdoaWxlIChpID4gMSAmJiB0W2ldID4gdFtnb1AoaSldKSB7CiAgICAgICAgc3dhcCgmdFtpXSwgJnRbZ29QKGkpXSk7CiAgICAgICAgaSA9IGdvUChpKTsgIC8vIOimquODjuODvOODieOBq+enu+WLlQogICAgfQp9CgppbnQgbWFpbih2b2lkKXsKICAgIGludCBpLHgsbjsKICAgIHNjYW5mKCIlZCIsJm4pOwogICAgLy8g5pyo44Gu5Yid5pyf5YyWCiAgICBpbml0VHJlZShuKTsKICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgc2NhbmYoIiVkIiwmdFtpKzFdKTsKICAgIH0KICAgIHN6ID0gbjsKICAgIC8vIOS4remWk+mghuOBp+ihqOekugoJaW5PcmRlcigxKTsKIAoJLy8gcG9wCglpbnQgYT1wb3BIZWFwKCk7CglwcmludGYoInBvcCA6ICVkXG4iLGEpOwoJaW5PcmRlcigxKTsKIAoJYSA9IHBvcEhlYXAoKTsKCXByaW50ZigicG9wIDogJWRcbiIsYSk7CiAKCWluT3JkZXIoMSk7CiAKCXByaW50ZigicHVzaCA6IDEwMFxuIik7CglwdXNoSGVhcCgxMDApOwoJaW5PcmRlcigxKTsKIAoJcHJpbnRmKCJwdXNoIDogMzBcbiIpOwoJcHVzaEhlYXAoMzApOwoJaW5PcmRlcigxKTsKICAgIHJldHVybiAwOwp9CiA=