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:
- C++
- Java
- Python
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include <iostream>
#include <utility>
#include <cmath>
using namespace std;
// Data structure to store a binary tree node
struct Node
{
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = nullptr;
}
};
// Recursive function to check if a given binary tree is height-balanced or not
int isHeightBalanced(Node* root, bool &isBalanced)
{
// base case: tree is empty or not balanced
if (root == nullptr || !isBalanced) {
return 0;
}
// get the height of the left subtree
int left_height = isHeightBalanced(root->left, isBalanced);
// get the height of the right subtree
int right_height = isHeightBalanced(root->right, isBalanced);
// tree is unbalanced if the absolute difference between the height of
// its left and right subtree is more than 1
if (abs(left_height – right_height) > 1) {
isBalanced = false;
}
// return height of subtree rooted at the current node
return max(left_height, right_height) + 1;
}
// The main function to check if a given binary tree is height-balanced or not
bool isHeightBalanced(Node* root)
{
bool isBalanced = true;
isHeightBalanced(root, isBalanced);
return isBalanced;
}
int main()
{
/* Construct the following tree
1
/ \
/ \
2 3
/ \ /
4 5 6
*/
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
if (isHeightBalanced(root)) {
cout << “Binary tree is balanced”;
}
else {
cout << “Binary tree is not balanced”;
}
return 0;
}
|
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?