#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct NODE
{
int val;
struct NODE *left;
struct NODE *right;
};
struct NODE *root = NULL;
void insert(int val)
{
struct NODE *new_node;
struct NODE *p;
new_node
= malloc(sizeof(struct NODE
)); new_node->val = val;
new_node->left = new_node->right = NULL;
if(root == NULL){
root = new_node;
}
else{
p = root;
while(p != NULL){
if(val < p->val){
if(p->left == NULL){
p->left = new_node;
break;
}
p = p->left;
}
else{
if(p->right == NULL){
p->right = new_node;
break;
}
p = p->right;
}
}
}
}
void print(struct NODE *n)
{
if(n == NULL){
return;
}
print(n->left);
print(n->right);
}
void insertValues(FILE *f)
{
char c[10];
while (fgets(c
, 10, f
) != NULL
) { }
}
int main(void)
{
FILE *f;
f
= fopen("numbers.txt", "r"); insertValues(f);
print(root);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKc3RydWN0IE5PREUKewogIGludCB2YWw7CiAgc3RydWN0IE5PREUgKmxlZnQ7CiAgc3RydWN0IE5PREUgKnJpZ2h0Owp9OwpzdHJ1Y3QgTk9ERSAqcm9vdCA9IE5VTEw7Cgp2b2lkIGluc2VydChpbnQgdmFsKQp7CiAgc3RydWN0IE5PREUgKm5ld19ub2RlOwogIHN0cnVjdCBOT0RFICpwOwoKICBuZXdfbm9kZSA9IG1hbGxvYyhzaXplb2Yoc3RydWN0IE5PREUpKTsKICBuZXdfbm9kZS0+dmFsID0gdmFsOwogIG5ld19ub2RlLT5sZWZ0ID0gbmV3X25vZGUtPnJpZ2h0ID0gTlVMTDsKCiAgaWYocm9vdCA9PSBOVUxMKXsKICAgIHJvb3QgPSBuZXdfbm9kZTsKICB9CiAgZWxzZXsgICAgIAogICAgcCA9IHJvb3Q7CiAgICB3aGlsZShwICE9IE5VTEwpewogICAgICBpZih2YWwgPCBwLT52YWwpewogICAgICAgIGlmKHAtPmxlZnQgPT0gTlVMTCl7Ci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