What’s a Excellent Binary Tree?
An ideal Binary Tree is a binary tree during which every of the inner nodes has precisely two little one nodes and all of the leaf nodes are located on the identical degree of the tree.
In different phrases, it may be mentioned that every degree of the tree is totally crammed by the nodes.
Examples of Excellent Binary Tree:Â
A tree with solely the basis node can also be an ideal binary tree. Â Â Â
The next tree is just not an ideal binary tree as a result of the final degree of the tree is just not utterly crammed.
Properties of a Excellent Binary Tree:
- Diploma: The diploma of a node of a tree is outlined because the variety of youngsters of that node. All the inner nodes have a level of two. The leaf nodes of an ideal binary tree have a level of 0.
- Variety of leaf nodes: If the peak of the proper binary tree is h, then the variety of leaf nodes might be 2h as a result of the final degree is totally crammed.
- Depth of a node: Common depth of a node in an ideal binary tree is Θ(ln(n)).
- Relation between leaf nodes and non-leaf nodes: No. of leaf nodes = No. of non-leaf nodes +1.
- Whole variety of nodes: A tree of top h has complete nodes = 2h+1 – 1. Every node of the tree is crammed. So complete variety of nodes will be calculated as 20 + 21 + . . . + 2h = 2h+1 – 1.
- Peak of the tree: The peak of an ideal binary tree with N variety of nodes = log(N + 1) – 1 = Θ(ln(n)). This may be calculated utilizing the relation proven whereas calculating the overall variety of nodes in an ideal binary tree.
Test whether or not a tree is a Excellent Binary Tree or not:
- Discover the depth of any node (within the under tree we discover the depth of the leftmost node). Let this depth be d.
- Now recursively traverse the tree and examine for the next two circumstances.
- Each inside node ought to have each youngsters non-empty.
- All leaves are at depth d
For extra details about this seek advice from the article article: Test whether or not a given binary tree is ideal or not