22.6 C
New York
Thursday, September 26, 2024

Full Binary Tree – GeeksforGeeks

Share To Your Friends

[ad_1]

We all know tree is a non-linear knowledge 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 an entire binary tree when all the degrees are stuffed utterly besides the bottom degree nodes that are stuffed from as left as potential.

Full Binary Tree

Some terminology:

  • Root – Node by which no edge is coming from the guardian. Instance -node A
  • Youngster – 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 guardian are sibling. Instance- J, Ok are siblings as they’ve the identical guardian E.
  • Diploma of a node – Variety of kids of a selected guardian. 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.
  • Stage – Depend nodes in a path to succeed in a vacation spot node. Instance- Stage of node H is 3 as nodes A, D and H themselves kind the trail.
  • Peak – Variety of edges to succeed in the vacation spot node, Root is at peak 0. Instance – Peak of node E is 2 because it has two edges from the basis.

Properties of Full Binary Tree:

  • An entire binary tree is alleged to be a correct binary tree the place all leaves have the identical depth.
  • In an entire binary tree variety of nodes at depth d is 2d
  • In a  full binary tree with n nodes peak of the tree is log(n+1).
  • All the degrees besides the final degree are utterly full.

Good Binary Tree vs Full Binary Tree:

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

A full binary tree of peak h is a correct binary tree as much as peak h-1, and within the final degree 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 will conclude it’s an ideal binary tree.
Now for a whole binary tree, It’s full as much as peak h-1 i.e.; 1, and the final degree 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

Factor saved in an array degree by degree

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 a whole binary tree, It’s full as much as peak h-1 i.e.; 1, and the final degree ingredient are saved in left to proper order. Therefore it is a full binary tree. Retailer the ingredient in an array and it will likely be like;

Factor saved in an array degree by degree

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 an entire binary tree, we see that within the final degree parts usually are not stuffed from left to proper order. So it’s not an entire binary tree.

Factor saved in an array degree by degree

The weather within the array usually are 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 an entire binary tree, parts are saved in degree by degree and never from the leftmost facet within the final degree. Therefore that is not an entire binary tree. The array illustration is:

Factor saved in an array degree by degree

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 an entire binary tree, parts are saved in a degree by degree method and stuffed from the leftmost facet of the final degree. Therefore this a full binary tree. Beneath is the array illustration of the tree:

Factor saved in an array degree by degree

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 an entire binary tree, parts are saved in degree by degree method and stuffed from the leftmost facet of the final degree. Therefore it is a full binary tree. Array illustration of the binary tree is:

Factor saved in an array degree by degree

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 an entire binary tree, parts are saved in degree by degree method and stuffed from the leftmost facet of the final degree. Right here node E violates the situation. Therefore that is not an entire binary tree

Creation of Full Binary Tree:

We all know a full binary tree is a tree by which apart from the final degree (say l)all the opposite degree has (2l) nodes and the nodes are lined up from left to proper facet.
It may be represented utilizing an array. If the guardian is it index i so the left little one is at 2i+1 and the correct 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 knowledge construction to maintain monitor of the inserted nodes.

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

Step 2: If the tree just isn’t 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 correct little one just isn’t current set the correct little one as a brand new node

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

Step 4: Enqueue the brand new knowledge.

Illustration:

Take into account the under array:

1. The first ingredient will the basis (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 degree order traversal is given in this put up.

Software of the Full binary tree:

  • Heap Kind
  • Heap type based mostly knowledge construction

Examine if a given binary tree is full or not: Observe this put up to test if the given binary tree is full or not.

[ad_2]


Share To Your Friends

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles