Advertisement
justaCprogrammer

Treefinal.c

Sep 30th, 2022
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct binary_tree
  5. {
  6.     int value;
  7.     struct binary_tree * leftSideOfTree;
  8.     struct binary_tree * rightSideOfTree;
  9. };
  10.  
  11. typedef struct binary_tree tree;
  12.  
  13. struct data
  14. {
  15.     int point;
  16. };
  17.  
  18. typedef struct data pointer;
  19.  
  20. tree * createTree ()
  21. {
  22.     return NULL;
  23. }
  24.  
  25. int ifVoid(tree * source)
  26. {
  27.     return source == NULL;
  28. }
  29.  
  30. void showTree(tree * source)
  31. {
  32.     printf("\n<");
  33.     if(!ifVoid(source))
  34.     {
  35.         printf("%d",source->value);
  36.         showTree(source->leftSideOfTree);
  37.         showTree(source->rightSideOfTree);
  38.     }
  39.     printf(">\n");
  40. }
  41.  
  42. void writeInTree(tree ** source,int tempPut)
  43. {
  44.     fflush(stdin);
  45.     if(*source == NULL)
  46.     {
  47.         *source = (tree*)malloc(sizeof(source));
  48.        (*source)->leftSideOfTree = NULL;
  49.        (*source)->rightSideOfTree = NULL;
  50.        (*source)->value = 0;
  51.        (*source)->value = tempPut;
  52.        printf("Value:%d\n",(*source)->value);
  53.     }else
  54.     {
  55.         if(tempPut >(*source)->value)
  56.         {
  57.             writeInTree(&(*source)->leftSideOfTree,tempPut);
  58.         }
  59.         if(tempPut<(*source)->value)
  60.         {
  61.             writeInTree(&(*source)->rightSideOfTree,tempPut);
  62.         }
  63.     }
  64. }
  65.  
  66.     void putData(pointer * conducter)
  67.     {
  68.         fflush(stdin);
  69.         printf("Digite um numero para adicionar na arvore");
  70.         scanf("%d",&conducter->point);
  71.     }
  72.  
  73.     int isInTheTree (tree * source,int tempPut)
  74.     {
  75.         if(ifVoid(source))
  76.             {
  77.             return 0;
  78.             }
  79.  
  80.         return source ->value == tempPut || isInTheTree(source->leftSideOfTree,tempPut) || isInTheTree(source->rightSideOfTree,tempPut);
  81.     }
  82.  
  83.  
  84. int main ()
  85. {
  86.     int templatenumber = 0,resp = 0;
  87.     pointer * conducter;
  88.     conducter = (pointer*)malloc(sizeof(pointer));
  89.     tree * source = createTree();
  90.     ifVoid(source);
  91.     do
  92.     {
  93.         system("CLS");
  94.         printf("=================|Menu da Arvore|===========================\n");
  95.         printf("-------------------------------------------------------------\n");
  96.         printf("[1] - Adicionar um elemento na arvore\n");
  97.         printf("[2] - Mostrar a Arvore\n");
  98.         printf("[3] - Fechar o programa\n");
  99.         printf("---------------------------------------------------------------\n");
  100.         printf("Sua opcao:");
  101.         scanf("%d",&resp);
  102.  
  103.         switch(resp)
  104.         {
  105.  
  106.         case 1:
  107.         putData(conducter);
  108.         templatenumber = conducter->point;
  109.         writeInTree(&source,templatenumber);
  110.         system("PAUSE");
  111.         break;
  112.  
  113.         case 2:
  114.             isInTheTree(source,templatenumber);
  115.             showTree(source);
  116.             system("PAUSE");
  117.             break;
  118.  
  119.         case 3:
  120.             printf("Encerrando o programa\n");
  121.             break;
  122.  
  123.         default :
  124.             printf("Opcao invalida!\n Escolha uma opcao valida!\n");
  125.             break;
  126.         }
  127.     }while(resp != 3);
  128.  
  129.    return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement