Advertisement
Zuneve

Binary_Tree

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