87

I am confused about the terminology of the below trees, I have been studying the Tree, and I am unable to distinguish between these trees:

a) Complete Binary Tree

b) Strict Binary Tree

c) Full Binary Tree

Please help me to differentiate among these trees. When and where these trees are used in Data Structure?

Ajay
  • 18,086
  • 12
  • 59
  • 105
kTiwari
  • 1,488
  • 1
  • 14
  • 21

13 Answers13

91

Perfect Tree:

       x
     /   \
    /     \
   x       x
  / \     / \
 x   x   x   x
/ \ / \ / \ / \
x x x x x x x x

Complete Tree:

       x
     /   \
    /     \
   x       x
  / \     / \
 x   x   x   x
/ \ /
x x x

Strict/Full Tree:

       x
     /   \
    /     \
   x       x
  / \ 
 x   x 
    / \
    x x 
Shubham
  • 160
  • 1
  • 4
  • 13
japreiss
  • 11,111
  • 2
  • 40
  • 77
  • 4
    By perfect binary tree you mean full binary tree referred by the OP? – RBT May 04 '17 at 02:12
  • 4
    Perfect binary tree is both Strict/Full binary tree as well as Complete binary tree but vice versa may not be always true. – neo Nov 07 '18 at 05:54
74

Wikipedia yielded

A full binary tree (sometimes proper binary tree or 2-tree or strictly binary tree) is a tree in which every node other than the leaves has two children.

So you have no nodes with only 1 child. Appears to be the same as strict binary tree.

Here is an image of a full/strict binary tree, from google:

enter image description here

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

It seems to mean a balanced tree.

Here is an image of a complete binary tree, from google, full tree part of image is bonus.

enter image description here

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
  • 35
    Your complete tree example also fulfills the criteria of being a full binary tree so the difference is apparently blurred , in my opinion you might wanna give an example of a complete tree which is not a full binary tree and vice-versa , that would make the answer complete :) – 0decimal0 Aug 30 '14 at 17:30
  • 1
    There is a difference between full binary tree and strictly binary tree. Refer to the answer: http://stackoverflow.com/a/32064101/5237727 – Saurabh Bhatia Mar 29 '17 at 04:39
  • 2
    Also, while all complete trees are balanced trees, all balanced trees are not necessarily complete trees. – sfarbota Sep 06 '17 at 02:32
  • 1
    What does it mean that every level is completely filled? – lolololol ol Dec 26 '17 at 03:22
  • 2
    @lololololol it means that all of the nodes that can possibly be in that level are present. – Sam I am says Reinstate Monica Dec 26 '17 at 04:52
  • 1
    On the Complete tree, the "all nodes are as far left as possible" is a little confusing. I think this means in the last row, there will be no gaps if you do a level traverse from left to right. Once you hit a node on the second to last level with no left or right child, you know that's the last one. – Joel Cunningham Mar 13 '18 at 00:01
  • 1
    One thing to keep in mind too is that there is some literature out there which uses "complete binary tree" and "nearly complete binary tree" (e.g. http://homepages.math.uic.edu/~leon/cs-mcs401-s08/handouts/nearly_complete.pdf) and their definition of "complete binary tree" is actually what most would consider to be a "full binary tree". Unfortunately the Wikipedia page has this sentence which confused me terribly and I think is borrowing from this terminology `a heap is a specialized tree-based data structure which is essentially an almost complete[1] tree` – Pace Mar 12 '19 at 00:27
53

There is a difference between a STRICT and FULL BINARY TREE.

1) FULL BINARY TREE: A binary tree of height h that contains exactly (2^h)-1 elements is called a full binary tree. (Ref: Pg 427, Data Structures, Algorithms and Applications in C++ [University Press], Second Edition by Sartaj Sahni).

or in other words

In a FULL BINARY TREE each node has exactly 0 or 2 children and all leaf nodes are on the same level.

The following are examples of a FULL BINARY TREE:

a.

          18
       /      \   
     15       30    
    /  \     /   \   
  40    50  100  40 

b.

        18
       /  \   
     15    30 
                   

2) STRICT BINARY TREE: Each node has exactly 0 or 2 children.

The following are examples of a STRICT BINARY TREE:

a.

         18
       /    \   
     15      30    
    /  \          
  40    50

b.

          18
        /    \   
      15      30    
     /  \          
   40    50
  /  \
20    45      

I think there's no confusion in the definition of a Complete Binary Tree, still for the completeness of the post I would like to tell what a Complete Binary Tree is.

3) COMPLETE BINARY TREE: A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible.

For Example: The following is a COMPLETE BINARY TREE:

           18
       /       \  
     15         30  
    /  \        /  \
  40    50    100   40
 /  \   /
8   7  9 

Note: The following is a Complete Binary Tree and also a Full Binary Tree by definition:

         18
       /     \   
     15       30    
    /  \     /   \   
  40    50  100  40 

Conclusion So, a Full Binary Tree is also a Complete Binary Tree. But the converse is not true.

Saurabh Bhatia
  • 1,875
  • 1
  • 20
  • 29
  • Can you give a source for the full binary tree definition? It contradicts the one on [Wikipedia](https://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees) that is sourced to the [NIST](https://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html). – Calvin Li Apr 17 '19 at 21:49
  • @CalvinLi The source is mentioned in the definition of FULL BINARY TREE. Here is a link of the pdf (pg 447 of the pdf) - https://o6ucs.files.wordpress.com/2012/10/data-structures-algorithms-and-applications-in-c-by-sartraj-sahani.pdf – Saurabh Bhatia Jun 15 '19 at 05:47
  • @SaurabhBhatia, the last representation also holds true for the full binary tree. Correct me if I'm wrong. How can be one representation holds true for different varieties? – Rose Dec 13 '19 at 12:32
  • @Rose yes, the last representation also holds true for Full Binary Tree. So, we can also say that a Full Binary Tree is also a Complete Binary Tree. But the converse is not true. Hope that's clear. – Saurabh Bhatia Jan 12 '23 at 11:57
10

Disclaimer- The main source of some definitions are wikipedia, any suggestion to improve my answer is welcome.

Although this post has an accepted answer and is a good one I was still in confusion and would like to add some more clarification regarding the difference between these terms.

(1)FULL BINARY TREE- A full binary tree is a binary tree in which every node other than the leaves has two children.This is also called strictly binary tree.

enter image description here enter image description here

The above two are the examples of full or strictly binary tree.

(2)COMPLETE BINARY TREE- Now, the definition of complete binary tree is quite ambiguous, it states :- A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. It can have between 1 and 2h nodes, as far left as possible, at the last level h

Notice the lines in italic.

The ambiguity lies in the lines in italics , "except possibly the last" which means that the last level may also be completely filled , i.e this exception need not always be satisfied. If the exception doesn't hold then it is exactly like the second image I posted, which can also be called as perfect binary tree. So, a perfect binary tree is also full and complete but not vice-versa which will be clear by one more definition I need to state:

ALMOST COMPLETE BINARY TREE- When the exception in the definition of complete binary tree holds then it is called almost complete binary tree or nearly complete binary tree . It is just a type of complete binary tree itself , but a separate definition is necessary to make it more unambiguous.

So an almost complete binary tree will look like this, you can see in the image the nodes are as far left as possible so it is more like a subset of complete binary tree , to say more rigorously every almost complete binary tree is a complete binary tree but not vice versa . :

enter image description here

0decimal0
  • 3,884
  • 2
  • 24
  • 39
6

Concluding from above answers, Here is the exact difference between full/strictly, complete and perfect binary trees

  1. Full/Strictly binary tree :- Every node except the leaf nodes have two children

  2. Complete binary tree :- Every level except the last level is completely filled and all the nodes are left justified.

  3. Perfect binary tree :- Every node except the leaf nodes have two children and every level (last level too) is completely filled.

pantech
  • 69
  • 2
  • 6
3

Consider a binary tree whose nodes are drawn in a tree fashion. Now start numbering the nodes from top to bottom and left to right. A complete tree has these properties:

If n has children then all nodes numbered less than n have two children.

If n has one child it must be the left child and all nodes less than n have two children. In addition no node numbered greater than n has children.

If n has no children then no node numbered greater than n has children.

A complete binary tree can be used to represent a heap. It can be easily represented in contiguous memory with no gaps (i.e. all array elements are used save for any space that may exist at the end).

Craig Wright
  • 1,575
  • 1
  • 11
  • 19
2

Full binary tree are a complete binary tree but reverse is not possible, and if the depth of the binary is n the no. of nodes in the full binary tree is ( 2^n-1 ). It is not necessary in the binary tree that it have two child but in the full binary it every node have no or two child.

bmu
  • 35,119
  • 13
  • 91
  • 108
  • 1
    You cannot strictly say that "reverse is not possible " in fact your this very assumption is defied in the example of complete tree in the accepted answer ... you should rather say that may or may not be possible – 0decimal0 Aug 30 '14 at 17:34
  • 1
    if the depth of the binary is n the no. of nodes in the full binary tree is ( 2^n-1 ): but a full binary tree definition is a tree where every node is either a leaf or has two children. So the max possible no. of children is ( 2^n-1 ) but it may be less than that. – mrida Sep 27 '14 at 10:49
2

To start with basics, it is very important to understand binary tree itself to understand different types of it.

A tree is a binary tree if and only if :-

– It has a root node , which may not have any child nodes (0 childnodes, NULL tree)

–Root node may have 1 or 2 child nodes . Each such node forms abinary tree itself 

–Number of child nodes can be 0 ,1 ,2.......not more than 2

–There is a unique path from the root to every other node

Example :

        X
      /    \
     X      X
          /   \
         X     X

Coming to your inquired terminologies:

A binary tree is a complete binary tree ( of height h , we take root node as 0 ) if and only if :-

Level 0 to h-1 represent a full binary tree of height h-1

– One or more nodes in level h-1 may have 0, or 1 child nodes

If j,k are nodes in level h-1, then j has more child nodes than k if and only if j is to the left of k , i.e. the last level (h) can be missing leaf nodes, however the ones present must be shifted to the left

Example :

                          X
                     /          \
                   /              \
                 /                  \
                X                    X
             /     \              /     \
           X       X             X       X
         /   \   /   \         /   \    /  \ 
        X    X   X   X        X    X    X   X 

A binary tree is a strictly binary tree if and only if :-

Each node has exactly two child nodes or no nodes

Example :

         X
       /   \
      X     X 
          /   \
         X      X
        / \    / \ 
       X   X  X   X 

A binary tree is a full binary tree if and only if :-

Each non leaf node has exactly two child nodes

All leaf nodes are at the same level

Example :

                          X
                     /          \
                   /              \
                 /                  \
                X                    X
             /     \              /     \
           X       X             X       X
         /   \   /   \         /   \    /  \ 
        X    X   X   X        X    X    X   X 
      /  \  / \ / \ / \      / \  / \  / \ / \ 
     X   X X  X X X X X     X  X  X X  X X X X 

You should also know what a perfect binary tree is?

A binary tree is a perfect binary tree if and only if :-

– is a full binary tree

– All leaf nodes are at the same level

Example :

                          X
                     /          \
                   /              \
                 /                  \
                X                    X
             /     \              /     \
           X       X             X       X
         /   \   /   \         /   \    /  \ 
        X    X   X   X        X    X    X   X 
      /  \  / \ / \ / \      / \  / \  / \ / \ 
     X   X X  X X X X X     X  X  X X  X X X X 

Well, I am sorry I cannot post images as I do not have 10 reputation. Hope this helps you and others!

Minderov
  • 521
  • 1
  • 5
  • 20
2

In my limited experience with binary tree, I think:

  1. Strictly Binary Tree:Every node except the leaf nodes have two children or only have a root node.
  2. Full Binary Tree: A binary tree of H strictly(or exactly) containing 2^(H+1) -1 nodes , it's clear that which every level has the most nodes. Or in short a strict binary tree where all leaf nodes are at same level.
  3. Complete Binary Tree: It is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
BertKing
  • 513
  • 5
  • 13
  • 2
    You're definition for a full binary tree is incorrect, that is the definition of a perfect binary tree. A full binary tree is synonymous with a strictly binary tree. (source: see strictly binary tree: http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/bintree.html) (source: see perfect binary tree: https://www.slideshare.net/ajaykumarc137151/difference-between-complete-binary-tree) – Keego Aug 12 '17 at 15:50
  • 1
    oh, my god, I am confused just now,I will make sure of this. Many thanks. – BertKing Aug 15 '17 at 03:32
  • 1
    No problem :) See [the answer by @Lotus below](https://stackoverflow.com/a/28252424/3234235), he nailed it. I just recommended edits for your answer to reflect this. – Keego Aug 16 '17 at 18:27
1

Let us consider a binary tree of height 'h'. A binary tree is called a complete binary tree if all the leaves are present at height 'h' or 'h-1' without any missing numbers in the sequence.

                   1
                 /   \
              2       3
            /    \         
         4        5

It is a complete binary tree.

                   1
                 /   \
              2       3
            /        /    
         4         6    

It is not a complete binary tree as the node of number 5 is missing in the sequence

0

full binary tree is full if every node has 0 or 2 children. in full binary number of leaf nodes is number of internal nodes plus 1 L=l+1

0

Complete Binary Tree: All levels are completely filled except the lowest level and one main thing all the leaf elements must have left child. Strict Binary Tree: In this tree every non-leaf node has no child i.e. neither left nor right. Full Binary Tree: Every Node has either zero child or Two children (never having single child).

  • > In this tree every non-leaf node has no child i.e. neither left nor right Non-leaf node has to have at least one children, else it is leaf node – nkrivenko Nov 04 '20 at 22:04
0

In simple terms:-

Full BT:- A Binary Tree of h height having the maximum number of nodes. i.e.,
n = 2^(h+1) - 1;
Eg. if the height of any tree is 2 then nodes should be 7 then it is a Full Binary Tree


Complete BT:- Every level except the last level is completely filled and all the nodes are left-justified.
Or
Any BT which can represent an array without having blank space (or null values).
Or
A Complete BT of having h will be a Full BT up to h-1 height and In the last level, the elements will be filled from left to right without skipping.
enter image description here

Strict BT:- A Binary Tree having degree 0 or degree 2.
enter image description here




Image Source:- Abdul Bari Lectures.

Mridul Bagla
  • 917
  • 1
  • 5
  • 9