#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 top = t[1];
t[1] = t[sz];
t[sz] = -1;
sz--;
int i = 1;
while (1) {
int left = goL(i);
int right = goR(i);
int largest = i;
if (left <= sz && t[left] > t[largest]) largest = left;
if (right <= sz && t[right] > t[largest]) largest = right;
if (largest == i) break;
swap(&t[i], &t[largest]);
i = largest;
}
return top;
}
//末尾に要素を追加する
//アップヒープ
void pushHeap(int x) {
if (sz >= MAX - 1) {
return;
}
sz++;
t[sz] = x;
int i = sz;
while (i > 1 && t[goP(i)] < t[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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAojZGVmaW5lIGhlaWdodCA0CiNkZWZpbmUgTUFYICgxPDxoZWlnaHQpICAvL+ODk+ODg+ODiOOCt+ODleODiOa8lOeulyAyXmhlaWdodCDjgajlkIzjgZgKIAppbnQgdFtNQVgrMV07IC8v6YWN5YiX5aSW44Ki44Kv44K744K56Ziy5q2i44Gu44Gf44KB44Gu44OA44Of44O844Gn77yL77yRCmludCBzeiA9IDA7CiAKdm9pZCBzd2FwKGludCAqeCwgaW50ICp5KXsKICAgIGludCB0bXAgPSAqeDsKICAgICp4ID0gKnk7CiAgICAqeSA9IHRtcDsKfQogCnZvaWQgaW5pdFRyZWUoaW50IG4pewogICAgaW50IGk7CiAgICBmb3IoaT0wO2k8TUFYO2krKyl7CiAgICAgICAgdFtpXSA9IC0xOwogICAgfQp9CiAKdm9pZCBwcmludEEoKXsKICAgIGludCBpOwogICAgZm9yKGk9MTtpPD1zejtpKyspIHByaW50ZigiJWQgIix0W2ldKTsKICAgIHByaW50ZigiXG4iKTsKfQogCnZvaWQgcHJpbnRUKGludCBpKXsKICAgIGludCB4ID0gaTsKICAgIHdoaWxlKHgvMiE9MCl7CiAgICAgICAgcHJpbnRmKCIgICIpOwogICAgICAgIHgvPTI7CiAgICB9CiAgICBwcmludGYoIiVkXG4iLHRbaV0pOwp9CiAKaW50IGdvUChpbnQgaSl7CiAgICBpZihpLzIgPT0gMCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiBpLzI7Cn0KIAppbnQgZ29MKGludCBpKXsKICAgIGlmKDIqaSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippOwp9CiAKaW50IGdvUihpbnQgaSl7CiAgICBpZigyKmkrMSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippKzE7Cn0KIAp2b2lkIHByZU9yZGVyKGludCBpKXsKICAgIGlmKHRbaV0gPT0gLTEpIHJldHVybjsKICAgIHByaW50VChpKTsKICAgIHByZU9yZGVyKGdvTChpKSk7CiAgICBwcmVPcmRlcihnb1IoaSkpOwp9CiAKdm9pZCBpbk9yZGVyKGludCBpKXsKICAgIGlmKHRbaV0gPT0gLTEpIHJldHVybjsKICAgIGluT3JkZXIoZ29MKGkpKTsKICAgIHByaW50VChpKTsKICAgIGluT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgcG9zdE9yZGVyKGludCBpKXsKICAgIGlmKHRbaV0gPT0gLTEpIHJldHVybjsKICAgIHBvc3RPcmRlcihnb0woaSkpOwogICAgcG9zdE9yZGVyKGdvUihpKSk7CiAgICBwcmludFQoaSk7Cn0KIAp2b2lkIGluc0JUKGludCB4KXsKICAgIGludCBrLGkgPSAxOwogICAgZm9yKGs9MDtrPGhlaWdodDtrKyspewogICAgICAgIGlmKHRbaV09PS0xKXsKICAgICAgICAgICAgdFtpXSA9IHg7CiAgICAgICAgICAgIHN6Kys7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgaWYoeCA8IHRbaV0pIGkgPSBnb0woaSk7CiAgICAgICAgZWxzZSBpID0gZ29SKGkpOwogICAgfQogICAgcHJpbnRmKCJFcnJvciA6IHRvbyBoaWdoIC0+ICVkXG4iLHgpOwp9CiAKLy/lhYjpoK3jga7opoHntKDjgpLlj5bjgorlh7rjgZkKLy/jg4Djgqbjg7Pjg5Ljg7zjg5cKaW50IHBvcEhlYXAoKSB7CiAgICBpZiAoc3ogPT0gMCkgewogICAgICAgIHByaW50ZigiaGVhcCBpcyBlbXB0eSFcbiIpOwogICAgICAgIHJldHVybiAtMTsKICAgIH0KICAgIGludCB0b3AgPSB0WzFdOyAKICAgIHRbMV0gPSB0W3N6XTsgICAKICAgIHRbc3pdID0gLTE7ICAgICAKICAgI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KCiAgICAvLyDkuK3plpPpoIbjgafooajnpLoKICAgIGluT3JkZXIoMSk7CgogICAgLy8gcG9wCiAgICBpbnQgYSA9IHBvcEhlYXAoKTsKICAgIHByaW50ZigicG9wIDogJWRcbiIsIGEpOwogICAgaW5PcmRlcigxKTsKCiAgICBhID0gcG9wSGVhcCgpOwogICAgcHJpbnRmKCJwb3AgOiAlZFxuIiwgYSk7CiAgICBpbk9yZGVyKDEpOwoKICAgIHByaW50ZigicHVzaCA6IDEwMFxuIik7CiAgICBwdXNoSGVhcCgxMDApOwogICAgaW5PcmRlcigxKTsKCiAgICBwcmludGYoInB1c2ggOiAzMFxuIik7CiAgICBwdXNoSGVhcCgzMCk7CiAgICBpbk9yZGVyKDEpOwogICAgcmV0dXJuIDA7Cn0K