Advertisement
Guest User

Untitled

a guest
May 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct noarvore {
  5. struct noarvore* esq;
  6. int valor ;
  7. struct noarvore* dir;
  8. };
  9. typedef struct noarvore No;
  10. typedef No* ArvoreBinaria;
  11.  
  12. void criarArvore(ArvoreBinaria *arvore){
  13. *arvore = NULL;
  14. };
  15. void inserir(ArvoreBinaria *arvore, int novovalor){
  16. if((*arvore) == NULL){
  17. *arvore = (No*) malloc(sizeof(No));
  18. (*arvore)-> esq = NULL;
  19. (*arvore)-> dir = NULL;
  20. (*arvore)-> valor = novovalor;
  21. }
  22. if (novovalor < (*arvore)->valor){
  23. inserir((&(*arvore)->esq),novovalor);
  24. }
  25. else {
  26. if (novovalor > (*arvore)->valor){
  27. inserir(&((*arvore)-> dir),novovalor);
  28. }
  29. }
  30. }
  31.  
  32. void prefixado(ArvoreBinaria no){
  33. if (no != NULL){
  34. printf("%d\n", no ->valor);
  35. if (no->esq != MULL) prefixado(no->esq);
  36. if (no->dir != NULL) prefixado(no->dir);
  37. }
  38. }
  39.  
  40. void central(ArvoreBinaria no){
  41. if (no != NULL){
  42. if (no-> != NULL) central (no->dir esq);
  43. printf("$d\n", no->valor);
  44. if (no->dir != NULL) central (no ->dir);
  45. }
  46. }
  47.  
  48. void posfixado(ArvoreBinaria no){
  49. if (no != NULL){
  50. if (no->esq != NULL) posfixado(no->esq);
  51. if (no->dir != NULL) posfixado(no->dir);
  52. }
  53. }
  54.  
  55. void menu(){
  56. ArvoreBinaria binaria;
  57. criarArvore(&binaria);
  58. int op = 0,valor;
  59. do{
  60. system("CLS");
  61. printf("1- Inserir\n2-Listar CENTRAL\n4-Listar POS\n5-Sair\nEscolha uma opcao:");
  62. scanf("%d", &op);
  63. switch(op){
  64. case 1:printf("valor:\n");
  65. scanf("%d",&valor);
  66. inserir(&binaria,valor);
  67. break;
  68. case 2:prefixado(binaria);
  69. system ("pause");
  70. break;
  71. case 3:central(binaria);
  72. system("pause");
  73. break;
  74. case 4:posfixado(binaria);
  75. system("pause");
  76. break;
  77. }
  78. }while(op != 5);
  79. }
  80.  
  81. int main(int argc, char *argv[])
  82. {
  83. menu();
  84. system("PAUSE");
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement