C Program to Create a Balanced Binary Tree of the Incoming Data

This is a C Program to creat a balanced binary search tree. AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees cannot be more than one for all nodes.

Here is source code of the C Program to Create a Balanced Binary Tree of the Incoming Data. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. // An AVL tree node
  5. struct node {
  6.     int key;
  7.     struct node *left;
  8.     struct node *right;
  9.     int height;
  10. };
  11.  
  12. // A utility function to get maximum of two integers
  13. int max(int a, int b);
  14.  
  15. // A utility function to get height of the tree
  16. int height(struct node *N) {
  17.     if (N == NULL)
  18.         return 0;
  19.     return N->height;
  20. }
  21.  
  22. // A utility function to get maximum of two integers
  23. int max(int a, int b) {
  24.     return (a > b) ? a : b;
  25. }
  26.  
  27. /* Helper function that allocates a new node with the given key and
  28.  NULL left and right pointers. */
  29. struct node* newNode(int key) {
  30.     struct node* node = (struct node*) malloc(sizeof(struct node));
  31.     node->key = key;
  32.     node->left = NULL;
  33.     node->right = NULL;
  34.     node->height = 1; // new node is initially added at leaf
  35.     return (node);
  36. }
  37.  
  38. // A utility function to right rotate subtree rooted with y
  39. // See the diagram given above.
  40. struct node *rightRotate(struct node *y) {
  41.     struct node *x = y->left;
  42.     struct node *T2 = x->right;
  43.  
  44.     // Perform rotation
  45.     x->right = y;
  46.     y->left = T2;
  47.  
  48.     // Update heights
  49.     y->height = max(height(y->left), height(y->right)) + 1;
  50.     x->height = max(height(x->left), height(x->right)) + 1;
  51.  
  52.     // Return new root
  53.     return x;
  54. }
  55.  
  56. // A utility function to left rotate subtree rooted with x
  57. // See the diagram given above.
  58. struct node *leftRotate(struct node *x) {
  59.     struct node *y = x->right;
  60.     struct node *T2 = y->left;
  61.  
  62.     // Perform rotation
  63.     y->left = x;
  64.     x->right = T2;
  65.  
  66.     //  Update heights
  67.     x->height = max(height(x->left), height(x->right)) + 1;
  68.     y->height = max(height(y->left), height(y->right)) + 1;
  69.  
  70.     // Return new root
  71.     return y;
  72. }
  73.  
  74. // Get Balance factor of node N
  75. int getBalance(struct node *N) {
  76.     if (N == NULL)
  77.         return 0;
  78.     return height(N->left) - height(N->right);
  79. }
  80.  
  81. struct node* insert(struct node* node, int key) {
  82.     /* 1.  Perform the normal BST rotation */
  83.     if (node == NULL)
  84.         return (newNode(key));
  85.  
  86.     if (key < node->key)
  87.         node->left = insert(node->left, key);
  88.     else
  89.         node->right = insert(node->right, key);
  90.  
  91.     /* 2. Update height of this ancestor node */
  92.     node->height = max(height(node->left), height(node->right)) + 1;
  93.  
  94.     /* 3. Get the balance factor of this ancestor node to check whether
  95.      this node became unbalanced */
  96.     int balance = getBalance(node);
  97.  
  98.     // If this node becomes unbalanced, then there are 4 cases
  99.  
  100.     // Left Left Case
  101.     if (balance > 1 && key < node->left->key)
  102.         return rightRotate(node);
  103.  
  104.     // Right Right Case
  105.     if (balance < -1 && key > node->right->key)
  106.         return leftRotate(node);
  107.  
  108.     // Left Right Case
  109.     if (balance > 1 && key > node->left->key) {
  110.         node->left = leftRotate(node->left);
  111.         return rightRotate(node);
  112.     }
  113.  
  114.     // Right Left Case
  115.     if (balance < -1 && key < node->right->key) {
  116.         node->right = rightRotate(node->right);
  117.         return leftRotate(node);
  118.     }
  119.  
  120.     /* return the (unchanged) node pointer */
  121.     return node;
  122. }
  123.  
  124. // A utility function to print preorder traversal of the tree.
  125. // The function also prints height of every node
  126. void preOrder(struct node *root) {
  127.     if (root != NULL) {
  128.         printf("%d ", root->key);
  129.         preOrder(root->left);
  130.         preOrder(root->right);
  131.     }
  132. }
  133.  
  134. /* Drier program to test above function*/
  135. int main() {
  136.     struct node *root = NULL;
  137.  
  138.     /* Constructing tree given in the above figure */
  139.     root = insert(root, 10);
  140.     root = insert(root, 20);
  141.     root = insert(root, 30);
  142.     root = insert(root, 40);
  143.     root = insert(root, 50);
  144.     root = insert(root, 25);
  145.  
  146.     /* The constructed AVL Tree would be
  147.       30
  148.      /  \
  149.    20   40
  150.   /  \     \
  151. 10  25    50
  152.      */
  153.  
  154.     printf("Pre order traversal of the constructed Balanced Binary tree is \n");
  155.     preOrder(root);
  156.  
  157.     return 0;
  158. }

Output:

$ gcc BalancedBinaryTree.c
$ ./a.out
 
Pre order traversal of the constructed Balanced Binary tree is 
30 20 10 25 40 50

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.