×

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

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.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Recursion - Factorial and Fibonacci

In this article, we will learn how to find the factorial of a number and the Fibonacci series up to n using the recursion method. What is recursion? Defining anything in terms...

7 minutes read.

Right side view of binary tree

The right view of the binary tree is generally known to be that side viewed from the right direction of the point of view. To be more precise, the right-side...

8 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Finding the Maximum Element in a Binary Tree

Implementation // Creating a C++ program to excavate the minimum and maximum in a given binary tree. #include <bits/stdc++.h> #include <iostream> using namespace std; // creating a new tree node. class __nod { public: int record; __nod *Lft, *Rt; /*...

4 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Intersection Point in Y Shaped Linked Lists in Java

Intersection Point in Y Shaped Linked Lists in Java In this article, we are going to see how to find the intersection point in a Y-shaped linked list. Method 1: We need to...

4 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Given a Generate all Structurally Unique Binary Search Trees

Implementation // Creating a C++ program that will help us build all the binary search trees for the keys from 1 to n.  #include <bits/stdc++.h> using namespace std; // creating a structure that will...

8 minutes read.

Invert binary tree

Invert binary tree is a mirror image of a tree. It is pretty much the same compared to the only difference: its left and right children are swapped with the...

4 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an...

4 minutes read.

Bottom view of the binary tree

The bottom of the binary tree is generally defined as the number of nods present in the bottom-most part of the tree. In this article, we will see the implementation...

3 minutes read.