fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. { int data;
  6. struct node * left;
  7. struct node * right;
  8. }node;
  9.  
  10. //node * root;
  11.  
  12. node *CreateBST(node *root, int elem)
  13. {
  14. if (root == NULL) {
  15. root = (node *) malloc(sizeof(node));
  16. root->left = root->right = NULL;
  17. root->data = elem;
  18. return root;
  19. }
  20.  
  21. else
  22. {
  23. if (elem < root->data)
  24. root->left = CreateBST(root->left, elem);
  25. else if (elem > root->data)
  26. root->right = CreateBST(root->right, elem);
  27. else
  28. printf(" Duplicate Element !! Not Allowed !!!");
  29.  
  30. return (root);
  31. }
  32. }
  33.  
  34. void Inorder(node *root)
  35. {
  36. if (root != NULL) {
  37. Inorder(root->left);
  38. printf(" %d ", root->data);
  39. Inorder(root->right);
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45. node * root;
  46. root=CreateBST(root, 7);root=CreateBST(root, 5);root=CreateBST(root, 9);root=CreateBST(root, 8);root=CreateBST(root, 4);root=CreateBST(root, 4);
  47.  
  48. Inorder(root);
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 2140KB
stdin
Standard input is empty
stdout
 Duplicate Element !! Not Allowed !!! 4  5  7  8  9