#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;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KIAojZGVmaW5lIGhlaWdodCA0CiNkZWZpbmUgTUFYICgxPDxoZWlnaHQpIAogCmludCB0W01BWCsxXTsgLy/phY3liJflpJbjgqLjgq/jgrvjgrnpmLLmraLjga7jgZ/jgoHjga7jg4Djg5/jg7zjgafvvIvvvJEKaW50IHN6ID0gMDsKIAp2b2lkIHN3YXAoaW50ICp4LCBpbnQgKnkpewogICAgaW50IHRtcCA9ICp4OwogICAgKnggPSAqeTsKICAgICp5ID0gdG1wOwp9CiAKdm9pZCBpbml0VHJlZShpbnQgbil7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7aTxNQVg7aSsrKXsKICAgICAgICB0W2ldID0gLTE7CiAgICB9Cn0KIAp2b2lkIHByaW50QSgpewogICAgaW50IGk7CiAgICBmb3IoaT0xO2k8TUFYO2krKykgcHJpbnRmKCIlZCAiLHRbaV0pOwogICAgcHJpbnRmKCJcbiIpOwp9CiAKaW50IGdvUChpbnQgaSl7CiAgICBpZihpLzIgPT0gMCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiBpLzI7Cn0KIAppbnQgZ29MKGludCBpKXsKICAgIGlmKDIqaSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippOwp9CiAKaW50IGdvUihpbnQgaSl7CiAgICBpZigyKmkrMSA+PSBNQVgpIHJldHVybiAwOwogICAgZWxzZSByZXR1cm4gMippKzE7Cn0KIAp2b2lkIGluc0JUKGludCB4KXsKICAgIGludCBrLGkgPSAxOwogICAgZm9yKGs9MDtrPGhlaWdodDtrKyspewogICAgICAgIGlmKHRbaV09PS0xKXsKICAgICAgICAgICAgdFtpXSA9IHg7CiAgICAgICAgICAgIHN6Kys7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgaWYoeCA8IHRbaV0pIGkgPSBnb0woaSk7CiAgICAgICAgZWxzZSBpID0gZ29SKGkpOwogICAgfQogICAgcHJpbnRmKCJFcnJvciA6IHRvbyBoZWlnaHQgLT4gJWRcbiIseCk7Cn0KIAp2b2lkIHByaW50VChpbnQgaSl7CiAgICBpbnQgeCA9IGk7CiAgICB3aGlsZSh4LzIhPTApewogICAgICAgIHByaW50ZigiICAiKTsKICAgICAgICB4Lz0yOwogICAgfQogICAgcHJpbnRmKCIlZFxuIix0W2ldKTsKfQogCnZvaWQgcHJlT3JkZXIoaW50IGkpewoJaWYodFtpXT09LTEpIHJldHVybiA7CglwcmludFQoaSk7CglwcmVPcmRlcihnb0woaSkpOwoJcHJlT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgaW5PcmRlcihpbnQgaSl7CglpZih0W2ldPT0tMSkgcmV0dXJuIDsKCWluT3JkZXIoZ29MKGkpKTsKCXByaW50VChpKTsKCWluT3JkZXIoZ29SKGkpKTsKfQogCnZvaWQgcG9zdE9yZGVyKGludCBpKXsKCWlmKHRbaV09PS0xKSByZXR1cm4gOwoJcG9zdE9yZGVyKGdvTChpKSk7Cglwb3N0T3JkZXIoZ29SKGkpKTsKCXByaW50VChpKTsKfQogCmludCBtYWluKHZvaWQpewogICAgaW50IGkseCxuOwogICAgc2NhbmYoIiVkIiwmbik7CiAgICBpbml0VHJlZShuKTsKICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgc2NhbmYoIiVkIiwmeCk7CiAgICAgICAgaW5zQlQoeCk7CiAgICB9CiAgICBwcmludGYoIj09IHByZU9yZGVyID09PT1cbiIpOwogICAgcHJlT3JkZXIoMSk7CiAgICBwcmludGYoIlxuIik7CiAgICBwcmludGYoIj09IGluT3JkZXIgPT09PVxuIik7CiAgICBpbk9yZGVyKDEpOwogICAgcHJpbnRmKCJcbiIpOwogICAgcHJpbnRmKCI9PSBwb3N0T3JkZXIgPT09PVxuIik7CiAgICBwb3N0T3JkZXIoMSk7CiAgICBwcmludGYoIlxuIik7CiAgICByZXR1cm4gMDsKfQog