fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int value;
  7. struct Node *left;
  8. struct Node *right;
  9. };
  10.  
  11. struct Node *new_node(int v)
  12. {
  13. struct Node *n = malloc(sizeof(*n));
  14. n->value = v;
  15. n->left = NULL;
  16. n->right = NULL;
  17. return n;
  18. }
  19.  
  20. struct Node *add_node(struct Node *n, int v)
  21. {
  22. if (n == NULL)
  23. {
  24. return new_node(v);
  25. }
  26. else
  27. {
  28. if (v < n->value)
  29. {
  30. n->left = add_node(n->left, v);
  31. }
  32. else
  33. {
  34. n->right = add_node(n->right, v);
  35. }
  36.  
  37. return n;
  38. }
  39. }
  40.  
  41. int exists_node(struct Node *n, int v)
  42. {
  43. if (n == NULL)
  44. {
  45. return 0;
  46. }
  47. else
  48. {
  49. if (v < n->value)
  50. {
  51. return exists_node(n->left, v);
  52. }
  53. else if (n->value < v)
  54. {
  55. return exists_node(n->right, v);
  56. }
  57. else
  58. {
  59. return 1;
  60. }
  61. }
  62. }
  63.  
  64. int min_node(struct Node *n)
  65. {
  66. if (n->left == NULL)
  67. {
  68. return n->value;
  69. }
  70. else
  71. {
  72. return min_node(n->left);
  73. }
  74. }
  75.  
  76. int main() {
  77. int i;
  78. int v;
  79. int n;
  80. FILE *f;
  81. struct Node *t;
  82.  
  83. t = NULL;
  84. f = fopen("input.txt", "r");
  85. fscanf(f, "%d", &n);
  86. for (i = 0; i < n; i++)
  87. {
  88. fscanf(f, "%d", &v);
  89. t = add_node(t, v);
  90. }
  91. fclose(f);
  92.  
  93. printf("> ");
  94. scanf("%d", &v);
  95.  
  96. if (exists_node(t, v))
  97. {
  98. printf("yes\n");
  99. }
  100. else
  101. {
  102. printf("no\n");
  103. }
  104.  
  105. if (t != NULL)
  106. {
  107. printf("%d\n", min_node(t));
  108. }
  109.  
  110. return 0;
  111. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty