Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int value;
  7.     Node* left = nullptr;
  8.     Node* right = nullptr;
  9.     int level;
  10.    
  11.     Node(int value, int level = 1) {
  12.         this->value = value;
  13.         this->level = level;
  14.     }
  15.    
  16.     void add(int value) {
  17.         if (value < this->value) {
  18.             if (left) {
  19.                 left->add(value);
  20.             }
  21.             else {
  22.                 left = new Node(value, level + 1);
  23.             }
  24.         }
  25.         else if (value > this->value) {
  26.             if (right) {
  27.                 right->add(value);
  28.             }
  29.             else {
  30.                 right = new Node(value, level + 1);
  31.             }
  32.         }
  33.     }
  34.    
  35.     void print() {
  36.         cout << value << " ";
  37.         if (left) {
  38.             left->print();
  39.         }
  40.         if (right) {
  41.             right->print();
  42.         }
  43.     }
  44.    
  45.     void print_odd_layers() {
  46.         queue<Node*> q;
  47.         q.push(this);
  48.         while(!q.empty()) {
  49.             Node* current = q.front();
  50.             q.pop();
  51.            
  52.             if (current->level % 2 == 1) {
  53.                 cout << current->value << " ";
  54.             }
  55.            
  56.             if (current->left) {
  57.                 q.push(current->left);
  58.             }
  59.             if (current->right) {
  60.                 q.push(current->right);
  61.             }
  62.         }
  63.     }
  64.  
  65.     Node* remove(Node* current, int value) {
  66.         if (current == nullptr) {
  67.             return nullptr;
  68.         }
  69.        
  70.         if(value < current->value) {
  71.             current->left = remove(current->left, value);
  72.         }
  73.         else if (value > current->value) {
  74.             current->right = remove(current->right, value);
  75.         }
  76.         else {
  77.             if (!current->left && !current->right) {
  78.                 delete current;
  79.                 return nullptr;
  80.             }
  81.             else if(!current->left && current->right) {
  82.                 Node* oldRight = current->right;
  83.                 delete current;
  84.                 return oldRight;
  85.             }
  86.             else if(current->left && !current->right) {
  87.                 Node* oldLeft = current->left;
  88.                 delete current;
  89.                 return oldLeft;
  90.             }
  91.             else {
  92.                 Node* toSwap = current->right;
  93.                 while (toSwap->left != nullptr) {
  94.                     toSwap = toSwap -> left;
  95.                 }
  96.                
  97.                 current->value = toSwap->value;
  98.                 current->right = remove(current->right, toSwap->value);
  99.             }
  100.         }
  101.        
  102.         return current;
  103.     }
  104. };
  105.  
  106. int main() {
  107.     int n;
  108.     cin >> n;
  109.     string input;
  110.     int num;
  111.    
  112.     Node* tree = nullptr;
  113.    
  114.     for (int i = 0; i < n; i++) {
  115.         cin >> input;
  116.        
  117.         if (input == "add") {
  118.             cin >> num;
  119.            
  120.             if (tree) {
  121.                 tree->add(num);
  122.             }
  123.             else {
  124.                 tree = new Node(num);
  125.             }
  126.         }
  127.         else if (input == "print") {
  128.             if (tree) {
  129.                 tree->print();
  130.             }
  131.         }
  132.         else if (input == "print_odd_layers") {
  133.             if (tree) {
  134.                 tree->print_odd_layers();
  135.             }
  136.         }
  137.         else if (input == "remove") {
  138.             cin >> num;
  139.            
  140.             if (tree) {
  141.                 tree = tree->remove(tree, num);
  142.             }
  143.         }
  144.     }
  145.    
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement