Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include "binary_tree.h"
  2.  
  3. struct binaryTree
  4. {
  5. int value;
  6. struct binaryTree *left;
  7. struct binaryTree *right;
  8. };
  9.  
  10. BinaryTree* create_empty_binary_tree()
  11. {
  12. return NULL;
  13. }
  14.  
  15. int is_empty(BinaryTree *bt)
  16. {
  17. return bt==NULL;
  18. }
  19.  
  20. BinaryTree* create_binary_tree(int value, BinaryTree *left, BinaryTree *right)
  21. {
  22. BinaryTree *bt = (BinaryTree*)malloc(sizeof(BinaryTree));
  23. bt->value = value;
  24. bt->left = left;
  25. bt->right = right;
  26. return bt;
  27. }
  28.  
  29. void print_in_order(BinaryTree *bt)
  30. {
  31. if (!is_empty(bt))
  32. {
  33. print_in_order(bt->left);
  34. printf("%d ", bt->value);
  35. print_in_order(bt->right);
  36. }
  37. }
  38.  
  39. void print_pre_order(BinaryTree *bt)
  40. {
  41. if (!is_empty(bt))
  42. {
  43. printf("%d ", bt->value);
  44. print_pre_order(bt->left);
  45. print_pre_order(bt->right);
  46. }
  47. }
  48.  
  49. void print_post_order(BinaryTree *bt)
  50. {
  51. if (!is_empty(bt))
  52. {
  53. print_pre_order(bt->left);
  54. print_pre_order(bt->right);
  55. printf("%d ", bt->value);
  56. }
  57. }
  58.  
  59. BinaryTree* search(BinaryTree *bt, int value)
  60. {
  61. if ((bt == NULL) || (bt->value == value))
  62. {
  63. return bt;
  64. }
  65. else if (bt->value > value)
  66. {
  67. return search(bt->left, value);
  68. }
  69. else
  70. {
  71. return search(bt->right, value);
  72. }
  73. }
  74.  
  75. BinaryTree* add(BinaryTree *bt, int value)
  76. {
  77. if (bt == NULL)
  78. {
  79. bt = create_binary_tree(value, NULL, NULL);
  80. }
  81. else if (bt->value > value)
  82. {
  83. bt->left = add(bt->left, value);
  84. }
  85. else
  86. {
  87. bt->right = add(bt->right, value);
  88. }
  89. return bt;
  90. }
  91. //ADICIONAR NÓ, MAS USANDO PONTEIRO DUPLO
  92. /*
  93. void add(BinaryTree **bt, int value)
  94. {
  95. if (*bt == NULL)
  96. {
  97. *bt = create_binary_tree(value, NULL, NULL);
  98. }
  99. else if ((*bt)->value > value)
  100. {
  101. add(&(*bt)->left, value);
  102. }
  103. else
  104. {
  105. add(&(*bt)->right, value);
  106. }
  107. }
  108. */
  109. int main()
  110. {
  111. BinaryTree *bt = create_binary_tree(6, create_binary_tree(4, NULL, NULL), create_binary_tree(9, create_binary_tree(1, NULL, NULL), create_binary_tree(2, NULL, NULL)));
  112. puts("***ARVORE IN ORDER***");
  113. print_in_order(bt);
  114. puts("\n");
  115. puts("***ARVORE IN PRE ORDER***");
  116. print_pre_order(bt);
  117. puts("\n");
  118. puts("***ARVORE IN POST ORDER***");
  119. print_post_order(bt);
  120. puts("\n");
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement