Advertisement
ludaludaed

avl

Dec 19th, 2022 (edited)
2,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. struct Node {
  2.     int height{1};
  3.     Node *left{nullptr};
  4.     Node *right{nullptr};
  5.     int value{};
  6. };
  7.  
  8. class AVLTree {
  9.     Node *root_;
  10.     int count_;
  11.  
  12. public:
  13.     AVLTree() {
  14.         root_ = nullptr;
  15.         count_ = 0;
  16.     }
  17.  
  18.     int getHeight() {
  19.         if (root_ == nullptr) {
  20.             return 0;
  21.         } else {
  22.             return root_->height;
  23.         }
  24.     }
  25.  
  26.     void insert(int value) {
  27.         root_ = insert(root_, value);
  28.         count_++;
  29.     }
  30.  
  31.     void erase(int value) {
  32.         root_ = erase(root_, value);
  33.         count_--;
  34.     }
  35.  
  36.     int *find(int value) {
  37.         auto node = find(root_, value);
  38.         if (node == nullptr) {
  39.             return nullptr;
  40.         }
  41.         return &node->value;
  42.     }
  43.  
  44.     int *traversal() {
  45.         int *array = new int[count_];
  46.         int index = 0;
  47.         traversal(array, &index, root_);
  48.         return array;
  49.     }
  50.  
  51.     int *lowerBound(int value) {
  52.         Node *current = root_;
  53.         int *res = nullptr;
  54.         while (current != nullptr) {
  55.             if (value >= current->value) {
  56.                 if (current->value >= value) {
  57.                     return &(current->value);
  58.                 }
  59.                 current = current->right;
  60.             } else {
  61.                 res = &current->value;
  62.                 current = current->left;
  63.             }
  64.         }
  65.         return res;
  66.     }
  67.  
  68.     bool empty() const {
  69.         return count_ == 0;
  70.     }
  71.  
  72.     Node *getRoot() {
  73.         return root_;
  74.     }
  75.  
  76.     int getSize() const {
  77.         return count_;
  78.     }
  79.  
  80.     ~AVLTree() {
  81.         clear(root_);
  82.     }
  83.  
  84. private:
  85.     int height(const Node *node) const {
  86.         if (node == nullptr) {
  87.             return 0;
  88.         }
  89.         return node->height;
  90.     }
  91.  
  92.     int bfactor(const Node *node) {
  93.         return height(node->right) - height(node->left);
  94.     }
  95.  
  96.     Node *insert(Node *root, int value) {
  97.         if (root == nullptr) {
  98.             Node *new_node = new Node;
  99.             new_node->value = value;
  100.             return new_node;
  101.         }
  102.         if (value < root->value) {
  103.             root->left = insert(root->left, value);
  104.         } else if (value > root->value) {
  105.             root->right = insert(root->right, value);
  106.         }
  107.         return balance(root);
  108.     }
  109.  
  110.     Node *eraseMin(Node *root) {
  111.         if (root->left == nullptr) {
  112.             return root->right;
  113.         }
  114.         root->left = eraseMin(root->left);
  115.         return balance(root);
  116.     }
  117.  
  118.     Node *erase(Node *root, int value) {
  119.         if (root == nullptr) {
  120.             return nullptr;
  121.         }
  122.         if (value < root->value) {
  123.             root->left = erase(root->left, value);
  124.         } else if (value > root->value) {
  125.             root->right = erase(root->right, value);
  126.         } else {
  127.             if (root->left == nullptr && root->right == nullptr) {
  128.                 delete root;
  129.                 return nullptr;
  130.             } else if (root->left == nullptr) {
  131.                 Node *right = root->right;
  132.                 delete root;
  133.                 Node *min_node = min(right);
  134.                 min_node->right = eraseMin(right);
  135.                 return balance(min_node);
  136.             } else if (root->right == nullptr) {
  137.                 Node *left = root->left;
  138.                 delete root;
  139.                 return balance(left);
  140.             } else {
  141.                 Node *right = root->right;
  142.                 Node *left = root->left;
  143.                 delete root;
  144.                 Node *min_node = min(right);
  145.                 min_node->right = eraseMin(right);
  146.                 min_node->left = left;
  147.                 return balance(min_node);
  148.             }
  149.         }
  150.         return balance(root);
  151.     }
  152.  
  153.     Node *find(Node *root, int value) {
  154.         if (root == nullptr) {
  155.             return nullptr;
  156.         }
  157.         if (value < root->value) {
  158.             return find(root->left, value);
  159.         } else if (value > root->value) {
  160.             return find(root->right, value);
  161.         } else {
  162.             return root;
  163.         }
  164.     }
  165.  
  166.     static Node *min(Node *root) {
  167.         Node *current = root->left;
  168.         while (current != nullptr) {
  169.             root = current;
  170.             current = current->left;
  171.         }
  172.         return root;
  173.     }
  174.  
  175.     Node *rightRotate(Node *node) {
  176.         Node *new_root = node->left;
  177.         node->left = new_root->right;
  178.         new_root->right = node;
  179.         node->height = std::max(height(node->left), height(node->right)) + 1;
  180.         new_root->height = std::max(height(new_root->left), height(new_root->right)) + 1;
  181.         return new_root;
  182.     }
  183.  
  184.     Node *leftRotate(Node *node) {
  185.         Node *new_root = node->right;
  186.         node->right = new_root->left;
  187.         new_root->left = node;
  188.         node->height = std::max(height(node->left), height(node->right)) + 1;
  189.         new_root->height = std::max(height(new_root->left), height(new_root->right)) + 1;
  190.         return new_root;
  191.     }
  192.  
  193.     Node *balance(Node *node) {
  194.         node->height = std::max(height(node->left), height(node->right)) + 1;
  195.         if (bfactor(node) == 2) {
  196.             if (bfactor(node->right) < 0) {
  197.                 node->right = rightRotate(node->right);
  198.             }
  199.             return leftRotate(node);
  200.         }
  201.         if (bfactor(node) == -2) {
  202.             if (bfactor(node->left) > 0) {
  203.                 node->left = leftRotate(node->left);
  204.             }
  205.             return rightRotate(node);
  206.         }
  207.         return node;
  208.     }
  209.  
  210.     void traversal(int *array, int *index, Node *node) {
  211.         if (node == nullptr) {
  212.             return;
  213.         }
  214.         traversal(array, index, node->left);
  215.         array[(*index)++] = node->value;
  216.         traversal(array, index, node->right);
  217.     }
  218.  
  219.     void clear(Node *node) {
  220.         if (node == nullptr) {
  221.             return;
  222.         }
  223.         clear(node->left);
  224.         clear(node->right);
  225.         delete node;
  226.     }
  227. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement