// 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(){
// 取り出して
// 末尾の要素を根に持ってきて
// 子のうちの大きい方と比較
// 子の方が大きかったら交換
// をHeapが完成するまで繰り返す
// 事前学習資料のP.56~60
int top=t[1];
t[1]=t[sz];
t[sz]=-1;
sz-=1;
int i=1;
while(1){
int Big=i;
if(goL(i)<=sz && t[goL(i)]>t[Big]){
Big=goL(i);
}
if (goR(i)<=sz && t[goR(i)]>t[Big]) {
Big=goR(i);
}
if(Big==i) break;
swap(&t[i],&t[Big]);
i=Big;
}
return top;
}
//末尾に要素を追加する
//アップヒープ
void pushHeap(int x){
// 末尾に追加
// 親と比較して、子の方が大きかったら交換
// をHeapが完成するまで繰り返す
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;
}
Ly8gaGVhcC1raQoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgaGVpZ2h0IDQKI2RlZmluZSBNQVggKDE8PGhlaWdodCkgIC8v44OT44OD44OI44K344OV44OI5ryU566XIDJeaGVpZ2h0IOOBqOWQjOOBmAoKaW50IHRbTUFYKzFdOyAvL+mFjeWIl+WkluOCouOCr+OCu+OCuemYsuatouOBruOBn+OCgeOBruODgOODn+ODvOOBp++8i++8kQppbnQgc3ogPSAwOwoKdm9pZCBzd2FwKGludCAqeCwgaW50ICp5KXsKICAgIGludCB0bXAgPSAqeDsKICAgICp4ID0gKnk7CiAgICAqeSA9IHRtcDsKfQoKdm9pZCBpbml0VHJlZShpbnQgbil7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7aTxNQVg7aSsrKXsKICAgICAgICB0W2ldID0gLTE7CiAgICB9Cn0KCnZvaWQgcHJpbnRBKCl7CiAgICBpbnQgaTsKICAgIGZvcihpPTE7aTw9c3o7aSsrKSBwcmludGYoIiVkICIsdFtpXSk7CiAgICBwcmludGYoIlxuIik7Cn0KCnZvaWQgcHJpbnRUKGludCBpKXsKICAgIGludCB4ID0gaTsKICAgIHdoaWxlKHgvMiE9MCl7CiAgICAgICAgcHJpbnRmKCIgICIpOwogICAgICAgIHgvPTI7CiAgICB9CiAgICBwcmludGYoIiVkXG4iLHRbaV0pOwp9CgppbnQgZ29QKGludCBpKXsKICAgIGlmKGkvMiA9PSAwKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIGkvMjsKfQoKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQoKaW50IGdvUihpbnQgaSl7CiAgICBpZigyKmkrMSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippKzE7Cn0KCnZvaWQgcHJlT3JkZXIoaW50IGkpewogICAgaWYodFtpXSA9PSAtMSkgcmV0dXJuOwogICAgcHJpbnRUKGkpOwogICAgcHJlT3JkZXIoZ29MKGkpKTsKICAgIHByZU9yZGVyKGdvUihpKSk7Cn0KCnZvaWQgaW5PcmRlcihpbnQgaSl7CiAgICBpZih0W2ldID09IC0xKSByZXR1cm47CiAgICBpbk9yZGVyKGdvTChpKSk7CiAgICBwcmludFQoaSk7CiAgICBpbk9yZGVyKGdvUihpKSk7Cn0KCnZvaWQgcG9zdE9yZGVyKGludCBpKXsKICAgIGlmKHRbaV0gPT0gLTEpIHJldHVybjsKICAgIHBvc3RPcmRlcihnb0woaSkpOwogICAgcG9zdE9yZGVyKGdvUihpKSk7CiAgICBwcmludFQoaSk7Cn0KCnZvaWQgaW5zQlQoaW50IHgpewogICAgaW50IGssaSA9IDE7CiAgICBmb3Ioaz0wO2s8aGVpZ2h0O2srKyl7CiAgICAgICAgaWYodFtpXT09LTEpewogICAgICAgICAgICB0W2ldID0geDsKICAgICAgICAgICAgc3orKzsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZih4IDwgdFtpXSkgaSA9IGdvTChpKTsKICAgICAgICBlbHNlIGkgPSBnb1IoaSk7CiAgICB9CiAgICBwcmludGYoIkVycm9yIDogdG9vIGhpZ2ggLT4gJWRcbiIseCk7Cn0KCi8v5YWI6aCt44Gu6KaB57Sg44KS5Y+W44KK5Ye644GZCi8v44OA44Km44Oz44OS44O844OXCmludCBwb3BIZWFwKCl7Ci8vIOWPluOCiuWHuuOBl+OBpgovLyDmnKvlsL7jga7opoHntKDjgp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