Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // AVL Tree Implementation
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- typedef struct Node{
- int key;
- struct Node *left;
- struct Node *right;
- struct Node *parent;
- int height;
- }node;
- int max(int a, int b);
- int getHeight(node *n);
- int getBalanceFactor(node *n);
- void insert(node **n, node *parent, int key);
- void rotateLeft(node **n);
- void rotateRight(node **n);
- // Traversal
- // In-Order Traversal
- // Pre-Order Traversal
- void InOrder(node *n){
- if (n != NULL){
- printf("%d ", n->key);
- InOrder(n->left);
- InOrder(n->right);
- }
- }
- #define MAX_SIZE 10
- int main(){
- node *root = NULL;
- int a[MAX_SIZE] = {10,6,8,2,7,9,4,13,22,1};
- int i;
- printf("\n\nInserting items in the Tree...\n\n");
- for(i = 0; i < MAX_SIZE; i++){
- printf("\nInserted: %d",a[i]);
- insert(&root, NULL, a[i]);
- }
- printf("\n\nElements in the Tree: ");
- InOrder(root);
- printf("\n\n");
- if(getBalanceFactor(root) > 1 || getBalanceFactor(root) < -1)
- printf("\nThe tree is not balanced");
- else
- printf("\nThe tree is balanced");
- printf("\nLeft Subtree's Height = %d", root->left->height);
- printf("\nRight Subtree's Height = %d", root->right->height);
- printf("\nRoot = %d", root->key);
- }
- int max(int a, int b){
- return (a > b)? a : b;
- }
- /* Get the height of a Node */
- int getHeight(node *n){
- if(n == NULL)
- return 0;
- return n->height;
- }
- /* Get the Balance Factor of a Node */
- int getBalanceFactor(node *n){
- if(n == NULL)
- return 0;
- return (getHeight(n->left)-getHeight(n->right));
- }
- void insert(node **n, node *parent, int key){
- if(*n == NULL){
- /* Create a New Node */
- *n = (node*)malloc(sizeof(node));
- if(*n != NULL){
- (*n)->key = key;
- (*n)->left = (*n)->right = NULL;
- (*n)->height = 1;
- (*n)->parent = parent;
- return;
- } else {
- printf("\nError: Memory Allocation Fail");
- return;
- }
- }
- if (key < (*n)->key){
- insert(&((*n)->left), *n, key);
- }else{
- insert(&((*n)->right), *n, key);
- }
- (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
- // printf("\nThe Balance Factor of %d is updated to %d",(*n)->key, getBalanceFactor(*n));
- if(getBalanceFactor(*n) > 1){
- if(key > (*n)->left->key){
- printf("\nDouble Rotation > 1");
- rotateLeft(&((*n)->left));
- }
- rotateRight(&(*n));
- }
- else if(getBalanceFactor(*n) < -1){
- if(key < (*n)->right->key){
- printf("\nDouble Rotation < -1");
- rotateRight(&((*n)->right));
- }
- rotateLeft(&(*n));
- }
- }
- void rotateRight(node **n){
- node *lChild, *subtree, *oldN, *parent;
- lChild = (*n)->left;
- subtree = lChild->right;
- parent = (*n)->parent;
- oldN = (*n);
- // Rotate Right
- printf("\nRotating %d to the Right",(*n)->key);
- lChild->right = *n;
- (*n)->left = subtree;
- (*n) = lChild;
- // Update Link Parent
- if(parent != NULL){
- if(parent->left == oldN){
- parent->left == (*n);
- }else{
- parent->right == (*n);
- }
- }
- // Update Height
- lChild->right->height = max(getHeight(lChild->right->left), getHeight(lChild->right->right)) + 1;
- (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
- }
- void rotateLeft(node **n){
- node *rChild, *subtree, *oldN, *parent;
- rChild = (*n)->right;
- subtree = rChild->left;
- parent = (*n)->parent;
- oldN = (*n);
- // Rotate Left
- printf("\nRotating %d to the Left",(*n)->key);
- rChild->left = *n;
- (*n)->right = subtree;
- (*n) = rChild;
- // Update Link Parent
- if(parent != NULL){
- if(parent->left == oldN){
- parent->left == (*n);
- }else{
- parent->right == (*n);
- }
- }
- // Update Height
- rChild->left->height = max(getHeight(rChild->left->left), getHeight(rChild->left->right)) + 1;
- (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement