C Program to Convert Doubly Linked List to Balanced BST

This C Program creates a balanced binary search tree which has same data members as the given Doubly linked list by changing the pointers internally.

Here is a source code of the C program to create a balanced binary search tree which has same data members as that of a doubly linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Construct a Balanced Binary Search Tree
  3.  * which has same data members as the given Doubly Linked List  
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *left;
  12.     struct node *right;
  13. };
  14.  
  15. void create(struct node **);
  16. void treemaker(struct node **, int);
  17. void display(struct node *);
  18. void displayTree(struct node *);
  19. void delete(struct node **);
  20.  
  21. int main()
  22. {
  23.     struct node *headList = NULL, *rootTree, *p;
  24.     int count = 1, flag = 0;
  25.  
  26.     create(&headList);
  27.     printf("Displaying the doubly linked list:\n");
  28.     display(headList);
  29.     rootTree = p = headList;
  30.     while (p->right != NULL)
  31.     {
  32.         p = p->right;
  33.         count = count + 1;
  34.         if (flag)
  35.         {
  36.             rootTree = rootTree->right;
  37.         }
  38.         flag = !flag;
  39.     }
  40.     treemaker(&rootTree, count / 2);
  41.     printf("Displaying the tree: (Inorder)\n");
  42.     displayTree(rootTree);
  43.     printf("\n");
  44.  
  45.     return 0;
  46. }
  47.  
  48. void create(struct node **head)
  49. {
  50.     struct node *rear, *temp;
  51.     int a, ch;
  52.  
  53.     do
  54.     {
  55.         printf("Enter a number: ");
  56.         scanf("%d", &a);
  57.         temp = (struct node *)malloc(sizeof(struct node));
  58.         temp->num = a;
  59.         temp->right = NULL;
  60.         temp->left = NULL;
  61.         if (*head == NULL)
  62.         {
  63.             *head = temp;
  64.         }
  65.         else
  66.         {
  67.             rear->right = temp;
  68.             temp->left = rear;
  69.         }
  70.         rear = temp;
  71.         printf("Do you wish to continue [1/0] ?: ");
  72.         scanf("%d", &ch);
  73.     } while (ch != 0);
  74. }
  75.  
  76. void treemaker(struct node **root, int count)
  77. {
  78.     struct node *quarter, *thirdquarter;
  79.     int n = count, i = 0;
  80.  
  81.     if ((*root)->left != NULL)
  82.     {
  83.         quarter = (*root)->left;
  84.         for (i = 1; (i < count / 2) && (quarter->left != NULL); i++)
  85.         {
  86.             quarter = quarter->left;
  87.         }
  88.         (*root)->left->right = NULL;
  89.         (*root)->left = quarter;
  90.         /*
  91.          * Uncomment the following line to see when the pointer changes
  92.          */
  93.         //printf("%d's left child is now %d\n", (*root)->num, quarter->num);
  94.         if (quarter != NULL)
  95.         {
  96.             treemaker(&quarter, count / 2);
  97.         }
  98.     }
  99.     if ((*root)->right != NULL)
  100.     {
  101.         thirdquarter = (*root)->right;
  102.         for (i = 1; (i < count / 2) && (thirdquarter->right != NULL); i++)
  103.         {
  104.             thirdquarter = thirdquarter->right;
  105.         }
  106.         (*root)->right->left = NULL;
  107.         (*root)->right = thirdquarter;
  108.         /*
  109.          * Uncomment the following line to see when the pointer changes
  110.          */
  111.         //printf("%d's right child is now %d\n", (*root)->num, thirdquarter->num);
  112.         if (thirdquarter != NULL)
  113.         {
  114.             treemaker(&thirdquarter, count / 2);
  115.         }
  116.     }
  117. }
  118.  
  119. void display(struct node *head)
  120. {
  121.     while (head != NULL)
  122.     {
  123.         printf("%d  ", head->num);
  124.         head = head->right;
  125.     }
  126.     printf("\n");
  127. }
  128.  
  129. /*DisplayTree performs inorder traversal*/
  130. void displayTree(struct node *root)
  131. {
  132.     if (root != NULL)
  133.     {
  134.         displayTree(root->left);
  135.         printf("%d  ", root->num);
  136.         displayTree(root->right);
  137.     }
  138. }
  139.  
  140. void delete(struct node **root)
  141. {
  142.     if (*root != NULL)
  143.     {
  144.         displayTree((*root)->left);
  145.         displayTree((*root)->right);
  146.         free(*root);
  147.     }
  148. }

$ gcc doublytotree.c
$ ./a.out 
Enter a number: 1
Do you wish to continue [1/0] ?: 1
Enter a number: 2
Do you wish to continue [1/0] ?: 1
Enter a number: 3
Do you wish to continue [1/0] ?: 1
Enter a number: 4
Do you wish to continue [1/0] ?: 1
Enter a number: 5
Do you wish to continue [1/0] ?: 1
Enter a number: 6
Do you wish to continue [1/0] ?: 0
Displaying the doubly linked list:
1  2  3  4  5  6  
Displaying the tree: (Inorder)
1  2  3  4  5  6

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

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

If you wish to look at programming examples on all topics, go to C Programming Examples.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.