// 2bun-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 initTree(int n){
int i;
for(i=0;i<MAX;i++){
t[i] = -1;
}
}
void printA(){
int i;
for(i
=1;i
<MAX
;i
++) printf("%d ",t
[i
]); }
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 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 searchBT(int x){
//ここを書く
int k,i=1;
for(k=0; k<height; k++){
if(t[i]==x){
return i;
}
if(x < t[i]) i = goL(i);
else i = goR(i);
}
return -1;
}
int main(void){
int i,x,y,n;
initTree(n);
for(i=0;i<n;i++){
insBT(y);
}
i = searchBT(x);
if(i
==-1) printf("x is not found\n"); else printf("t[%d] = %d\n",i
,x
); return 0;
}
Ly8gMmJ1bi1raQoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgaGVpZ2h0IDQKI2RlZmluZSBNQVggKDE8PGhlaWdodCkgIC8v44OT44OD44OI44K344OV44OI5ryU566XIDJeaGVpZ2h0IOOBqOWQjOOBmAoKaW50IHRbTUFYKzFdOyAvL+mFjeWIl+WkluOCouOCr+OCu+OCuemYsuatouOBruOBn+OCgeOBruODgOODn+ODvOOBp++8i++8kQppbnQgc3ogPSAwOwoKCnZvaWQgaW5pdFRyZWUoaW50IG4pewogICAgaW50IGk7CiAgICBmb3IoaT0wO2k8TUFYO2krKyl7CiAgICAgICAgdFtpXSA9IC0xOwogICAgfQp9Cgp2b2lkIHByaW50QSgpewogICAgaW50IGk7CiAgICBmb3IoaT0xO2k8TUFYO2krKykgcHJpbnRmKCIlZCAiLHRbaV0pOwogICAgcHJpbnRmKCJcbiIpOwp9CgppbnQgZ29QKGludCBpKXsKICAgIGlmKGkvMiA9PSAwKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIGkvMjsKfQoKaW50IGdvTChpbnQgaSl7CiAgICBpZigyKmkgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaTsKfQoKaW50IGdvUihpbnQgaSl7CiAgICBpZigyKmkrMSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippKzE7Cn0KCnZvaWQgaW5zQlQoaW50IHgpewogICAgaW50IGssaSA9IDE7CiAgICBmb3Ioaz0wO2s8aGVpZ2h0O2srKyl7CiAgICAgICAgaWYodFtpXT09LTEpewogICAgICAgICAgICB0W2ldID0geDsKICAgICAgICAgICAgc3orKzsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZih4IDwgdFtpXSkgaSA9IGdvTChpKTsKICAgICAgICBlbHNlIGkgPSBnb1IoaSk7CiAgICB9CiAgICBwcmludGYoIkVycm9yIDogdG9vIGhpZ2ggLT4gJWRcbiIseCk7Cn0KCmludCBzZWFyY2hCVChpbnQgeCl7CgkvL+OBk+OBk+OCkuabuOOBjwoJaW50IGssaT0xOwoJZm9yKGs9MDsgazxoZWlnaHQ7IGsrKyl7CgkgaWYodFtpXT09eCl7CgkgICByZXR1cm4gaTsKCSB9CgkgaWYoeCA8IHRbaV0pIGkgPSBnb0woaSk7CiAgICAgICAgZWxzZSBpID0gZ29SKGkpOwoJfQoJCglyZXR1cm4gLTE7Cn0KCmludCBtYWluKHZvaWQpewogICBpbnQgaSx4LHksbjsKICAgIHNjYW5mKCIlZCAlZCIsJm4sJngpOwogICAgaW5pdFRyZWUobik7CiAgICBmb3IoaT0wO2k8bjtpKyspewogICAgICAgIHNjYW5mKCIlZCIsJnkpOwogICAgICAgIGluc0JUKHkpOwogICAgfQogICAgaSA9IHNlYXJjaEJUKHgpOwogICAgaWYoaT09LTEpIHByaW50ZigieCBpcyBub3QgZm91bmRcbiIpOwogICAgZWxzZSBwcmludGYoInRbJWRdID0gJWRcbiIsaSx4KTsKICAgIHJldHVybiAwOwp9Cg==