Guest User

Untitled

a guest
May 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct tree{
  5. int number;
  6. struct tree *left;
  7. struct tree *right ;
  8. };
  9.  
  10. struct tree *newTree(int number){
  11. struct tree *newElement = (struct tree*) malloc(sizeof(struct tree));
  12. newElement->number = number;
  13. newElement->left = NULL;
  14. newElement->right = NULL;
  15. return newElement;
  16. }
  17.  
  18. void printAllPositiveNumbers(struct tree *root){
  19. if (root != NULL){
  20. printAllPositiveNumbers(root->left);
  21. if (root != NULL){
  22. if (root->number > 0) {
  23. printf("%d |", root->number);
  24. }
  25. }
  26. printAllPositiveNumbers(root->right);
  27. }
  28. }
  29.  
  30. void printAllNegativeNumbers(struct tree *root){
  31. if (root != NULL){
  32. printAllNegativeNumbers(root->left);
  33. if (root != NULL){
  34. if (root->number < 0) {
  35. printf("%d |", root->number);
  36. }
  37. }
  38. printAllNegativeNumbers(root->right);
  39. }
  40. }
  41.  
  42. int main(){
  43. struct tree *root = newTree(1);
  44. root->left = newTree(2);
  45. root->right = newTree(-3);
  46. root->left->left = newTree(4);
  47. root->right->left = newTree(-58);
  48. root->right->right = newTree(-60);
  49. root->right->left->right = newTree(7);
  50. root->right->right->right = newTree(8656);
  51. root->right->left->right->left = newTree(9);
  52.  
  53. puts("All positive numbers from the binary tree:");
  54. printAllPositiveNumbers(root);
  55. puts("");
  56.  
  57. puts("All negative numbers from the binary tree:");
  58. printAllNegativeNumbers(root);
  59. puts("");
  60.  
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment