Advertisement
Zuneve

B_yandex

Jun 18th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;
  4. struct node {
  5.     node *l, *r;
  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 == NULL) {
  17.             return NULL;
  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.     int depth(node * cur, int key) {
  49.         if (cur->key == key) return 1;
  50.         else if (key > cur->key) return 1 + depth(cur->r, key);
  51.         return 1 + depth(cur->l, key);
  52.     }
  53.     node *minimum(node *cur) {
  54.         if (cur->l == nullptr) {
  55.             return cur;
  56.         }
  57.         return minimum(cur->l);
  58.     }
  59.     //*(cur).l == cur->l
  60.     node * erase(node *cur, int key) {
  61.         if (cur == nullptr) {
  62.             return cur;
  63.         }
  64.         if (key < cur->key) {
  65.             cur->l = erase(cur->l, key);
  66.         } else if (key > cur->key) {
  67.             cur->r = erase(cur->r, key);
  68.         } else if (cur->l != nullptr && cur->r != nullptr) {
  69.             cur->key = minimum(cur->r)->key;
  70.             cur->r = erase(root->r, cur->key);
  71.         } else {
  72.             if (cur->l != nullptr) {
  73.                 cur = cur->l;
  74.             } else if (cur->r != nullptr) {
  75.                 cur = cur->r;
  76.             } else {
  77.                 cur = nullptr;
  78.             }
  79.         }
  80.     };
  81. public:
  82.     bool search(int key) {
  83.         if (search(root, key) == nullptr) {
  84.             return 0;
  85.         }
  86.         return 1;
  87.     }
  88.  
  89.     void add(int key) {
  90.         if (!search(key)) {
  91.             root = add(root, key);
  92.         }
  93.     }
  94.  
  95.     void print() {
  96.         print(root);
  97.     }
  98.  
  99.     void erase(int key) {
  100.  
  101.         erase(root, key);
  102.     }
  103.     int depth (int x) {
  104.         return depth(root, x);
  105.     }
  106. };
  107.  
  108. int main() {
  109.     binary_tree a;
  110.     int x;
  111.     cin >> x;
  112.     while (x!=0) {
  113.         if (!a.search(x)) {
  114.             a.add(x);
  115.             cout << a.depth(x);
  116.         }
  117.         cin >> x;
  118.     }
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement