Data structures & algorithms (Beginner to Intermediate)

Free guided path13 chapters99+ problems

Earn badges and level up

Introduction

A binary tree consists of a finite number of nodes, either empty or having a root, and two disjoint binary trees called left and right subtrees. A binary tree node is made of a left pointer, a right pointer, and a data element.

There are mainly five types of Binary Trees frequently used. But sometimes, their names turn out confusing to identify distinctly. In this article, we will understand their unique properties and how they are different from each other through examples.

Types

Frequently used these types are -

Full or Strict Binary Tree

Complete Binary Tree

Perfect Binary Tree

Degenerate Binary Tree

Balanced Binary Tree

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Full or Strict Binary Tree

Binary tree in which every node contains zero or two children nodes. In other words, the binary tree with all nodes having two children except leaf nodes.

Letâ€™s visualize Full Binary Tree through examples

10

/ \

5 3

/ \ / \

4 15 11 14

In the above binary tree, every node has two children nodes

Except for the leaf nodes at the last level.

10

/ \

5 3

/ \

11 14

For this binary tree, 5,11,14 are leaf nodes that contain 0 children.

Complete Binary Tree

In this type of binary tree, all levels except the last level must be completely filled. In the last level, all nodes must be as left as possible.

Letâ€™s visualize a Complete Binary Tree through examples

10

/ \

5 3

/ \ / \

4 15 11 14

In the above binary tree, all levels are completely filled.

10

/ \

5 3

/ \ /

4 15 11

Only the last level is not completely filled for this binary tree, and nodes 4, 5, and 11 are as much as to the left.

Perfect Binary Tree

A binary tree in which all internal nodes have two children and all leaf nodes are at the same level (i.e., last level).

Letâ€™s visualize this type of Binary Tree through an example

10

/ \

5 3

/ \ / \

4 15 11 14

In the above binary tree, all internal nodes 10,5 and 3 have two children, and all leaf nodes are also at the same level.

So, all perfect binary trees are full as well as complete binary trees but not vice versa.

What are the different types of traversals in Binary Trees?

Three are mainly three types of traversal techniques in Binary Trees. In order Preorder Postorder

What are Binary Trees?

A tree is called a binary tree if every node has at most two children nodes. A binary tree node constitutes a left pointer, a right pointer, and a data element. An empty binary tree (with zero nodes) is also a valid binary tree.

What are some of the advantages of using binary trees?

Insertion and deletion of data are faster than linked lists and arrays. A hierarchical way of storing dataAccessing data is faster than a linked list also signifies the structural relationship existing in the given data.

What is a Perfect Binary Tree?

Binary tree in which all internal nodes have two children and all leaf nodes are at the same level (i.e., last level).

What is the need for balancing Binary Trees?

A balanced binary tree optimizes search time in the tree. Search time complexity in a balanced binary tree is O(log n) in the worst case, but for an unbalanced tree, it is O(n), where n is the number of nodes. So maintaining a balanced tree is beneficial for large trees.

Conclusion

In this article, we have explored various types of binary trees and how they are different from each other.

Side by side, we should also learn about insertion, searching, and deletion operations in Binary trees.

You can practice these concepts along with a wide range of coding questions commonly asked in interviews in Coding Ninjas Studio. Along with coding questions, we can also find the interview experience of scholars working in renowned product-based companies here.