Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node
  4. {
  5. int data;
  6. struct Node* left;
  7. struct Node* right;
  8. }Node;
  9.  
  10. int cnt = 0;
  11.  
  12.  
  13. Node* BST_insert(Node* root, int data)
  14. {
  15. if (root == NULL)
  16. {
  17. root = (Node*)malloc(sizeof(Node));
  18. root->left = root->right = NULL;
  19. root->data = data;
  20. return root;
  21. }
  22. else
  23. {
  24. if (root->data > data)
  25. root->left = BST_insert(root->left, data);
  26. else if (root->data < data)
  27. root->right = BST_insert(root->right, data);
  28. else
  29. printf("Unable to create same value.\n");
  30. }
  31. return root;
  32. }
  33.  
  34. Node* BST_search(Node* root, int data)
  35. {
  36. if (root == NULL)
  37. {
  38. printf("No value.\n");
  39. return root;
  40. }
  41. else
  42. {
  43. if (root->data > data)
  44. root->left = BST_search(root->left, data);
  45. else if (root->data < data)
  46. root->right = BST_search(root->right, data);
  47. else if (root->data == data)
  48. printf("%d", root->data);
  49. return root;
  50. }
  51. }
  52.  
  53. Node* BST_minNode(Node* root)
  54. {
  55. Node* node = root;
  56. while (node->left != NULL) {
  57. node = node->left;
  58. }
  59. return node;
  60. }
  61.  
  62. Node* BST_delete(Node* root, int key)
  63. {
  64. Node* temp = NULL;
  65. if (root == NULL)
  66. {
  67. printf("No value.\n");
  68. return NULL;
  69. }
  70. else if (root->data < key)
  71. {
  72. root->right = BST_delete(root->right, key);
  73. }
  74. else if (root->data > key)
  75. {
  76. root->left = BST_delete(root->left, key);
  77. }
  78. else if (root->data == key)
  79. {
  80. if (root->left != NULL && root->right != NULL)
  81. {
  82. temp = BST_minNode(root->right);
  83. root->data = temp->data;
  84. root->right = BST_delete(root->right, temp->data);
  85. }
  86. else {
  87. temp = (root->left == NULL) ? root->right : root->left;
  88. free(root);
  89. return temp;
  90. }
  91. }
  92. return root;
  93. }
  94. void BST_print(Node* root)
  95. {
  96. if (root == NULL)
  97. return;
  98.  
  99. BST_print(root->left);
  100. printf("%d ", root->data);
  101. BST_print(root->right);
  102.  
  103. }
  104.  
  105. int check_num(int *result, int form) {
  106. int i;
  107. int j = 1;
  108. int sign = 1;
  109. int input_size;
  110. char *str;
  111. *result = 0;
  112. str = (char *)malloc(sizeof(char) * form + 2);
  113. while (1) {
  114. sign = 1;
  115. fgets(str, form + 2, stdin);
  116. for (i = 0; i < form + 1; i++) {
  117. if (str[i] == '\n') {
  118. input_size = i;
  119. break;
  120. }
  121. }
  122. if (str[0] == 10) {
  123. printf("Enter the number and press Enter. \n Please re-enter : ");
  124. continue;
  125. }
  126. if (i == form + 1) {
  127. printf("The input range has been exceeded. \n");
  128. while (getchar() != '\n') {
  129. }
  130. printf("Please re-enter : ");
  131. continue;
  132. }
  133. for (i = 0; i < input_size; i++) {
  134. if (str[i] < 48 || str[i] > 57) {
  135. if (str[i] != '\n') {
  136. printf("Only integer greater than 0 can be entered.\n");
  137. printf("Please re-enter : ");
  138. sign = 0;
  139. break;
  140. }
  141. }
  142. }
  143. if (sign == 0) {
  144. continue;
  145. }
  146. for (i = 0; i < input_size; i++) {
  147. *result = *result + (str[input_size - i - 1] - 48) * j;
  148. j = j * 10;
  149. }
  150. break;
  151. }
  152. free(str);
  153. return 0;
  154. }
  155.  
  156.  
  157.  
  158.  
  159. int main(void)
  160. {
  161. int digit = 8;
  162. int select, input_num;
  163. Node* root = NULL;
  164. while (1) {
  165. printf("\n\n0 : exit\n");
  166. printf("1 : Create Node\n");
  167. printf("2 : Delete Node\n");
  168. printf("3 : Print Node\n");
  169. printf("4 : Search Node\n");
  170. printf("Select 0-4 : ");
  171. check_num(&select, digit);
  172. if (select == 0) {
  173. BST_print(root);
  174. system("pause");
  175. return 0;
  176. }
  177. else if (select == 1) {
  178. cnt++;
  179. printf("Please enter a value: ");
  180. check_num(&input_num, digit);
  181. root = BST_insert(root, input_num);
  182. }
  183. else if (select == 2) {
  184. cnt--;
  185. printf("Please enter a value: ");
  186. check_num(&input_num, digit);
  187. root = BST_delete(root, input_num);
  188. }
  189. else if (select == 3) {
  190. BST_print(root);
  191. printf("\n");
  192. system("pause");
  193. }
  194. else if (select == 4) {
  195. printf("Please enter a value: ");
  196. check_num(&input_num, digit);
  197. root = BST_search(root, input_num);
  198. }
  199. else
  200. printf("only 0 ~ 4");
  201. if (cnt == 100) {
  202. BST_print(root);
  203. printf("\nFinish");
  204. system("pause");
  205. break;
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement