Advertisement
noctual

Untitled

Oct 10th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int field;
  7.     Node* bottom;
  8.     Node* right;
  9. };
  10.  
  11. void print_Tree(Node* p, int level)
  12. {
  13.     if (p)
  14.     {
  15.         print_Tree(p->bottom, level + 1);
  16.         for (int i = 0; i < level; i++) cout << "   ";
  17.         cout << p->field << endl;
  18.         print_Tree(p->right, level + 1);
  19.     }
  20. }
  21.  
  22. int getMax(Node* node, bool Max, int level) {
  23.     if (node->bottom != NULL && node->right != NULL) {
  24.         int bottom = getMax(node->bottom, !Max, level+1);
  25.         int right = getMax(node->right, !Max, level + 1);
  26.         cout << string(level*3, '-') << node->field << ' ' << ((Max && (bottom > right)) || (!Max && (bottom < right)) ? bottom : right) + node->field << (Max ? " max" : " min") << '\n';
  27.         return ((Max && (bottom > right)) || (!Max && (bottom < right)) ? bottom : right) + node->field;
  28.     }
  29.     else {
  30.         if (node->bottom != NULL) {
  31.             int bottom = getMax(node->bottom, !Max, level + 1);
  32.             cout << string(level * 3, '-') << node->field << ' ' << bottom + node->field << ' ' << (Max ? " max" : " min") << '\n';
  33.             return bottom + node->field;
  34.         }
  35.         else if (node->right != NULL) {
  36.             int right = getMax(node->right, !Max, level + 1);
  37.             cout << string(level * 3, '-') << node->field << ' ' << right + node->field << (Max ? " max" : " min") << '\n';
  38.             return right + node->field;
  39.         }
  40.     }
  41.     cout << string(level * 3, '-') << node->field  << ' ' << node->field << (Max ? " max" : " min") << '\n';
  42.     return node->field;
  43. }
  44.  
  45. int main() {
  46.     ios::sync_with_stdio(0);
  47.     cin.tie(0);
  48.  
  49.     int N=3, M=4;
  50.  
  51.     Node*** nodeMass = new Node**[N];
  52.     for (int i = 0; i < N; i++) {
  53.         nodeMass[i] = new Node * [M];
  54.         for (int j = 0; j < M; j++) {
  55.             nodeMass[i][j] = new Node;
  56.             cin >> nodeMass[i][j]->field;
  57.         }
  58.     }
  59.  
  60.     for (int i = 0; i < N; i++) {
  61.         for (int j = 0; j < M; j++) {
  62.             nodeMass[i][j]->bottom = i < N - 1 ? nodeMass[i + 1][j] : NULL;
  63.             nodeMass[i][j]->right = j < M - 1 ? nodeMass[i][j + 1] : NULL;
  64.         }
  65.     }
  66.  
  67.     print_Tree(nodeMass[0][0], 0);
  68.  
  69.     for (int i = 0; i < N; i++) {
  70.         for (int j = 0; j < M; j++) {
  71.             cout << nodeMass[i][j]->field << ' ';
  72.         }
  73.         cout << '\n';
  74.     }
  75.  
  76.     cout << getMax(nodeMass[0][0], true, 0) << '\n';
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement