Amongst different types of data structures are binary trees that come with more uses than most of the other types. Their most notable applications include peer-to-peer programming, search, cryptography, network routers with higher bandwidth than others, and 3D video games. We will now discuss in detail what binary trees in data structures are, what are their types, and how are they represented.

**What are binary trees?**

If you have worked on normal trees before or even know about their basics, you would know that there are no restrictions when it comes to the number of children that different nodes are allowed to have in these trees. Binary trees are a little different in this sense. Every parent or node in binary trees can have a maximum of only two children.

**All nodes in a binary tree have three primary components – **

- a data element
- a right reference
- a left reference

The node that lies at the top of the tree is referred to as the root node. Parent nodes are those that have children. Children nodes and parent nodes are connected to each other through references. Nodes that don’t have any children are referred to as leaf nodes.

It is clearly evident that nodes in binary trees can have one child, two children, or no children at all. Binary trees aren’t linear data structures like queues, arrays, stacks, and linked lists. They are hierarchical data structures instead.

**Check out:** Data Science Project Ideas for Beginners

**Important properties of nodes in binary trees**

A better understanding of these properties will help you in making the most of this discussion on binary trees. The depth of different nodes is defined as the number of nodes that exist on the way that connects the root to a particular node. That is why the depth of the root node is 0. On the other hand, the height of different nodes in a binary tree is the number of nodes that lie in the path that connects a particular node with the root node. That is why the height of leaf nodes is 0.

As you can clearly see, the depth of a node is measured by starting from the root node and then going down to reach that node. On the other hand, when it comes to calculating the height, we start at the node in question and then journey towards the root node. Both the times, we start at 0. There are people who also measure height and depth from1 and not from 0, which isn’t wrong and is just what different people prefer.

Now the maximum depth of a node is defined as the depth of a binary tree. Similarly, the maximum height of a node is defined as the height of a binary tree. So the height and depth of a binary tree are always the same.

**Learn more: **Data Structures & Algorithm in Python

**What is a binary search tree? **

A binary search tree is the most common of all the other types of binary trees. It is a specialized binary tree that comes with properties that are different and more useful than any other form of a binary tree. What exactly is a binary search tree or BST? Just as its name suggests, a binary search tree is used to search data in the tree.

A BST comes with properties that allow it to facilitate efficient searches. A BST is a binary tree that has the key of the node that is smaller and greater than nodes in the right sub-tree and nodes in the left sub-tree respectively.

**Representation of binary trees**

**1. Linked representation**

Binary trees in linked representation are stored in the memory as linked lists. These lists have nodes that aren’t stored at adjacent or neighboring memory locations and are linked to each other through the parent-child relationship associated with trees.

**In this representation, each node has three different parts – **

- pointer that points towards the right node,
- pointer that points towards the left node,
- data element.

This is the more common representation. All binary trees consist of a root pointer that points in the direction of the root node. When you see a root node pointing towards null or 0, you should know that you are dealing with an empty binary tree. The right and left pointers store the address of the right and left children of the tree.

**2. Sequential representation**

Although it is simpler than linked representation, its inefficiency makes it a less preferred binary tree representation of the two. The inefficiency lies in the amount of space it requires for the storage of different tree elements. The sequential representation uses an array for the storage of tree elements.

The number of nodes a binary tree has defines the size of the array being used. The root node of the binary tree lies at the array’s first index. The index at which a particular node is stored will define the indices at which the right and left children of the node will be stored. An empty tree has null or 0 as its first index.

**Types of binary trees**

**Full binary trees:**Full binary trees are those binary trees whose nodes either have two children or none. In other words, a binary tree becomes a full binary tree when apart from leaves, all its other nodes have two children.**Complete binary trees:**Complete binary trees are those that have all their different levels completely filled. The only exception to this could be their last level, whose keys are predominantly on the left. A binary heap is often taken as an example of a complete binary tree.**Perfect binary trees:**Perfect binary trees are binary trees whose leaves are present at the same level and whose internal nodes carry two children. A common example of a perfect binary tree is an ancestral family tree.**Pathological degenerate binary trees:**Degenerate trees are those binary trees whose internal nodes have one child. Their performance levels are similar to linked lists.

**Read: **The Six Most Commonly Used Data Structures in R

**Benefits of binary trees**

- An ideal way to go with the hierarchical way of storing data
- Reflect structural relationships that exist in the given data set
- Make insertion and deletion faster than linked lists and arrays
- A flexible way of holding and moving data
- Are used to store as many nodes as possible
- Are faster than linked lists and slower than arrays when comes to accessing elements

**Conclusion**

In this blog, we have discussed what binary trees in data structures are as well as talked about their types, their representations, and their benefits. The two major uses of the trees are for searching and storing data, and hence they are integral to the study of Data Science and its related fields.

If you are curious to learn about binary trees in data structures, data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.