Advertisement
Guest User

FIXED - C Implementation of AVL Tree

a guest
Mar 18th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. // AVL Tree Implementation
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6.  
  7. typedef struct Node{
  8.     int key;
  9.     struct Node *left;
  10.     struct Node *right;
  11.     struct Node *parent;
  12.     int height;
  13. }node;
  14.  
  15. int max(int a, int b);
  16. int getHeight(node *n);
  17. int getBalanceFactor(node *n);
  18. void insert(node **n, node *parent, int key);
  19. void rotateLeft(node **n);
  20. void rotateRight(node **n);
  21.  
  22. // Traversal
  23. // In-Order Traversal
  24. // Pre-Order Traversal
  25. void InOrder(node *n){
  26.     if (n != NULL){
  27.         printf("%d ", n->key);
  28.         InOrder(n->left);
  29.         InOrder(n->right);
  30.     }
  31. }
  32.  
  33. #define MAX_SIZE 10
  34.  
  35. int main(){
  36.     node *root = NULL;
  37.  
  38.     int a[MAX_SIZE] = {10,6,8,2,7,9,4,13,22,1};
  39.  
  40.     int i;
  41.     printf("\n\nInserting items in the Tree...\n\n");
  42.     for(i = 0; i < MAX_SIZE; i++){
  43.         printf("\nInserted: %d",a[i]);
  44.         insert(&root, NULL, a[i]);
  45.     }
  46.  
  47.     printf("\n\nElements in the Tree: ");
  48.     InOrder(root);
  49.     printf("\n\n");
  50.  
  51.     if(getBalanceFactor(root) > 1 || getBalanceFactor(root) < -1)
  52.         printf("\nThe tree is not balanced");
  53.     else
  54.         printf("\nThe tree is balanced");
  55.  
  56.     printf("\nLeft Subtree's Height = %d", root->left->height);
  57.     printf("\nRight Subtree's Height = %d", root->right->height);
  58.  
  59.     printf("\nRoot = %d", root->key);
  60. }
  61.  
  62. int max(int a, int b){
  63.     return (a > b)? a : b;
  64. }
  65.  
  66. /* Get the height of a Node */
  67. int getHeight(node *n){
  68.     if(n == NULL)
  69.         return 0;
  70.     return n->height;
  71. }
  72.  
  73. /* Get the Balance Factor of a Node */
  74. int getBalanceFactor(node *n){
  75.     if(n == NULL)
  76.         return 0;
  77.     return (getHeight(n->left)-getHeight(n->right));
  78. }
  79.  
  80.  
  81. void insert(node **n, node *parent, int key){
  82.     if(*n == NULL){
  83.         /* Create a New Node */
  84.         *n = (node*)malloc(sizeof(node));
  85.         if(*n != NULL){
  86.             (*n)->key = key;
  87.             (*n)->left = (*n)->right = NULL;
  88.             (*n)->height = 1;
  89.             (*n)->parent = parent;
  90.             return;
  91.         } else {
  92.             printf("\nError: Memory Allocation Fail");
  93.             return;
  94.         }
  95.     }
  96.  
  97.     if (key < (*n)->key){
  98.         insert(&((*n)->left), *n, key);
  99.     }else{
  100.         insert(&((*n)->right), *n, key);
  101.     }
  102.  
  103.     (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
  104.  
  105. //   printf("\nThe Balance Factor of %d is updated to %d",(*n)->key, getBalanceFactor(*n));
  106.  
  107.     if(getBalanceFactor(*n) > 1){
  108.  
  109.         if(key > (*n)->left->key){
  110.             printf("\nDouble Rotation > 1");
  111.             rotateLeft(&((*n)->left));
  112.         }
  113.  
  114.         rotateRight(&(*n));
  115.     }
  116.     else if(getBalanceFactor(*n) < -1){
  117.  
  118.         if(key < (*n)->right->key){
  119.             printf("\nDouble Rotation < -1");
  120.             rotateRight(&((*n)->right));
  121.         }
  122.  
  123.         rotateLeft(&(*n));
  124.     }
  125. }
  126.  
  127. void rotateRight(node **n){
  128.     node *lChild, *subtree, *oldN, *parent;
  129.  
  130.     lChild = (*n)->left;
  131.     subtree = lChild->right;
  132.     parent = (*n)->parent;
  133.     oldN = (*n);
  134.  
  135.     // Rotate Right
  136.     printf("\nRotating %d to the Right",(*n)->key);
  137.     lChild->right = *n;
  138.     (*n)->left = subtree;
  139.     (*n) = lChild;
  140.  
  141.     // Update Link Parent
  142.     if(parent != NULL){
  143.         if(parent->left == oldN){
  144.             parent->left == (*n);
  145.         }else{
  146.             parent->right == (*n);
  147.         }
  148.     }
  149.  
  150.     // Update Height
  151.     lChild->right->height = max(getHeight(lChild->right->left), getHeight(lChild->right->right)) + 1;
  152.     (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
  153. }
  154.  
  155. void rotateLeft(node **n){
  156.     node *rChild, *subtree, *oldN, *parent;
  157.  
  158.     rChild = (*n)->right;
  159.     subtree = rChild->left;
  160.     parent = (*n)->parent;
  161.     oldN = (*n);
  162.  
  163.     // Rotate Left
  164.     printf("\nRotating %d to the Left",(*n)->key);
  165.     rChild->left = *n;
  166.     (*n)->right = subtree;
  167.     (*n) = rChild;
  168.  
  169.     // Update Link Parent
  170.     if(parent != NULL){
  171.         if(parent->left == oldN){
  172.             parent->left ==  (*n);
  173.         }else{
  174.             parent->right ==  (*n);
  175.         }
  176.     }
  177.  
  178.     // Update Height
  179.     rChild->left->height = max(getHeight(rChild->left->left), getHeight(rChild->left->right)) + 1;
  180.     (*n)->height = max(getHeight((*n)->left), getHeight((*n)->right)) + 1;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement