EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Data Structures Tutorial Binary Tree Types
 

Binary Tree Types

Updated March 13, 2023

Binary Tree Types

 

 

Introduction to Binary Tree Types

The following article provides an outline for Binary Tree Types. A binary tree is a data structure in which each Node has a maximum of two children or each node connected to at most two subtrees. Those subtrees are also a binary tree. In a binary tree, there are three components.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

  1. Value: Value store at Node
  2. *Left: Address of Left Child Node
  3. *Right: Address of Left-Right Node

Capture 03

A Binary tree has many characteristics, and based on their characteristics there are 5 types of Binary Tree. Let’s understand in detail: –

Capture 01

Types of Binary Tree

Below are the different types of binary tree:

1. Full Binary Tree

A Full Binary Tree is a Binary Tree with an additional property that each node in the binary tree should have two children or no children. The node of which address stored in root node those nodes are called children node of root. Those nodes which have no children are called leaf nodes.

Capture 02

Let’s see whether a binary tree is a Full Binary Tree is or not. In the below code, we are taking binary tree as same as above. To check the full binary tree, we have to travel all the nodes; if any node has one child, then we simply return False. On the other hand, if all node has only two children or no children, then the only binary tree is called a full binary tree.

Code:

struct BTNode
{
int value; // element value
struct BTNode * left; //To store address of left child
struct BTNode * right; //To store address of right child
};
struct BTNode *GenerateBTNode(int data) {
struct BTNode *node = new BTNode;
node->value = data;
node->right = NULL;
node->left = NULL;
return node;
}
bool isFullTree(struct BTNode *root) {
if(root==NULL){
return true;
}
if (root->left==NULL && root->right==NULL) {
return true;
}
if(root->left!=NULL && root->right!=NULL) {
return(isFullTree(root->left) && (isFullTree(root->right)));
}
return false;
}
void displayTreeInorder (struct BTNode *root) {
if(root != NULL){
displayTreeInorder(root->left);
cout<<root->value<<endl;
displayTreeInorder(root->right);
}
}
int main(){
BTNode *root=NULL;
root = GenerateBTNode(10);
root->left = GenerateBTNode(8);
root->right = GenerateBTNode(15);
root->left->left = GenerateBTNode(12);
root->left->right = GenerateBTNode(13);
root->left->right->left = GenerateBTNode(18);
root->left->right->left->left = GenerateBTNode(32);
root->left->right->left->right = GenerateBTNode(25);
root->left->right->right = GenerateBTNode(28);
root->right->left = GenerateBTNode(20);
root->right->right = GenerateBTNode(22);
bool flag = isFullTree(root);
cout<< flag <<endl;
displayTreeInorder(root);
}

Output:

Full Binary Tree

2. Complete Binary Tree

Complete Binary Tree is those binary trees in which all the levels of the tree are completely filled, the last level of the binary tree may or may not be completely filled, but in the last level of the node, each node should be at the leftmost position.

Capture 04

Now, Let’s check whether the above binary tree completes the binary tree or not. In the below code for checking the complete binary tree, first, get the total number of nodes. So, any node in a complete binary tree should not have an index greater than a total number of nodes. So, if we found any node in which the index is greater than a total number of nodes, then the binary tree is not a complete binary tree, and we simply return False.

Code:

struct BTNode
{
int value; // element value
struct BTNode * left; //To store address of left child
struct BTNode * right; //To store address of right child
};
struct BTNode *GenerateBTNode(int data) {
struct BTNode *node = new BTNode;
node->value = data;
node->right = NULL;
node->left = NULL;
return node;
}
int TotalBTNodes(BTNode* root) {
if (root == NULL) {
return (0);
}
return (1 + TotalBTNodes(root->left) + TotalBTNodes(root->right));
}
bool isCompleteTree( BTNode* root, int totalnode, int ind) {
if (root == NULL) {
return true;
}
if (ind >= totalnode) {
return false;
}
return (isCompleteTree(root->left, totalnode, 2*ind+ 1 ) && isCompleteTree(root->right, totalnode, 2*ind + 2));
}
void displayTreeInorder (struct BTNode *root) {
if(root != NULL){
displayTreeInorder(root->left);
cout<<root->value<<endl;
displayTreeInorder(root->right);
}
}
int main(){
BTNode *root=NULL;
root = GenerateBTNode(12);
root->left = GenerateBTNode(18);
root->right = GenerateBTNode(20);
root->left->left = GenerateBTNode(10);
root->left->right = GenerateBTNode(7);
root->right->left = GenerateBTNode(21);
int totalnode = TotalBTNodes(root);
bool flag = isCompleteTree(root, totalnode, 0);
cout<< flag <<endl;
displayTreeInorder(root);
}

Output:

Complete Binary Tree

3. Perfect Binary Tree

Perfect Binary Tree is those binary trees in which each node should have two children or no children, and the level of each leaf node should be the same. The level of a node is the height or distance from the root node. A perfect binary tree is a complete binary tree in which their last level is also completely filled.

Capture 05

4. Balance Binary Tree

Balance Binary Tree is those binary trees in which the height of binary is log2 of the total number of nodes in that binary tree. Let the h is the height of the tree and n is the number of nodes of the tree. Then h = log(n).

Capture 06

5. Degenerate Binary Tree

A degenerate Binary Tree is that binary in which each has only one child. It can be left children or right children, but any node should not have both children.

Capture 07

Code:

struct BTNode
{
int value; // element value
struct BTNode * left; //To store address of left child
struct BTNode * right; //To store address of right child
};
struct BTNode *GenerateBTNode(int data) {
struct BTNode *node = new BTNode;
node->value = data;
node->right = NULL;
node->left = NULL;
return node;
}
bool isDegenerateTree(struct BTNode *root) {
if(root == NULL){
return true;
}
if(root->left == NULL && root->right == NULL){
return true;
}
// If root have two children
if(root->left!=NULL && root->right!=NULL){
return false;
}
if(root->left != NULL){
return(isDegenerateTree(root->left));
}
else{
return(isDegenerateTree(root->right));
}
}
void displayTreeInorder (struct BTNode *root) {
if(root != NULL){
displayTreeInorder(root->left);
cout<<root->value<<endl;
displayTreeInorder(root->right);
}
}
int main(){
BTNode *root=NULL;
root = GenerateBTNode(15);
root->left = GenerateBTNode(31);
root->left->right = GenerateBTNode(28);
root->left->right->left = GenerateBTNode(18);
root->left->right->left->left = GenerateBTNode(10);
bool flag = isDegenerateTree(root);
cout<< flag <<endl;
displayTreeInorder(root);
}

Output:

Degenerate Binary Tree

Conclusion

These all are the types of Binary trees. Therefore, studying different types of Binary trees will help us perform better operations in better Time complexity.

Recommended Articles

This is a guide to Binary Tree Types. Here we discuss the different types like full, complete, perfect, balance, and degenerate Binary Tree in a data structure. You may also look at the following articles to learn more –

  1. Binary Search C++
  2. Oracle B Tree Index
  3. B Tree in Data Structure
  4. What is a Binary Tree in Java?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW