Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5. int code;
  6. struct node *left, *right;
  7. } Node;
  8.  
  9. Node * createNode()
  10. {
  11. Node *new = (Node *) malloc(sizeof(Node));
  12. printf("Code: ");
  13. scanf("%d", &new->code);
  14. new->left = NULL;
  15. new->right = NULL;
  16.  
  17. return new;
  18. }
  19.  
  20. Node * insNode(Node *root, Node *new)
  21. {
  22. if (root == NULL) return new;
  23. else if (new->code < root->code) root->left = insNode(root->left, new);
  24. else root->right = insNode(root->right, new);
  25. return root;
  26. }
  27.  
  28. void prntTree(Node *root)
  29. {
  30. if (root == NULL) return;
  31. prntTree(root->left);
  32. printf("%d ", root->code);
  33. prntTree(root->right);
  34. }
  35.  
  36. Node * searchTree(Node *root, int targ)
  37. {
  38. while (root != NULL){
  39. if (root->code == targ){
  40. printf("Product found. Code: %d", root->code);
  41. return root;
  42. }
  43. if (targ < root->code) root = root->left;
  44. else root = root->right;
  45. }
  46. printf("Product not found.");
  47. return NULL;
  48. }
  49.  
  50. Node * findClosest(Node *root)
  51. {
  52. Node *big, *small, *aux;
  53.  
  54. aux = root->left;
  55. while (1){
  56. if (aux->right == NULL){
  57. big = aux;
  58. break;
  59. }
  60. aux = aux->right;
  61. }
  62.  
  63. aux = root->right;
  64. while (1){
  65. if (aux->left == NULL){
  66. small = aux;
  67. break;
  68. }
  69. aux = aux->left;
  70. }
  71.  
  72. if (root->code - big->code < small->code - root->code) return big;
  73. else return small;
  74. }
  75.  
  76. void delNode(Node *root)
  77. {
  78. Node *leaf, **nroot;
  79. int found = 0, target;
  80.  
  81. printf("\nNode to be removed: ");
  82. scanf("%d", &target);
  83. printf("\n");
  84.  
  85. while (root != NULL){
  86. if (root->code == target){
  87. nroot = &root;
  88. found = 1;
  89. break;
  90. }
  91. if (target < root->code) root = root->left;
  92. else root = root->right;
  93. }
  94.  
  95. if (!found){
  96. printf("Node not found.");
  97. return;
  98. }
  99.  
  100. if ((*nroot)->left == NULL && (*nroot)->right == NULL) *nroot = NULL;
  101. else if ((*nroot)->left != NULL) *nroot = (*nroot)->left;
  102. else if ((*nroot)->right != NULL) *nroot = (*nroot)->right;
  103. else {
  104. leaf = findClosest(*nroot);
  105. *nroot = leaf;
  106. leaf = NULL;
  107. free(leaf);
  108. }
  109. if (*nroot == NULL) free(*nroot);
  110. return;
  111. }
  112.  
  113. int main()
  114. {
  115. Node *root = NULL;
  116. for (int i=0; i<10; i++){
  117. if (root == NULL) root = insNode(root, createNode());
  118. else insNode(root, createNode());
  119. }
  120. printf("\n");
  121. prntTree(root);
  122. printf("\n");
  123. delNode(root);
  124. prntTree(root);
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement