Advertisement
Zuneve

sgssdgdsg

Jun 18th, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. struct node {
  5.     node *l= nullptr, *r= nullptr;
  6.     int key;
  7.     node(int k) {
  8.         this->key = k;
  9.     }
  10. };
  11.  
  12. struct binary_tree {
  13. private:
  14.     node *root = nullptr;
  15.     node * search(node * cur, int k) {
  16.         if (cur == nullptr) {
  17.             return nullptr;
  18.         }
  19.         if (k == cur->key) {
  20.             return cur;
  21.         }
  22.  
  23.         if (k < cur->key) {
  24.             return search(cur->l, k);
  25.         } else {
  26.             return search(cur->r, k);
  27.         }
  28.     }
  29.     node * add(node *cur, int key) {
  30.         if (cur == NULL) {
  31.             return new node(key);
  32.         }
  33.         if (key < cur->key) {
  34.             cur->l = add(cur->l, key);
  35.         } else {
  36.             cur->r = add(cur->r, key);
  37.         }
  38.         return cur;
  39.     }
  40.  
  41.     void print(node *cur) {
  42.         if (cur != nullptr) {
  43.             print(cur->l);
  44.             cout << cur->key << " ";
  45.             print(cur->r);
  46.         }
  47.     }
  48.     node *minimum(node *cur) {
  49.         if (cur->l == nullptr) {
  50.             return cur;
  51.         }
  52.         return minimum(cur->l);
  53.     }
  54.     node *maximim(node *cur) {
  55.         if (cur->r == nullptr) return cur;
  56.         return maximim(cur->r);
  57.     }
  58.     //*(cur).l == cur->l
  59.     node * erase(node *cur, int key) {
  60.         if (cur == nullptr) {
  61.             return cur;
  62.         }
  63.         if (key < cur->key) {
  64.             cur->l = erase(cur->l, key);
  65.         } else if (key > cur->key) {
  66.             cur->r = erase(cur->r, key);
  67.         } else if (cur->l != nullptr && cur->r != nullptr) {
  68.             cur->key = minimum(cur->r)->key;
  69.             cur->r = erase(root->r, cur->key);
  70.         } else {
  71.             if (cur->l != nullptr) {
  72.                 cur = cur->l;
  73.             } else if (cur->r != nullptr) {
  74.                 cur = cur->r;
  75.             } else {
  76.                 cur = nullptr;
  77.             }
  78.         } return cur;
  79.     };
  80. public:
  81.     bool search(int key) {
  82.         if (search(root, key) == nullptr) {
  83.             return 0;
  84.         }
  85.         return 1;
  86.     }
  87.  
  88.     void add(int key) {
  89.         if (!search(key)) {
  90.             root = add(root, key);
  91.         }
  92.     }
  93.  
  94.     void print() {
  95.         print(root);
  96.     }
  97.  
  98.     void erase(int key) {
  99.  
  100.         erase(root, key);
  101.     }
  102.     int maximum() {
  103.         return maximim(root)->key;
  104.     }
  105. };
  106.  
  107. int main() {
  108.     binary_tree a;
  109.     int x;
  110.     cin >> x;
  111.     while (x!=0) {
  112.         a.add(x);
  113.         cin >> x;
  114.     } a.erase(a.maximum());
  115.     cout << a.maximum();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement