argentinapb

Untitled

Jun 25th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int elemento;
  7. struct Node *dir;
  8. struct Node *esq;
  9. };
  10. typedef struct Node node;
  11.  
  12. node *aloca(int elemento)
  13. {
  14. node *novo =(node*) malloc(sizeof(node));
  15. novo->dir = NULL;
  16. novo->esq = NULL;
  17. novo->elemento = elemento;
  18.  
  19. return novo;
  20.  
  21. }
  22. void inserir(node *estrutura, int elemento)
  23. {
  24. if(elemento < estrutura->elemento)
  25. {
  26. if(estrutura->esq == NULL)
  27. {
  28. estrutura->esq = (aloca(elemento));
  29. }
  30. else
  31. inserir(estrutura->esq,elemento);
  32. }
  33.  
  34. if(elemento > estrutura->elemento)
  35. {
  36. if(estrutura->dir == NULL)
  37. {
  38. estrutura->dir = aloca(elemento);
  39. }
  40. else
  41. inserir(estrutura->dir,elemento);
  42. }
  43. }
  44.  
  45. void em_ordem(node *estrutura)
  46. {
  47. if(estrutura == NULL ){
  48. return;
  49. }
  50. em_ordem(estrutura->esq);
  51.  
  52. printf("%d ",estrutura->elemento);
  53. em_ordem(estrutura->dir);
  54. }
  55.  
  56. void pre_ordem(node *estrutura)
  57. {
  58. if(estrutura == NULL)
  59. return;
  60. printf("%d ",estrutura->elemento);
  61. pre_ordem(estrutura->esq);
  62. pre_ordem(estrutura->dir);
  63. }
  64.  
  65. void pos_ordem(node *estrutura)
  66. {
  67. if(estrutura == NULL)
  68. return;
  69. pos_ordem(estrutura->esq);
  70. pos_ordem(estrutura->dir);
  71. printf("%d ",estrutura->elemento);
  72. }
  73.  
  74. node *buscar(node *estrutura, int elemento)
  75. {
  76. if(estrutura == NULL || estrutura->elemento == elemento)
  77. return estrutura;
  78. if(estrutura->elemento > elemento)
  79. return buscar(estrutura->esq,elemento);
  80. else
  81. return buscar(estrutura->dir,elemento);
  82. }
  83.  
  84. node *encontrar_substituto(node* p)
  85. {
  86. node* aux = p;
  87.  
  88. /* loop down to find the leftmost leaf */
  89. while (aux && aux->esq != NULL)
  90. aux = aux->esq;
  91.  
  92. return aux;
  93. }
  94.  
  95. node* remover(node* p, int x)
  96. {
  97. // caso base
  98. if (p == NULL) return p;
  99.  
  100. // If the key to be deleted is smaller than the root's key
  101. if (x < p->elemento)
  102. p->esq = remover(p->esq, x);
  103.  
  104. // If the key to be deleted is greater than the root's key,
  105. // then it lies in right subtree
  106. else if (x > p->elemento)
  107. p->dir = remover(p->dir, x);
  108.  
  109. // if key is same as root's key, then This is the node
  110. // to be deleted
  111. else
  112. {
  113. // node with only one child or no child
  114. if (p->esq == NULL)
  115. {
  116. node *temp = p->dir;
  117. free(p);
  118. return temp;
  119. }
  120. else if (p->dir == NULL)
  121. {
  122. node *temp = p->esq;
  123. free(p);
  124. return temp;
  125. }
  126.  
  127. // node with two children: Get the inorder successor (smallest
  128. // in the right subtree)
  129. node* temp = encontrar_substituto(p->dir);
  130.  
  131. // Copy the inorder successor's content to this node
  132. p->elemento = temp->elemento;
  133.  
  134. // Delete the inorder successor
  135. p->dir = remover(p->dir, temp->elemento);
  136. }
  137. return p;
  138. }
  139.  
  140. int contar_nos(node *p)
  141. {
  142. int cnt = 1;
  143.  
  144. if(p == NULL)
  145. return 0;
  146.  
  147. cnt += contar_nos(p->dir);
  148. cnt += contar_nos(p->esq);
  149.  
  150. return cnt;
  151.  
  152. }
  153.  
  154. int main()
  155. {
  156. node arvore;
  157. arvore.dir = NULL;
  158. arvore.esq = NULL;
  159.  
  160. printf("Digite o elemento da raiz da arvore: ");
  161. scanf("%d",&arvore.elemento);
  162.  
  163. inserir(&arvore,2);
  164. inserir(&arvore,1);
  165. inserir(&arvore,3);
  166.  
  167. inserir(&arvore,6);
  168. inserir(&arvore,5);
  169. inserir(&arvore,7);
  170.  
  171. printf("Em ordem: "); em_ordem(&arvore); printf("\n");
  172.  
  173.  
  174. printf("\nContar: %d",contar_nos(&arvore));
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment