Advertisement
Garey

danny_zad_4_SAA

Apr 16th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Tree
  6. {
  7.     int data;
  8.     Tree* left;
  9.     Tree* right;
  10. };
  11.  
  12. Tree* dobavi(int data) {
  13.   Tree* node = new Tree;
  14.   node->data = data;
  15.   node->left = NULL;
  16.   node->right = NULL;
  17.    
  18.   return(node);
  19. }
  20.  
  21. void ogledalno(Tree* node)
  22. {
  23.   if (node == nullptr)
  24.     return;  
  25.   else {
  26.     Tree* temp;
  27.      
  28.     ogledalno(node->left);
  29.     ogledalno(node->right);
  30.  
  31.     temp = node->left;
  32.     node->left = node->right;
  33.     node->right = temp;
  34.   }
  35. }
  36.  
  37. void podredi(Tree* node)
  38. {
  39.   if (node == nullptr)
  40.     return;
  41.    
  42.   podredi(node->left);
  43.   cout << node->data << " ";
  44.   podredi(node->right);
  45. }  
  46.  
  47. int main()
  48. {
  49.   Tree *root = dobavi(1);
  50.   root->left = dobavi(2);
  51.   root->right = dobavi(3);
  52.   root->left->left = dobavi(4);
  53.   root->left->right = dobavi(5);
  54.    
  55.   cout << "Dvoichno durvo: \n";
  56.  
  57.   podredi(root);
  58.    
  59.   ogledalno(root);
  60.    
  61.   cout << "\nOgledalnoto durvo: \n";  
  62.   podredi(root);
  63.  
  64.   cout << endl;
  65.   return 0;  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement