Full Binary Tree – GeeksforGeeks

[ad_1]

We all know tree is a non-linear information construction. It has no limitation on the variety of little one. A binary tree has a limitation as any node of the tree has atmost two kids: a left and a proper little one.

Introduction to Full Binary Tree:

A binary tree is alleged to be a whole binary tree when all the degrees are crammed fully besides the bottom stage nodes that are crammed from as left as attainable.

Full Binary Tree

Some terminology:

  • Root – Node during which no edge is coming from the mum or dad. Instance -node A
  • Baby – Node having some incoming edge known as little one. Instance – nodes B, H are the kid of A and D respectively.
  • Sibling – Nodes having the identical mum or dad are sibling. Instance- J, Okay are siblings as they’ve the identical mum or dad E.
  • Diploma of a node – Variety of kids of a selected mum or dad. Instance- Diploma of A is 2 and Diploma of H is 1. Diploma of L is 0.
  • Inside/Exterior nodes – Leaf nodes are exterior nodes and non leaf nodes are inner nodes.
  • Degree – Rely nodes in a path to succeed in a vacation spot node. Instance- Degree of node H is 3 as nodes A, D and H themselves type the trail.
  • Peak – Variety of edges to succeed in the vacation spot node, Root is at top 0. Instance – Peak of node E is 2 because it has two edges from the foundation.

Properties of Full Binary Tree:

  • A whole binary tree is alleged to be a correct binary tree the place all leaves have the identical depth.
  • In a whole binary tree variety of nodes at depth d is 2d
  • In a  full binary tree with n nodes top of the tree is log(n+1).
  • All the degrees besides the final stage are fully full.

Excellent Binary Tree vs Full Binary Tree:

A binary tree of top ‘h’ having the utmost variety of nodes is a good binary tree. 
For a given top h, the utmost variety of nodes is 2h+1-1.

A full binary tree of top h is a correct binary tree as much as top h-1, and within the final stage ingredient are saved in left to proper order.

Instance 1:

A Binary Tree

The peak of the given binary tree is 2 and the utmost variety of nodes in that tree is n= 2h+1-1 =  22+1-1 =  23-1 = 7.
Therefore we are able to conclude it’s an ideal binary tree.
Now for an entire binary tree, It’s full as much as top h-1 i.e.; 1, and the final stage parts are saved in left to proper order. Therefore it’s a full Binary tree additionally. Right here is the illustration of parts when saved in an array

Aspect saved in an array stage by stage

Within the array, all the weather are saved repeatedly.

Instance 2:

A binary tree

Peak of the given binary tree is 2 and the utmost variety of nodes that ought to be there are 2h+1 – 1 = 22+1 – 1 = 23 – 1 = 7
However the variety of nodes within the tree is 6. Therefore it’s not an ideal binary tree.
Now for an entire binary tree, It’s full as much as top h-1 i.e.; 1, and the final stage ingredient are saved in left to proper order. Therefore this can be a full binary tree. Retailer the ingredient in an array and it will likely be like;

Aspect saved in an array stage by stage

Instance 3:

A binary tree

The peak of the binary tree is 2 and the utmost variety of nodes that may be there may be 7, however there are solely 5 nodes therefore it’s not an ideal binary tree.
In case of a whole binary tree, we see that within the final stage parts should not crammed from left to proper order. So it’s not a whole binary tree.

Aspect saved in an array stage by stage

The weather within the array should not steady.

Full Binary Tree vs Full Binary tree:

For a full binary tree, each node has both 2 kids or 0 kids.

Instance 1:

A binary tree

Within the given binary tree there isn’t a node having diploma 1, both 2 or 0 kids for each node, therefore it’s a full binary tree.

For a whole binary tree, parts are saved in stage by stage and never from the leftmost aspect within the final stage. Therefore that is not a whole binary tree. The array illustration is:

Aspect saved in an array stage by stage

Instance 2:

A binary Tree

Within the given binary tree there isn’t a node having diploma 1. Each node has a level of both 2 or 0. Therefore it’s a full binary tree.

For a whole binary tree, parts are saved in a stage by stage method and crammed from the leftmost aspect of the final stage. Therefore this a full binary tree. Under is the array illustration of the tree:

Aspect saved in an array stage by stage

Instance 3:

A binary tree

Within the given binary tree node B has diploma 1 which violates the property of full binary tree therefore it’s not a full Binary tree

For a whole binary tree, parts are saved in stage by stage method and crammed from the leftmost aspect of the final stage. Therefore this can be a full binary tree. Array illustration of the binary tree is:

Aspect saved in an array stage by stage

Instance 4:
 

a binary tree

Within the given binary tree node C has diploma 1 which violates the property of a full binary tree therefore it’s not a full Binary tree

For a whole binary tree, parts are saved in stage by stage method and crammed from the leftmost aspect of the final stage. Right here node E violates the situation. Therefore that is not a whole binary tree

Creation of Full Binary Tree:

We all know a full binary tree is a tree during which apart from the final stage (say l)all the opposite stage has (2l) nodes and the nodes are lined up from left to proper aspect.
It may be represented utilizing an array. If the mum or dad is it index i so the left little one is at 2i+1 and the proper little one is at 2i+2.

Full binary tree and its array illustration

Algorithm:

For the creation of a Full Binary Tree, we require a queue information construction to maintain observe of the inserted nodes.

Step 1: Initialize the foundation with a brand new node when the tree is empty.

Step 2: If the tree is just not empty then get the entrance ingredient 

  • If the entrance ingredient doesn’t have a left little one then set the left little one to a brand new node
  • If the proper little one is just not current set the proper little one as a brand new node

Step 3: If the node has each the youngsters then pop it from the queue.

Step 4: Enqueue the brand new information.

Illustration:

Think about the under array:

1. The first ingredient will the foundation (worth at index = 0)

A is taken as root

2. The following ingredient (at index = 1) can be left and third ingredient (index = 2) can be proper little one of root

B as left little one and D as proper little one

3. fourth (index = 3) and fifth ingredient (index = 4) would be the left and proper little one of B node

E and F are left and proper little one of B

4. Subsequent ingredient (index = 5) can be left little one of the node D

G is made left little one of D node

That is how full binary tree is created.

Implementation: For the implementation of constructing a Full Binary Tree from stage order traversal is given in this publish.

Utility of the Full binary tree:

  • Heap Kind
  • Heap kind based mostly information construction

Test if a given binary tree is full or not: Comply with this publish to test if the given binary tree is full or not.

[ad_2]

Leave a Reply