#include <stdio.h>
#include <stdlib.h>
typedef struct node
{ int data;
struct node * left;
struct node * right;
}node;
//node * root;
node *CreateBST(node *root, int elem)
{
if (root == NULL) {
root
= (node
*) malloc(sizeof(node
)); root->left = root->right = NULL;
root->data = elem;
return root;
}
else
{
if (elem < root->data)
root->left = CreateBST(root->left, elem);
else if (elem > root->data)
root->right = CreateBST(root->right, elem);
else
printf(" Duplicate Element !! Not Allowed !!!");
return (root);
}
}
void Inorder(node *root)
{
if (root != NULL) {
Inorder(root->left);
Inorder(root->right);
}
}
int main()
{
node * root;
root=CreateBST(root, 7);root=CreateBST(root, 5);root=CreateBST(root, 9);root=CreateBST(root, 8);root=CreateBST(root, 4);root=CreateBST(root, 4);
Inorder(root);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IG5vZGUKeyBpbnQgZGF0YTsKICBzdHJ1Y3Qgbm9kZSAqIGxlZnQ7CiAgc3RydWN0IG5vZGUgKiByaWdodDsKfW5vZGU7CgovL25vZGUgKiByb290OwoKbm9kZSAqQ3JlYXRlQlNUKG5vZGUgKnJvb3QsIGludCBlbGVtKSAKewogaWYgKHJvb3QgPT0gTlVMTCkgewogIHJvb3QgPSAobm9kZSAqKSBtYWxsb2Moc2l6ZW9mKG5vZGUpKTsKICByb290LT5sZWZ0ID0gcm9vdC0+cmlnaHQgPSBOVUxMOwogIHJvb3QtPmRhdGEgPSBlbGVtOwogIHJldHVybiByb290OwogfSAKIAogZWxzZSAKIHsKICBpZiAoZWxlbSA8IHJvb3QtPmRhdGEpCiAgIHJvb3QtPmxlZnQgPSBDcmVhdGVCU1Qocm9vdC0+bGVmdCwgZWxlbSk7CiAgZWxzZSBpZiAoZWxlbSA+IHJvb3QtPmRhdGEpCiAgIHJvb3QtPnJpZ2h0ID0gQ3JlYXRlQlNUKHJvb3QtPnJpZ2h0LCBlbGVtKTsKICBlbHNlCiAgIHByaW50ZigiIER1c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