Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct tipoNo{
  6. int valor;
  7. struct tipoNo *esq;
  8. struct tipoNo *dir;
  9.  
  10. }TNo;
  11.  
  12. TNo* inserir (TNo *raiz, int valorParametro){
  13.  
  14. if (raiz == NULL){
  15. raiz = new TNo; // criação de um novo no
  16. raiz->valor = valorParametro;
  17. raiz->dir = NULL;
  18. raiz->esq = NULL;
  19. } else{
  20. if (valorParametro < raiz->valor){
  21. raiz->esq = inserir(raiz->esq ,valorParametro);
  22. } else{
  23. raiz->dir = inserir(raiz->dir ,valorParametro);
  24. }
  25. }
  26. return raiz;
  27. }
  28.  
  29. int consultaRecursiva (TNo *raiz ,int itemConsulta){
  30. if (raiz == NULL){
  31. printf("Sua Arvore esta vazia !! n");
  32. system("pause");
  33. return 0;
  34. } else {
  35. if (raiz->valor == itemConsulta){
  36. printf("Achou !!!!");
  37. system("pause");
  38. return 1 ;
  39. } else{
  40. if (raiz->valor > itemConsulta){
  41. return consultaRecursiva(raiz->esq ,itemConsulta);
  42.  
  43. } else{
  44. return consultaRecursiva(raiz->dir ,itemConsulta);
  45.  
  46. }
  47. }
  48. }
  49. }
  50.  
  51. void preOrdem (TNo *raiz_aux){
  52. if (raiz_aux != NULL){
  53. printf("%d", raiz_aux->valor);
  54. preOrdem(raiz_aux->esq);
  55. preOrdem(raiz_aux->dir);
  56. system("pause");
  57. }
  58. }
  59.  
  60.  
  61.  
  62. int main (){
  63. TNo *raiz; // ponteiro do tipo NO
  64. raiz = NULL; // a raiz DEVE estar nula
  65. int op;
  66.  
  67. do {
  68. system("cls");
  69. printf ("INFORME UMA OPCAO ");
  70. printf ("nPara inserir digite 1 ");
  71. printf ("nPara consultar digite 2 ");
  72. printf ("nPara preOrdem digite 3 n");
  73. printf ("Para sair digite 0 : ");
  74. scanf ("%d",&op);
  75.  
  76. if (op == 1){
  77. int novoValor;
  78. printf("Entre com um novo valor : ");
  79. scanf ("%d",&novoValor);
  80. inserir (raiz ,novoValor);
  81. }
  82. if (op == 2){
  83. int itemPesquisa;
  84. printf("Entre com uma elemento para a busca : ");
  85. scanf ("%d",&itemPesquisa);
  86. consultaRecursiva(raiz ,itemPesquisa);
  87.  
  88. }
  89. if (op == 3){
  90.  
  91. preOrdem(raiz);
  92. }
  93.  
  94. }while(op != 0);
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement