Javatpoint Logo
Javatpoint Logo

Find if Binary Tree Satisfies Balanced Height Property

A height-balanced binary tree generally appears to be the one in which the right subtree and the left subtree of any given node do not change or predicate by more than one, and also it focuses on the fact that both the subtrees are height-balanced.

To identify whether the given binary tree satisfies the height-balanced property, we need to check whether the heights of the left and the right subtree differ at least by one. To find out about this, we have an algorithm approach:

  1. The very first thing we need to do is to explain a function named "is balanced" that will immediately take a binary tree node as an input and have to return a Boolean value.
  2. If, by any chance, the input node appears to be empty, then we have to return the actual value since an empty tree is always height-balanced.
  3. Now we have to calculate the height of the left subtree by calling out the function "is balanced" on the left child, and then we have to store that in the left height.
  4. Now we have to calculate the height of the right subtree by calling out the function "is balanced" on the left child and then store that in the right height.
  5. Now, we have to calculate the difference between the left and right subtree, and if it appears to be more than one, then we have to return the false value as the tree disobeys the height-balanced property.
  6. If the above condition doesn't turn out to be true, we have to return the true value.

Implementation

Output:

Find if Binary Tree Satisfies Balanced Height Property

A step-by-step explanation of the code

  1. Code begins by including the necessary header files, including bits/stdc++.h> containing all standard C++ libraries.
  2. Several classes are defined in the code, including Node, which represents a binary node. Among the nodes are integer values (data) and pointers to the left and right children (left and right).
  3. To calculate the height of a tree, the height function is defined. A node pointer (node) is input, and the tree's height rooted at that node is returned as an integer. In the case of an empty tree, the height is considered to be zero if the node is NULL. If not, the height is calculated as one plus the maximum left and right subtrees.
  4. An isBalanced function determines whether a binary tree is height-balanced. It will take a root node value as an input and return a boolean value.
  5. With the isbalanced function, we get two variables, lh and rh, and then store the height of both subtrees.
  6. Now, we will have a function to check whether the absolute difference between the two heights is less than or equal to 1. If this condition is satisfied, and both the subtrees are height balanced, then we have to return the actual value.
  7. If the above conditions are not satisfied, we must return the false value as the subtrees are not height-balanced.
  8. Next, the primary function typically serves as the entry gate of the program.
  9. After this, it calls the isbalanced function to verify the argument and print the results based on the returned Boolean value.
  10. Finally, the main function returns 0, which emphasizes the complete execution of the program.

Example 2)

Output:

Find if Binary Tree Satisfies Balanced Height Property

A step-by-step explanation of the code

  1. The code starts by declaring two classes, 'node' and 'binary tree'. Here the node class represents the node in the binary tree and consists of a pointer to the left and right child, whereas the binary tree represents the binary tree and only has one property called the 'root'.
  2. We have an isbalanced function in the binary tree, which takes a node as an argument and returns a boolean value.
  3. The isbalanced function starts by checking whether the node is NULL; if it is, it represents an empty tree.
  4. If the tree is not empty, we have to start by calculating the left and right subtrees using the height method.
  5. The height method will calculate the height of the binary tree. If the node appears to be NULL, then we have to consider the height of the binary tree as 0. The tree's height is 1 + the maximum height between the left and right subtrees.
  6. After we finish the calculations, the isbalanced function checks whether the height between the left and right subtree is less than 1.
  7. If all the above-stated conditions are met, the method returns the actual value as the tree is balanced, and if not, it returns the value false.
  8. Now, in the primary method of the function, a tree is created in the program with all the specified values for the root and the children.
  9. Finally, the isbalanced function is called on to the tree's object 'root' to verify whether the tree is balanced.

Example 3)

Output:

Find if Binary Tree Satisfies Balanced Height Property

A step-by-step explanation of the code

  1. We will start by defining a class in the program named 'Node'. Here the node class represents the node in the binary tree and consists of a pointer to the left and right child.
  2. Now, we will define a function called height(root) that will calculate the height of the binary tree. The function will use recursion to determine the binary tree's height. The primary case will be when the height of the binary tree is 0. The tree's height is 1 + maximum between the left and right subtrees.
  3. We have to define a function called isbalanced which verifies whether a tree is height-balanced; the isbalanced function checks whether the height between the left and right subtree is less than 1. 7. If all the above-stated conditions are met, the method returns the value actual as the tree is balanced, and if not, it returns the value false.
  4. Now, in the primary method of the function, a tree is created in the program by creating nodes and using the left and right attributes.
  5. The isbalanced function will be called with the tree's root as an argument and return a value based on that.
  6. Finally, based on the value returned, we will print whether the tree is balanced.

Conclusion

The code in the above approach provides a solution to check whether a tree is height-balanced. It typically defines a node class for the binary tree and various other functions to check whether a tree is height-balanced. In conclusion, the code provides a straightforward answer to the height-balanced property of the binary tree.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA