#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;
};
void Tree_add(Tree tree, int value) {
Node n;
Node p;
n
= malloc(sizeof(struct Node
)); n->value = value;
n->left = NULL;
n->right = NULL;
if (tree->root == NULL) {
tree->root = n;
} else {
p = tree->root;
while (1) {
if (value < p->value) {
if (p->left == NULL) {
p->left = n;
break;
} else {
p = p->left;
}
} else {
if (p->right == NULL) {
p->right = n;
break;
} else {
p = p->right;
}
}
}
}
}
void Tree_dump(Tree tree) {
}
int main(void) {
Tree tree
= malloc(sizeof(struct Tree
)); Tree_add(tree, 3);
Tree_add(tree, 1);
Tree_add(tree, 5);
Tree_add(tree, 0);
Tree_add(tree, 2);
Tree_add(tree, 4);
Tree_add(tree, 6);
Tree_dump(tree);
return EXIT_SUCCESS;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IE5vZGUqIE5vZGU7CnN0cnVjdCBOb2RlIHsKICAgaW50IHZhbHVlOwogICBOb2RlIGxlZnQ7CiAgIE5vZGUgcmlnaHQ7Cn07Cgp0eXBlZGVmIHN0cnVjdCBUcmVlKiBUcmVlOwpzdHJ1Y3QgVHJlZSB7CiAgIE5vZGUgcm9vdDsKfTsKCnZvaWQgVHJlZV9hZGQoVHJlZSB0cmVlLCBpbnQgdmFsdWUpIHsKICAgTm9kZSBuOwogICBOb2RlIHA7CiAgIAogICBuID0gbWFsbG9jKHNpemVvZihzdHJ1Y3QgTm9kZSkpOwogICBuLT52YWx1ZSA9IHZhbHVlOwogICBuLT5sZWZ0ID0gTlVMTDsKICAgbi0+cmlnaHQgPSBOVUxMOwogICAKICAgaWYgKHRyZWUtPnJvb3QgPT0gTlVMTCkgewogICAgICB0cmVlLT5yb290ID0gbjsKICAgfSBlbHNlIHsKICAgICAgcCA9IHRyZWUtPnJvb3Q7CiAgICAgIHdoaWxlICgxKSB7CiAgICAgICAgIGlmICh2YWx1ZSA8IHAtPnZhbHVlKSB7CiAgICAgICAgICAgIGlmIChwLT5sZWZ0ID09IE5VTEwpIHsKICAgICAgICAgICAgICAgcC0+bGVmdCA9IG47CiAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICBwID0gcC0+bGVmdDsKICAgICAgICAgICAgfQogICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBpZiAocC0+cmlnaHQgPT0gTlVMTCkgewogICAgICAgICAgICAgICBwLT5yaWdodCA9IG47CiAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICBwID0gcC0+cmlnaHQ7CiAgICAgICAgICAgIH0KICAgICAgICAgfQogICAgICB9CiAgIH0KfQoKdm9pZCBUcmVlX2R1bXAoVHJlZSB0cmVlKSB7Cgp9CgppbnQgbWFpbih2b2lkKSB7CiAgIFRyZWUgdHJlZSA9IG1hbGxvYyhzaXplb2Yoc3RydWN0IFRyZWUpKTsKICAgVHJlZV9hZGQodHJlZSwgMyk7CiAgIFRyZWVfYWRkKHRyZWUsIDEpOwogICBUcmVlX2FkZCh0cmVlLCA1KTsKICAgVHJlZV9hZGQodHJlZSwgMCk7CiAgIFRyZWVfYWRkKHRyZWUsIDIpOwogICBUcmVlX2FkZCh0cmVlLCA0KTsKICAgVHJlZV9hZGQodHJlZSwgNik7CiAgIFRyZWVfZHVtcCh0cmVlKTsKICAgcmV0dXJuIEVYSVRfU1VDQ0VTUzsKfQo=