Advertisement
justaCprogrammer

binarytreetry1.c

Sep 8th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 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\n",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. pointer * conducter;
  45. conducter = (pointer*)malloc(sizeof(pointer));
  46. tempPut = conducter->point;
  47. *source = (tree*)malloc(sizeof(source));
  48. if(*source == NULL)
  49. {
  50. (*source)->leftSideOfTree = NULL;
  51. (*source)->rightSideOfTree = NULL;
  52. (*source)->value = tempPut;
  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. printf("Digite um numero para adicionar na arvore");
  69. scanf("%d",&conducter->point);
  70. }
  71.  
  72. int isInTheTree (tree * source,int tempPut)
  73. {
  74. if(ifVoid(source))
  75. {
  76. return 0;
  77. }
  78.  
  79. return source ->value == tempPut || isInTheTree(source->leftSideOfTree,tempPut) || isInTheTree(source->rightSideOfTree,tempPut);
  80. }
  81.  
  82.  
  83. int main ()
  84. {
  85. int templatenumber,resp = 0;
  86. pointer * conducter;
  87. conducter = (pointer*)malloc(sizeof(pointer));
  88. tree * source = createTree();
  89.  
  90. do
  91. {
  92. system("CLS");
  93. printf("=================|Menu da Arvore|===========================\n");
  94. printf("-------------------------------------------------------------\n");
  95. printf("[1] - Adicionar um elemento na arvore\n");
  96. printf("[2] - Mostrar a Arvore\n");
  97. printf("[3] - Fechar o programa\n");
  98. printf("---------------------------------------------------------------\n");
  99. printf("Sua opcao:");
  100. scanf("%d",&resp);
  101.  
  102. switch(resp)
  103. {
  104.  
  105. case 1:
  106. putData(conducter);
  107. ifVoid(source);
  108. writeInTree(&source,templatenumber);
  109. break;
  110.  
  111. case 2:
  112. isInTheTree(source,templatenumber);
  113. showTree(source);
  114. system("PAUSE");
  115. break;
  116.  
  117. case 3:
  118. printf("Encerrando o programa\n");
  119. break;
  120.  
  121. default :
  122. printf("Opcao invalida!\n Escolha uma opcao valida!\n");
  123. break;
  124. }
  125. }while(resp != 3);
  126.  
  127. return 0;
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement