×

What is a full Binary Tree?

A full binary tree is considered to be a special kind of binary tree in which every single node or leaf node present either contains two children or no children at all. They are all interconnected and fully versed. They are also popularly known as the proper binary tree. They are the ones that keep in check the order and balance of the binary tree. In simpler words, a full binary tree is a tree in which all the nodes are situated at a certain distance from the root node and have two children each.

Theorem for the full binary tree

Suppose we have a binary tree named T which is non-empty then: -

  • Let I be the internal node present in the tree, then L will be the leaf nodes present in the tree, and it will be given by: -
    L = I + 1
  • If the tree T has I number of internal nodes and N stands for the total number of nodes present in that tree, then the formula for the same will be: -
    N = 2I + 1
  • If the tree T consists of N total number of nodes present in the tree, and L is the number of leaf nodes present in the tree, then the formula will be: -
    I = (N-1)/2
  • If the tree T consists of N number of total nodes present in the tree and L stands to be the total amount of leaf nodes present in that tree, then the number of leaf nodes present in the tree is given by: -
    L = (N+1)/2
  •  If the tree T contains L number of leaf nodes and so, to figure out the total amount of leaf nodes, we have to calculate: -
    N = 2L - 1

Algorithm for a full binary tree

Let, i = the number of internal nodes

       n = be the total number of nodes

       l = number of leaves

      λ = number of levels

Implementation

Example 1)

#include <iostream>
using namespace std;


struct Node {
  int key;
  struct Node *lft, *rt;
};


// We will create a new node
struct Node *newNod(char m) {
  struct Node *nod = (struct Node *)malloc(sizeof(struct Node));
  nod->key = m;
  nod->rt = nod->lft = NILL;
  return node;
}


bool isFullBinaryTree(struct Node *rot) {
  
  // Verify if we have any vacant spaces lft.
  if (root == NILL)
    return true;


  // Checking for the presence of children
  if (root->lft == NULL && root->rt == NULL)
    return true;


  if ((root->lft) && (root->rt))
    return (isFullBinaryTree(root->lft) && isFullBinaryTree(root->rt));


  return false;
}


int main() {
  struct Node *root = NULL;
  root = newNode(1);
  root->lft = newNode(2);
  root->rt = newNode(3);
  root->lft->lft = newNode(4);
  root->lft->rt = newNode(5);
  root->lft->rt->lft = newNode(6);
  root->lft->rt->rt = newNode(7);


  if (isFullBinaryTree(root))
    cout << "The tree is a full binary tree\n";
  else
    cout << "The tree is not a full binary tree\n";
}

Output:

WHAT IS A FULL BINARY TREE

Example 2)

// Check whether a C++ program is a full binary tree or not.
#include <bits/stdc++.h>
using namespace std;


/* Creating a tree structure*/
struct Node
{
	int key;
	struct Node *lft, *rt;
};


/* We gain a new function named helper that probably helps us to deposit a new node present in the provided key and the NILL lft and rt pointers.
struct Node *newNod(char m)
{
	struct Node *node = new Nod;
	node->key = m;
	node->rt = node->lft = NILL;
	return node;
}
// This specific function will tell us whether a given tree is a full binary tree or not.
bool isFullTree (struct Node* rot)
{
	// If empty tree
	if (root == NILL)
		return true;


	// If leaf node
	if (root->lft == NULL && root->rt == NULL)
		return true;


	// If both lft and rt are not NULL, and lft & rt subtrees
	// are full
	if ((root->lft) && (root->rt))
		return (isFullTree(root->lft) && isFullTree(root->rt));


	// We reach here when none of the above conditions work
	return false;
}


// Driver Program
int main()
{
	struct Node* root = NULL;
	root = newNode(10);
	root->lft = newNode(20);
	root->rt = newNode(30);


	root->lft->rt = newNode(40);
	root->lft->lft = newNode(50);
	root->rt->lft = newNode(60);
	root->rt->rt = newNode(70);


	root->lft->lft->lft = newNode(80);
	root->lft->lft->rt = newNode(90);
	root->lft->rt->lft = newNode(80);
	root->lft->rt->rt = newNode(90);
	root->rt->lft->lft = newNode(80);
	root->rt->lft->rt = newNode(90);
	root->rt->rt->lft = newNode(80);
	root->rt->rt->rt = newNode(90);


	if (isFullTree(root))
		cout << "The Binary Tree is full\n";
	else
		cout << "The Binary Tree is not full\n";


	return(0);
}

Output:

WHAT IS A FULL BINARY TREE

Related Topics

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Union and Intersection of two Linked Lists

Union and Intersection of two Linked Lists This article explains how we can do the union and intersection of two linked lists. In this problem, we have given two linked lists...

3 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

How to Start Learning DSA

All programmer experiences a point along the way where they wish they could approach a problem in a more effective manner. They finally learn about the terminology DSA while trying...

10 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Stack Data Structure

The stack is a non-primitive and linear data structure. It works on the principle of LIFO (Last In First Out). That is, the element that is added to the end...

3 minutes read.

Number of visible boxes putting one inside another

You have given one array, which consists of values which represent the sizes of different boxes. We can put one box inside another if the size of the outside box...

3 minutes read.

Trim a binary search tree

Implementation //writing a C++ program will help us eliminate the keys that are out of the league.  #include<bits/stdc++.h> using namespace std; //we are now creating a binary search tree node consisting of key left...

8 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Timsort

TimSort Time Complexity Timsort is a sorting algorithm that is quite efficient for real-world data. Timsort is created in 2001 by Tim Peters for the python programming language. Timsort is a...

3 minutes read.

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Delete N nodes after M nodes of a linked list

Delete N nodes after M nodes of a linked list In this problem, we have given a linked list and two integers M and N. We need to traverse the linked...

3 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Recursion in Fibonacci

Fibonacci heap is considered to be a particular execution of the heap data structure that ultimately helps in making use of not just any number but the Fibonacci numbers. It...

3 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

What are Forest Trees in Data Structure

Data structure A data model manages and optimizes computer resources, and a database stores and manages data. It's one of many uses for data structures to hold data. Data structures come...

5 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.