murki
murki

Reputation: 11

AVL rotations in C

When rotations occurs only first rotation works in my code I couldnt understand why I think rotation functions return wrong nodes? maybe. Below, there are my node and tree structure

#define ll unsigned long
typedef struct NODE_s *NODE; //Node structure
typedef struct NODE_s{
    NODE right,left;
    ll key;
    int height;
    void *data;
} NODE_t[1];

typedef struct AVL_s *AVL; //Tree structure
typedef struct AVL_s{
    NODE root;
} AVL_t[1];

Initialize functions

AVL AVL_init(){
    AVL t = (AVL)malloc(sizeof(AVL_t));
    t->root = NULL;
    return t;
}
NODE node_init(ll init_key){
    NODE node = (NODE)malloc(sizeof(NODE_t));
    node->left = NULL;
    node->right = NULL;
    node->key = init_key;
    node->height = 1;
    node->data = NULL;
    return node;
}

Rotations functions

NODE rightRotate(NODE node){

    NODE child  = node->left;
    NODE childR = child->right;

    child->right = node;
    node->left = childR;


        node->height = max(height(node->left), height(node->right))+1;
        child->height = max(height(child->left), height(child->right))+1;

    return child;
}
NODE leftRotate(NODE node)
{
    NODE child  = node->right;
    NODE childL = child->left;

    child->left = node;
    node->right = childL;


        node->height = max(height(node->left), height(node->right))+1;
        child->height = max(height(child->left), height(child->right))+1;

        return child;
}

Inserting recursively I think have to use two functions sending tree from main then tree->root in insert function.

NODE insert_rec(NODE node,ll rec_key){
    if(rec_key < node->key){
        if(node->left == NULL)
            node->left = node_init(rec_key);
        else
            insert_rec(node->left, rec_key);
    }else if(rec_key > node->key){
        if(node->right == NULL)
            node->right = node_init(rec_key);
        else
            insert_rec(node->right, rec_key);
    }else
        printf("Duplicate %lu Data\n",rec_key);

    //If I delete from here to end of functions, it is clearly recursive BST insert and works successfully

    node->height = 1 + max(height(node->left),height(node->right));
    int balanceFactor = balance(node);

    if (balanceFactor > 1 && rec_key < node->left->key) //leftleft
       node = rightRotate(node);   //return rightRotate(node) also working

    if (balanceFactor < -1 && rec_key > node->right->key) //rightright
        node = leftRotate(node);   //return leftRotate(node) also working

    if (balanceFactor > 1 && rec_key > node->left->key) //leftright
    {
        node->left =  leftRotate(node->left);
        node = rightRotate(node); 
    }
    if (balanceFactor < -1 && rec_key < node->right->key) //rightright
    {
        node->right = rightRotate(node->right);
        node = leftRotate(node);
    }
   return node;
}
void insert(AVL tree,ll key){
    if(key == -1){
        printf("Invalid data %lu\n",key);
    }
    else if(tree->root == NULL){
        tree->root = node_init(key);
    }
    else{
       tree->root = insert_rec(tree->root,key);
    }
}
int main() {
    AVL tree = AVL_init();
    insert(tree,111);
    }

This my source code I cannot understand what I do not see. If I replace it normal BST it is working but when rotation happens nodes comes somewhere

Upvotes: 0

Views: 383

Answers (1)

murki
murki

Reputation: 11

I solved it. Function insert() goes trash and put it in insert_rec(). It is just root check if root = null conditions.

Then main fucntion replaced like that,

int main() {
    AVL tree = AVL_init();
    NODE node = tree->root;
    insert_rec(node,111);
}

Lastly, In balance factor cases I just need return the functions

return leftRotate(node); //instead of node = leftRotate(node); 
return rightRotate(node); //instead of node = rightRotate(node);

This perspective made it easy to deal with returning values ​​from functions

Upvotes: 1

Related Questions