D_L3

Дървесни операции(втора част)

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