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

This is a C++ Program to create a balanced binary tree. In computer science, a self-balancing (or height-balanced) binary search tree is any node-based binary search tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.

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 <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct bin_tree_node
  6. {
  7.         int v;
  8.         struct bin_tree_node *left;
  9.         struct bin_tree_node *right;
  10. } BTNode;
  11.  
  12. BTNode *create_bin_tree_node(int v)
  13. {
  14.     BTNode *p = new BTNode;
  15.  
  16.     if (p != NULL)
  17.     {
  18.         p->v = v;
  19.         p->left = NULL;
  20.         p->right = NULL;
  21.     }
  22.  
  23.     return p;
  24. }
  25.  
  26. void create_balanced_bin_tree(BTNode **root, int arr[], int start, int end)
  27. {
  28.     if (start <= end)
  29.     {
  30.         int mid = (start + end + 1) / 2;
  31.  
  32.         *root = create_bin_tree_node(arr[mid]);
  33.         create_balanced_bin_tree(&((*root)->left), arr, start, mid - 1);
  34.         create_balanced_bin_tree(&((*root)->right), arr, mid + 1, end);
  35.     }
  36. }
  37.  
  38. void print_bin_tree(BTNode *root)
  39. {
  40.     if (root != NULL)
  41.     {
  42.         cout << root->v << " ";
  43.         print_bin_tree(root->left);
  44.         print_bin_tree(root->right);
  45.     }
  46. }
  47. void print_bin_tree1(BTNode *root)
  48. {
  49.     if (root != NULL)
  50.     {
  51.         print_bin_tree1(root->left);
  52.         cout << root->v << " ";
  53.         print_bin_tree1(root->right);
  54.     }
  55. }
  56.  
  57. int main(int argc, char* argv[])
  58. {
  59.     int arr[30];
  60.     for (int i = 0; i < 30; i++)
  61.     {
  62.         arr[i] = i;
  63.     }
  64.     BTNode *root = NULL;
  65.     create_balanced_bin_tree(&root, arr, 0, 29);
  66.     cout << "Preorder of balanced tree is: ";
  67.     print_bin_tree(root);
  68.     cout << "\nInorder of balanced tree is: ";
  69.     print_bin_tree1(root);
  70.     return 0;
  71. }

Output:

$ g++ BalancedBinaryTree.cpp
$ a.out
 
Preorder of balanced tree is: 15 7 3 1 0 2 5 4 6 11 9 8 10 13 12 14 23 19 17 16 18 21 20 22 27 25 24 26 29 28 
Inorder of balanced tree is: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

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