struct Node {
int data;
int height;
int8_t balance;
Node* left;
Node* right;
Node* parent;
};

struct AVL {
Node* top = nullptr;

int height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}

void UpdateHeightAndBalance(Node* node) {
if (node == nullptr)
return;

node->height = 1 + std::max(height(node->left), height(node->right));
node->balance = height(node->left) - height(node->right);
}

Node* newNode(int value, Node* parent) {
Node* node = new Node();
node->data = value;
node->left = nullptr;
node->right = nullptr;
node->parent = parent;
node->height = 1;
node->balance = 0;
return node;
}

Node* RightRotation(Node* y) {
Node* x = y->left;
Node* T2 = x->right;

x->right = y;
y->left = T2;

x->parent = y->parent;
y->parent = x;
if (T2 != nullptr) {
T2->parent = y;
}

if (x->parent != nullptr) {
if (x->parent->left == y) {
x->parent->left = x;
} else {
x->parent->right = x;
}
} else {
top = x;
}

// Обновляем высоты и балансировку
UpdateHeightAndBalance(y);
UpdateHeightAndBalance(x);

return x;
}

Node* LeftRotation(Node* x) {
Node* y = x->right;
Node* T2 = y->left;

y->left = x;
x->right = T2;

y->parent = x->parent;
x->parent = y;
if (T2 != nullptr) {
T2->parent = x;
}

if (y->parent != nullptr) {
if (y->parent->left == x) {
y->parent->left = y;
} else {
y->parent->right = y;
}
} else {
top = y;
}

UpdateHeightAndBalance(x);
UpdateHeightAndBalance(y);

return y;
}

Node* BalanceTree(Node* node) {
if (node == nullptr) return node;

UpdateHeightAndBalance(node);

if (node->balance == 2 && node->left->balance != -1) {
return RightRotation(node);
} else if (node->balance == 2 && node->left->balance == -1) {
node->left = LeftRotation(node->left);
return RightRotation(node);
} else if (node->balance == -2 && node->right->balance != 1) {
return LeftRotation(node);
} else if (node->balance == -2 && node->right->balance == 1) {
node->right = RightRotation(node->right);
return LeftRotation(node);
}

return node;
}

Node* Insert(Node* node, int key, Node* parent) {
if (node == nullptr) {
return newNode(key, parent);
}

if (key > node->data) {
node->left = Insert(node->left, key, node);
} else if (key < node->data) {
node->right = Insert(node->right, key, node);
} else {
return node;
}

UpdateHeightAndBalance(node);
return BalanceTree(node);
}


Node* minValueNode(Node* node) {
Node* current = node;
while (current && current->right != nullptr)
current = current->right;
return current;
}

Node* Delete(Node* node, int key) {
if (node == nullptr) return node;

if (key > node->data) {
node->right = Delete(node->right, key);
} else if (key < node->data) {
node->left = Delete(node->left, key);
} else {
if ((node->left == nullptr) || (node->right == nullptr)) {
Node* temp = node->left ? node->left : node->right;
if (temp == nullptr) {
temp = node;
node = nullptr;
} else {
node = temp; // Copy the contents of the non-empty child
}
delete(temp);
} else {
Node temp = minValueNode(node->right);
node->data = temp->data;
node->right = Delete(node->right, temp->data); // Recursively delete the successor
}
}

// If the tree had only one node then return
if (node == nullptr) {
return node;
}

// STEP 1: UPDATE HEIGHT OF THE CURRENT NODE
node->height = 1 + std::max(height(node->left), height(node->right));

// STEP 2: GET THE BALANCE FACTOR OF THIS NODE (to check whether this node became unbalanced)
int balance = height(node->left) - height(node->right);

// If this node becomes unbalanced, then there are 4 cases

// Case 1 - Left Left Case
if (balance > 1 && height(node->left->left) >= height(node->left->right)) {
return RightRotation(node);
}

// Case 2 - Left Right Case
if (balance > 1 && height(node->left->left) < height(node->left->right)) {
node->left = LeftRotation(node->left);
return RightRotation(node);
}

// Case 3 - Right Right Case
if (balance < -1 && height(node->right->right) >= height(node->right->left)) {
return LeftRotation(node);
}

// Case 4 - Right Left Case
if (balance < -1 && height(node->right->right) < height(node->right->left)) {
node->right = RightRotation(node->right);
return LeftRotation(node);
}

return node;
}

bool Exists(Node node, int key) {
if (node == nullptr) return false;

if (node->data == key) {
return true;
}

else if (node->data < key) {
return Exists(node->right, key);
} else {
return Exists(node->left, key);
}
}
};

int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
char cmd;
AVL tree;
int n;
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> cmd;
if (cmd == ‘A’) {
int x;
std::cin >> x;
tree.top = tree.Insert(tree.top, x, nullptr);
std::cout << (tree.top == nullptr? 0 : tree.top->balance);
if (i < n - 1) {
std::cout << ‘\n’;
}
} else if (cmd == ‘D’) {
int x;
std::cin >> x;
tree.top = tree.Delete(tree.top, x);
std::cout << (tree.top == nullptr? 0 : tree.top->balance);
if (i < n - 1) {
std::cout << ‘\n’;
}
} else if (cmd == ‘C’) {
int x;
std::cin >> x;
tree.Exists(tree.top, x) ? std::cout << ‘Y’ : std::cout << ‘N’;
if (i < n - 1) {
std::cout << ‘\n’;
}
}
}
}


ошибка в функции insert
6
A 3
0
A 4
1
A 5
0
C 5
N

почему пишет что нету 5? я же ее добавил

функция Exists правильная скорее всего ошибка в поворотах(слева у меня храниться большее значение, спарва меньшее, мне просто так нужно для задачи)