Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- struct Node {
- int value;
- int height = 1;
- Node* left = nullptr;
- Node* right = nullptr;
- explicit Node(int v) {
- value = v;
- }
- Node() {}
- };
- struct AVL {
- Node* head = nullptr;
- int Height(Node* x){
- if(x == nullptr){
- return 0;
- }
- return x->height;
- }
- void fixHeight(Node* x){
- if(x == nullptr){
- return;
- }
- x->height = std::max(Height(x->left), Height(x->right)) + 1;
- }
- int CheckBalance(Node* x){
- if(x == nullptr){
- return 0;
- }
- return Height(x->right) - Height(x->left);
- }
- Node* RotateRight(Node* x){
- Node* y = x->left;
- x->left = y->right;
- y->right = x;
- fixHeight(x);
- fixHeight(y);
- return y;
- }
- Node* RotateLeft(Node* x){
- Node* y = x->right;
- x->right = y->left;
- y->left = x;
- fixHeight(x);
- fixHeight(y);
- return y;
- }
- Node* BalanceTree(Node* x){
- fixHeight(x);
- if(CheckBalance(x) == 2){
- if(CheckBalance(x->right) < 0){
- x->right = RotateRight(x->right);
- }
- return RotateLeft(x);
- }
- if(CheckBalance(x) == -2){
- if(CheckBalance(x->left) > 0){
- x->left = RotateLeft(x->left);
- }
- return RotateRight(x);
- }
- return x;
- }
- Node* Insert(Node* x, int value){
- if(x == nullptr){
- return new Node(value);
- }
- if(x->value > value){
- x->left = Insert(x->left, value);
- }
- else {
- x->right = Insert(x->right, value);
- }
- return BalanceTree(x);
- }
- bool Exists(int value){
- if(head == nullptr){
- return false;
- }
- Node* head_copy = head;
- while(head_copy != nullptr){
- if(value == head_copy->value){
- return true;
- }
- else if(head_copy->value > value){
- head_copy = head_copy->left;
- }
- else{
- head_copy = head_copy->right;
- }
- }
- return false;
- }
- Node* remove(int value, Node* head) {
- if (head == nullptr) {
- return nullptr;
- }
- if (head->value == value) {
- if (head->left == nullptr && head->right == nullptr) {
- delete head;
- return nullptr;
- }
- if (head->left == nullptr && head->right != nullptr) {
- Node* temp = head->right;
- delete head;
- return temp;
- }
- if (head->left != nullptr && head->right == nullptr) {
- Node* temp = head->left;
- delete head;
- return temp;
- }
- // Если осталось два ребенка
- Node* max_deleted = head->left;
- head->height--;
- if (head->left->right == nullptr) {
- head->left->height--;
- }
- while (max_deleted->right != nullptr) {
- max_deleted->height--;
- max_deleted = max_deleted;
- }
- head->value = max_deleted->value;
- head->left = remove(max_deleted->value, head->left);
- } else if (head->value > value) {
- head->left = remove(value, head->left);
- } else if (head->value < value) {
- head->right = remove(value, head->right);
- }
- return BalanceTree(head);
- }
- };
- int main() {
- srand(time(0));
- std::ios::sync_with_stdio(false);
- std::cin.tie(nullptr);
- std::cout.tie(nullptr);
- int n;
- std::cin >> n;
- AVL avl_tree;
- char c;
- int i;
- for (int t = 0; t < n; t++) {
- std::cin >> c >> i;
- if(c == 'A'){
- if (avl_tree.Exists(i)) {
- std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
- continue;
- }
- avl_tree.head = avl_tree.Insert(avl_tree.head, i);
- std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
- } else if(c == 'D') {
- if (!avl_tree.Exists(i)) {
- std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
- continue;
- }
- avl_tree.head = avl_tree.remove(i, avl_tree.head);
- std::cout << avl_tree.CheckBalance(avl_tree.head) << '\n';
- } else {
- if (avl_tree.Exists(i)) {
- std::cout << "Y\n";
- } else {
- std::cout << "N\n";
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement