C Program to Convert Sorted Array to Balanced BST

This C Program Construct a Balanced Binary Tree using Sorted Array.

Here is source code of the C Program to Construct a Balanced Binary Tree using Sorted Array. 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 Balenced Binary Tree using Sorted Array
  3.  *                   40
  4.  *                   /  \
  5.  *                  20   60
  6.  *                 /  \   \
  7.  *                10  30   80
  8.  *                          \
  9.  *                           90    
  10.  *            (Given Binary Search Tree)
  11.  */
  12. #include <stdio.h>
  13. #include <stdlib.h> 
  14.  
  15. struct btnode
  16. {
  17.     int value;
  18.     struct btnode *l;
  19.     struct btnode *r;
  20. };
  21.  
  22. typedef struct btnode N;
  23. N* bst(int arr[], int first, int last);
  24. N* new(int val);
  25. void display(N *temp);
  26.  
  27. int main()
  28. {   
  29.     int arr[] = {10, 20, 30, 40, 60, 80, 90};
  30.     N *root = (N*)malloc(sizeof(N));  
  31.     int n = sizeof(arr) / sizeof(arr[0]), i;
  32.  
  33.     printf("Given sorted array is\n");
  34.     for (i = 0;i < n;i++)
  35.         printf("%d\t", arr[i]);
  36.     root = bst(arr, 0, n - 1); 
  37.     printf("\n The preorder traversal of binary search tree is as follows\n");
  38.     display(root);
  39.     printf("\n");   
  40.     return 0;
  41. }
  42.  
  43. /* To create a new node */
  44. N* new(int val)
  45. {
  46.     N* node = (N*)malloc(sizeof(N));
  47.  
  48.     node->value = val;
  49.     node->l = NULL;
  50.     node->r  =  NULL;
  51.     return node;
  52. }
  53.  
  54. /* To create a balanced binary search tree */
  55. N* bst(int arr[], int first, int last)
  56. {
  57.     int mid;
  58.     N* temp = (N*)malloc(sizeof(N));
  59.     if (first > last)
  60.         return NULL;
  61.     mid = (first + last) / 2;
  62.     temp = new(arr[mid]);
  63.     temp->l = bst(arr, first, mid - 1);
  64.     temp->r = bst(arr, mid + 1, last);
  65.     return temp;
  66. }
  67.  
  68. /* To display the preorder */
  69. void display(N *temp)
  70. {
  71.     printf("%d->", temp->value);
  72.     if (temp->l != NULL)
  73.         display(temp->l);
  74.     if (temp->r != NULL)
  75.         display(temp->r);
  76. }

$ cc tree21.c
$ a.out
Given sorted array is
10      20      30      40      60      80      90
The preorder traversal of binary search tree is as follows
40->20->10->30->80->60->90

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
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.

If you find any mistake above, kindly email to [email protected]

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.