class
Solution{
public
:
int
height(
struct
Node *N)
{
if
(N == NULL)
return
0;
return
N->height;
}
Node *rightRotate(Node *y)
{
Node *x = y->left;
Node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return
x;
}
Node *leftRotate(
struct
Node *x)
{
struct
Node *y = x->right;
struct
Node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
return
y;
}
int
getBalance(
struct
Node *N)
{
if
(N == NULL)
return
0;
return
height(N->left) - height(N->right);
}
bool
__getBalance__(
struct
Node *root)
{
int
lh;
int
rh;
if
(root == NULL)
return
1;
lh = height(root->left);
rh = height(root->right);
if
(
abs
(lh-rh) <= 1 &&
__getBalance__(root->left) &&
__getBalance__(root->right))
return
1;
return
0;
}
Node* insertToAVL(Node* node,
int
data)
{
if
(node == NULL)
return
new
Node(data);
if
(data < node->data)
node->left = insertToAVL(node->left, data);
else
if
(data > node->data)
node->right = insertToAVL(node->right, data);
else
return
node;
node->height = max(height(node->left), height(node->right)) + 1;
int
balance = getBalance(node);
if
(balance > 1 && data < node->left->data)
return
rightRotate(node);
if
(balance < -1 && data > node->right->data)
return
leftRotate(node);
if
(balance > 1 && data > node->left->data)
{
node->left = leftRotate(node->left);
return
rightRotate(node);
}
if
(balance < -1 && data < node->right->data)
{
node->right = rightRotate(node->right);
return
leftRotate(node);
}
return
node;
}
};