Advertisement
allia

обход дерева

Nov 9th, 2020 (edited)
1,063
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int n, m, tek_znach = 0;
  9.   cin >> n;
  10.   int *tree = new int[n];
  11.  
  12.   for (int i = 0; i < n; i++)
  13.     cin >> tree[i];
  14.  
  15.   cin >> m;
  16.   tek_znach = 0;
  17.  
  18. string *word = new string[m];
  19.  
  20. for (int i = 0; i<m; i++)
  21.  cin >> word[i];
  22.  
  23. if (tree[0] != -1)
  24. cout << tree[0] << endl;
  25. else cout << "empty" << endl;
  26.  
  27. for (int i = 0; i < m; i++)
  28.   if (word[i] == "left" && tek_znach*2+1 < n && tree[tek_znach*2+1] != -1)
  29.   {
  30.     tek_znach = tek_znach*2+1;
  31.     cout << tree[tek_znach] << endl;
  32.   }
  33.   else if (word[i] == "left" && tek_znach*2+1 >= n)
  34.     {
  35.       cout << "empty" << endl;
  36.       tek_znach = n;
  37.     }
  38.   else if (word[i] == "left" && tree[tek_znach*2+1] == -1)
  39.    {
  40.      tek_znach = tek_znach*2+1;
  41.      cout << "empty" << endl;
  42.    }
  43.   else if (word[i] == "right" && tek_znach*2+2 < n &&  tree[tek_znach*2+2] != -1)
  44.   {
  45.     tek_znach = tek_znach*2+2;
  46.     cout << tree[tek_znach] << endl;
  47.   }
  48.   else if (word[i] == "right" && tek_znach*2+2 >= n)
  49.     {
  50.       cout << "empty" << endl;
  51.       tek_znach = n;
  52.     }
  53.   else if (word[i] == "right" && tree[tek_znach*2+2] == -1)
  54.    {
  55.      tek_znach = tek_znach*2+2;
  56.      cout << "empty" << endl;
  57.    }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement