kburnik

C++ - Binarno stablo i algoritmi obilaska

Oct 6th, 2012
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. TEMA : Binarno stablo i tri tipicna obilaska
  4.  
  5. Autor zadatka: Kristijan Burnik
  6.  
  7. Autor rjesenja: Kristijan Burnik
  8.  
  9. Datum rjesavanja: 2012-10-06
  10.      
  11. Primjer:
  12. 5            
  13. 10 20 30 40 50            
  14. 2
  15. 1 2 3        
  16. 2 4 5      
  17.  
  18. */
  19. #include <iostream>
  20. #include <cstdlib>
  21. #include <vector>
  22.  
  23. using namespace std;
  24.  
  25. typedef struct node {
  26.         int val;
  27.         node *left;
  28.         node *right;        
  29. } node;
  30.  
  31. /////////////////////////////////
  32.  
  33. void handle(node *p) {
  34.      cout << p->val << " ";
  35. }
  36.  
  37. void preorder(node *p) {
  38.      if (p == NULL) return;
  39.      handle(p);
  40.      preorder(p->left);
  41.      preorder(p->right);
  42. }
  43.  
  44. void inorder(node *p) {
  45.      if (p == NULL) return;
  46.      inorder(p->left);
  47.      handle(p);
  48.      inorder(p->right);
  49. }
  50.  
  51. void postorder(node *p) {
  52.      if (p == NULL) return;
  53.      postorder(p->left);
  54.      postorder(p->right);
  55.      handle(p);
  56. }
  57.  
  58.  
  59.  
  60. int main() {
  61.  
  62.     int n;
  63.     cin >> n;
  64.    
  65.     vector < node > stablo;    
  66.     stablo.resize( n + 1 ) ;
  67.     for (int i = 1;  i <= n; i++) {
  68.         cin >> stablo[i].val;
  69.     }
  70.    
  71.     int m;
  72.     cin >> m;
  73.     int p,l,r;
  74.     for (int i = 0 ; i < m ; i++) {
  75.         cin >> p >> l >> r;
  76.         stablo[p].left = &stablo[l];
  77.         stablo[p].right  = &stablo[r];
  78.     }
  79.    
  80.    
  81.     cout << "preorder: ";
  82.     preorder(&stablo[1]);
  83.     cout << endl;
  84.    
  85.     cout << "inorder: ";
  86.     inorder(&stablo[1]);
  87.     cout << endl;
  88.    
  89.     cout << "postorder: ";
  90.     postorder(&stablo[1]);
  91.     cout << endl;
  92.  
  93.  
  94.     system("pause");
  95.     return 0;
  96. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×