Advertisement
Guest User

Untitled

a guest
Oct 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NODE_VALUES_1_SIZE 7
  5. #define NODE_VALUES_2_SIZE 5
  6.  
  7. /* Nome: Reberth
  8. *
  9. * Turma: Regina - RA: 141589
  10. *
  11. * */
  12.  
  13. typedef struct BinaryNode {
  14. int value;
  15. struct BinaryNode *left;
  16. struct BinaryNode *right;
  17. } BNode;
  18.  
  19. BNode *createBinaryNode(int value, BNode *left, BNode *right) {
  20. BNode *newNode = (BNode *) malloc(sizeof(BNode));
  21. newNode->value = value;
  22. newNode->left = left;
  23. newNode->right = right;
  24. return newNode;
  25. }
  26.  
  27. BNode *createABBNode(BNode *root, int value) {
  28. /*
  29. * root =>
  30. * - Is NULL
  31. * Create binary node with no childs
  32. * - Is not NULL
  33. * Check if the new child was be placed on the left/right
  34. * */
  35. if (root == NULL) {
  36. return createBinaryNode(
  37. value,
  38. NULL,
  39. NULL
  40. );
  41. } else if (root->value > value) {
  42. return createBinaryNode(
  43. root->value,
  44. createABBNode(
  45. root->left,
  46. value
  47. ),
  48. root->right
  49. );
  50. } else if (root->value < value) {
  51. return createBinaryNode(
  52. root->value,
  53. root->left,
  54. createABBNode(
  55. root->right,
  56. value
  57. )
  58. );
  59. }
  60. return root;
  61. }
  62.  
  63.  
  64. void infixBinaryRoot(BNode *root) {
  65. /*
  66. * Print on infix order
  67. * */
  68. if (root != NULL) {
  69. infixBinaryRoot(root->left);
  70. printf("%i ", root->value);
  71. infixBinaryRoot(root->right);
  72. }
  73. }
  74.  
  75. BNode *clearBinaryRoot(BNode *root) {
  76. /*
  77. * Clear node, last to first.
  78. * */
  79. if (root != NULL) {
  80. clearBinaryRoot(root->left);
  81. clearBinaryRoot(root->right);
  82. free(root);
  83. }
  84. return NULL;
  85. }
  86.  
  87. BNode *createBinaryRoot(BNode *root, int *nodeValues, int size) {
  88. for (int i = 0; i < size; i++) {
  89. root = createABBNode(root, nodeValues[i]);
  90. }
  91. return root;
  92. }
  93.  
  94. /* função verif_se_ABB */
  95. int checkIfIsABBBinary(BNode *root) {
  96. if (root != NULL) {
  97. /* Existe Lado esquerdo, não existe lado direito */
  98. /* Não existe lado esquerdo, existe lado direito */
  99. /* Não existe lado esquerdo, não existe lado direito */
  100. /* Existe lado esquerdo, existe lado direito */
  101.  
  102. if (root->left != NULL && root->right != NULL) {
  103. if (root->left->value > root->value || root->right->value < root->value) {
  104. return 0;
  105. } else {
  106. return 1;
  107. }
  108. } else if (root->left != NULL && root->right == NULL) {
  109. if (root->left->value > root->value) {
  110. return 0;
  111. } else {
  112. return 1;
  113. }
  114. } else if (root->left == NULL && root->right != NULL) {
  115. if (root->right->value < root->value) {
  116. return 0;
  117. } else {
  118. return 1;
  119. }
  120. } else {
  121. return 1;
  122. }
  123. } else {
  124. return 0;
  125. }
  126. }
  127.  
  128. /*função une_ABB*/
  129. BNode *combineABBs(BNode *root, BNode *bRoot1, BNode *bRoot2) {
  130. /* bRoot1 and bRoot2 =>
  131. * - Both are different than NULL;
  132. * -> Create ABB Node then call combineABBs again (for left and right of bRoot1/bRoot2)
  133. * - bRoot2 is NULL;
  134. * -> Create ABB Node then call combineABBs again (for left and right of bRoot2)
  135. * - bRoot1 is NULL;
  136. * -> Create ABB node then call combine ABBs again (for left and right of bRoot1);
  137. * - Both are NULL
  138. * -> Return root
  139. * */
  140. if (bRoot1 != NULL && bRoot2 != NULL) {
  141. if (bRoot1->value > bRoot2->value) {
  142. root = createABBNode(root, bRoot1->value);
  143. root = combineABBs(root, bRoot1->left, bRoot2);
  144. root = combineABBs(root, bRoot1->right, bRoot2);
  145. } else if (bRoot1->value < bRoot2->value) {
  146. root = createABBNode(root, bRoot2->value);
  147. root = combineABBs(root, bRoot1, bRoot2->left);
  148. root = combineABBs(root, bRoot1, bRoot2->right);
  149. } else {
  150. root = createABBNode(root, bRoot1->value);
  151. root = combineABBs(root, bRoot1->left, bRoot2->left);
  152. root = combineABBs(root, bRoot1->right, bRoot2->right);
  153. }
  154. } else if (bRoot1 != NULL && bRoot2 == NULL) {
  155. root = createABBNode(root, bRoot1->value);
  156. root = combineABBs(root, bRoot1->left, NULL);
  157. root = combineABBs(root, bRoot1->right, NULL);
  158. } else if (bRoot1 == NULL && bRoot2 != NULL) {
  159. root = createABBNode(root, bRoot2->value);
  160. root = combineABBs(root, NULL, bRoot2->left);
  161. root = combineABBs(root, NULL, bRoot2->right);
  162. }
  163. return root;
  164. }
  165.  
  166. int main() {
  167. int nodeValues1[] = {10, 8, 5, 9, 4, 15, 20};
  168. int nodeValues2[] = {8, 4, 20, 15, 25};
  169. BNode *binaryRoot1 = NULL;
  170. BNode *binaryRoot2 = NULL;
  171.  
  172. BNode *combinedBinaryRoot = NULL;
  173.  
  174. printf("Initializing program...\n");
  175.  
  176. binaryRoot1 = createBinaryRoot(binaryRoot1, nodeValues1, NODE_VALUES_1_SIZE);
  177. binaryRoot2 = createBinaryRoot(binaryRoot2, nodeValues2, NODE_VALUES_2_SIZE);
  178.  
  179. if (binaryRoot1 == NULL || binaryRoot2 == NULL) {
  180. printf("Error on initializing program. Please try again...\n");
  181. return 0;
  182. } else {
  183. printf("Programa iniciado com sucesso!\n");
  184. }
  185.  
  186. printf("Infix binary root 1 =>");
  187. infixBinaryRoot(binaryRoot1);
  188.  
  189. printf("\nInfix binary root 2 =>");
  190. infixBinaryRoot(binaryRoot2);
  191.  
  192. printf("\nBinary root 1 is ABB? => %i\n", checkIfIsABBBinary(binaryRoot1));
  193.  
  194. combinedBinaryRoot = combineABBs(combinedBinaryRoot, binaryRoot1, binaryRoot2);
  195. if (combinedBinaryRoot == NULL) {
  196. printf("Error on combine binarys ABB. Please try again.\n");
  197. } else {
  198. printf("Combined infix combined binary root => ");
  199. infixBinaryRoot(combinedBinaryRoot);
  200. }
  201.  
  202. printf("\nStarting clean of program...\n");
  203. binaryRoot1 = clearBinaryRoot(binaryRoot1);
  204. binaryRoot2 = clearBinaryRoot(binaryRoot2);
  205. printf("Successful clean program...\n");
  206. printf("Exiting...\n");
  207. printf("Bye!\n");
  208. return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement