How to determine if a binary tree is height-balanced?

A basic arrangement is work out the stature of the left and right subtree for every hub in the tree. In the event that for any hub, the outright distinction between the tallness of its left and right subtree is mutiple, the tree is unequal. The time intricacy of this arrangement is O(n2) as there are n hubs in the tree, and for each hub, we are working out the stature of its left and right subtree that takes O(n) time.

We can tackle this issue in direct time by doing a postorder crossing on the tree. Rather than ascertaining the stature of the left and right subtree for each tree hub, we can get the tallness in consistent time. The thought is to begin from the lower part of the tree and return the stature of the subtree established at the given hub to its parent. The stature of a subtree established at any hub is one more than the greatest tallness of the left subtree or the right subtree.

It’s been some time from those school years. Found a new line of work as IT expert at a clinic. Attempting to move to do some real programming now. I’m chipping away at paired trees now, and I was considering what might be the most effective way to decide whether the tree is stature adjusted.

A tree where no leaf is a lot farther away from the root than some other leaf. Distinctive adjusting plans permit various meanings of “a lot farther” and various measures of work to keep them adjusted.
Consider a stature adjusting plan where following conditions ought to be checked to decide whether a paired tree is adjusted.

An unfilled tree is stature adjusted. A non-void double tree T is adjusted if:

1) Left subtree of T is adjusted
2) Right subtree of T is adjusted
3) The distinction between statures of left subtree and right subtree isn’t mutiple.

The above stature adjusting plan is utilized in AVL trees. The chart beneath shows two trees, one of them is tallness adjusted and other isn’t. The subsequent tree isn’t tallness adjusted in light of the fact that stature of left subtree is 2 a greater number of than tallness of right subtree. To check assuming that a tree is tallness adjusted, get the stature of left and right subtrees. Return valid in the event that distinction between statures isn’t mutiple and left and right subtrees are adjusted, in any case return bogus.

The algorithm can be implemented as follows in C++, Java, and Python:

Output:

Binary tree is balanced

/* C++ program to check if a tree
is height-balanced or not */
#include <bits/stdc++.h>
using namespace std;
#define bool int
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class node {
public:
    int data;
    node* left;
    node* right;
};
/* The function returns true if root is
balanced else false The second parameter
is to store the height of tree. Initially,
we need to pass a pointer to a location with
value as 0. We can also write a wrapper
over this function */
bool isBalanced(node* root, int* height)
{
    /* lh --> Height of left subtree
    rh --> Height of right subtree */
    int lh = 0, rh = 0;
    /* l will be true if left subtree is balanced
    and r will be true if right subtree is balanced */
    int l = 0, r = 0;
    if (root == NULL) {
        *height = 0;
        return 1;
    }
    /* Get the heights of left and right subtrees in lh and rh
    And store the returned values in l and r */
    l = isBalanced(root->left, &lh);
    r = isBalanced(root->right, &rh);
    /* Height of current node is max of heights of left and
    right subtrees plus 1*/
    *height = (lh > rh ? lh : rh) + 1;
    /* If difference between heights of left and right
    subtrees is more than 2 then this node is not balanced
    so return 0 */
    if (abs(lh - rh) >= 2)
        return 0;
    /* If this node is balanced and left and right subtrees
    are balanced then return true */
    else
        return l && r;
}
/* UTILITY FUNCTIONS TO TEST isBalanced() FUNCTION */
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(int data)
{
    node* Node = new node();
    Node->data = data;
    Node->left = NULL;
    Node->right = NULL;
    return (Node);
}
// Driver code
int main()
{
    int height = 0;
    /* Constructed binary tree is
            1
            / \
            2 3
            / \ /
            4 5 6
            /
            7
    */
    node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->left->left->left = newNode(7);
    if (isBalanced(root, &height))
        cout << "Tree is balanced";
    else
        cout << "Tree is not balanced";
    return 0;
}
// This is code is contributed by rathbhupendra

Output

Tree is balanced

Also Read: How to Create a Social Media App on Android Studio?

Leave a Reply

Your email address will not be published. Required fields are marked *