#include <stdio.h>
#include <stdlib.h>
typedef struct Node* Node;
struct Node {
int value;
Node left;
Node right;
};
typedef struct Tree* Tree;
struct Tree {
Node root;
};
Node Node_new(int value) {
Node s
= malloc(sizeof(struct Node
)); if (s != NULL) {
s->value = value;
s->left = NULL;
s->right = NULL;
}
return s;
}
void Node_add(Node s, Node n) {
if (n->value < s->value) {
if (s->left == NULL) {
s->left = n;
} else {
Node_add(s->left, n);
}
} else {
if (s->right == NULL) {
s->right = n;
} else {
Node_add(s->right, n);
}
}
}
void Node_print(Node s) {
if (s->left != NULL) {
Node_print(s->left);
}
if (s->right != NULL) {
Node_print(s->right);
}
}
void Node_clean(Node s) {
if (s->left != NULL) {
Node_clean(s->left);
}
if (s->right != NULL) {
Node_clean(s->right);
}
}
Tree Tree_new(void) {
Tree s
= malloc(sizeof(struct Tree
)); if (s != NULL) {
s->root = NULL;
}
return s;
}
Node Tree_add(Tree s, int value) {
Node n = Node_new(value);
if (n != NULL) {
if (s->root == NULL) {
s->root = n;
} else {
Node_add(s->root, n);
}
}
return n;
}
void Tree_print(Tree s) {
if (s->root == NULL) {
} else {
Node_print(s->root);
}
}
void Tree_clean(Tree s) {
if (s->root != NULL) {
Node_clean(s->root);
}
}
void use_Tree(Tree t, FILE* f) {
char c[10];
while (fgets(c
, 10, f
) != NULL
) { if (Tree_add
(t
, atoi(c
)) == NULL
) { return;
}
}
Tree_print(t);
}
void use_File(FILE* f) {
Tree t = Tree_new();
if (t != NULL) {
use_Tree(t, f);
Tree_clean(t);
}
}
int main(void) {
FILE* f;
f
= fopen("numbers.txt", "r"); if (f != NULL) {
use_File(f);
}
return EXIT_SUCCESS;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IE5vZGUqIE5vZGU7CnN0cnVjdCBOb2RlIHsKICAgaW50IHZhbHVlOwogICBOb2RlIGxlZnQ7CiAgIE5vZGUgcmlnaHQ7Cn07Cgp0eXB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