Advertisement
arsovski

Mirror Tree

Jan 23rd, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include<queue>
  7. #include<string>
  8. #include<iomanip>
  9. using namespace std;
  10.  
  11. struct Node {
  12.  
  13.     int data;
  14.     Node* left;
  15.     Node* right;
  16.  
  17.     Node(int data) {
  18.         this->data = data;
  19.         left = nullptr;
  20.         right = nullptr;
  21.     }
  22. };
  23.  
  24. class BST {
  25.  
  26.     Node* root = nullptr;
  27.  
  28.     Node* _add(Node* tmp, int data) {
  29.         if (tmp == nullptr)
  30.             return new Node(data);
  31.  
  32.         if (tmp->data > data)
  33.             tmp->left = _add(tmp->left, data);
  34.         if (tmp->data < data)
  35.             tmp->right = _add(tmp->right, data);
  36.  
  37.         return tmp;
  38.  
  39.     }
  40.  
  41.     Node* _remove(Node* tmp, int data) {
  42.  
  43.         if (tmp == nullptr)
  44.             return nullptr;
  45.  
  46.         if (tmp->data > data)
  47.             tmp->left = _remove(tmp->left, data);
  48.         else if (tmp->data < data)
  49.             tmp->right = _remove(tmp->right, data);
  50.         else
  51.         {
  52.             if (!tmp->left && !tmp->right) {
  53.                 //leaf
  54.                 delete tmp;
  55.                 return nullptr;
  56.             }
  57.             else if (!tmp->left) {
  58.                 Node* del = tmp;
  59.                 tmp = tmp->right;
  60.                 delete del;
  61.                 del = nullptr;
  62.                 return tmp;
  63.  
  64.             }
  65.             else if (!tmp->right) {
  66.                 Node*del = tmp;
  67.                 tmp = tmp->left;
  68.                 delete del;
  69.                 del = nullptr;
  70.                 return tmp;
  71.             }
  72.             else {
  73.                 //has both nodes;
  74.  
  75.                 //take minimal from right and swap with current
  76.  
  77.                 Node* minimal = tmp->right;
  78.                 while (minimal->left != nullptr)
  79.                     minimal = minimal->left;
  80.  
  81.                 std::swap(tmp->data, minimal->data);
  82.  
  83.                 tmp->right = _remove(tmp->right, minimal->data);
  84.  
  85.             }
  86.         }
  87.  
  88.         return tmp;
  89.     }
  90.  
  91.     void _print(Node* tmp) {
  92.         if (tmp == nullptr)
  93.             return;
  94.  
  95.         std::cout << tmp->data << " ";
  96.         _print(tmp->left);
  97.         _print(tmp->right);
  98.     }
  99.  
  100.     void _print_odd_layers(Node* tmp) {
  101.  
  102.         if (tmp == nullptr)
  103.             return;
  104.  
  105.         int level = 1;
  106.         std::queue< std::pair<Node*, int> > next_to_process;
  107.         next_to_process.push({ tmp,level });
  108.  
  109.         while (!next_to_process.empty()) {
  110.             Node* next = next_to_process.front().first;
  111.             int current_level = next_to_process.front().second;
  112.             next_to_process.pop();
  113.  
  114.             if (next)
  115.                 if (current_level % 2 != 0)
  116.                     std::cout << next->data << " ";
  117.  
  118.             if (next->left)
  119.                 next_to_process.push({ next->left,current_level + 1 });
  120.             if (next->right)
  121.                 next_to_process.push({ next->right,current_level + 1 });
  122.         }
  123.     }
  124.  
  125.     Node* _mirror_tree(Node* tmp)
  126.     {
  127.         if (tmp == nullptr)
  128.             return nullptr;
  129.  
  130.         _mirror_tree(tmp->left);
  131.         _mirror_tree(tmp->right);
  132.         Node* right = tmp->right;
  133.         tmp->right = tmp->left;
  134.         tmp->left = right;
  135.  
  136.         return tmp;
  137.     }
  138.  
  139. public:
  140.  
  141.     BST() {
  142.  
  143.     }
  144.  
  145.     void add(int data) {
  146.         this->root = _add(this->root, data);
  147.     }
  148.  
  149.  
  150.     void print() {
  151.         _print(this->root);
  152.     }
  153.  
  154.     void remove(int data) {
  155.         this->root = _remove(this->root, data);
  156.     }
  157.  
  158.     void print_odd_layers() {
  159.         _print_odd_layers(this->root);
  160.     }
  161.  
  162.     void mirror_tree()
  163.     {
  164.         this->root = _mirror_tree(this->root);
  165.     }
  166. };
  167.  
  168.  
  169. int main() {
  170.     /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  171.  
  172.     BST* tree = new BST();
  173.     int operations;
  174.     std::cin >> operations;
  175.  
  176.     for (int i = 0; i < operations; i++) {
  177.         std::string operation;
  178.         std::cin >> operation;
  179.  
  180.  
  181.         if (operation == "add") {
  182.             int number;
  183.             std::cin >> number;
  184.             tree->add(number);
  185.         }
  186.  
  187.     }
  188.  
  189.  
  190.     tree->mirror_tree();
  191.     tree->print();
  192.  
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement