## Find if Binary Tree Satisfies Balanced Height PropertyA 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: - 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.
- 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.
- 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.
- 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.
- 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.
- If the above condition doesn't turn out to be true, we have to return the true value.
## Implementation
## A step-by-step explanation of the code- Code begins by including the necessary header files, including bits/stdc++.h> containing all standard C++ libraries.
- 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).
- 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.
- 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.
- With the isbalanced function, we get two variables, lh and rh, and then store the height of both subtrees.
- 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.
- If the above conditions are not satisfied, we must return the false value as the subtrees are not height-balanced.
- Next, the primary function typically serves as the entry gate of the program.
- After this, it calls the isbalanced function to verify the argument and print the results based on the returned Boolean value.
- Finally, the main function returns 0, which emphasizes the complete execution of the program.
## Example 2)
## A step-by-step explanation of the code- 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'.
- We have an isbalanced function in the binary tree, which takes a node as an argument and returns a boolean value.
- The isbalanced function starts by checking whether the node is NULL; if it is, it represents an empty tree.
- If the tree is not empty, we have to start by calculating the left and right subtrees using the height method.
- 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.
- After we finish the calculations, the isbalanced function checks whether the height between the left and right subtree is less than 1.
- 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.
- 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.
- Finally, the isbalanced function is called on to the tree's object 'root' to verify whether the tree is balanced.
## Example 3)
## A step-by-step explanation of the code- 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.
- 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.
- 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.
- 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.
- The isbalanced function will be called with the tree's root as an argument and return a value based on that.
- Finally, based on the value returned, we will print whether the tree is balanced.
## ConclusionThe 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. |