Advertisement
bitwise_gamgee

Untitled

Jun 7th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.61 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4.  
  5. enum Balance  { SLeft=-1, SEven=0, SRight=1 };
  6.  
  7. typedef struct node {
  8.     int key;
  9.     int count;
  10.     struct node *left, *right;
  11.     enum Balance  bal;
  12. } Node;
  13.  
  14. void insert(int x, Node **p, int *h);
  15. void balanceL(Node **p, int *h);
  16. void balanceR(Node **p, int *h);
  17. void removeNode(int x, Node **p, int *h);
  18. void destroyTree(Node **tree);
  19. void printTree(Node **tree);
  20.  
  21. /**
  22.  * @brief Function to insert a key into the AVL tree.
  23.  *
  24.  * @param x : key to be inserted.
  25.  * @param p : double pointer to the root of the AVL tree.
  26.  * @param h : pointer to a flag used to indicate if tree height increased.
  27.  *
  28.  * This function inserts the key into the AVL tree and adjusts the balance
  29.  * factors of the nodes accordingly. It also updates the height of the tree.
  30.  */
  31.  
  32. void insert(int x, Node **p, int *h) {
  33.     if (*p == NULL) {
  34.         *p = (Node*) malloc(sizeof(Node));
  35.         *h = 1;
  36.         (*p)->key = x;
  37.         (*p)->count = 1;
  38.         (*p)->left = NULL;
  39.         (*p)->right = NULL;
  40.         (*p)->bal = SEven;
  41.     } else if ((*p)->key > x ) {
  42.         insert(x, &((*p)->left), h);
  43.         if (*h) balanceL(p, h);
  44.     } else if ((*p)->key < x ) {
  45.         insert(x, &((*p)->right), h);
  46.         if (*h) balanceR(p, h);
  47.     } else {
  48.         (*p)->count++;
  49.     }
  50. }
  51.  
  52. /**
  53.  * @brief Function to left balance the AVL tree.
  54.  *
  55.  * @param p : double pointer to the root of the AVL tree.
  56.  * @param h : pointer to a flag used to indicate if tree height increased.
  57.  *
  58.  * This function is used after insertion into the right subtree. If the
  59.  * balance factor becomes 2 then depending upon the balance factor of the
  60.  * right child, appropriate rotations are performed to balance the tree.
  61.  */
  62.  
  63. void balanceL(Node **p, int *h) {
  64.     Node *p1 = NULL, *p2 = NULL;
  65.     if ((*p)->bal == SRight) {
  66.         (*p)->bal = SEven;
  67.         *h = 0;
  68.     } else if ((*p)->bal == SEven) {
  69.         (*p)->bal = SLeft;
  70.     } else if ((*p)->bal == SLeft) {
  71.         p1 = (*p)->left;
  72.         if(p1->bal == SLeft) {
  73.             (*p)->left = p1->right;
  74.             p1->right = *p;
  75.             (*p)->bal = SEven;
  76.             *p = p1;
  77.         } else {
  78.             p2 = p1->right;
  79.             p1->right = p2->left;
  80.             p2->left = p1;
  81.             (*p)->left = p2->right;
  82.             p2->right = *p;
  83.             if(p2->bal == SLeft) {
  84.                 (*p)->bal = SRight;
  85.             } else {
  86.                 (*p)->bal = SEven;
  87.             }
  88.             if(p2->bal == SRight) {
  89.                 p1->bal = SLeft;
  90.             } else {
  91.                 p1->bal = SEven;
  92.             }
  93.             *p = p2;
  94.         }
  95.         (*p)->bal = SEven;
  96.         *h = 0;
  97.     }
  98. }
  99.  
  100. /**
  101.  * @brief Function to right balance the AVL tree.
  102.  *
  103.  * @param p : double pointer to the root of the AVL tree.
  104.  * @param h : pointer to a flag used to indicate if tree height increased.
  105.  *
  106.  * This function is used after insertion into the left subtree. If the
  107.  * balance factor becomes -2 then depending upon the balance factor of the
  108.  * left child, appropriate rotations are performed to balance the tree.
  109.  */
  110. void balanceR(Node **p, int *h) {
  111.     Node *p1 = NULL, *p2 = NULL;
  112.     if ((*p)->bal == SLeft) {
  113.         (*p)->bal = SEven;
  114.         *h = 0;
  115.     } else if ((*p)->bal == SEven) {
  116.         (*p)->bal = SRight;
  117.     } else if ((*p)->bal == SRight) {
  118.         p1 = (*p)->right;
  119.         if(p1->bal == SRight) {
  120.             (*p)->right = p1->left;
  121.             p1->left = *p;
  122.             (*p)->bal = SEven;
  123.             *p = p1;
  124.         } else {
  125.             p2 = p1->left;
  126.             p1->left = p2->right;
  127.             p2->right = p1;
  128.             (*p)->right = p2->left;
  129.             p2->left = *p;
  130.             if(p2->bal == SRight) {
  131.                 (*p)->bal = SLeft;
  132.             } else {
  133.                 (*p)->bal = SEven;
  134.             }
  135.             if(p2->bal == SLeft) {
  136.                 p1->bal = SRight;
  137.             } else {
  138.                 p1->bal = SEven;
  139.             }
  140.             *p = p2;
  141.         }
  142.         (*p)->bal = SEven;
  143.         *h = 0;
  144.     }
  145. }
  146.  
  147. /**
  148.  * @brief Function to remove a node from the AVL tree.
  149.  *
  150.  * @param x : key to be removed.
  151.  * @param p : double pointer to the root of the AVL tree.
  152.  * @param h : pointer to a flag used to indicate if tree height decreased.
  153.  *
  154.  * This function removes the key from the AVL tree and adjusts the balance
  155.  * factors of the nodes accordingly. It also updates the height of the tree.
  156.  */
  157. void removeNode(int x, Node **p, int *h) {
  158.     Node *temp = NULL;
  159.     Node *prev = NULL;
  160.     if (*p == NULL) return;
  161.     else if ((*p)->key > x){
  162.         removeNode(x, &((*p)->left), h);
  163.         if (*h) balanceL(p, h);
  164.     } else if ((*p)->key < x){
  165.         removeNode(x, &((*p)->right), h);
  166.         if (*h) balanceR(p, h);
  167.     } else {
  168.         temp = *p;
  169.         if (temp->right == NULL) {
  170.             *p = temp->left;
  171.             *h = 1;
  172.         } else if (temp->left == NULL) {
  173.             *p = temp->right;
  174.             *h = 1;
  175.         } else {
  176.             prev = NULL;
  177.             temp = (*p)->left;
  178.             while (temp->right) {
  179.                 prev = temp;
  180.                 temp = temp->right;
  181.             }
  182.             (*p)->key = temp->key;
  183.             if (prev != NULL) {
  184.                 prev->right = temp->left;
  185.             } else {
  186.                 (*p)->left = temp->left;
  187.             }
  188.             *h = 1;
  189.         }
  190.         free(temp);
  191.     }
  192. }
  193.  
  194. /**
  195.  * @brief Function to delete an AVL tree.
  196.  *
  197.  * @param tree : double pointer to the root of the AVL tree.
  198.  *
  199.  * This function recursively deletes all nodes of the AVL tree pointed by tree.
  200.  */
  201. void destroyTree(Node **tree) {
  202.     if (*tree) {
  203.         destroyTree(&((*tree)->left));
  204.         destroyTree(&((*tree)->right));
  205.         free(*tree);
  206.         *tree = NULL;
  207.     }
  208. }
  209.  
  210. /**
  211.  * @brief Function to print the AVL tree.
  212.  *
  213.  * @param tree : double pointer to the root of the AVL tree.
  214.  *
  215.  * This function prints the keys of the AVL tree in an in-order fashion.
  216.  */
  217. void printTree(Node **tree) {
  218.     if(*tree) {
  219.         printTree(&((*tree)->left));
  220.         printf("%d ", (*tree)->key);
  221.         printTree(&((*tree)->right));
  222.     }
  223. }
  224.  
  225. int main() {
  226.     Node *root = NULL;
  227.     int h = 0;
  228.     int treeSize = 100;  // Set your desired tree size here.
  229.  
  230.    
  231.     srand(time(NULL));
  232.     for (int i = 0; i < treeSize; i++) {
  233.         insert(rand() % 1000, &root, &h);  
  234.     }
  235.  
  236.     printf("\n Sorted output AVL tree is: \n");
  237.     printTree(&root);
  238.  
  239.     // Free the memory allocated for the tree.
  240.     destroyTree(&root);
  241.     return 0;
  242. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement