Binary Tree

Last Updated : 30 Jul 2026

A Binary Tree is a tree data structure in which each node has at most two child nodes. These child nodes are referred to as the left child and the right child.

In this chapter, you will learn about the Binary Tree, its structure, characteristics, and node representation, and implementation.

What is a Binary Tree?

A Binary Tree is a non-linear hierarchical data structure in which each node can have a maximum of two children. These children are known as the left child and the right child.

The term binary means two, indicating that each node can have zero, one, or two child nodes. A node with no children is called a leaf node, while a node with one or more children is called an internal node.

Binary trees are widely used in computer science for representing hierarchical data and form the basis of many other data structures, such as Binary Search Trees (BSTs), Heaps, and Expression Trees.

Characteristics of a Binary Tree

A binary tree has the following characteristics:

  • It consists of a finite number of nodes connected by edges.
  • Each node has at most two child nodes.
  • The two child nodes are referred to as the left child and the right child.
  • A node may have zero, one, or two children.
  • The topmost node is called the root.
  • Nodes without children are called leaf nodes.
  • Every subtree of a binary tree is itself a binary tree.

Example of a Binary Tree

The following figure illustrates a binary tree in which every node has at most two child nodes.

Binary Tree

In the above binary tree, each node has at most one left child and one right child. For example, the root node has two children, while some nodes have only one child or no children.

The leaf nodes do not have any children. Therefore, their left and right child references are NULL.

Node Representation of a Binary Tree

A binary tree node contains three components:

  • Data: It stores the value or information of the node.
  • Left Pointer: It stores the address of the left child.
  • Right Pointer: It stores the address of the right child.

The following figure shows the logical representation of a binary tree node.

Binary Tree

In the node representation, the left pointer points to the left child, and the right pointer points to the right child. If a child does not exist, the corresponding pointer contains NULL. This representation enables traversal and manipulation of the binary tree.

Properties of Binary Tree

  • At each level of i, the maximum number of nodes is 2i.
  • The height of the tree is defined as the longest path from the root node to the leaf node. The tree which is shown above has a height equal to 3. Therefore, the maximum number of nodes at height 3 is equal to (1+2+4+8) = 15. In general, the maximum number of nodes possible at height h is (20 + 21 + 22+….2h) = 2h+1 -1.
  • The minimum number of nodes possible at height h is equal to h+1.
  • If the number of nodes is minimum, then the height of the tree would be maximum. Conversely, if the number of nodes is maximum, then the height of the tree would be minimum.

To read more Properties of Binary Tree

If there are 'n' number of nodes in the binary tree, then

The minimum height can be computed as:

As we know that,

n = 2h+1 -1

n+1 = 2h+1

Taking log on both the sides,

log2(n+1) = log2(2h+1)

log2(n+1) = h+1

h = log2(n+1) - 1

The maximum height can be computed as:

As we know that,

n = h+1

h= n-1

Types of Binary Tree

There are the following four types of Binary tree:

  • Full or Proper or Strict Binary Tree
  • Complete Binary Tree
  • Perfect Binary Tree
  • Degenerate Binary Tree
  • Balanced Binary Tree

1. Full or Strict Binary Tree

The full binary tree is also known as a proper or strict binary tree. The tree can only be considered as the full binary tree if each node must contain either 0 or 2 children. The full binary tree can also be defined as the tree in which each node must contain 2 children except the leaf nodes.

To read more Full Binary Tree

Example of Full Binary Tree

Types of Binary Tree

In the above tree, we can observe that each node is either containing zero or two children; therefore, it is a Full Binary tree.

Properties of Full Binary Tree

  • The number of leaf nodes is equal to the number of internal nodes plus 1. In the above example, the number of internal nodes is 5; therefore, the number of leaf nodes is equal to 6.
  • The maximum number of nodes is the same as the number of nodes in the binary tree, i.e., 2h+1 -1.
  • The minimum number of nodes in the full binary tree is 2*h-1.
  • The minimum height of the full binary tree is log2(n+1) - 1.
  • The maximum height of the full binary tree can be computed as:

n= 2*h - 1

n+1 = 2*h

h = n+1/2

2. Complete Binary Tree

The complete binary tree is a tree in which all the nodes are completely filled except the last level. In the last level, all the nodes must be as left as possible. In a complete binary tree, the nodes should be added from the left.

To read more Complete Binary Tree

Example of Complete Binary Tree

Types of Binary Tree

The above tree is a complete binary tree because all the nodes are completely filled, and all the nodes in the last level are added at the left first.

Properties of Complete Binary Tree

  • The maximum number of nodes in complete binary tree is 2h+1 - 1.
  • The minimum number of nodes in complete binary tree is 2h.
  • The minimum height of a complete binary tree is log2(n+1) - 1.
  • The maximum height of a complete binary tree is ⌊log2(n)⌋.

3. Perfect Binary Tree

A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf nodes are at the same level.

To read more Perfect Binary Tree

Example of Perfect Binary Tree

Types of Binary Tree

Let's look at a simple example of a perfect binary tree.

The below tree is not a perfect binary tree because all the leaf nodes are not at the same level.

Types of Binary Tree

Note: All the perfect binary trees are the complete binary trees as well as the full binary tree, but vice versa is not true, i.e., all complete binary trees and full binary trees are the perfect binary trees.

4. Degenerate Binary Tree

The degenerate binary tree is a tree in which all the internal nodes have only one child.

Example of Degenerate Binary Tree

Types of Binary Tree

The above tree is a degenerate binary tree because all the nodes have only one child. It is also known as a right-skewed tree as all the nodes have a right child only.

Types of Binary Tree

The above tree is also a degenerate binary tree because all the nodes have only one child. It is also known as a left-skewed tree as all the nodes have a left child only.

5. Balanced Binary Tree

The balanced binary tree is a tree in which both the left and right trees differ by at most 1. For example, AVL and Red-Black trees are balanced binary tree.

Example of Balanced Binary Tree

Types of Binary Tree

The above tree is a balanced binary tree because the difference between the left subtree and right subtree is zero.

Types of Binary Tree

The above tree is not a balanced binary tree because the difference between the left subtree and the right subtree is greater than 1.

Binary Tree Implementation

A Binary tree is implemented with the help of pointers. The first node in the tree is represented by the root pointer. Each node in the tree consists of three parts, i.e., data, left pointer and right pointer. To create a binary tree, we first need to create the node. We will create the node of user-defined as shown below:

In the above structure, data is the value, left pointer contains the address of the left node, and right pointer contains the address of the right node.

Implementation of Binary Tree in Python/ Java/ C++/ C/ C#

Python

Execute Now

Java

Compile and Run

C

Compile and Run

C++

Compile and Run

C#

Compile and Run

Output:

Inorder Traversal (Left → Root → Right):
12 24 36 48 60 72 84 
Preorder Traversal (Root → Left → Right):
12 72 24 60 36 48 84 
Postorder Traversal (Left → Right → Root):
48 36 60 24 84 72 12 
Key 40 not found in the tree.
Minimum value in the tree: 12
Maximum value in the tree: 84

Traversal in a Binary Tree

Traversal means visiting each node in a binary tree exactly once in a specific order to process (read, print, or modify) the data stored in them. There are three main depth-first traversal methods:

  1. Inorder
  2. Preorder
  3. Postorder

To read more Binary Tree Traversal in Data Structure

Inorder Traversal (Left → Root → Right)

  • In this traversal, the algorithm goes to the root node first, then the left subtree, and lastly the right subtree.
  • In order to sort nodes in a Binary Search Tree (BST), inorder traversal is used.

Preorder Traversal (Root → Left → Right)

  • In this instance, the traversal recursively visits the left subtree after processing the root node, and then moves on to the right subtree.
  • It is primarily utilized to obtain a prefix expression of an expression tree or to make a clone of the tree.

Postorder Traversal (Left → Right → Root)

  • The algorithm goes to the left subtree, then the right subtree, and lastly the root node in this traversal.
  • It is helpful for getting a postfix expression in an expression tree or for removing or clearing up the tree's memory.