Wednesday, May 11, 2011

AVL Tree example c c++

Following code is an example of AVL Tree implementation in C language.
AVL Tree is a tree data structure whose height is always balanced by rotation tree nodes.

[AVL Tree example]
source code : AVL_tree.cpp
#include <stdio.h> 
#include <stdlib.h> 

#define max(a,b)    (((a) > (b)) ? (a) : (b)) 

typedef struct AvlNode 
{ 
    int data; 
    AvlNode *left_child, *right_child; 
}AvlNode; 

AvlNode *root; 



AvlNode* rotate_LL(AvlNode *parent) 
{ 
    AvlNode *child = parent->left_child; 
    parent->left_child = child->right_child; 
    child->right_child = parent; 
    return child; 
} 


AvlNode* rotate_RR(AvlNode *parent) 
{ 
    AvlNode *child = parent->right_child; 
    parent->right_child = child->left_child; 
    child->left_child = parent; 
    return child; 
} 


AvlNode* rotate_RL(AvlNode *parent) 
{ 
    AvlNode *child = parent->right_child; 
    parent->right_child = rotate_LL(child); 
    return rotate_RR(parent); 
} 


AvlNode* rotate_LR(AvlNode *parent) 
{ 
    AvlNode *child = parent->left_child; 
    parent->left_child = rotate_RR(child); 
    return rotate_LL(parent); 
} 

int get_height(AvlNode *node) 
{ 
    int height=0; 
    if(node != NULL) 
        height = 1+max(get_height(node->left_child),get_height(node->right_child)); 
    return height; 
} 

 
int get_balance(AvlNode *node) 
{ 
    if(node == NULL) return 0; 
    return get_height(node->left_child) - get_height(node->right_child); 
} 

 
AvlNode* balance_tree(AvlNode **node) 
{ 
    int height_diff= get_balance(*node); 
     
    if(height_diff > 1
{ 
        if(get_balance((*node)->left_child) > 0) 
            *node = rotate_LL(*node); 
        else  
            *node = rotate_LR(*node); 
    } 
    else if(height_diff < -1)
{ 
        if(get_balance((*node)->right_child) < 0) 
            *node = rotate_RR(*node); 
        else 
            *node = rotate_RL(*node); 
    } 
    return *node; 
} 

AvlNode* avl_add(AvlNode **root,int key) 
{ 
    if(*root == NULL) 
    { 
        *root = (AvlNode*)malloc(sizeof(AvlNode)); 
        if(*root == NULL) 
        { 
            printf("fail: memory allocation\n"); 
            exit(-1); 
        } 

        (*root)->data = key;     
        (*root)->left_child = (*root)->right_child = NULL; 
    } 
    else if(key < (*root)->data) 
    { 
        (*root)->left_child = avl_add(&((*root)->left_child),key); 
        (*root) = balance_tree(root);  
    } 
    else if(key > (*root)->data) 
    { 
        (*root)->right_child = avl_add(&((*root)->right_child), key); 
        (*root) = balance_tree(root); 
    } 
    else 
    { 
        printf("fail! - duplicated key\n"); 
        exit(-1); 
    } 
    return *root; 
} 


AvlNode* avl_search(AvlNode *node, int key) 
{ 
    if(node == NULL) return NULL; 
     
    printf("%d->",node->data); 

    if(key == node->data)  
        return node; 
    else if(key < node->data) 
        avl_search(node->left_child,key); 
    else  
        avl_search(node->right_child,key); 
} 

int main() 
{ 
    avl_add(&root,8); 
avl_add(&root,9); 
    avl_add(&root,10); 
    avl_add(&root,2); 
    avl_add(&root,1); 
    avl_add(&root,5); 
    avl_add(&root,3); 
    avl_add(&root,6); 
    avl_add(&root,4); 
    avl_add(&root,7); 
    avl_add(&root,11); 
    avl_add(&root,12); 

    avl_search(root,12); 

    return 0; 
}