#include<stdio.h>
#include<stdlib.h>
int max(int a, int b)
{
    return (a > b)? a : b;
}
struct AVLNode
{
    int data;
    struct AVLNode * left;
    struct AVLNode * right;
    int height;
};
class Tree {
public:
    AVLNode * root;
    
    Tree() {
        root = NULL;
    }
    Tree(int val) {
        root->data = val;
        root->left = root->right = NULL;
        root->height = 0;
    }
    Tree(struct AVLNode * r) {
        root->data = r->data;
        root->left = r->left;
        root->right = r->right;
        root->height = r->height;
    }
    ~Tree() {
        printf("Inside destructor");
        root->data = -1;
        root->left = root->right = NULL;
        root->height = -1;
    }
};
int height(struct AVLNode * node){
    if (node == NULL)
        return -1;
    return node->height;
}
struct AVLNode * newNode(int data) {
    struct AVLNode * tmp = (struct AVLNode *)malloc(sizeof(struct AVLNode));
    tmp->data   = data;
    tmp->left   = NULL;
    tmp->right  = NULL;
    
    tmp->height = 0;
    return(tmp);
}
struct AVLNode * rightRotate(struct AVLNode * q)
{
    struct AVLNode * p;
    struct AVLNode * hold;
    printf("Right Rotation is Required\n");
    p = q->left;
    hold = p->right;
    p->right = q;
    q->left = hold;
    
    q->height = max(height(q->left), height(q->right))+1;
    p->height = max(height(p->left), height(p->right))+1;
    
    return p;
}
struct AVLNode * leftRotate(struct AVLNode * p)
{
    struct AVLNode * q;
    struct AVLNode * hold;
    printf("Left Rotation is Required\n");
    q = p->right;
    hold = q->left;
    q->left = p;
    p->right = hold;
    
    p->height = max(height(p->left), height(p->right))+1;
    q->height = max(height(q->left), height(q->right))+1;
    
    return q;
}
void preorder(struct AVLNode *root)
{
    if(root != NULL)
    {
        printf("%d ", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}
int getBalance(struct AVLNode * N)
{
    if (N == NULL)
        return 0;
    return height(N->left) - height(N->right);
}
struct AVLNode * insert(struct AVLNode * node, int data)
{
    int balance;
    
    if (node == NULL)
        return(newNode(data));
    if (data < node->data)
        node->left  = insert(node->left, data);
    else if (data > node->data)
        node->right = insert(node->right, data);
    else 
        return node;
    
    node->height = 1 + max(height(node->left),height(node->right));
    
    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;
}
struct AVLNode * minvalNode(struct AVLNode * node)
{
    struct AVLNode * current = node;
    
    while(current->left != NULL)
        current = current->left;
    return current;
}
struct AVLNode* deleteNode(struct AVLNode* root, int key)
{
    struct AVLNode * temp;
    int balance;
    
    if(root == NULL)
        return root;
    
    if(key < root->data)
        root->left = deleteNode(root->left, key);
    
    else if(key > root->data)
        root->right = deleteNode(root->right, key);
    
    
    else
    {
        
        if( (root->left == NULL) || (root->right == NULL) )
        {
            if(root->left)
                temp = root->left;
            else
                temp = root->right;
            
            
            if(temp == NULL)
            {
                temp = root;
                root = NULL;
            }
            else 
             *root = *temp; 
            free(temp);
        }
        else
        {
            
            temp = minvalNode(root->right);
            
            root->data = temp->data;
            
            root->right = deleteNode(root->right, temp->data);
        }
    }
    
    if (root == NULL)
      return root;
    
    root->height = 1 + max(height(root->left), height(root->right));
    
    balance = getBalance(root);
    
    
    if (balance > 1 && getBalance(root->left) >= 0)
        return rightRotate(root);
    
    if (balance > 1 && getBalance(root->left) < 0)
    {
        root->left =  leftRotate(root->left);
        return rightRotate(root);
    }
    
    if (balance < -1 && getBalance(root->right) <= 0)
        return leftRotate(root);
    
    if (balance < -1 && getBalance(root->right) > 0)
    {
        root->right = rightRotate(root->right);
        return leftRotate(root);
    }
    return root;
}
bool search(struct AVLNode* root, int val) {
    if(root == NULL) {
        return false;
    }
    if(root->data == val) {
        return true;
    }
    if(root->data > val) return search(root->left, val);
    else return search(root->right, val);
}
int main()
{
    Tree r;
    int x;
        
    x = 60;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x= 50;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x= 30;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x= 70;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x= 80;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x=20;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x=25;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x=100;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    x=90;
    printf("Inserting %d\n",x);
    r.root = insert(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    if(search(r.root, 100)) {
        printf("Found 100\n");
    }
    else {
        printf("100 Not Found\n");
    }
    if(search(r.root, 10000)) {
        printf("Found 10000\n");
    }
    else {
        printf("10000 Not Found\n");
    }
    x=90;
    printf("Deleting %d\n",x);
    r.root = deleteNode(r.root, x);
    printf("Preorder traversal of the constructed AVL tree is \n");
    preorder(r.root);
    printf("\n");
    return 0;
}
                             
Comments