C++ Program to Create a Height Balanced Binary Tree














































C++ Program to Create a Height Balanced Binary Tree




Program:
#include <iostream>
 
using namespace std;
 
typedef struct bin_tree_node
{
        int v;
        struct bin_tree_node *left;
        struct bin_tree_node *right;
} BTNode;
 
BTNode *create_bin_tree_node(int v)
{
    BTNode *p = new BTNode;
 
    if (p != NULL)
    {
        p->v = v;
        p->left = NULL;
        p->right = NULL;
    }
 
    return p;
}
 
void create_balanced_bin_tree(BTNode **root, int arr[], int start, int end)
{
    if (start <= end)
    {
        int mid = (start + end + 1) / 2;
 
        *root = create_bin_tree_node(arr[mid]);
        create_balanced_bin_tree(&((*root)->left), arr, start, mid - 1);
        create_balanced_bin_tree(&((*root)->right), arr, mid + 1, end);
    }
}
 
void print_bin_tree(BTNode *root)
{
    if (root != NULL)
    {
        cout << root->v << " ";
        print_bin_tree(root->left);
        print_bin_tree(root->right);
    }
}
void print_bin_tree1(BTNode *root)
{
    if (root != NULL)
    {
        print_bin_tree1(root->left);
        cout << root->v << " ";
        print_bin_tree1(root->right);
    }
}
 
int main(int argc, char* argv[])
{
    int arr[30];
    for (int i = 0; i < 30; i++)
    {
        arr[i] = i;
    }
    BTNode *root = NULL;
    create_balanced_bin_tree(&root, arr, 0, 29);
    cout << "Preorder of balanced tree is: ";
    print_bin_tree(root);
    cout << "\nInorder of balanced tree is: ";
    print_bin_tree1(root);
    return 0;
}
Output:
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 



More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 303 1
C++ program to Sort a Linked List Using Merge Sort 281 1
C++ Program to Implement a Linked List representation of a Binary tree 267 1
C++ Program to Check for balanced parentheses in an expression 178 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 228 1
C++ program to print Indian flag 266 1
C++ program to Convert a multi level linked list to a singly linked list 175 1
C++ program to print right view of a Binary tree using queue 195 1
C++ Program to implement Huffman Coding Compression Algorithm 809 1
C++ Program to Create a Height Balanced Binary Tree 212 1
C++ program to implement Prims algorithm 308 1
C++ Program for BFS Traversal 217 1
C++ Progam to Evaluate a Prefix Expression 205 1
C++ Program to Implement Queue using Linked List 209 1
C++ implementation of Stack using Linked list 226 1
C++ program to find the intersection point of two linked lists 255 1
C++ program to count the inversions in the given array 235 1
C++ program to perform D.N.F sort 275 1
C++ program to print all possible subsets of a String 248 1
C++ program to count the number of ones in a binary representation of a number 256 1
C++ program to print all possible subsets of a set 279 1
C++ program to find the largest word in a String 240 1
C++ Program to print a matrix in Spiral order 273 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 265 1
C limits library 298 1
Program to add two Binary numbers 259 1

Comments