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