// 2bun-ki
#include <stdio.h>
#include <stdlib.h>
#define height 4
#define MAX (1<<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
<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 height -> %d\n",x
); }
void printT(int i){
int x = i;
while(x/2!=0){
x/=2;
}
}
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);
}
int main(void){
int i,x,n;
initTree(n);
for(i=0;i<n;i++){
insBT(x);
}
preOrder(1);
inOrder(1);
printf("== postOrder ====\n"); postOrder(1);
return 0;
}
Ly8gMmJ1bi1raQoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgaGVpZ2h0IDQKI2RlZmluZSBNQVggKDE8PGhlaWdodCkgCgppbnQgdFtNQVgrMV07IC8v6YWN5YiX5aSW44Ki44Kv44K744K56Ziy5q2i44Gu44Gf44KB44Gu44OA44Of44O844Gn77yL77yRCmludCBzeiA9IDA7Cgp2b2lkIHN3YXAoaW50ICp4LCBpbnQgKnkpewogICAgaW50IHRtcCA9ICp4OwogICAgKnggPSAqeTsKICAgICp5ID0gdG1wOwp9Cgp2b2lkIGluaXRUcmVlKGludCBuKXsKICAgIGludCBpOwogICAgZm9yKGk9MDtpPE1BWDtpKyspewogICAgICAgIHRbaV0gPSAtMTsKICAgIH0KfQoKdm9pZCBwcmludEEoKXsKICAgIGludCBpOwogICAgZm9yKGk9MTtpPE1BWDtpKyspIHByaW50ZigiJWQgIix0W2ldKTsKICAgIHByaW50ZigiXG4iKTsKfQoKaW50IGdvUChpbnQgaSl7CiAgICBpZihpLzIgPT0gMCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiBpLzI7Cn0KCmludCBnb0woaW50IGkpewogICAgaWYoMippID49IE1BWCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiAyKmk7Cn0KCmludCBnb1IoaW50IGkpewogICAgaWYoMippKzEgPj0gTUFYKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIDIqaSsxOwp9Cgp2b2lkIGluc0JUKGludCB4KXsKICAgIGludCBrLGkgPSAxOwogICAgZm9yKGs9MDtrPGhlaWdodDtrKyspewogICAgICAgIGlmKHRbaV09PS0xKXsKICAgICAgICAgICAgdFtpXSA9IHg7CiAgICAgICAgICAgIHN6Kys7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgaWYoeCA8IHRbaV0pIGkgPSBnb0woaSk7CiAgICAgICAgZWxzZSBpID0gZ29SKGkpOwogICAgfQogICAgcHJpbnRmKCJFcnJvciA6IHRvbyBoZWlnaHQgLT4gJWRcbiIseCk7Cn0KCnZvaWQgcHJpbnRUKGludCBpKXsKICAgIGludCB4ID0gaTsKICAgIHdoaWxlKHgvMiE9MCl7CiAgICAgICAgcHJpbnRmKCIgICIpOwogICAgICAgIHgvPTI7CiAgICB9CiAgICBwcmludGYoIiVkXG4iLHRbaV0pOwp9Cgp2b2lkIHByZU9yZGVyKGludCBpKXsKCS8vCglpZih0W2ldPT0tMSl7CgkJcmV0dXJuOwoJfQoJcHJpbnRUKGkpOwoJ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