#include <stdlib.h>
#include <stdio.h>
typedef struct node{
int data;
node *left;
node *right;
node *parent;
} node;
void InorderTraversal(struct node *root){
if(root != NULL){
InorderTraversal(root->left);
printf("%d ", root->data);
InorderTraversal(root->right);
}
}
void TreeInsert(struct node *root, struct node *z){
struct node *y = NULL;
struct node *x = root;
while(x != NULL){
y = x;
if(z->data < x->data)
x = x->left;
else
x = x->right;
}
z->parent = y;
if(y == NULL)
root = z;
else{
if(z->data < y->data)
y->left = z;
else
y->right = z;
}
}
int main(void) {
node *tree;
node *p;
tree = NULL;
for(int i = 0; i < 10; i++){
p = (struct node *)malloc(sizeof(struct node));
p->data = i;
p->left = NULL;
p->right = NULL;
TreeInsert(tree, p);
}
InorderTraversal(tree);
return 0;
}
I2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RkaW8uaD4KCnR5cGVkZWYgc3RydWN0IG5vZGV7CglpbnQgZGF0YTsKCW5vZGUgKmxlZnQ7Cglub2RlICpyaWdodDsKCW5vZGUgKnBhcmVudDsKfSBub2RlOwoKdm9pZCBJbm9yZGVyVHJhdmVyc2FsKHN0cnVjdCBub2RlICpyb290KXsKCWlmKHJvb3QgIT0gTlVMTCl7CgkJSW5vcmRlclRyYXZlcnNhbChyb290LT5sZWZ0KTsKCQlwcmludGYoIiVkICIsIHJvb3QtPmRhdGEpOwoJCUlub3JkZXJUcmF2ZXJzYWwocm9vdC0+cmlnaHQpOwoJfQp9Cgp2b2lkIFRyZWVJbnNlcnQoc3RydWN0IG5vZGUgKnJvb3QsIHN0cnVjdCBub2RlICp6KXsKCXN0cnVjdCBub2RlICp5ID0gTlVMTDsKCXN0cnVjdCBub2RlICp4ID0gcm9vdDsKCgl3aGlsZSh4ICE9IE5VTEwpewoJCXkgPSB4OwoJCWlmKHotPmRhdGEgPCB4LT5kYXRhKQoJCQl4ID0geC0+bGVmdDsKCQllbHNlCgkJCXggPSB4LT5yaWdodDsKCX0KCXotPnBhcmVudCA9IHk7CglpZih5ID09IE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